From 00a8d29df98439d8997a9c12aff4352bd910b67b Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 18 Jun 2024 16:09:37 +0100 Subject: [PATCH] refactor new encoding param --- src/main/java/org/apache/xmlbeans/Filer.java | 16 +++++- .../java/org/apache/xmlbeans/XmlOptions.java | 15 ++---- .../xmlbeans/impl/schema/SchemaTypePool.java | 4 +- .../impl/schema/SchemaTypeSystemCompiler.java | 6 +-- .../impl/schema/SchemaTypeSystemImpl.java | 14 ++--- .../xmlbeans/impl/schema/StscState.java | 18 ++++--- .../xmlbeans/impl/tool/CodeGenUtil.java | 53 ++++++++++++++++--- .../apache/xmlbeans/impl/tool/Parameters.java | 6 +-- .../xmlbeans/impl/tool/SchemaCompiler.java | 13 ++--- .../apache/xmlbeans/impl/util/FilerImpl.java | 18 ++++--- .../scomp/common/mockobj/TestFiler.java | 8 +-- .../detailed/XmlObjectAbstractClassTest.java | 2 +- 12 files changed, 109 insertions(+), 64 deletions(-) diff --git a/src/main/java/org/apache/xmlbeans/Filer.java b/src/main/java/org/apache/xmlbeans/Filer.java index cf4008259..a511e5b77 100755 --- a/src/main/java/org/apache/xmlbeans/Filer.java +++ b/src/main/java/org/apache/xmlbeans/Filer.java @@ -39,10 +39,22 @@ public interface Filer * Creates a new binding source file (.java) and returns a writer for it. * * @param typename fully qualified type name - * @param sourceCodeEncoding whether use CustomEncoding * @return a stream to write the type to * * @throws IOException when the file can't be created */ - public Writer createSourceFile(String typename, boolean sourceCodeEncoding ) 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 ba73fc4ec..71b7ba7cc 100644 --- a/src/main/java/org/apache/xmlbeans/XmlOptions.java +++ b/src/main/java/org/apache/xmlbeans/XmlOptions.java @@ -1089,19 +1089,14 @@ public boolean isCompileDownloadUrls() { } /** - * If this option is set, then the schema compiler will use utf_8 to generate java source file - * + * An optional encoding to use when compiling generated source code (can be null). */ - public XmlOptions setCompileSourceCodeEncoding () { - return setCompileSourceCodeEncoding (true); - } - - public XmlOptions setCompileSourceCodeEncoding (boolean b) { - return set(XmlOptionsKeys.SOURCE_CODE_ENCODING, b); + public XmlOptions setCompileSourceCodeEncoding(String enc) { + return set(XmlOptionsKeys.SOURCE_CODE_ENCODING, enc); } - public boolean isCompileSourceCodeEncoding () { - return hasOption(XmlOptionsKeys.SOURCE_CODE_ENCODING); + public String getCompileSourceCodeEncoding() { + return (String) get(XmlOptionsKeys.SOURCE_CODE_ENCODING); } /** diff --git a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypePool.java b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypePool.java index 1f49f0fc7..8bbc52214 100644 --- a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypePool.java +++ b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypePool.java @@ -89,7 +89,7 @@ String handleForElement(SchemaGlobalElement element) { } String handle = _componentsToHandles.get(element); if (handle == null) { - if(typeSystem.getUseShortName()) { + if(typeSystem.isUseJavaShortName()) { SchemaType type = element.getType(); String javaName = type.getShortJavaName(); if (javaName != null && !javaName.isEmpty()) { @@ -189,7 +189,7 @@ String handleForType(SchemaType type) { if (name == null) { baseName = "Anon" + uniq + "Type"; } else { - if(typeSystem.getUseShortName()) { + if(typeSystem.isUseJavaShortName()) { String javaName = type.getShortJavaName(); if (javaName == null || javaName.isEmpty()) javaName = name.getLocalPart(); 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 7059631e0..bd0281bf7 100644 --- a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemCompiler.java +++ b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemCompiler.java @@ -380,7 +380,7 @@ public static boolean generateTypes(SchemaTypeSystem system, Filer filer, XmlOpt String indexClassName = SchemaTypeCodePrinter.indexClassForSystem(system); - try (Writer out = filer.createSourceFile(indexClassName, (options == null) ? false : options.isCompileSourceCodeEncoding())) { + try (Writer out = filer.createSourceFile(indexClassName, (options == null) ? null : options.getCompileSourceCodeEncoding())) { 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, (options == null) ? false : options.isCompileSourceCodeEncoding())) { + try (Writer writer = filer.createSourceFile(fjn, (options == null) ? null : options.getCompileSourceCodeEncoding())) { // 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, (options == null) ? false : options.isCompileSourceCodeEncoding())) { + try (Writer writer = filer.createSourceFile(fjn, (options == null) ? null : options.getCompileSourceCodeEncoding())) { // 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 3306e239a..a878c742c 100644 --- a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java +++ b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeSystemImpl.java @@ -165,8 +165,8 @@ public class SchemaTypeSystemImpl extends SchemaTypeLoaderBase implements Schema private Set _namespaces; // the additional config option - private boolean _sourceCodeEncoding ; - private boolean _useShortName; + private String _sourceCodeEncoding ; + private boolean _useJavaShortName; static String nameToPathString(String nameForSystem) { nameForSystem = nameForSystem.replace('.', '/'); @@ -319,7 +319,7 @@ void savePointers() { void savePointersForComponents(SchemaComponent[] components, String dir) { for (SchemaComponent component : components) { - if(_useShortName) { + if(_useJavaShortName) { String javaName = _localHandles.handleForComponent(component); if (javaName != null && !javaName.isEmpty()) { @@ -436,12 +436,12 @@ SchemaContainer getContainerNonNull(String namespace) { return result; } - Boolean getSourceCodeEncoding (){ + String getSourceCodeEncoding() { return _sourceCodeEncoding ; } - Boolean getUseShortName(){ - return _useShortName; + boolean isUseJavaShortName(){ + return _useJavaShortName; } @SuppressWarnings("unchecked") @@ -648,7 +648,7 @@ public void loadFromStscState(StscState state) { _annotations = state.annotations(); _namespaces = new HashSet<>(Arrays.asList(state.getNamespaces())); _containers = state.getContainerMap(); - _useShortName = state.useShortName(); + _useJavaShortName = state.useShortName(); _sourceCodeEncoding = state.sourceCodeEncoding(); fixupContainers(); // Checks that data in the containers matches the lookup maps 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 7022086d4..20a3a9662 100644 --- a/src/main/java/org/apache/xmlbeans/impl/schema/StscState.java +++ b/src/main/java/org/apache/xmlbeans/impl/schema/StscState.java @@ -103,8 +103,8 @@ public class StscState { private boolean _noPvr; private boolean _noAnn; private boolean _mdefAll; - private boolean _useShortName; - private boolean _sourceCodeEncoding ; + private boolean _useJavaShortName; + private String _sourceCodeEncoding ; private final Set _mdefNamespaces = buildDefaultMdefNamespaces(); private EntityResolver _entityResolver; private File _schemasDir; @@ -461,9 +461,11 @@ public void setOptions(XmlOptions options) { !"true".equals(SystemProperties.getProperty("xmlbean.schemaannotations", "true")); _doingDownloads = options.isCompileDownloadUrls() || "true".equals(SystemProperties.getProperty("xmlbean.downloadurls", "false")); - _sourceCodeEncoding = options.isCompileSourceCodeEncoding() || - "true".equals(SystemProperties.getProperty("xmlbean.sourcecodeencoding ", "false")); - _useShortName = options.isCompileUseShortJavaName() || + _sourceCodeEncoding = options.getCharacterEncoding(); + if (_sourceCodeEncoding == null || _sourceCodeEncoding.isEmpty()) { + _sourceCodeEncoding = SystemProperties.getProperty("xmlbean.sourcecodeencoding"); + } + _useJavaShortName = options.isCompileUseShortJavaName() || "true".equals(SystemProperties.getProperty("xmlbean.useshortjavaname", "false")); _entityResolver = options.getEntityResolver(); @@ -530,10 +532,10 @@ public boolean allowPartial() { } /** - * True if use customEncoding to generate the java source file + * An optional encoding to use when compiling generated java source file (can be null) */ // EXPERIMENTAL - public boolean sourceCodeEncoding () { + public String sourceCodeEncoding() { return _sourceCodeEncoding ; } @@ -542,7 +544,7 @@ public boolean sourceCodeEncoding () { */ // EXPERIMENTAL public boolean useShortName() { - return _useShortName; + return _useJavaShortName; } /** 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 a2d49b35b..7519b4e98 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,21 +89,57 @@ 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, 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, boolean sourceCodeEncoding) { + /** + * 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); } + /** + * 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 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, boolean sourceCodeEncoding) { + 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,9 +157,9 @@ public static boolean externalCompile(List srcFiles, File outdir, File[] c cp = systemClasspath(); } - if(sourceCodeEncoding) { + if(sourceCodeEncoding != null && !sourceCodeEncoding.isEmpty()) { args.add("-encoding"); - args.add("utf-8"); + args.add(sourceCodeEncoding); } if (cp.length > 0) { @@ -162,10 +199,12 @@ public static boolean externalCompile(List srcFiles, File outdir, File[] c addAllJavaFiles(srcFiles, args); + final Charset charset = sourceCodeEncoding == null || sourceCodeEncoding.isEmpty() ? + StandardCharsets.ISO_8859_1 : Charset.forName(sourceCodeEncoding); File clFile = null; try { clFile = Files.createTempFile(IOUtil.getTempDir(), "javac", ".tmp").toFile(); - try (Writer fw = Files.newBufferedWriter(clFile.toPath(), sourceCodeEncoding ? StandardCharsets.UTF_8 : StandardCharsets.ISO_8859_1)) { + try (Writer fw = Files.newBufferedWriter(clFile.toPath(), charset)) { Iterator i = args.iterator(); for (i.next(); i.hasNext(); ) { String arg = i.next(); 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 1952d0db2..8ee32c462 100644 --- a/src/main/java/org/apache/xmlbeans/impl/tool/Parameters.java +++ b/src/main/java/org/apache/xmlbeans/impl/tool/Parameters.java @@ -55,7 +55,7 @@ public class Parameters { private boolean debug; private boolean copyAnn; private boolean useShortName; - private boolean sourceCodeEncoding; + private String sourceCodeEncoding; private boolean incrementalSrcGen; private String repackage; private List extensions = Collections.emptyList(); @@ -209,7 +209,7 @@ public boolean isUseShortName() { return useShortName; } - public boolean isSourceCodeEncoding() { + public String getSourceCodeEncoding() { return sourceCodeEncoding; } @@ -253,7 +253,7 @@ public void setUseShortName(boolean useShortName) { this.useShortName = useShortName; } - public void setSourceCodeEncoding(boolean sourceCodeEncoding) { + public void setSourceCodeEncoding(String sourceCodeEncoding) { this.sourceCodeEncoding = sourceCodeEncoding; } 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 5f26c1d63..7efc42b38 100644 --- a/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCompiler.java +++ b/src/main/java/org/apache/xmlbeans/impl/tool/SchemaCompiler.java @@ -186,7 +186,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); - boolean sourceCodeEncoding = (cl.getOpt("sourcecodeencoding") != null); + String sourceCodeEncoding = cl.getOpt("sourcecodeencoding"); boolean useShortName = (cl.getOpt("useshortname") != null); String allowmdef = cl.getOpt("allowmdef"); @@ -360,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 sourceCodeEncoding, boolean useShortName, + boolean download, boolean noUpa, boolean noPvr, boolean noAnn, boolean noVDoc, boolean noExt, String sourceCodeEncoding, boolean useShortName, Set mdefNamespaces, File baseDir, Map sourcesToCopyMap, Collection outerErrorListener, File schemasDir, EntityResolver entResolver, File[] classpath) { XmlErrorWatcher errorListener = new XmlErrorWatcher(outerErrorListener); @@ -525,8 +525,8 @@ private static SchemaTypeSystem loadTypeSystem(String name, File[] xsdFiles, Fil if (noAnn) { opts.setCompileNoAnnotations(); } - if (sourceCodeEncoding) { - opts.setCompileSourceCodeEncoding(); + if (sourceCodeEncoding != null ) { + opts.setCompileSourceCodeEncoding(sourceCodeEncoding); } if (useShortName) { opts.setCompileUseShortJavaName(); @@ -624,7 +624,7 @@ public static boolean compile(Parameters params) { boolean noExt = params.isNoExt(); boolean incrSrcGen = params.isIncrementalSrcGen(); boolean copyAnn = params.isCopyAnn(); - boolean sourceCodeEncoding = params.isSourceCodeEncoding(); + String sourceCodeEncoding = params.getSourceCodeEncoding(); boolean useShortName = params.isUseShortName(); Collection outerErrorListener = params.getErrorListener(); Set partialMethods = params.getPartialMethods(); @@ -736,7 +736,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, sourceCodeEncoding)) { + 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 b2e21a532..bf23b8471 100755 --- a/src/main/java/org/apache/xmlbeans/impl/util/FilerImpl.java +++ b/src/main/java/org/apache/xmlbeans/impl/util/FilerImpl.java @@ -85,10 +85,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 whether use CustomEncoding + * @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, boolean sourceCodeEncoding) throws IOException { + public Writer createSourceFile(String typename, String sourceCodeEncoding) throws IOException { if (incrSrcGen) { seenTypes.add(typename); } @@ -128,9 +128,11 @@ public Repackager getRepackager() { return repackager; } - private static Writer writerForFile(File f, boolean sourceCodeEncoding) throws IOException { - if (CHARSET == null) { - return Files.newBufferedWriter(f.toPath(), sourceCodeEncoding ? StandardCharsets.UTF_8 : StandardCharsets.ISO_8859_1); + private static Writer writerForFile(File f, String sourceCodeEncoding) throws IOException { + if (sourceCodeEncoding != null) { + return Files.newBufferedWriter(f.toPath(), Charset.forName(sourceCodeEncoding)); + } else if (CHARSET == null) { + return Files.newBufferedWriter(f.toPath(), StandardCharsets.ISO_8859_1); } OutputStream fileStream = Files.newOutputStream(f.toPath()); @@ -163,9 +165,9 @@ public void close() throws IOException { Diff.readersAsText(sReader, "", fReader, _file.getName(), diffs); } - if (diffs.size() > 0) { + if (!diffs.isEmpty()) { // Diffs encountered, replace the file on disk with text from the buffer - try (Writer fw = writerForFile(_file, false)) { + try (Writer fw = writerForFile(_file, null)) { fw.write(str); } } @@ -181,7 +183,7 @@ public RepackagingWriter(File file, Repackager repackager) { public void close() throws IOException { super.close(); - try (Writer fw = writerForFile(_file, false)) { + try (Writer fw = writerForFile(_file, null)) { fw.write(_repackager.repackage(getBuffer()).toString()); } } diff --git a/src/test/java/compile/scomp/common/mockobj/TestFiler.java b/src/test/java/compile/scomp/common/mockobj/TestFiler.java index 69bc8b112..8f6c60655 100644 --- a/src/test/java/compile/scomp/common/mockobj/TestFiler.java +++ b/src/test/java/compile/scomp/common/mockobj/TestFiler.java @@ -48,13 +48,7 @@ public OutputStream createBinaryFile(String typename) throws IOException { return impl.createBinaryFile(typename); } - public Writer createSourceFile(String typename) throws IOException { - srcFileVec.add(typename); - isCreateSourceFile = true; - return impl.createSourceFile(typename, false); - } - - public Writer createSourceFile(String typename, boolean sourceCodeEncoding) throws IOException { + public Writer createSourceFile(String typename, String sourceCodeEncoding) throws IOException { srcFileVec.add(typename); isCreateSourceFile = true; return impl.createSourceFile(typename, sourceCodeEncoding); diff --git a/src/test/java/xmlobject/detailed/XmlObjectAbstractClassTest.java b/src/test/java/xmlobject/detailed/XmlObjectAbstractClassTest.java index a696892b3..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, false); + CodeGenUtil.DEFAULT_MEM_MAX, false, false, null); } /**