From ba56db7a5265a5ad6dc0b35b92b902f34ec7db37 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 24 Jun 2024 11:12:24 +0000 Subject: [PATCH] [XMLBEANS-652] add sourceCodeEncoding param for schema compilation / code generation. Thanks to @whatever098 git-svn-id: https://svn.apache.org/repos/asf/xmlbeans/trunk@1918538 13f79535-47bb-0310-9956-ffa450edef68 --- src/main/java/org/apache/xmlbeans/Filer.java | 14 ++++- .../java/org/apache/xmlbeans/XmlOptions.java | 4 +- .../impl/schema/SchemaTypeCodePrinter.java | 2 +- .../impl/schema/SchemaTypeSystemCompiler.java | 8 +-- .../impl/schema/SchemaTypeSystemImpl.java | 8 ++- .../xmlbeans/impl/schema/StscState.java | 13 +++++ .../xmlbeans/impl/tool/CodeGenUtil.java | 58 ++++++++++++++++--- .../xmlbeans/impl/tool/MavenPlugin.java | 11 ++++ .../apache/xmlbeans/impl/tool/Parameters.java | 9 +++ .../xmlbeans/impl/tool/SchemaCompiler.java | 16 ++++- .../apache/xmlbeans/impl/util/FilerImpl.java | 36 ++++++------ .../checkin/XmlBeansCompCheckinTests.java | 13 +++++ .../scomp/common/mockobj/TestFiler.java | 4 +- .../detailed/XmlObjectAbstractClassTest.java | 2 +- 14 files changed, 156 insertions(+), 42 deletions(-) diff --git a/src/main/java/org/apache/xmlbeans/Filer.java b/src/main/java/org/apache/xmlbeans/Filer.java index 5c0076366..a511e5b77 100755 --- a/src/main/java/org/apache/xmlbeans/Filer.java +++ b/src/main/java/org/apache/xmlbeans/Filer.java @@ -43,6 +43,18 @@ public interface Filer * * @throws IOException when the file can't be created */ - public Writer createSourceFile(String typename) throws IOException; + default Writer createSourceFile(String typename) throws IOException { + return createSourceFile(typename, null); + } + /** + * Creates a new binding source file (.java) and returns a writer for it. + * + * @param typename fully qualified type name + * @param sourceCodeEncoding an optional encoding used when compiling source code (can be null) + * @return a stream to write the type to + * + * @throws IOException when the file can't be created + */ + public Writer createSourceFile(String typename, String sourceCodeEncoding) throws IOException; } diff --git a/src/main/java/org/apache/xmlbeans/XmlOptions.java b/src/main/java/org/apache/xmlbeans/XmlOptions.java index 1799da9b5..3d055b867 100644 --- a/src/main/java/org/apache/xmlbeans/XmlOptions.java +++ b/src/main/java/org/apache/xmlbeans/XmlOptions.java @@ -156,8 +156,7 @@ public enum XmlOptionsKeys { LOAD_USE_LOCALE_CHAR_UTIL, XPATH_USE_SAXON, XPATH_USE_XMLBEANS, - ATTRIBUTE_VALIDATION_COMPAT_MODE, - + ATTRIBUTE_VALIDATION_COMPAT_MODE } @@ -213,7 +212,6 @@ public boolean isSaveNamespacesFirst() { return hasOption(XmlOptionsKeys.SAVE_NAMESPACES_FIRST); } - /** * This option will cause the saver to reformat white space for easier reading. * diff --git a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java index 43e63fcc7..6bb973116 100644 --- a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java +++ b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java @@ -83,7 +83,7 @@ void emit(String s) throws IOException { } private static String makeSafe(String s) { - Charset charset = Charset.forName(System.getProperty("file.encoding")); + final Charset charset = Charset.defaultCharset(); CharsetEncoder cEncoder = charset.newEncoder(); StringBuilder result = new StringBuilder(); int i; diff --git a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemCompiler.java b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemCompiler.java index 528a51a5a..1566e1049 100644 --- a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemCompiler.java +++ b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemCompiler.java @@ -373,14 +373,14 @@ public static boolean generateTypes(SchemaTypeSystem system, Filer filer, XmlOpt types.addAll(Arrays.asList(system.attributeTypes())); - SchemaCodePrinter printer = (options == null) ? null : options.getSchemaCodePrinter(); + SchemaCodePrinter printer = options == null ? null : options.getSchemaCodePrinter(); if (printer == null) { printer = new SchemaTypeCodePrinter(); } String indexClassName = SchemaTypeCodePrinter.indexClassForSystem(system); - try (Writer out = filer.createSourceFile(indexClassName)) { + try (Writer out = filer.createSourceFile(indexClassName, options == null ? null : options.getCharacterEncoding())) { Repackager repackager = (filer instanceof FilerImpl) ? ((FilerImpl) filer).getRepackager() : null; printer.printHolder(out, system, options, repackager); } catch (IOException e) { @@ -398,7 +398,7 @@ public static boolean generateTypes(SchemaTypeSystem system, Filer filer, XmlOpt String fjn = type.getFullJavaName(); - try (Writer writer = filer.createSourceFile(fjn)) { + try (Writer writer = filer.createSourceFile(fjn, options == null ? null : options.getCharacterEncoding())) { // Generate interface class printer.printType(writer, type, options); } catch (IOException e) { @@ -408,7 +408,7 @@ public static boolean generateTypes(SchemaTypeSystem system, Filer filer, XmlOpt fjn = type.getFullJavaImplName(); - try (Writer writer = filer.createSourceFile(fjn)) { + try (Writer writer = filer.createSourceFile(fjn, options == null ? null : options.getCharacterEncoding())) { // Generate Implementation class printer.printTypeImpl(writer, type, options); } catch (IOException e) { diff --git a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java index 4c2734a5b..ed6b7919f 100644 --- a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java +++ b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java @@ -164,7 +164,8 @@ public class SchemaTypeSystemImpl extends SchemaTypeLoaderBase implements Schema private Map _typeRefsByClassname = new HashMap<>(); private Set _namespaces; - + // the additional config option + private String _sourceCodeEncoding ; static String nameToPathString(String nameForSystem) { nameForSystem = nameForSystem.replace('.', '/'); @@ -416,6 +417,10 @@ SchemaContainer getContainerNonNull(String namespace) { return result; } + String getSourceCodeEncoding() { + return _sourceCodeEncoding ; + } + @SuppressWarnings("unchecked") private void buildContainersHelper(Map elements, BiConsumer adder) { elements.forEach((k, v) -> adder.accept(getContainerNonNull(k.getNamespaceURI()), (T) v)); @@ -620,6 +625,7 @@ public void loadFromStscState(StscState state) { _annotations = state.annotations(); _namespaces = new HashSet<>(Arrays.asList(state.getNamespaces())); _containers = state.getContainerMap(); + _sourceCodeEncoding = state.sourceCodeEncoding(); fixupContainers(); // Checks that data in the containers matches the lookup maps assertContainersSynchronized(); diff --git a/src/main/java/org/apache/xmlbeans/impl/schema/StscState.java b/src/main/java/org/apache/xmlbeans/impl/schema/StscState.java index b61220c79..eb9ad1003 100644 --- a/src/main/java/org/apache/xmlbeans/impl/schema/StscState.java +++ b/src/main/java/org/apache/xmlbeans/impl/schema/StscState.java @@ -103,6 +103,7 @@ public class StscState { private boolean _noPvr; private boolean _noAnn; private boolean _mdefAll; + private String _sourceCodeEncoding ; private final Set _mdefNamespaces = buildDefaultMdefNamespaces(); private EntityResolver _entityResolver; private File _schemasDir; @@ -459,6 +460,10 @@ public void setOptions(XmlOptions options) { !"true".equals(SystemProperties.getProperty("xmlbean.schemaannotations", "true")); _doingDownloads = options.isCompileDownloadUrls() || "true".equals(SystemProperties.getProperty("xmlbean.downloadurls", "false")); + _sourceCodeEncoding = options.getCharacterEncoding(); + if (_sourceCodeEncoding == null || _sourceCodeEncoding.isEmpty()) { + _sourceCodeEncoding = SystemProperties.getProperty("xmlbean.sourcecodeencoding"); + } _entityResolver = options.getEntityResolver(); if (_entityResolver == null) { @@ -523,6 +528,14 @@ public boolean allowPartial() { return _allowPartial; } + /** + * An optional encoding to use when compiling generated java source file (can be null) + */ + // EXPERIMENTAL + public String sourceCodeEncoding() { + return _sourceCodeEncoding ; + } + /** * Get count of recovered errors. Not for public. */ diff --git a/src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java b/src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java index 900e5f1e3..a7f24fff5 100644 --- a/src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java +++ b/src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java @@ -22,6 +22,7 @@ import java.io.*; import java.net.URI; import java.net.URISyntaxException; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.security.CodeSource; @@ -88,12 +89,34 @@ static private String quoteAndEscapeFilename(String filename) { * @deprecated */ public static boolean externalCompile(List srcFiles, File outdir, File[] cp, boolean debug) { - return externalCompile(srcFiles, outdir, cp, debug, DEFAULT_COMPILER, null, DEFAULT_MEM_START, DEFAULT_MEM_MAX, false, false); + return externalCompile(srcFiles, outdir, cp, debug, DEFAULT_COMPILER, null, DEFAULT_MEM_START, DEFAULT_MEM_MAX, + false, false, null); } - // KHK: temporary to avoid build break - public static boolean externalCompile(List srcFiles, File outdir, File[] cp, boolean debug, String javacPath, String memStart, String memMax, boolean quiet, boolean verbose) { - return externalCompile(srcFiles, outdir, cp, debug, javacPath, null, memStart, memMax, quiet, verbose); + /** + * Invokes javac on the generated source files in order to turn them + * into binary files in the output directory. This will return a list of + * {@code GenFile}s for all of the classes produced or null if an + * error occurred. + * + * @deprecated + */ + public static boolean externalCompile(List srcFiles, File outdir, File[] cp, boolean debug, String javacPath, String memStart, String memMax, + boolean quiet, boolean verbose) { + return externalCompile(srcFiles, outdir, cp, debug, javacPath, null, memStart, memMax, quiet, verbose, null); + } + + /** + * Invokes javac on the generated source files in order to turn them + * into binary files in the output directory. This will return a list of + * {@code GenFile}s for all of the classes produced or null if an + * error occurred. + * + * @deprecated + */ + public static boolean externalCompile(List srcFiles, File outdir, File[] cp, boolean debug, String javacPath, String memStart, String memMax, + boolean quiet, boolean verbose, String sourceCodeEncoding) { + return externalCompile(srcFiles, outdir, cp, debug, javacPath, null, memStart, memMax, quiet, verbose, sourceCodeEncoding); } /** @@ -101,8 +124,22 @@ public static boolean externalCompile(List srcFiles, File outdir, File[] c * into binary files in the output directory. This will return a list of * {@code GenFile}s for all of the classes produced or null if an * error occurred. + * + * @deprecated */ - public static boolean externalCompile(List srcFiles, File outdir, File[] cp, boolean debug, String javacPath, String genver, String memStart, String memMax, boolean quiet, boolean verbose) { + public static boolean externalCompile(List srcFiles, File outdir, File[] cp, boolean debug, String javacPath, String genver, String memStart, String memMax, + boolean quiet, boolean verbose) { + return externalCompile(srcFiles, outdir, cp, debug, javacPath, genver, memStart, memMax, quiet, verbose, null); + } + + /** + * Invokes javac on the generated source files in order to turn them + * into binary files in the output directory. This will return a list of + * {@code GenFile}s for all of the classes produced or null if an + * error occurred. + */ + public static boolean externalCompile(List srcFiles, File outdir, File[] cp, boolean debug, String javacPath, String genver, String memStart, String memMax, + boolean quiet, boolean verbose, String sourceCodeEncoding) { List args = new ArrayList<>(); File javac = findJavaTool(javacPath == null ? DEFAULT_COMPILER : javacPath); @@ -120,6 +157,11 @@ public static boolean externalCompile(List srcFiles, File outdir, File[] c cp = systemClasspath(); } + if(sourceCodeEncoding != null && !sourceCodeEncoding.isEmpty()) { + args.add("-encoding"); + args.add(sourceCodeEncoding); + } + if (cp.length > 0) { StringBuilder classPath = new StringBuilder(); // Add the output directory to the classpath. We do this so that @@ -160,7 +202,7 @@ public static boolean externalCompile(List srcFiles, File outdir, File[] c File clFile = null; try { clFile = Files.createTempFile(IOUtil.getTempDir(), "javac", ".tmp").toFile(); - try (Writer fw = Files.newBufferedWriter(clFile.toPath(), StandardCharsets.ISO_8859_1)) { + try (Writer fw = Files.newBufferedWriter(clFile.toPath(), Charset.defaultCharset())) { Iterator i = args.iterator(); for (i.next(); i.hasNext(); ) { String arg = i.next(); @@ -228,7 +270,7 @@ public static boolean externalCompile(List srcFiles, File outdir, File[] c e.printStackTrace(System.err); return false; } finally { - if (clFile != null) { + if (!debug && clFile != null) { clFile.delete(); } } @@ -304,7 +346,7 @@ private static File findJavaTool(String tool) { * nothing left to read. */ private static Thread copy(InputStream stream, final StringBuilder output) { - final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.ISO_8859_1)); + final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, Charset.defaultCharset())); Thread readerThread = new Thread(() -> reader.lines().forEach(s -> output.append(s).append("\n")) ); diff --git a/src/main/java/org/apache/xmlbeans/impl/tool/MavenPlugin.java b/src/main/java/org/apache/xmlbeans/impl/tool/MavenPlugin.java index 952afb383..8a7e2f9ae 100644 --- a/src/main/java/org/apache/xmlbeans/impl/tool/MavenPlugin.java +++ b/src/main/java/org/apache/xmlbeans/impl/tool/MavenPlugin.java @@ -184,6 +184,14 @@ public class MavenPlugin extends AbstractMojo { @Parameter( defaultValue = "false" ) private boolean copyAnn; + /** + * The source code encoding to use when compiling the generated sources. + * + * @since 5.2.2 + */ + @Parameter + private String sourceCodeEncoding; + @Parameter private List extensions; @@ -301,6 +309,9 @@ public void execute() throws MojoExecutionException, MojoFailureException { params.setOutputJar(outputJar); params.setDebug(debug); params.setExtensions(extensions); + if (sourceCodeEncoding != null && !sourceCodeEncoding.isEmpty()) { + params.setSourceCodeEncoding(sourceCodeEncoding); + } boolean result = SchemaCompiler.compile(params); diff --git a/src/main/java/org/apache/xmlbeans/impl/tool/Parameters.java b/src/main/java/org/apache/xmlbeans/impl/tool/Parameters.java index 9f496a0c5..dd6340fb2 100644 --- a/src/main/java/org/apache/xmlbeans/impl/tool/Parameters.java +++ b/src/main/java/org/apache/xmlbeans/impl/tool/Parameters.java @@ -54,6 +54,7 @@ public class Parameters { private boolean noExt; private boolean debug; private boolean copyAnn; + private String sourceCodeEncoding; private boolean incrementalSrcGen; private String repackage; private List extensions = Collections.emptyList(); @@ -203,6 +204,10 @@ public boolean isNoAnn() { return noAnn; } + public String getSourceCodeEncoding() { + return sourceCodeEncoding; + } + public void setNoAnn(boolean noAnn) { this.noAnn = noAnn; } @@ -239,6 +244,10 @@ public void setDebug(boolean debug) { this.debug = debug; } + public void setSourceCodeEncoding(String sourceCodeEncoding) { + this.sourceCodeEncoding = sourceCodeEncoding; + } + public String getMemoryInitialSize() { return memoryInitialSize; } diff --git a/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCompiler.java b/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCompiler.java index 449453990..c5340bb6f 100644 --- a/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCompiler.java +++ b/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCompiler.java @@ -67,6 +67,7 @@ public static void printUsage() { System.out.println(" processed left-to-right, e.g. \"ALL,-GET_LIST\" exclude java.util.List getters - see XmlOptions.BeanMethod" ); System.out.println(" -repackage - repackage specification, e.g. \"org.apache.xmlbeans.metadata:mypackage.metadata\" to change the metadata directory"); System.out.println(" -copyann - copy schema annotations to javadoc (default false) - don't activate on untrusted schema sources!"); + System.out.println(" -sourcecodeencoding [encodingName] - Generate Java source code with the specified encoding (ISO-8859-1 is the legacy default)"); /* Undocumented feature - pass in one schema compiler extension and related parameters System.out.println(" -extension - registers a schema compiler extension"); System.out.println(" -extensionParms - specify parameters for the compiler extension"); @@ -117,6 +118,7 @@ public static void main(String[] args) { opts.add("catalog"); opts.add("partialMethods"); opts.add("copyann"); + opts.add("sourcecodeencoding"); CommandLine cl = new CommandLine(args, flags, opts); @@ -186,6 +188,7 @@ public static void main(String[] args) { boolean nojavac = (cl.getOpt("srconly") != null); boolean debug = (cl.getOpt("debug") != null); boolean copyAnn = (cl.getOpt("copyann") != null); + String sourceCodeEncoding = cl.getOpt("sourcecodeencoding"); String allowmdef = cl.getOpt("allowmdef"); Set mdefNamespaces = (allowmdef == null ? Collections.emptySet() : @@ -333,6 +336,7 @@ public static void main(String[] args) { params.setNoVDoc(noVDoc); params.setNoExt(noExt); params.setDebug(debug); + params.setSourceCodeEncoding(sourceCodeEncoding); params.setErrorListener(err); params.setRepackage(repackage); params.setExtensions(extensions); @@ -356,7 +360,7 @@ public static void main(String[] args) { private static SchemaTypeSystem loadTypeSystem(String name, File[] xsdFiles, File[] wsdlFiles, URL[] urlFiles, File[] configFiles, File[] javaFiles, ResourceLoader cpResourceLoader, - boolean download, boolean noUpa, boolean noPvr, boolean noAnn, boolean noVDoc, boolean noExt, + boolean download, boolean noUpa, boolean noPvr, boolean noAnn, boolean noVDoc, boolean noExt, String sourceCodeEncoding, Set mdefNamespaces, File baseDir, Map sourcesToCopyMap, Collection outerErrorListener, File schemasDir, EntityResolver entResolver, File[] classpath) { XmlErrorWatcher errorListener = new XmlErrorWatcher(outerErrorListener); @@ -521,6 +525,9 @@ private static SchemaTypeSystem loadTypeSystem(String name, File[] xsdFiles, Fil if (noAnn) { opts.setCompileNoAnnotations(); } + if (sourceCodeEncoding != null ) { + opts.setCharacterEncoding(sourceCodeEncoding); + } if (mdefNamespaces != null) { opts.setCompileMdefNamespaces(mdefNamespaces); } @@ -614,6 +621,7 @@ public static boolean compile(Parameters params) { boolean noExt = params.isNoExt(); boolean incrSrcGen = params.isIncrementalSrcGen(); boolean copyAnn = params.isCopyAnn(); + String sourceCodeEncoding = params.getSourceCodeEncoding(); Collection outerErrorListener = params.getErrorListener(); Set partialMethods = params.getPartialMethods(); @@ -666,7 +674,7 @@ public static boolean compile(Parameters params) { // build the in-memory type system XmlErrorWatcher errorListener = new XmlErrorWatcher(outerErrorListener); SchemaTypeSystem system = loadTypeSystem(name, xsdFiles, wsdlFiles, urlFiles, configFiles, - javaFiles, cpResourceLoader, download, noUpa, noPvr, noAnn, noVDoc, noExt, mdefNamespaces, + javaFiles, cpResourceLoader, download, noUpa, noPvr, noAnn, noVDoc, noExt, sourceCodeEncoding, mdefNamespaces, baseDir, sourcesToCopyMap, errorListener, schemasDir, cmdLineEntRes, classpath); if (errorListener.hasError()) { result = false; @@ -693,6 +701,7 @@ public static boolean compile(Parameters params) { options.setCompilePartialMethod(partialMethods); options.setCompileNoAnnotations(noAnn); options.setCompileAnnotationAsJavadoc(copyAnn); + options.setCharacterEncoding(sourceCodeEncoding); // save .xsb files system.save(filer); @@ -722,7 +731,8 @@ public static boolean compile(Parameters params) { if (javaFiles != null) { sourcefiles.addAll(java.util.Arrays.asList(javaFiles)); } - if (!CodeGenUtil.externalCompile(sourcefiles, classesDir, classpath, debug, compiler, memoryInitialSize, memoryMaximumSize, quiet, verbose)) { + if (!CodeGenUtil.externalCompile(sourcefiles, classesDir, classpath, debug, compiler, null, + memoryInitialSize, memoryMaximumSize, quiet, verbose, sourceCodeEncoding)) { result = false; } diff --git a/src/main/java/org/apache/xmlbeans/impl/util/FilerImpl.java b/src/main/java/org/apache/xmlbeans/impl/util/FilerImpl.java index e099a0605..836b5b39a 100755 --- a/src/main/java/org/apache/xmlbeans/impl/util/FilerImpl.java +++ b/src/main/java/org/apache/xmlbeans/impl/util/FilerImpl.java @@ -40,16 +40,7 @@ public class FilerImpl implements Filer { private final List sourceFiles; private final boolean incrSrcGen; private Set seenTypes; - private static final Charset CHARSET; - - static { - Charset temp = null; - try { - temp = Charset.forName(System.getProperty("file.encoding")); - } catch (Exception ignored) { - } - CHARSET = temp; - } + private static final Charset DEFAULT_CHARSET = Charset.defaultCharset(); public FilerImpl(File classdir, File srcdir, Repackager repackager, boolean verbose, boolean incrSrcGen) { this.classdir = classdir; @@ -85,9 +76,10 @@ public OutputStream createBinaryFile(String typename) throws IOException { * Creates a new binding source file (.java) and returns a writer for it. * * @param typename fully qualified type name + * @param sourceCodeEncoding an optional encoding used when compiling source code (can be null) * @return a stream to write the type to */ - public Writer createSourceFile(String typename) throws IOException { + public Writer createSourceFile(String typename, String sourceCodeEncoding) throws IOException { if (incrSrcGen) { seenTypes.add(typename); } @@ -114,7 +106,7 @@ public Writer createSourceFile(String typename) throws IOException { return new IncrFileWriter(sourcefile, repackager); } else { return repackager == null ? - writerForFile(sourcefile) : + writerForFile(sourcefile, sourceCodeEncoding) : new RepackagingWriter(sourcefile, repackager); } } @@ -127,17 +119,25 @@ public Repackager getRepackager() { return repackager; } - private static Writer writerForFile(File f) throws IOException { - if (CHARSET == null) { - return Files.newBufferedWriter(f.toPath(), StandardCharsets.ISO_8859_1); + private static Writer writerForFile(File f, String sourceCodeEncoding) throws IOException { + if (sourceCodeEncoding != null && !sourceCodeEncoding.isEmpty()) { + return Files.newBufferedWriter(f.toPath(), getCharset(sourceCodeEncoding)); } OutputStream fileStream = Files.newOutputStream(f.toPath()); - CharsetEncoder ce = CHARSET.newEncoder(); + CharsetEncoder ce = DEFAULT_CHARSET.newEncoder(); ce.onUnmappableCharacter(CodingErrorAction.REPORT); return new OutputStreamWriter(fileStream, ce); } + private static Charset getCharset(final String sourceCodeEncoding) throws IOException { + try { + return Charset.forName(sourceCodeEncoding); + } catch (RuntimeException e) { + throw new IOException("Unsupported encoding: " + sourceCodeEncoding, e); + } + } + static class IncrFileWriter extends StringWriter { private final File _file; private final Repackager _repackager; @@ -164,7 +164,7 @@ public void close() throws IOException { if (!diffs.isEmpty()) { // Diffs encountered, replace the file on disk with text from the buffer - try (Writer fw = writerForFile(_file)) { + try (Writer fw = writerForFile(_file, null)) { fw.write(str); } } @@ -180,7 +180,7 @@ public RepackagingWriter(File file, Repackager repackager) { public void close() throws IOException { super.close(); - try (Writer fw = writerForFile(_file)) { + try (Writer fw = writerForFile(_file, null)) { fw.write(_repackager.repackage(getBuffer()).toString()); } } diff --git a/src/test/java/compile/scomp/checkin/XmlBeansCompCheckinTests.java b/src/test/java/compile/scomp/checkin/XmlBeansCompCheckinTests.java index ab69f54ad..1b443c06b 100644 --- a/src/test/java/compile/scomp/checkin/XmlBeansCompCheckinTests.java +++ b/src/test/java/compile/scomp/checkin/XmlBeansCompCheckinTests.java @@ -36,6 +36,7 @@ public class XmlBeansCompCheckinTests { private final List xm_errors = new ArrayList<>(); private final XmlOptions xm_opts = new XmlOptions(); private final List expBinType; + private final List expBinShortnameType; private final List expSrcType; public XmlBeansCompCheckinTests() { @@ -51,6 +52,18 @@ public XmlBeansCompCheckinTests() { "org/apache/xmlbeans/metadata/javaname/baz/AType.xsb" ); + expBinShortnameType = Arrays.asList( + "org/apache/xmlbeans/metadata/system/apiCompile/atypedb57type.xsb", + "org/apache/xmlbeans/metadata/system/apiCompile/elnamedocument429edoctype.xsb", + "org/apache/xmlbeans/metadata/system/apiCompile/atypeelement.xsb", + "org/apache/xmlbeans/metadata/system/apiCompile/index.xsb", + "org/apache/xmlbeans/metadata/element/http_3A_2F_2Fbaz/atypeelement.xsb", + "org/apache/xmlbeans/metadata/type/http_3A_2F_2Fbaz/atypedb57type.xsb", + "org/apache/xmlbeans/metadata/namespace/http_3A_2F_2Fbaz/xmlns.xsb", + "org/apache/xmlbeans/metadata/javaname/baz/ElNameDocument.xsb", + "org/apache/xmlbeans/metadata/javaname/baz/AType.xsb" + ); + expSrcType = Arrays.asList( "org.apache.xmlbeans.metadata.system.apiCompile.TypeSystemHolder", "baz.AType", diff --git a/src/test/java/compile/scomp/common/mockobj/TestFiler.java b/src/test/java/compile/scomp/common/mockobj/TestFiler.java index 1382333eb..8f6c60655 100644 --- a/src/test/java/compile/scomp/common/mockobj/TestFiler.java +++ b/src/test/java/compile/scomp/common/mockobj/TestFiler.java @@ -48,10 +48,10 @@ public OutputStream createBinaryFile(String typename) throws IOException { return impl.createBinaryFile(typename); } - public Writer createSourceFile(String typename) throws IOException { + public Writer createSourceFile(String typename, String sourceCodeEncoding) throws IOException { srcFileVec.add(typename); isCreateSourceFile = true; - return impl.createSourceFile(typename); + return impl.createSourceFile(typename, sourceCodeEncoding); } public boolean isCreateBinaryFile() { diff --git a/src/test/java/xmlobject/detailed/XmlObjectAbstractClassTest.java b/src/test/java/xmlobject/detailed/XmlObjectAbstractClassTest.java index 016ede357..b1ba5e28b 100755 --- a/src/test/java/xmlobject/detailed/XmlObjectAbstractClassTest.java +++ b/src/test/java/xmlobject/detailed/XmlObjectAbstractClassTest.java @@ -58,7 +58,7 @@ private boolean compileFile(File source) { return CodeGenUtil.externalCompile(srcFiles, dir, classpath, false, CodeGenUtil.DEFAULT_COMPILER, null, CodeGenUtil.DEFAULT_MEM_START, - CodeGenUtil.DEFAULT_MEM_MAX, false, false); + CodeGenUtil.DEFAULT_MEM_MAX, false, false, null); } /**