Skip to content

Commit

Permalink
Fixes #348. Fix encoding when reading xml file (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
haramon authored Jan 29, 2025
1 parent 42dd6d2 commit 7d90242
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 11 deletions.
85 changes: 85 additions & 0 deletions src/it/schemagen-encoding-utf8/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.mojo.jaxb2.its</groupId>
<artifactId>schemagen-encoding-utf8</artifactId>
<version>1.0-SNAPSHOT</version>

<description>Purpose: Test for bug reported in mojohaus#348 (Use encoding parameter when reading xml on
transformSchemas).
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>@maven-compiler-plugin.version@</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>schemagen</id>
<goals>
<goal>schemagen</goal>
</goals>
</execution>
</executions>
<configuration>
<!--
Post-processing: Transform the generated XML Schemas.
-->
<transformSchemas>
<transformSchema>
<uri>test</uri>
<toFile>transform-test.xsd</toFile>
</transformSchema>
</transformSchemas>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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;
}
}
38 changes: 38 additions & 0 deletions src/it/schemagen-encoding-utf8/verify.groovy
Original file line number Diff line number Diff line change
@@ -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."
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<String, SimpleNamespaceResolver> resolverMap,
final List<TransformSchema> configuredTransformSchemas,
final Log mavenLog,
final File schemaDirectory,
final String charsetName) {
final String encoding) {

// Create the map relating namespace URI to desired filenames.
Map<String, String> namespaceUriToDesiredFilenameMap = new TreeMap<String, String>();
Expand All @@ -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(
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 7d90242

Please sign in to comment.