30
30
import java .io .BufferedWriter ;
31
31
import java .io .File ;
32
32
import java .io .FileFilter ;
33
+ import java .io .FileInputStream ;
33
34
import java .io .FileNotFoundException ;
34
35
import java .io .FileOutputStream ;
35
- import java .io .FileReader ;
36
36
import java .io .IOException ;
37
+ import java .io .InputStreamReader ;
37
38
import java .io .OutputStreamWriter ;
38
39
import java .io .Reader ;
39
40
import java .io .StringWriter ;
41
+ import java .io .UnsupportedEncodingException ;
40
42
import java .io .Writer ;
41
43
import java .util .ArrayList ;
42
44
import java .util .Arrays ;
@@ -235,7 +237,7 @@ public static int insertJavaDocAsAnnotations(
235
237
for (File current : foundFiles ) {
236
238
237
239
// Create an XSD document from the current File.
238
- final Document generatedSchemaFileDocument = parseXmlToDocument (current );
240
+ final Document generatedSchemaFileDocument = parseXmlToDocument (current , encoding );
239
241
240
242
// Replace all namespace prefixes within the provided document.
241
243
process (generatedSchemaFileDocument .getFirstChild (), true , classProcessor );
@@ -303,7 +305,7 @@ public static void replaceNamespacePrefixes(
303
305
304
306
// Get the Document of the current schema file.
305
307
if (generatedSchemaFileDocument == null ) {
306
- generatedSchemaFileDocument = parseXmlToDocument (generatedSchemaFile );
308
+ generatedSchemaFileDocument = parseXmlToDocument (generatedSchemaFile , encoding );
307
309
}
308
310
309
311
// Replace all namespace prefixes within the provided document.
@@ -335,14 +337,14 @@ public static void replaceNamespacePrefixes(
335
337
* @param configuredTransformSchemas The Schema instances read from the configuration of this plugin.
336
338
* @param mavenLog The active Log.
337
339
* @param schemaDirectory The directory where all generated schema files reside.
338
- * @param charsetName The encoding / charset name.
340
+ * @param encoding The encoding / charset name.
339
341
*/
340
342
public static void renameGeneratedSchemaFiles (
341
343
final Map <String , SimpleNamespaceResolver > resolverMap ,
342
344
final List <TransformSchema > configuredTransformSchemas ,
343
345
final Log mavenLog ,
344
346
final File schemaDirectory ,
345
- final String charsetName ) {
347
+ final String encoding ) {
346
348
347
349
// Create the map relating namespace URI to desired filenames.
348
350
Map <String , String > namespaceUriToDesiredFilenameMap = new TreeMap <String , String >();
@@ -355,7 +357,7 @@ public static void renameGeneratedSchemaFiles(
355
357
// Replace the schemaLocation values to correspond to the new filenames
356
358
for (SimpleNamespaceResolver currentResolver : resolverMap .values ()) {
357
359
File generatedSchemaFile = new File (schemaDirectory , currentResolver .getSourceFilename ());
358
- Document generatedSchemaFileDocument = parseXmlToDocument (generatedSchemaFile );
360
+ Document generatedSchemaFileDocument = parseXmlToDocument (generatedSchemaFile , encoding );
359
361
360
362
// Replace all namespace prefixes within the provided document.
361
363
process (
@@ -368,7 +370,7 @@ public static void renameGeneratedSchemaFiles(
368
370
mavenLog .debug ("Changed schemaLocation entries within [" + currentResolver .getSourceFilename () + "]. "
369
371
+ "Result: [" + getHumanReadableXml (generatedSchemaFileDocument ) + "]" );
370
372
}
371
- savePrettyPrintedDocument (generatedSchemaFileDocument , generatedSchemaFile , charsetName );
373
+ savePrettyPrintedDocument (generatedSchemaFileDocument , generatedSchemaFile , encoding );
372
374
}
373
375
374
376
// Now, rename the actual files.
@@ -509,17 +511,20 @@ private static void validatePrefixSubstitutionIsPossible(
509
511
/**
510
512
* Creates a Document from parsing the XML within the provided xmlFile.
511
513
*
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.
513
516
* @return The Document corresponding to the xmlFile.
514
517
*/
515
- private static Document parseXmlToDocument (final File xmlFile ) {
518
+ private static Document parseXmlToDocument (final File xmlFile , final String encoding ) {
516
519
Document result = null ;
517
520
Reader reader = null ;
518
521
try {
519
- reader = new FileReader ( xmlFile );
522
+ reader = new InputStreamReader ( new FileInputStream ( xmlFile ), encoding );
520
523
result = parseXmlStream (reader );
521
- } catch (FileNotFoundException e ) {
524
+ } catch (final FileNotFoundException e ) {
522
525
// This should never happen...
526
+ } catch (final UnsupportedEncodingException e ) {
527
+ throw new IllegalArgumentException ("Could not read xml file using encoding [" + encoding + "]" , e );
523
528
} finally {
524
529
IOUtil .close (reader );
525
530
}
0 commit comments