Skip to content

Commit

Permalink
refactor new encoding param
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Jun 18, 2024
1 parent 5452fce commit 00a8d29
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 64 deletions.
16 changes: 14 additions & 2 deletions src/main/java/org/apache/xmlbeans/Filer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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;
}
15 changes: 5 additions & 10 deletions src/main/java/org/apache/xmlbeans/XmlOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>null</code>).
*/
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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, (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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ public class SchemaTypeSystemImpl extends SchemaTypeLoaderBase implements Schema
private Set<String> _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('.', '/');
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/org/apache/xmlbeans/impl/schema/StscState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> _mdefNamespaces = buildDefaultMdefNamespaces();
private EntityResolver _entityResolver;
private File _schemasDir;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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 <code>null</code>)
*/
// EXPERIMENTAL
public boolean sourceCodeEncoding () {
public String sourceCodeEncoding() {
return _sourceCodeEncoding ;
}

Expand All @@ -542,7 +544,7 @@ public boolean sourceCodeEncoding () {
*/
// EXPERIMENTAL
public boolean useShortName() {
return _useShortName;
return _useJavaShortName;
}

/**
Expand Down
53 changes: 46 additions & 7 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, 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, 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<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) {
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, boolean sourceCodeEncoding) {
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,9 +157,9 @@ public static boolean externalCompile(List<File> 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) {
Expand Down Expand Up @@ -162,10 +199,12 @@ public static boolean externalCompile(List<File> 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<String> i = args.iterator();
for (i.next(); i.hasNext(); ) {
String arg = i.next();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/apache/xmlbeans/impl/tool/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Extension> extensions = Collections.emptyList();
Expand Down Expand Up @@ -209,7 +209,7 @@ public boolean isUseShortName() {
return useShortName;
}

public boolean isSourceCodeEncoding() {
public String getSourceCodeEncoding() {
return sourceCodeEncoding;
}

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

Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/apache/xmlbeans/impl/tool/SchemaCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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<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 @@ -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();
Expand Down Expand Up @@ -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<XmlError> outerErrorListener = params.getErrorListener();
Set<BeanMethod> partialMethods = params.getPartialMethods();
Expand Down Expand Up @@ -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;
}

Expand Down
Loading

0 comments on commit 00a8d29

Please sign in to comment.