Skip to content

Commit

Permalink
[XMLBEANS-652] add sourceCodeEncoding param for schema compilation / …
Browse files Browse the repository at this point in the history
…code generation. Thanks to @whatever098

git-svn-id: https://svn.apache.org/repos/asf/xmlbeans/trunk@1918538 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Jun 24, 2024
1 parent cbe7de7 commit ba56db7
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 42 deletions.
14 changes: 13 additions & 1 deletion src/main/java/org/apache/xmlbeans/Filer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>null</code>)
* @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;
}
4 changes: 1 addition & 3 deletions src/main/java/org/apache/xmlbeans/XmlOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ public class SchemaTypeSystemImpl extends SchemaTypeLoaderBase implements Schema
private Map<String, SchemaComponent.Ref> _typeRefsByClassname = new HashMap<>();
private Set<String> _namespaces;


// the additional config option
private String _sourceCodeEncoding ;

static String nameToPathString(String nameForSystem) {
nameForSystem = nameForSystem.replace('.', '/');
Expand Down Expand Up @@ -416,6 +417,10 @@ SchemaContainer getContainerNonNull(String namespace) {
return result;
}

String getSourceCodeEncoding() {
return _sourceCodeEncoding ;
}

@SuppressWarnings("unchecked")
private <T extends SchemaComponent.Ref> void buildContainersHelper(Map<QName, SchemaComponent.Ref> elements, BiConsumer<SchemaContainer, T> adder) {
elements.forEach((k, v) -> adder.accept(getContainerNonNull(k.getNamespaceURI()), (T) v));
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/apache/xmlbeans/impl/schema/StscState.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class StscState {
private boolean _noPvr;
private boolean _noAnn;
private boolean _mdefAll;
private String _sourceCodeEncoding ;
private final Set<String> _mdefNamespaces = buildDefaultMdefNamespaces();
private EntityResolver _entityResolver;
private File _schemasDir;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -523,6 +528,14 @@ public boolean allowPartial() {
return _allowPartial;
}

/**
* An optional encoding to use when compiling generated java source file (can be <code>null</code>)
*/
// EXPERIMENTAL
public String sourceCodeEncoding() {
return _sourceCodeEncoding ;
}

/**
* Get count of recovered errors. Not for public.
*/
Expand Down
58 changes: 50 additions & 8 deletions src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,21 +89,57 @@ static private String quoteAndEscapeFilename(String filename) {
* @deprecated
*/
public static boolean externalCompile(List<File> 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<File> 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<File> 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<File> 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<File> srcFiles, File outdir, File[] cp, boolean debug, String javacPath, String genver, String memStart, String memMax, boolean quiet, boolean verbose) {
public static boolean externalCompile(List<File> 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<File> srcFiles, File outdir, File[] cp, boolean debug, String javacPath, String genver, String memStart, String memMax,
boolean quiet, boolean verbose, String sourceCodeEncoding) {
List<String> args = new ArrayList<>();

File javac = findJavaTool(javacPath == null ? DEFAULT_COMPILER : javacPath);
Expand All @@ -120,6 +157,11 @@ public static boolean externalCompile(List<File> 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
Expand Down Expand Up @@ -160,7 +202,7 @@ public static boolean externalCompile(List<File> 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<String> i = args.iterator();
for (i.next(); i.hasNext(); ) {
String arg = i.next();
Expand Down Expand Up @@ -228,7 +270,7 @@ public static boolean externalCompile(List<File> srcFiles, File outdir, File[] c
e.printStackTrace(System.err);
return false;
} finally {
if (clFile != null) {
if (!debug && clFile != null) {
clFile.delete();
}
}
Expand Down Expand Up @@ -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"))
);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/apache/xmlbeans/impl/tool/MavenPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Extension> extensions;

Expand Down Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/apache/xmlbeans/impl/tool/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Extension> extensions = Collections.emptyList();
Expand Down Expand Up @@ -203,6 +204,10 @@ public boolean isNoAnn() {
return noAnn;
}

public String getSourceCodeEncoding() {
return sourceCodeEncoding;
}

public void setNoAnn(boolean noAnn) {
this.noAnn = noAnn;
}
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/apache/xmlbeans/impl/tool/SchemaCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<String> mdefNamespaces = (allowmdef == null ? Collections.emptySet() :
Expand Down Expand Up @@ -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);
Expand All @@ -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<String> mdefNamespaces, File baseDir, Map<String, String> sourcesToCopyMap,
Collection<XmlError> outerErrorListener, File schemasDir, EntityResolver entResolver, File[] classpath) {
XmlErrorWatcher errorListener = new XmlErrorWatcher(outerErrorListener);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<XmlError> outerErrorListener = params.getErrorListener();
Set<BeanMethod> partialMethods = params.getPartialMethods();

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
Loading

0 comments on commit ba56db7

Please sign in to comment.