From 7d9024247c1e21dd888080dcff167564fac2db77 Mon Sep 17 00:00:00 2001 From: Harald Amon <78597170+haramon@users.noreply.github.com> Date: Wed, 29 Jan 2025 16:09:51 +0100 Subject: [PATCH] Fixes #348. Fix encoding when reading xml file (#349) --- src/it/schemagen-encoding-utf8/pom.xml | 85 +++++++++++++++++++ .../src/main/java/se/west/shauqra/Foo.java | 21 +++++ src/it/schemagen-encoding-utf8/verify.groovy | 38 +++++++++ .../schemageneration/XsdGeneratorHelper.java | 27 +++--- 4 files changed, 160 insertions(+), 11 deletions(-) create mode 100644 src/it/schemagen-encoding-utf8/pom.xml create mode 100644 src/it/schemagen-encoding-utf8/src/main/java/se/west/shauqra/Foo.java create mode 100644 src/it/schemagen-encoding-utf8/verify.groovy diff --git a/src/it/schemagen-encoding-utf8/pom.xml b/src/it/schemagen-encoding-utf8/pom.xml new file mode 100644 index 00000000..2412765f --- /dev/null +++ b/src/it/schemagen-encoding-utf8/pom.xml @@ -0,0 +1,85 @@ + + + + 4.0.0 + + org.codehaus.mojo.jaxb2.its + schemagen-encoding-utf8 + 1.0-SNAPSHOT + + Purpose: Test for bug reported in mojohaus#348 (Use encoding parameter when reading xml on + transformSchemas). + + + + UTF-8 + + + + + jakarta.xml.bind + jakarta.xml.bind-api + 3.0.0 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + @maven-compiler-plugin.version@ + + 1.8 + 1.8 + + + + + + + org.codehaus.mojo + jaxb2-maven-plugin + @project.version@ + + + schemagen + + schemagen + + + + + + + + test + transform-test.xsd + + + + + + + diff --git a/src/it/schemagen-encoding-utf8/src/main/java/se/west/shauqra/Foo.java b/src/it/schemagen-encoding-utf8/src/main/java/se/west/shauqra/Foo.java new file mode 100644 index 00000000..5a1cf5f1 --- /dev/null +++ b/src/it/schemagen-encoding-utf8/src/main/java/se/west/shauqra/Foo.java @@ -0,0 +1,21 @@ +package se.west.shauqra; + +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlType; + +@XmlType(namespace = "test") +@XmlAccessorType(XmlAccessType.FIELD) +public class Foo { + @XmlElement(name = "utf8-name_äöüÄÖÜáéúàèù") + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/it/schemagen-encoding-utf8/verify.groovy b/src/it/schemagen-encoding-utf8/verify.groovy new file mode 100644 index 00000000..41d92d97 --- /dev/null +++ b/src/it/schemagen-encoding-utf8/verify.groovy @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import groovy.xml.XmlSlurper + +// Assemble +final File outputDir = new File(basedir, 'target/generated-resources/schemagen'); +final File vanillaSchema = new File(basedir, 'target/schemagen-work/compile_scope/schema1.xsd'); +final File processedSchema = new File(outputDir, 'transform-test.xsd'); + +assert vanillaSchema.exists(), "Expected file [" + vanillaSchema.getAbsolutePath() + "] not found." +assert processedSchema.exists(), "Expected file [" + processedSchema.getAbsolutePath() + "] not found." + +// Act +def schemaElement = new XmlSlurper().parse(processedSchema) + +// Assert +println "\nValidating schema content" +println "===================================" + +assert schemaElement.complexType.sequence.element.@name == "utf8-name_äöüÄÖÜáéúàèù" +println "1. Got correct name (utf8-name_äöüÄÖÜáéúàèù) for foo element child." diff --git a/src/main/java/org/codehaus/mojo/jaxb2/schemageneration/XsdGeneratorHelper.java b/src/main/java/org/codehaus/mojo/jaxb2/schemageneration/XsdGeneratorHelper.java index c23f4815..3b7bd690 100644 --- a/src/main/java/org/codehaus/mojo/jaxb2/schemageneration/XsdGeneratorHelper.java +++ b/src/main/java/org/codehaus/mojo/jaxb2/schemageneration/XsdGeneratorHelper.java @@ -30,13 +30,15 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.FileReader; import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.StringWriter; +import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; @@ -235,7 +237,7 @@ public static int insertJavaDocAsAnnotations( for (File current : foundFiles) { // Create an XSD document from the current File. - final Document generatedSchemaFileDocument = parseXmlToDocument(current); + final Document generatedSchemaFileDocument = parseXmlToDocument(current, encoding); // Replace all namespace prefixes within the provided document. process(generatedSchemaFileDocument.getFirstChild(), true, classProcessor); @@ -303,7 +305,7 @@ public static void replaceNamespacePrefixes( // Get the Document of the current schema file. if (generatedSchemaFileDocument == null) { - generatedSchemaFileDocument = parseXmlToDocument(generatedSchemaFile); + generatedSchemaFileDocument = parseXmlToDocument(generatedSchemaFile, encoding); } // Replace all namespace prefixes within the provided document. @@ -335,14 +337,14 @@ public static void replaceNamespacePrefixes( * @param configuredTransformSchemas The Schema instances read from the configuration of this plugin. * @param mavenLog The active Log. * @param schemaDirectory The directory where all generated schema files reside. - * @param charsetName The encoding / charset name. + * @param encoding The encoding / charset name. */ public static void renameGeneratedSchemaFiles( final Map resolverMap, final List configuredTransformSchemas, final Log mavenLog, final File schemaDirectory, - final String charsetName) { + final String encoding) { // Create the map relating namespace URI to desired filenames. Map namespaceUriToDesiredFilenameMap = new TreeMap(); @@ -355,7 +357,7 @@ public static void renameGeneratedSchemaFiles( // Replace the schemaLocation values to correspond to the new filenames for (SimpleNamespaceResolver currentResolver : resolverMap.values()) { File generatedSchemaFile = new File(schemaDirectory, currentResolver.getSourceFilename()); - Document generatedSchemaFileDocument = parseXmlToDocument(generatedSchemaFile); + Document generatedSchemaFileDocument = parseXmlToDocument(generatedSchemaFile, encoding); // Replace all namespace prefixes within the provided document. process( @@ -368,7 +370,7 @@ public static void renameGeneratedSchemaFiles( mavenLog.debug("Changed schemaLocation entries within [" + currentResolver.getSourceFilename() + "]. " + "Result: [" + getHumanReadableXml(generatedSchemaFileDocument) + "]"); } - savePrettyPrintedDocument(generatedSchemaFileDocument, generatedSchemaFile, charsetName); + savePrettyPrintedDocument(generatedSchemaFileDocument, generatedSchemaFile, encoding); } // Now, rename the actual files. @@ -509,17 +511,20 @@ private static void validatePrefixSubstitutionIsPossible( /** * Creates a Document from parsing the XML within the provided xmlFile. * - * @param xmlFile The XML file to be parsed. + * @param xmlFile The XML file to be parsed. + * @param encoding The encoding to use when reading the XML file. * @return The Document corresponding to the xmlFile. */ - private static Document parseXmlToDocument(final File xmlFile) { + private static Document parseXmlToDocument(final File xmlFile, final String encoding) { Document result = null; Reader reader = null; try { - reader = new FileReader(xmlFile); + reader = new InputStreamReader(new FileInputStream(xmlFile), encoding); result = parseXmlStream(reader); - } catch (FileNotFoundException e) { + } catch (final FileNotFoundException e) { // This should never happen... + } catch (final UnsupportedEncodingException e) { + throw new IllegalArgumentException("Could not read xml file using encoding [" + encoding + "]", e); } finally { IOUtil.close(reader); }