Skip to content

xmlbeans supports Chinese tags to generate java source code and build JAR package #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: REL_5_1_1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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;
}
5 changes: 5 additions & 0 deletions src/main/java/org/apache/xmlbeans/FilterXmlObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipOutputStream;

/**
* A FilterXmlObject delegates to some other XmlObject, which it can use as
Expand Down Expand Up @@ -199,6 +200,10 @@ public void save(OutputStream os, XmlOptions options) throws IOException {
underlyingXmlObject().save(os, options);
}

public void save(ZipOutputStream zos, XmlOptions options) throws IOException {
underlyingXmlObject().save(zos, options);
}

public void save(Writer w, XmlOptions options) throws IOException {
underlyingXmlObject().save(w, options);
}
Expand Down
36 changes: 34 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_NAMESPACES,
LOAD_REPLACE_DOCUMENT_ELEMENT,
LOAD_STRIP_WHITESPACE,
LOAD_STRIP_COMMENTS,
Expand Down Expand Up @@ -157,7 +158,7 @@ public enum XmlOptionsKeys {
XPATH_USE_SAXON,
XPATH_USE_XMLBEANS,
ATTRIBUTE_VALIDATION_COMPAT_MODE,

USE_JAVA_SHORT_NAME
}


Expand Down Expand Up @@ -213,7 +214,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 +448,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_NAMESPACES, extraNamespaces);
}

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

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

/**
* 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_JAVA_SHORT_NAME, b);
}

public boolean isCompileUseShortJavaName() {
return hasOption(XmlOptionsKeys.USE_JAVA_SHORT_NAME);
}
/**
* If this option is set, then the schema compiler will permit and
* ignore multiple definitions of the same component (element, attribute,
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/apache/xmlbeans/XmlTokenSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import javax.xml.stream.XMLStreamReader;
import java.io.*;
import java.util.zip.ZipOutputStream;

/**
* Represents a holder of XML that can return an {@link XmlCursor}
Expand Down Expand Up @@ -170,6 +171,14 @@ public interface XmlTokenSource {
*/
void save(OutputStream os) throws IOException;

/**
* Writes the XML represented by this source to the given zip output stream.
* This method will save the XML declaration, including encoding information,
* with the XML.
*/
void save(ZipOutputStream os) throws IOException;


/**
* Writes the XML represented by this source to the given output.
* Note that this method does not save the XML declaration, including the encoding information.
Expand Down Expand Up @@ -336,6 +345,13 @@ public interface XmlTokenSource {
*/
void save(OutputStream os, XmlOptions options) throws IOException;

/**
* Writes the XML represented by this source to the given zip output stream.
* This method will save the XML declaration, including encoding information,
* with the XML.
*/
void save(ZipOutputStream os, XmlOptions options) throws IOException;

/**
* Writes the XML represented by this source to the given output.
* Note that this method does not save the XML declaration, including the encoding information.
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.isUseJavaShortName()) {
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.isUseJavaShortName()) {
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 @@ -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 @@ -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 String _sourceCodeEncoding ;
private boolean _useJavaShortName;

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(_useJavaShortName) {
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;
}

String getSourceCodeEncoding() {
return _sourceCodeEncoding ;
}

Boolean isUseJavaShortName() {
return _useJavaShortName;
}

@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();
_useJavaShortName = state.useShortName();
_sourceCodeEncoding = state.sourceCodeEncoding();
fixupContainers();
// Checks that data in the containers matches the lookup maps
assertContainersSynchronized();
Expand Down
24 changes: 24 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 _useJavaShortName;
private String _sourceCodeEncoding ;
private final Set<String> _mdefNamespaces = buildDefaultMdefNamespaces();
private EntityResolver _entityResolver;
private File _schemasDir;
Expand Down Expand Up @@ -459,6 +461,12 @@ 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");
}
_useJavaShortName = options.isCompileUseShortJavaName() ||
"true".equals(SystemProperties.getProperty("xmlbean.useshortjavaname", "false"));
_entityResolver = options.getEntityResolver();

if (_entityResolver == null) {
Expand Down Expand Up @@ -523,6 +531,22 @@ 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 ;
}

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

/**
* Get count of recovered errors. Not for public.
*/
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/org/apache/xmlbeans/impl/store/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;
import java.util.zip.ZipOutputStream;

public final class Cursor implements XmlCursor, ChangeListener {
static final int ROOT = Cur.ROOT;
Expand Down Expand Up @@ -509,6 +510,10 @@ public void _save(OutputStream os) throws IOException {
_save(os, null);
}

public void _save(ZipOutputStream zos) throws IOException {
_save(zos, null);
}

public void _save(Writer w) throws IOException {
_save(w, null);
}
Expand Down Expand Up @@ -578,6 +583,26 @@ public void _save(OutputStream os, XmlOptions options) throws IOException {
}
}

public void _save(ZipOutputStream zos, XmlOptions options) throws IOException {
if (zos == null) {
throw new IllegalArgumentException("Null ZipOutputStream specified");
}

try (InputStream is = _newInputStream(options)) {
byte[] bytes = new byte[8192];

for (; ; ) {
int n = is.read(bytes);

if (n < 0) {
break;
}

zos.write(bytes, 0, n);
}
}
}

public void _save(Writer w, XmlOptions options) throws IOException {
if (w == null) {
throw new IllegalArgumentException("Null Writer specified");
Expand Down Expand Up @@ -1974,6 +1999,10 @@ public void save(OutputStream os) throws IOException {
syncWrapIOEx(() -> _save(os));
}

public void save(ZipOutputStream zos) throws IOException {
syncWrapIOEx(() -> _save(zos));
}

public void save(Writer w) throws IOException {
syncWrapIOEx(() -> _save(w));
}
Expand Down Expand Up @@ -2006,6 +2035,10 @@ public void save(OutputStream os, XmlOptions options) throws IOException {
syncWrapIOEx(() -> _save(os, options));
}

public void save(ZipOutputStream zos, XmlOptions options) throws IOException {
syncWrapIOEx(() -> _save(zos, options));
}

public void save(Writer w, XmlOptions options) throws IOException {
syncWrapIOEx(() -> _save(w, options));
}
Expand Down
Loading