Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 useCustomEncoding 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 useCustomEncoding) 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_EXTRENAMESPACES,

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.

Should this be SAVE_EXTRA_NAMESPACES?

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.

You are right,I will fix it.

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,

USE_CUSTOM_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_EXTRENAMESPACES, extraNamespaces);
}

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

/**
* 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 setCompileUseCustomEncoding() {
return setCompileUseCustomEncoding(true);
}

public XmlOptions setCompileUseCustomEncoding(boolean b) {
return set(XmlOptionsKeys.USE_CUSTOM_ENCODING, b);
}

public boolean isCompileUseCustomEncoding() {
return hasOption(XmlOptionsKeys.USE_CUSTOM_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.isCompileUseCustomEncoding())) {
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.isCompileUseCustomEncoding())) {
// 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.isCompileUseCustomEncoding())) {
// 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 _useCustom;
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 getUseCustomEncoding(){
return _useCustom;
}

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();
_useCustom = state.useCustom();
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 _useCustom;
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"));
_useCustom = options.isCompileUseCustomEncoding() ||
"true".equals(SystemProperties.getProperty("xmlbean.usecustomencoding", "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 useCustom() {
return _useCustom;
}

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

/**
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 useCustom) {
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(useCustom) {
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(), useCustom ? 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 useCustom;
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 isUseCustom() {
return useCustom;
}

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 setUseCustom(boolean useCustom) {
this.useCustom = useCustom;
}

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