Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 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,10 @@ 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) throws IOException;

public Writer createSourceFile(String typename, boolean sourceCodeEncoding ) throws IOException;
}
53 changes: 51 additions & 2 deletions src/main/java/org/apache/xmlbeans/XmlOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public enum XmlOptionsKeys {
SAVE_CDATA_LENGTH_THRESHOLD,
SAVE_CDATA_ENTITY_COUNT_THRESHOLD,
SAVE_SAX_NO_NSDECLS_IN_ATTRIBUTES,
SAVE_EXTRA_ENAMESPACES,
LOAD_REPLACE_DOCUMENT_ELEMENT,
LOAD_STRIP_WHITESPACE,
LOAD_STRIP_COMMENTS,
Expand Down Expand Up @@ -157,7 +158,8 @@ public enum XmlOptionsKeys {
XPATH_USE_SAXON,
XPATH_USE_XMLBEANS,
ATTRIBUTE_VALIDATION_COMPAT_MODE,

SOURCE_CODE_ENCODING,
USE_SHORT_JAVA_NAME
}


Expand Down Expand Up @@ -213,7 +215,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 Expand Up @@ -448,6 +449,23 @@ public Map<String, String> getSaveSuggestedPrefixes() {
return (Map<String, String>) get(XmlOptionsKeys.SAVE_SUGGESTED_PREFIXES);
}

/**
* A map of hints to pass to the saver for which prefixes to use
* for which namespace URI.
*
* @param extraNamespaces a map from URIs to prefixes
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveExtraNamespaces(Map<String, String> extraNamespaces) {
return set(XmlOptionsKeys.SAVE_EXTRA_ENAMESPACES, extraNamespaces);
}

@SuppressWarnings("unchecked")
public Map<String, String> getSaveExtraNamespaces() {
return (Map<String, String>) get(XmlOptionsKeys.SAVE_EXTRA_ENAMESPACES);
}

/**
* This option causes the saver to filter a Processing Instruction
* with the given target
Expand Down Expand Up @@ -1070,6 +1088,37 @@ public boolean isCompileDownloadUrls() {
return hasOption(XmlOptionsKeys.COMPILE_DOWNLOAD_URLS);
}

/**
* If this option is set, then the schema compiler will use utf_8 to generate java source file
*
*/
public XmlOptions setCompileSourceCodeEncoding () {
return setCompileSourceCodeEncoding (true);
}

public XmlOptions setCompileSourceCodeEncoding (boolean b) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my suggestion was that the input should be a string - the name of an encoding, eg "utf-8"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll fix it tomorrow

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created #16 - this is just an initial change, needs more work

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created #16 - this is just an initial change, needs more work

Maybe you have the wrong branch? My changes are based on branch REL_5_1_1 instead of trunk, but your PR#16 is based on trunk

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the code using all your changes and added the ones I missed before, is there anything else I need to modify?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing these changes is hard. XMLBeans does't have great support for testing the code generation support.
If you can build this yourself and see if it does the job for you then that's great.
I will try to get #16 merged at some stage - but only when I have time to build and test it.
I am omitting some of your changes (like extra namespaces) because I would prefer to do separate PRs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I will upload it to a separate PR soon,and for the version #16 I will test it tomorrow when I go to work👍

@whatever098 whatever098 Jun 20, 2024

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pjfanning I just fixed a small problem and then used the jar with the command line parameters "-sourcecodeencoding UTF-8 -usejavashortname" to test my project. The build was successful and the unit tests passed completely. It will be fine after you apply my commit.(:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spotted that issue independently and already have a fix in #16

return set(XmlOptionsKeys.SOURCE_CODE_ENCODING, b);
}

public boolean isCompileSourceCodeEncoding () {
return hasOption(XmlOptionsKeys.SOURCE_CODE_ENCODING);
}

/**
* If this option is set, then the schema compiler will use the java_short_name to generate file name
*
*/
public XmlOptions setCompileUseShortJavaName() {
return setCompileUseShortJavaName(true);
}

public XmlOptions setCompileUseShortJavaName(boolean b) {
return set(XmlOptionsKeys.USE_SHORT_JAVA_NAME, b);
}

public boolean isCompileUseShortJavaName() {
return hasOption(XmlOptionsKeys.USE_SHORT_JAVA_NAME);
}
/**
* If this option is set, then the schema compiler will permit and
* ignore multiple definitions of the same component (element, attribute,
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypePool.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,17 @@ String handleForElement(SchemaGlobalElement element) {
}
String handle = _componentsToHandles.get(element);
if (handle == null) {
handle = addUniqueHandle(element, NameUtil.upperCamelCase(element.getName().getLocalPart()) + "Element");
if(typeSystem.getUseShortName()) {
SchemaType type = element.getType();
String javaName = type.getShortJavaName();
if (javaName != null && !javaName.isEmpty()) {
handle = addUniqueHandle(element, NameUtil.upperCamelCase(javaName) + "Element");
} else {
handle = addUniqueHandle(element, NameUtil.upperCamelCase(element.getName().getLocalPart()) + "Element");
}
} else {
handle = addUniqueHandle(element, NameUtil.upperCamelCase(element.getName().getLocalPart()) + "Element");
}
}
return handle;
}
Expand Down Expand Up @@ -179,7 +189,15 @@ String handleForType(SchemaType type) {
if (name == null) {
baseName = "Anon" + uniq + "Type";
} else {
baseName = NameUtil.upperCamelCase(name.getLocalPart()) + uniq + suffix + "Type";
if(typeSystem.getUseShortName()) {
String javaName = type.getShortJavaName();
if (javaName == null || javaName.isEmpty())
javaName = name.getLocalPart();
baseName = NameUtil.upperCamelCase(javaName) + uniq + suffix + "Type";
}
else {
baseName = NameUtil.upperCamelCase(name.getLocalPart()) + uniq + suffix + "Type";
}
}

handle = addUniqueHandle(type, baseName);
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)) {
try (Writer out = filer.createSourceFile(indexClassName, (options == null) ? false : options.isCompileSourceCodeEncoding())) {
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) ? false : options.isCompileSourceCodeEncoding())) {
// 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) ? false : options.isCompileSourceCodeEncoding())) {
// 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 @@ -162,7 +162,9 @@ public class SchemaTypeSystemImpl extends SchemaTypeLoaderBase implements Schema
private Map<String, SchemaComponent.Ref> _typeRefsByClassname = new HashMap<>();
private Set<String> _namespaces;


// the additional config option
private boolean _sourceCodeEncoding ;
private boolean _useShortName;

static String nameToPathString(String nameForSystem) {
nameForSystem = nameForSystem.replace('.', '/');
Expand Down Expand Up @@ -312,7 +314,25 @@ void savePointers() {

void savePointersForComponents(SchemaComponent[] components, String dir) {
for (SchemaComponent component : components) {
savePointerFile(dir + QNameHelper.hexsafedir(component.getName()), _name);
if(_useShortName) {
String javaName = _localHandles.handleForComponent(component);
if (javaName != null && !javaName.isEmpty())
{
QName nameTemp = component.getName();
String resultName;
if (nameTemp.getNamespaceURI() == null || nameTemp.getNamespaceURI().length() == 0) {
resultName = "_nons/" + QNameHelper.hexsafe(javaName);
} else {
resultName = QNameHelper.hexsafe(nameTemp.getNamespaceURI()) + "/"
+ QNameHelper.hexsafe(javaName);
}
savePointerFile(dir + resultName, _name);
} else {
savePointerFile(dir + QNameHelper.hexsafedir(component.getName()), _name);
}
} else {
savePointerFile(dir + QNameHelper.hexsafedir(component.getName()), _name);
}
}
}

Expand Down Expand Up @@ -411,6 +431,14 @@ SchemaContainer getContainerNonNull(String namespace) {
return result;
}

Boolean getSourceCodeEncoding (){
return _sourceCodeEncoding ;
}

Boolean getUseShortName(){
return _useShortName;
}

@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 @@ -615,6 +643,8 @@ public void loadFromStscState(StscState state) {
_annotations = state.annotations();
_namespaces = new HashSet<>(Arrays.asList(state.getNamespaces()));
_containers = state.getContainerMap();
_useShortName = state.useShortName();
_sourceCodeEncoding = state.sourceCodeEncoding();
fixupContainers();
// Checks that data in the containers matches the lookup maps
assertContainersSynchronized();
Expand Down
22 changes: 22 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,8 @@ public class StscState {
private boolean _noPvr;
private boolean _noAnn;
private boolean _mdefAll;
private boolean _useShortName;
private boolean _sourceCodeEncoding ;
private final Set<String> _mdefNamespaces = buildDefaultMdefNamespaces();
private EntityResolver _entityResolver;
private File _schemasDir;
Expand Down Expand Up @@ -459,6 +461,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.isCompileSourceCodeEncoding() ||
"true".equals(SystemProperties.getProperty("xmlbean.sourcecodeencoding ", "false"));
_useShortName = options.isCompileUseShortJavaName() ||
"true".equals(SystemProperties.getProperty("xmlbean.useshortjavaname", "false"));
_entityResolver = options.getEntityResolver();

if (_entityResolver == null) {
Expand Down Expand Up @@ -523,6 +529,22 @@ public boolean allowPartial() {
return _allowPartial;
}

/**
* True if use customEncoding to generate the java source file
*/
// EXPERIMENTAL
public boolean sourceCodeEncoding () {
return _sourceCodeEncoding ;
}

/**
* True if use the java_short_name to generate file name
*/
// EXPERIMENTAL
public boolean useShortName() {
return _useShortName;
}

/**
* Get count of recovered errors. Not for public.
*/
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ 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, false);
}

// 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);
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) {
return externalCompile(srcFiles, outdir, cp, debug, javacPath, null, memStart, memMax, quiet, verbose, sourceCodeEncoding);
}

/**
Expand All @@ -101,7 +101,7 @@ public static boolean externalCompile(List<File> srcFiles, File outdir, File[] c
* {@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) {
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) {
List<String> args = new ArrayList<>();

File javac = findJavaTool(javacPath == null ? DEFAULT_COMPILER : javacPath);
Expand All @@ -119,6 +119,11 @@ public static boolean externalCompile(List<File> srcFiles, File outdir, File[] c
cp = systemClasspath();
}

if(sourceCodeEncoding) {
args.add("-encoding");
args.add("utf-8");
}

if (cp.length > 0) {
StringBuilder classPath = new StringBuilder();
// Add the output directory to the classpath. We do this so that
Expand Down Expand Up @@ -159,7 +164,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(), sourceCodeEncoding ? StandardCharsets.UTF_8 : StandardCharsets.ISO_8859_1)) {
Iterator<String> i = args.iterator();
for (i.next(); i.hasNext(); ) {
String arg = i.next();
Expand Down
18 changes: 18 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,8 @@ public class Parameters {
private boolean noExt;
private boolean debug;
private boolean copyAnn;
private boolean useShortName;
private boolean sourceCodeEncoding;
private boolean incrementalSrcGen;
private String repackage;
private List<Extension> extensions = Collections.emptyList();
Expand Down Expand Up @@ -203,6 +205,14 @@ public boolean isNoAnn() {
return noAnn;
}

public boolean isUseShortName() {
return useShortName;
}

public boolean isSourceCodeEncoding() {
return sourceCodeEncoding;
}

public void setNoAnn(boolean noAnn) {
this.noAnn = noAnn;
}
Expand Down Expand Up @@ -239,6 +249,14 @@ public void setDebug(boolean debug) {
this.debug = debug;
}

public void setUseShortName(boolean useShortName) {
this.useShortName = useShortName;
}

public void setSourceCodeEncoding(boolean sourceCodeEncoding) {
this.sourceCodeEncoding = sourceCodeEncoding;
}

public String getMemoryInitialSize() {
return memoryInitialSize;
}
Expand Down
Loading