Author: fanningpj
Date: Sun Dec 10 17:27:38 2023
New Revision: 1914513
URL: http://svn.apache.org/viewvc?rev=1914513&view=rev
Log:
[XMLBEANS-644] support generating source when there are elements and attributes
with clashing names
Added:
xmlbeans/trunk/src/test/resources/xbean/compile/scomp/schemacompiler/xmlbeans644.xsd
xmlbeans/trunk/src/test/resources/xbean/compile/scomp/schemacompiler/xmlbeans644.xsdconfig
Modified:
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java
xmlbeans/trunk/src/test/java/compile/scomp/detailed/SchemaCompilerTests.java
Modified:
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java
URL:
http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java?rev=1914513&r1=1914512&r2=1914513&view=diff
==============================================================================
---
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java
(original)
+++
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java
Sun Dec 10 17:27:38 2023
@@ -275,8 +275,8 @@ public final class SchemaTypeCodePrinter
emit(factoryName + "<" + fullName + "> Factory = new " + factoryName +
- "<>(" + sysName + ".TypeSystemHolder.typeSystem, \"" +
((SchemaTypeSystemImpl) system).handleForType(sType) + "\");"
- );
+ "<>(" + sysName + ".TypeSystemHolder.typeSystem, \"" +
((SchemaTypeSystemImpl) system).handleForType(sType) + "\");"
+ );
emit("org.apache.xmlbeans.SchemaType type = Factory.getType();");
emit("");
}
@@ -318,7 +318,7 @@ public final class SchemaTypeCodePrinter
void printNestedInnerTypes(SchemaType sType, SchemaTypeSystem system)
throws IOException {
boolean redefinition = sType.getName() != null &&
-
sType.getName().equals(sType.getBaseType().getName());
+ sType.getName().equals(sType.getBaseType().getName());
while (sType != null) {
SchemaType[] anonTypes = sType.getAnonymousTypes();
for (SchemaType anonType : anonTypes) {
@@ -331,7 +331,7 @@ public final class SchemaTypeCodePrinter
// For redefinition other than by extension for complex types, go
ahead and print
// the anonymous types in the base
if (!redefinition ||
- (sType.getDerivationType() != SchemaType.DT_EXTENSION &&
!sType.isSimpleType())) {
+ (sType.getDerivationType() != SchemaType.DT_EXTENSION &&
!sType.isSimpleType())) {
break;
}
sType = sType.getBaseType();
@@ -424,7 +424,7 @@ public final class SchemaTypeCodePrinter
private void emitSpecializedAccessors(SchemaType sType) throws IOException
{
if (sType.getSimpleVariety() == SchemaType.ATOMIC &&
- sType.getPrimitiveType().getBuiltinTypeCode() ==
SchemaType.BTC_DECIMAL) {
+ sType.getPrimitiveType().getBuiltinTypeCode() ==
SchemaType.BTC_DECIMAL) {
int bits = sType.getDecimalSize();
int parentBits = sType.getBaseType().getDecimalSize();
if (bits != parentBits || sType.getBaseType().getFullJavaName() ==
null) {
@@ -505,8 +505,8 @@ public final class SchemaTypeCodePrinter
// add some poor mans code injection protection
// this is not protecting against annotation based RCEs like
CVE-2018-16621
String docClean = doc.trim()
- .replace("\t", "")
- .replace("*/", "* /");
+ .replace("\t", "")
+ .replace("*/", "* /");
for (String s : docClean.split("[\\n\\r]+")) {
emit(" * " + s);
@@ -1077,9 +1077,9 @@ public final class SchemaTypeCodePrinter
emit("public " + shortName + "(org.apache.xmlbeans.SchemaType sType)
{");
startBlock();
emit("super(sType" + (sType.getSimpleVariety() ==
SchemaType.NOT_SIMPLE ?
- "" :
- ", " + !sType.isSimpleType()) +
- ");");
+ "" :
+ ", " + !sType.isSimpleType()) +
+ ");");
endBlock();
if (sType.getSimpleVariety() != SchemaType.NOT_SIMPLE) {
@@ -1105,7 +1105,7 @@ public final class SchemaTypeCodePrinter
}
emit("public " + (isInner ? "static " : "") + "class " + shortName +
- " extends " + baseClass + " implements " + interfaces + " {");
+ " extends " + baseClass + " implements " + interfaces + " {");
startBlock();
@@ -1415,16 +1415,7 @@ public final class SchemaTypeCodePrinter
emit(em.replace("#VARNAME#", safeVarName) + ";");
}
- String getIdentifier(Map<QName, Integer> qnameMap, QName qName) {
- return "PROPERTY_QNAME[" + qnameMap.get(qName) + "]";
- }
-
- String getSetIdentifier(Map<QName, Integer> qnameMap, QName qName,
Map<QName, Integer> qsetMap) {
- Integer ord = qsetMap.get(qName);
- return ord == null ? getIdentifier(qnameMap, qName) :
"PROPERTY_QSET["+ ord + "]";
- }
-
- void printStaticFields(SchemaProperty[] properties, Map<QName, Integer>
qnameMap, Map<QName, Integer> qsetMap) throws IOException {
+ void printStaticFields(SchemaProperty[] properties, Map<SchemaProperty,
Identifier> propMap) throws IOException {
if (properties.length == 0) {
return;
}
@@ -1435,7 +1426,7 @@ public final class SchemaTypeCodePrinter
indent();
for (SchemaProperty prop : properties) {
final QName name = prop.getName();
- qnameMap.put(name, qnameMap.size());
+ propMap.put(prop, new Identifier(propMap.size()));
emit("new QName(\"" + name.getNamespaceURI() + "\", \"" +
name.getLocalPart() + "\"),");
countQSet = Math.max(countQSet, (prop.acceptedNames() == null ? 0
: prop.acceptedNames().length));
}
@@ -1446,10 +1437,10 @@ public final class SchemaTypeCodePrinter
if (countQSet > 1) {
emit("private static final QNameSet[] PROPERTY_QSET = {");
for (SchemaProperty prop : properties) {
- final QName name = prop.getName();
final QName[] qnames = prop.acceptedNames();
+ int index = 0;
if (qnames != null && qnames.length > 1) {
- qsetMap.put(name, qsetMap.size());
+ propMap.get(prop).setSetIndex(index++);
emit("QNameSet.forArray( new QName[] { ");
indent();
for (QName qname : qnames) {
@@ -1475,7 +1466,7 @@ public final class SchemaTypeCodePrinter
}
void emitAddTarget(String identifier, boolean isAttr, String xtype)
- throws IOException {
+ throws IOException {
if (isAttr) {
emit("target = (" + xtype + ")get_store().add_attribute_user(" +
identifier + ");");
} else {
@@ -1550,7 +1541,7 @@ public final class SchemaTypeCodePrinter
String index,
int nullBehaviour,
String xtype)
- throws IOException {
+ throws IOException {
assert setIdentifier != null && identifier != null;
emit(xtype + " target = null;");
@@ -1587,7 +1578,7 @@ public final class SchemaTypeCodePrinter
}
void printListGetterImpl(String propdesc, String propertyName, String
wrappedType, boolean xmltype, boolean xget)
- throws IOException {
+ throws IOException {
Set<BeanMethod> bmList = (opt == null) ? null :
opt.getCompilePartialMethod();
if (bmList != null && !bmList.contains(xget ? BeanMethod.XGET_LIST :
BeanMethod.GET_LIST)) {
return;
@@ -1638,11 +1629,11 @@ public final class SchemaTypeCodePrinter
endBlock();
}
- void printGetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap,
Map<QName, Integer> qsetMap)
- throws IOException {
+ void printGetterImpls(SchemaProperty prop, Map<SchemaProperty, Identifier>
propMap)
+ throws IOException {
final QName qName = prop.getName();
- final String identifier = getIdentifier(qnameMap, qName);
- final String setIdentifier = getSetIdentifier(qnameMap, qName,
qsetMap);
+ final String identifier = propMap.get(prop).getIdentifier();
+ final String setIdentifier = propMap.get(prop).getSetIdentifier();
final boolean several = prop.extendsJavaArray();
final boolean nillable = prop.hasNillable() != SchemaProperty.NEVER;
final String type = javaTypeForProperty(prop);
@@ -1677,7 +1668,7 @@ public final class SchemaTypeCodePrinter
emitGetTarget(setIdentifier, identifier, isAttr, "0", NOTHING,
jtargetType);
if (isAttr && (prop.hasDefault() ==
SchemaProperty.CONSISTENTLY ||
- prop.hasFixed() ==
SchemaProperty.CONSISTENTLY)) {
+ prop.hasFixed() == SchemaProperty.CONSISTENTLY)) {
emit("if (target == null) {");
startBlock();
makeAttributeDefaultValue(jtargetType, prop, identifier);
@@ -1685,7 +1676,7 @@ public final class SchemaTypeCodePrinter
}
emit("return (target == null) ? " + makeMissingValue(javaType)
+
- " : " + printJGetValue(javaType, type, (SchemaTypeImpl)
prop.getType()) + ";");
+ " : " + printJGetValue(javaType, type,
(SchemaTypeImpl) prop.getType()) + ";");
emitImplementationPostamble();
@@ -1704,7 +1695,7 @@ public final class SchemaTypeCodePrinter
emitGetTarget(setIdentifier, identifier, isAttr, "0", NOTHING,
xtype);
if (isAttr && (prop.hasDefault() ==
SchemaProperty.CONSISTENTLY ||
- prop.hasFixed() ==
SchemaProperty.CONSISTENTLY)) {
+ prop.hasFixed() == SchemaProperty.CONSISTENTLY)) {
emit("if (target == null) {");
startBlock();
makeAttributeDefaultValue(xtype, prop, identifier);
@@ -1859,11 +1850,11 @@ public final class SchemaTypeCodePrinter
}
}
- void printSetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap,
Map<QName, Integer> qsetMap, SchemaType sType)
- throws IOException {
+ void printSetterImpls(SchemaProperty prop, Map<SchemaProperty, Identifier>
propMap, SchemaType sType)
+ throws IOException {
final QName qName = prop.getName();
- final String identifier = getIdentifier(qnameMap, qName);
- final String setIdentifier = getSetIdentifier(qnameMap, qName,
qsetMap);
+ final String identifier = propMap.get(prop).getIdentifier();
+ final String setIdentifier = propMap.get(prop).getSetIdentifier();
final boolean several = prop.extendsJavaArray();
final boolean nillable = prop.hasNillable() != SchemaProperty.NEVER;
final String type = javaTypeForProperty(prop);
@@ -1895,7 +1886,7 @@ public final class SchemaTypeCodePrinter
if (xmltype && !isSubstGroup && !isAttr) {
emitPre(sType, PrePostExtension.OPERATION_SET, identifier,
false, several ? "0" : "-1");
emit("generatedSetterHelperImpl(" + safeVarName + ", " +
setIdentifier + ", 0, " +
-
"org.apache.xmlbeans.impl.values.XmlObjectBase.KIND_SETTERHELPER_SINGLETON);");
+
"org.apache.xmlbeans.impl.values.XmlObjectBase.KIND_SETTERHELPER_SINGLETON);");
emitPost(sType, PrePostExtension.OPERATION_SET,
identifier, false, several ? "0" : "-1");
} else {
emitImplementationPreamble();
@@ -2034,13 +2025,13 @@ public final class SchemaTypeCodePrinter
emit("org.apache.xmlbeans.SimpleValue[] dests =
arraySetterHelper(" + safeVarName + "Array.length" + ", " + identifier + ");");
emit("for ( int i = 0 ; i < dests.length ; i++ )
{");
emit(" " + getUserTypeStaticHandlerMethod(true,
(SchemaTypeImpl) prop.getType())
- + "(" + safeVarName + "Array[i], dests[i]);");
+ + "(" + safeVarName + "Array[i],
dests[i]);");
emit("}");
} else {
emit("org.apache.xmlbeans.SimpleValue[] dests =
arraySetterHelper(" + safeVarName + "Array.length" + ", " + identifier + ", " +
setIdentifier + ");");
emit("for ( int i = 0 ; i < dests.length ; i++ )
{");
emit(" " + getUserTypeStaticHandlerMethod(true,
(SchemaTypeImpl) prop.getType())
- + "(" + safeVarName + "Array[i], dests[i]);");
+ + "(" + safeVarName + "Array[i],
dests[i]);");
emit("}");
}
} else {
@@ -2067,7 +2058,7 @@ public final class SchemaTypeCodePrinter
if (xmltype && !isSubstGroup) {
emitPre(sType, PrePostExtension.OPERATION_SET, identifier,
isAttr, "i");
emit("generatedSetterHelperImpl(" + safeVarName + ", " +
setIdentifier + ", i, " +
-
"org.apache.xmlbeans.impl.values.XmlObjectBase.KIND_SETTERHELPER_ARRAYITEM);");
+
"org.apache.xmlbeans.impl.values.XmlObjectBase.KIND_SETTERHELPER_ARRAYITEM);");
emitPost(sType, PrePostExtension.OPERATION_SET,
identifier, isAttr, "i");
} else {
emitImplementationPreamble();
@@ -2143,7 +2134,7 @@ public final class SchemaTypeCodePrinter
} else // This is a subst group case
{
emit("(" + jtargetType +
")get_store().insert_element_user(" + setIdentifier + ", " +
- identifier + ", i);");
+ identifier + ", i);");
}
outdent();
printJSetValue(javaType, safeVarName, (SchemaTypeImpl)
prop.getType());
@@ -2184,7 +2175,7 @@ public final class SchemaTypeCodePrinter
} else // This is a subst group case
{
emit("target = (" + xtype +
")get_store().insert_element_user(" +
- setIdentifier + ", " + identifier + ", i);");
+ setIdentifier + ", " + identifier + ", i);");
}
emitPost(sType, PrePostExtension.OPERATION_INSERT, identifier,
isAttr, "i");
emit("return target;");
@@ -2264,7 +2255,7 @@ public final class SchemaTypeCodePrinter
}
void printInnerTypeImpl(
- SchemaType sType, SchemaTypeSystem system, boolean isInner) throws
IOException {
+ SchemaType sType, SchemaTypeSystem system, boolean isInner) throws
IOException {
String shortName = sType.getShortJavaImplName();
printInnerTypeJavaDoc(sType);
@@ -2277,15 +2268,14 @@ public final class SchemaTypeCodePrinter
if (!sType.isSimpleType()) {
SchemaProperty[] properties = getSchemaProperties(sType);
- Map<QName, Integer> qnameMap = new HashMap<>();
- Map<QName, Integer> qsetMap = new HashMap<>();
- printStaticFields(properties, qnameMap, qsetMap);
+ Map<SchemaProperty, Identifier> propMap = new HashMap<>();
+ printStaticFields(properties, propMap);
for (SchemaProperty prop : properties) {
- printGetterImpls(prop, qnameMap, qsetMap);
+ printGetterImpls(prop, propMap);
if (!prop.isReadOnly()) {
- printSetterImpls(prop, qnameMap, qsetMap, sType);
+ printSetterImpls(prop, propMap, sType);
}
}
}
@@ -2398,7 +2388,7 @@ public final class SchemaTypeCodePrinter
void printNestedTypeImpls(SchemaType sType, SchemaTypeSystem system)
throws IOException {
boolean redefinition = sType.getName() != null &&
-
sType.getName().equals(sType.getBaseType().getName());
+ sType.getName().equals(sType.getBaseType().getName());
while (sType != null) {
SchemaType[] anonTypes = sType.getAnonymousTypes();
for (SchemaType anonType : anonTypes) {
@@ -2411,7 +2401,7 @@ public final class SchemaTypeCodePrinter
// For redefinition by extension, go ahead and print the anonymous
// types in the base
if (!redefinition ||
- (sType.getDerivationType() != SchemaType.DT_EXTENSION &&
!sType.isSimpleType())) {
+ (sType.getDerivationType() != SchemaType.DT_EXTENSION &&
!sType.isSimpleType())) {
break;
}
sType = sType.getBaseType();
@@ -2441,4 +2431,25 @@ public final class SchemaTypeCodePrinter
outdent();
emit("}");
}
+
+ private static class Identifier {
+ private final int getindex;
+ private Integer setindex = null;
+
+ private Identifier(int index) {
+ this.getindex = index;
+ }
+
+ public String getIdentifier() {
+ return "PROPERTY_QNAME[" + getindex + "]";
+ }
+
+ public String getSetIdentifier() {
+ return setindex == null ? getIdentifier() : "PROPERTY_QSET["+
setindex + "]";
+ }
+
+ public void setSetIndex(int setindex) {
+ this.setindex = setindex;
+ }
+ }
}
Modified:
xmlbeans/trunk/src/test/java/compile/scomp/detailed/SchemaCompilerTests.java
URL:
http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/compile/scomp/detailed/SchemaCompilerTests.java?rev=1914513&r1=1914512&r2=1914513&view=diff
==============================================================================
---
xmlbeans/trunk/src/test/java/compile/scomp/detailed/SchemaCompilerTests.java
(original)
+++
xmlbeans/trunk/src/test/java/compile/scomp/detailed/SchemaCompilerTests.java
Sun Dec 10 17:27:38 2023
@@ -146,4 +146,12 @@ public class SchemaCompilerTests {
SchemaCompiler.compile(params);
assertFalse(hasSevereError(errors), "testNoExt(): failure when
executing scomp");
}
+
+ @Test
+ void testXmlBeans644() {
+ File[] xsdFiles = { new File(scompTestFilesRoot + "xmlbeans644.xsd") };
+ String outputDirName = "xmlbeans644";
+ String testname = "testXmlBeans644";
+ _testCompile(xsdFiles, outputDirName, testname);
+ }
}
Added:
xmlbeans/trunk/src/test/resources/xbean/compile/scomp/schemacompiler/xmlbeans644.xsd
URL:
http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/resources/xbean/compile/scomp/schemacompiler/xmlbeans644.xsd?rev=1914513&view=auto
==============================================================================
---
xmlbeans/trunk/src/test/resources/xbean/compile/scomp/schemacompiler/xmlbeans644.xsd
(added)
+++
xmlbeans/trunk/src/test/resources/xbean/compile/scomp/schemacompiler/xmlbeans644.xsd
Sun Dec 10 17:27:38 2023
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Copyright 2004 The Apache Software Foundation
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. -->
+
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+ elementFormDefault="qualified" attributeFormDefault="qualified">
+ <xs:element name="Object">
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element name="Name" type="xs:string" minOccurs="0"/>
+ <xs:element name="Code" type="xs:string" minOccurs="0"/>
+ <xs:element name="Value" type="xs:string" minOccurs="0"/>
+ </xs:sequence>
+ <xs:attribute name="Code" type="xs:string"/>
+ <xs:attribute name="Attr" type="xs:string"/>
+ </xs:complexType>
+ </xs:element>
+</xs:schema>
Added:
xmlbeans/trunk/src/test/resources/xbean/compile/scomp/schemacompiler/xmlbeans644.xsdconfig
URL:
http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/resources/xbean/compile/scomp/schemacompiler/xmlbeans644.xsdconfig?rev=1914513&view=auto
==============================================================================
---
xmlbeans/trunk/src/test/resources/xbean/compile/scomp/schemacompiler/xmlbeans644.xsdconfig
(added)
+++
xmlbeans/trunk/src/test/resources/xbean/compile/scomp/schemacompiler/xmlbeans644.xsdconfig
Sun Dec 10 17:27:38 2023
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Copyright 2004 The Apache Software Foundation
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. -->
+
+<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">
+ <xb:namespace uri="##any">
+ <xb:package>example.schema1</xb:package>
+ </xb:namespace>
+ <xb:namespace uriprefix="Schema1">
+ <xb:prefix>Schema1</xb:prefix>
+ </xb:namespace>
+</xb:config>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]