Skip to content

Commit 7d90242

Browse files
authored
Fixes #348. Fix encoding when reading xml file (#349)
1 parent 42dd6d2 commit 7d90242

File tree

4 files changed

+160
-11
lines changed

4 files changed

+160
-11
lines changed
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<groupId>org.codehaus.mojo.jaxb2.its</groupId>
25+
<artifactId>schemagen-encoding-utf8</artifactId>
26+
<version>1.0-SNAPSHOT</version>
27+
28+
<description>Purpose: Test for bug reported in mojohaus#348 (Use encoding parameter when reading xml on
29+
transformSchemas).
30+
</description>
31+
32+
<properties>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>jakarta.xml.bind</groupId>
39+
<artifactId>jakarta.xml.bind-api</artifactId>
40+
<version>3.0.0</version>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<pluginManagement>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>@maven-compiler-plugin.version@</version>
51+
<configuration>
52+
<source>1.8</source>
53+
<target>1.8</target>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</pluginManagement>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.codehaus.mojo</groupId>
61+
<artifactId>jaxb2-maven-plugin</artifactId>
62+
<version>@project.version@</version>
63+
<executions>
64+
<execution>
65+
<id>schemagen</id>
66+
<goals>
67+
<goal>schemagen</goal>
68+
</goals>
69+
</execution>
70+
</executions>
71+
<configuration>
72+
<!--
73+
Post-processing: Transform the generated XML Schemas.
74+
-->
75+
<transformSchemas>
76+
<transformSchema>
77+
<uri>test</uri>
78+
<toFile>transform-test.xsd</toFile>
79+
</transformSchema>
80+
</transformSchemas>
81+
</configuration>
82+
</plugin>
83+
</plugins>
84+
</build>
85+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package se.west.shauqra;
2+
3+
import jakarta.xml.bind.annotation.XmlAccessType;
4+
import jakarta.xml.bind.annotation.XmlAccessorType;
5+
import jakarta.xml.bind.annotation.XmlElement;
6+
import jakarta.xml.bind.annotation.XmlType;
7+
8+
@XmlType(namespace = "test")
9+
@XmlAccessorType(XmlAccessType.FIELD)
10+
public class Foo {
11+
@XmlElement(name = "utf8-name_äöüÄÖÜáéúàèù")
12+
private String name;
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import groovy.xml.XmlSlurper
21+
22+
// Assemble
23+
final File outputDir = new File(basedir, 'target/generated-resources/schemagen');
24+
final File vanillaSchema = new File(basedir, 'target/schemagen-work/compile_scope/schema1.xsd');
25+
final File processedSchema = new File(outputDir, 'transform-test.xsd');
26+
27+
assert vanillaSchema.exists(), "Expected file [" + vanillaSchema.getAbsolutePath() + "] not found."
28+
assert processedSchema.exists(), "Expected file [" + processedSchema.getAbsolutePath() + "] not found."
29+
30+
// Act
31+
def schemaElement = new XmlSlurper().parse(processedSchema)
32+
33+
// Assert
34+
println "\nValidating schema content"
35+
println "==================================="
36+
37+
assert schemaElement.complexType.sequence.element.@name == "utf8-name_äöüÄÖÜáéúàèù"
38+
println "1. Got correct name (utf8-name_äöüÄÖÜáéúàèù) for foo element child."

src/main/java/org/codehaus/mojo/jaxb2/schemageneration/XsdGeneratorHelper.java

+16-11
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
import java.io.BufferedWriter;
3131
import java.io.File;
3232
import java.io.FileFilter;
33+
import java.io.FileInputStream;
3334
import java.io.FileNotFoundException;
3435
import java.io.FileOutputStream;
35-
import java.io.FileReader;
3636
import java.io.IOException;
37+
import java.io.InputStreamReader;
3738
import java.io.OutputStreamWriter;
3839
import java.io.Reader;
3940
import java.io.StringWriter;
41+
import java.io.UnsupportedEncodingException;
4042
import java.io.Writer;
4143
import java.util.ArrayList;
4244
import java.util.Arrays;
@@ -235,7 +237,7 @@ public static int insertJavaDocAsAnnotations(
235237
for (File current : foundFiles) {
236238

237239
// Create an XSD document from the current File.
238-
final Document generatedSchemaFileDocument = parseXmlToDocument(current);
240+
final Document generatedSchemaFileDocument = parseXmlToDocument(current, encoding);
239241

240242
// Replace all namespace prefixes within the provided document.
241243
process(generatedSchemaFileDocument.getFirstChild(), true, classProcessor);
@@ -303,7 +305,7 @@ public static void replaceNamespacePrefixes(
303305

304306
// Get the Document of the current schema file.
305307
if (generatedSchemaFileDocument == null) {
306-
generatedSchemaFileDocument = parseXmlToDocument(generatedSchemaFile);
308+
generatedSchemaFileDocument = parseXmlToDocument(generatedSchemaFile, encoding);
307309
}
308310

309311
// Replace all namespace prefixes within the provided document.
@@ -335,14 +337,14 @@ public static void replaceNamespacePrefixes(
335337
* @param configuredTransformSchemas The Schema instances read from the configuration of this plugin.
336338
* @param mavenLog The active Log.
337339
* @param schemaDirectory The directory where all generated schema files reside.
338-
* @param charsetName The encoding / charset name.
340+
* @param encoding The encoding / charset name.
339341
*/
340342
public static void renameGeneratedSchemaFiles(
341343
final Map<String, SimpleNamespaceResolver> resolverMap,
342344
final List<TransformSchema> configuredTransformSchemas,
343345
final Log mavenLog,
344346
final File schemaDirectory,
345-
final String charsetName) {
347+
final String encoding) {
346348

347349
// Create the map relating namespace URI to desired filenames.
348350
Map<String, String> namespaceUriToDesiredFilenameMap = new TreeMap<String, String>();
@@ -355,7 +357,7 @@ public static void renameGeneratedSchemaFiles(
355357
// Replace the schemaLocation values to correspond to the new filenames
356358
for (SimpleNamespaceResolver currentResolver : resolverMap.values()) {
357359
File generatedSchemaFile = new File(schemaDirectory, currentResolver.getSourceFilename());
358-
Document generatedSchemaFileDocument = parseXmlToDocument(generatedSchemaFile);
360+
Document generatedSchemaFileDocument = parseXmlToDocument(generatedSchemaFile, encoding);
359361

360362
// Replace all namespace prefixes within the provided document.
361363
process(
@@ -368,7 +370,7 @@ public static void renameGeneratedSchemaFiles(
368370
mavenLog.debug("Changed schemaLocation entries within [" + currentResolver.getSourceFilename() + "]. "
369371
+ "Result: [" + getHumanReadableXml(generatedSchemaFileDocument) + "]");
370372
}
371-
savePrettyPrintedDocument(generatedSchemaFileDocument, generatedSchemaFile, charsetName);
373+
savePrettyPrintedDocument(generatedSchemaFileDocument, generatedSchemaFile, encoding);
372374
}
373375

374376
// Now, rename the actual files.
@@ -509,17 +511,20 @@ private static void validatePrefixSubstitutionIsPossible(
509511
/**
510512
* Creates a Document from parsing the XML within the provided xmlFile.
511513
*
512-
* @param xmlFile The XML file to be parsed.
514+
* @param xmlFile The XML file to be parsed.
515+
* @param encoding The encoding to use when reading the XML file.
513516
* @return The Document corresponding to the xmlFile.
514517
*/
515-
private static Document parseXmlToDocument(final File xmlFile) {
518+
private static Document parseXmlToDocument(final File xmlFile, final String encoding) {
516519
Document result = null;
517520
Reader reader = null;
518521
try {
519-
reader = new FileReader(xmlFile);
522+
reader = new InputStreamReader(new FileInputStream(xmlFile), encoding);
520523
result = parseXmlStream(reader);
521-
} catch (FileNotFoundException e) {
524+
} catch (final FileNotFoundException e) {
522525
// This should never happen...
526+
} catch (final UnsupportedEncodingException e) {
527+
throw new IllegalArgumentException("Could not read xml file using encoding [" + encoding + "]", e);
523528
} finally {
524529
IOUtil.close(reader);
525530
}

0 commit comments

Comments
 (0)