Skip to content

Commit e46a709

Browse files
committed
[XMLBEANS-644] support generating source when there are elements and attributes with clashing names
git-svn-id: https://svn.apache.org/repos/asf/xmlbeans/trunk@1914513 13f79535-47bb-0310-9956-ffa450edef68
1 parent f9a0ed8 commit e46a709

File tree

4 files changed

+123
-52
lines changed

4 files changed

+123
-52
lines changed

src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ void printStaticTypeDeclaration(SchemaType sType, SchemaTypeSystem system) throw
275275

276276

277277
emit(factoryName + "<" + fullName + "> Factory = new " + factoryName +
278-
"<>(" + sysName + ".TypeSystemHolder.typeSystem, \"" + ((SchemaTypeSystemImpl) system).handleForType(sType) + "\");"
279-
);
278+
"<>(" + sysName + ".TypeSystemHolder.typeSystem, \"" + ((SchemaTypeSystemImpl) system).handleForType(sType) + "\");"
279+
);
280280
emit("org.apache.xmlbeans.SchemaType type = Factory.getType();");
281281
emit("");
282282
}
@@ -318,7 +318,7 @@ void printInnerType(SchemaType sType, SchemaTypeSystem system) throws IOExceptio
318318

319319
void printNestedInnerTypes(SchemaType sType, SchemaTypeSystem system) throws IOException {
320320
boolean redefinition = sType.getName() != null &&
321-
sType.getName().equals(sType.getBaseType().getName());
321+
sType.getName().equals(sType.getBaseType().getName());
322322
while (sType != null) {
323323
SchemaType[] anonTypes = sType.getAnonymousTypes();
324324
for (SchemaType anonType : anonTypes) {
@@ -331,7 +331,7 @@ void printNestedInnerTypes(SchemaType sType, SchemaTypeSystem system) throws IOE
331331
// For redefinition other than by extension for complex types, go ahead and print
332332
// the anonymous types in the base
333333
if (!redefinition ||
334-
(sType.getDerivationType() != SchemaType.DT_EXTENSION && !sType.isSimpleType())) {
334+
(sType.getDerivationType() != SchemaType.DT_EXTENSION && !sType.isSimpleType())) {
335335
break;
336336
}
337337
sType = sType.getBaseType();
@@ -424,7 +424,7 @@ private static SchemaTypeImpl getImpl(SchemaType sType) {
424424

425425
private void emitSpecializedAccessors(SchemaType sType) throws IOException {
426426
if (sType.getSimpleVariety() == SchemaType.ATOMIC &&
427-
sType.getPrimitiveType().getBuiltinTypeCode() == SchemaType.BTC_DECIMAL) {
427+
sType.getPrimitiveType().getBuiltinTypeCode() == SchemaType.BTC_DECIMAL) {
428428
int bits = sType.getDecimalSize();
429429
int parentBits = sType.getBaseType().getDecimalSize();
430430
if (bits != parentBits || sType.getBaseType().getFullJavaName() == null) {
@@ -505,8 +505,8 @@ void printJavaDocBody(String doc) throws IOException{
505505
// add some poor mans code injection protection
506506
// this is not protecting against annotation based RCEs like CVE-2018-16621
507507
String docClean = doc.trim()
508-
.replace("\t", "")
509-
.replace("*/", "* /");
508+
.replace("\t", "")
509+
.replace("*/", "* /");
510510

511511
for (String s : docClean.split("[\\n\\r]+")) {
512512
emit(" * " + s);
@@ -1077,9 +1077,9 @@ void printConstructor(SchemaType sType, String shortName) throws IOException {
10771077
emit("public " + shortName + "(org.apache.xmlbeans.SchemaType sType) {");
10781078
startBlock();
10791079
emit("super(sType" + (sType.getSimpleVariety() == SchemaType.NOT_SIMPLE ?
1080-
"" :
1081-
", " + !sType.isSimpleType()) +
1082-
");");
1080+
"" :
1081+
", " + !sType.isSimpleType()) +
1082+
");");
10831083
endBlock();
10841084

10851085
if (sType.getSimpleVariety() != SchemaType.NOT_SIMPLE) {
@@ -1105,7 +1105,7 @@ void startClass(SchemaType sType, boolean isInner) throws IOException {
11051105
}
11061106

11071107
emit("public " + (isInner ? "static " : "") + "class " + shortName +
1108-
" extends " + baseClass + " implements " + interfaces + " {");
1108+
" extends " + baseClass + " implements " + interfaces + " {");
11091109

11101110
startBlock();
11111111

@@ -1415,16 +1415,7 @@ void printJSetValue(int javaType, String safeVarName, SchemaTypeImpl stype) thro
14151415
emit(em.replace("#VARNAME#", safeVarName) + ";");
14161416
}
14171417

1418-
String getIdentifier(Map<QName, Integer> qnameMap, QName qName) {
1419-
return "PROPERTY_QNAME[" + qnameMap.get(qName) + "]";
1420-
}
1421-
1422-
String getSetIdentifier(Map<QName, Integer> qnameMap, QName qName, Map<QName, Integer> qsetMap) {
1423-
Integer ord = qsetMap.get(qName);
1424-
return ord == null ? getIdentifier(qnameMap, qName) : "PROPERTY_QSET["+ ord + "]";
1425-
}
1426-
1427-
void printStaticFields(SchemaProperty[] properties, Map<QName, Integer> qnameMap, Map<QName, Integer> qsetMap) throws IOException {
1418+
void printStaticFields(SchemaProperty[] properties, Map<SchemaProperty, Identifier> propMap) throws IOException {
14281419
if (properties.length == 0) {
14291420
return;
14301421
}
@@ -1435,7 +1426,7 @@ void printStaticFields(SchemaProperty[] properties, Map<QName, Integer> qnameMap
14351426
indent();
14361427
for (SchemaProperty prop : properties) {
14371428
final QName name = prop.getName();
1438-
qnameMap.put(name, qnameMap.size());
1429+
propMap.put(prop, new Identifier(propMap.size()));
14391430
emit("new QName(\"" + name.getNamespaceURI() + "\", \"" + name.getLocalPart() + "\"),");
14401431
countQSet = Math.max(countQSet, (prop.acceptedNames() == null ? 0 : prop.acceptedNames().length));
14411432
}
@@ -1446,10 +1437,10 @@ void printStaticFields(SchemaProperty[] properties, Map<QName, Integer> qnameMap
14461437
if (countQSet > 1) {
14471438
emit("private static final QNameSet[] PROPERTY_QSET = {");
14481439
for (SchemaProperty prop : properties) {
1449-
final QName name = prop.getName();
14501440
final QName[] qnames = prop.acceptedNames();
1441+
int index = 0;
14511442
if (qnames != null && qnames.length > 1) {
1452-
qsetMap.put(name, qsetMap.size());
1443+
propMap.get(prop).setSetIndex(index++);
14531444
emit("QNameSet.forArray( new QName[] { ");
14541445
indent();
14551446
for (QName qname : qnames) {
@@ -1475,7 +1466,7 @@ void emitImplementationPostamble() throws IOException {
14751466
}
14761467

14771468
void emitAddTarget(String identifier, boolean isAttr, String xtype)
1478-
throws IOException {
1469+
throws IOException {
14791470
if (isAttr) {
14801471
emit("target = (" + xtype + ")get_store().add_attribute_user(" + identifier + ");");
14811472
} else {
@@ -1550,7 +1541,7 @@ void emitGetTarget(String setIdentifier,
15501541
String index,
15511542
int nullBehaviour,
15521543
String xtype)
1553-
throws IOException {
1544+
throws IOException {
15541545
assert setIdentifier != null && identifier != null;
15551546

15561547
emit(xtype + " target = null;");
@@ -1587,7 +1578,7 @@ void emitGetTarget(String setIdentifier,
15871578
}
15881579

15891580
void printListGetterImpl(String propdesc, String propertyName, String wrappedType, boolean xmltype, boolean xget)
1590-
throws IOException {
1581+
throws IOException {
15911582
Set<BeanMethod> bmList = (opt == null) ? null : opt.getCompilePartialMethod();
15921583
if (bmList != null && !bmList.contains(xget ? BeanMethod.XGET_LIST : BeanMethod.GET_LIST)) {
15931584
return;
@@ -1638,11 +1629,11 @@ void printListGetterImpl(String propdesc, String propertyName, String wrappedTyp
16381629
endBlock();
16391630
}
16401631

1641-
void printGetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap, Map<QName, Integer> qsetMap)
1642-
throws IOException {
1632+
void printGetterImpls(SchemaProperty prop, Map<SchemaProperty, Identifier> propMap)
1633+
throws IOException {
16431634
final QName qName = prop.getName();
1644-
final String identifier = getIdentifier(qnameMap, qName);
1645-
final String setIdentifier = getSetIdentifier(qnameMap, qName, qsetMap);
1635+
final String identifier = propMap.get(prop).getIdentifier();
1636+
final String setIdentifier = propMap.get(prop).getSetIdentifier();
16461637
final boolean several = prop.extendsJavaArray();
16471638
final boolean nillable = prop.hasNillable() != SchemaProperty.NEVER;
16481639
final String type = javaTypeForProperty(prop);
@@ -1677,15 +1668,15 @@ void printGetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap, Map<QNa
16771668
emitGetTarget(setIdentifier, identifier, isAttr, "0", NOTHING, jtargetType);
16781669

16791670
if (isAttr && (prop.hasDefault() == SchemaProperty.CONSISTENTLY ||
1680-
prop.hasFixed() == SchemaProperty.CONSISTENTLY)) {
1671+
prop.hasFixed() == SchemaProperty.CONSISTENTLY)) {
16811672
emit("if (target == null) {");
16821673
startBlock();
16831674
makeAttributeDefaultValue(jtargetType, prop, identifier);
16841675
endBlock();
16851676
}
16861677

16871678
emit("return (target == null) ? " + makeMissingValue(javaType) +
1688-
" : " + printJGetValue(javaType, type, (SchemaTypeImpl) prop.getType()) + ";");
1679+
" : " + printJGetValue(javaType, type, (SchemaTypeImpl) prop.getType()) + ";");
16891680

16901681
emitImplementationPostamble();
16911682

@@ -1704,7 +1695,7 @@ void printGetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap, Map<QNa
17041695
emitGetTarget(setIdentifier, identifier, isAttr, "0", NOTHING, xtype);
17051696

17061697
if (isAttr && (prop.hasDefault() == SchemaProperty.CONSISTENTLY ||
1707-
prop.hasFixed() == SchemaProperty.CONSISTENTLY)) {
1698+
prop.hasFixed() == SchemaProperty.CONSISTENTLY)) {
17081699
emit("if (target == null) {");
17091700
startBlock();
17101701
makeAttributeDefaultValue(xtype, prop, identifier);
@@ -1859,11 +1850,11 @@ void printGetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap, Map<QNa
18591850
}
18601851
}
18611852

1862-
void printSetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap, Map<QName, Integer> qsetMap, SchemaType sType)
1863-
throws IOException {
1853+
void printSetterImpls(SchemaProperty prop, Map<SchemaProperty, Identifier> propMap, SchemaType sType)
1854+
throws IOException {
18641855
final QName qName = prop.getName();
1865-
final String identifier = getIdentifier(qnameMap, qName);
1866-
final String setIdentifier = getSetIdentifier(qnameMap, qName, qsetMap);
1856+
final String identifier = propMap.get(prop).getIdentifier();
1857+
final String setIdentifier = propMap.get(prop).getSetIdentifier();
18671858
final boolean several = prop.extendsJavaArray();
18681859
final boolean nillable = prop.hasNillable() != SchemaProperty.NEVER;
18691860
final String type = javaTypeForProperty(prop);
@@ -1895,7 +1886,7 @@ void printSetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap, Map<QNa
18951886
if (xmltype && !isSubstGroup && !isAttr) {
18961887
emitPre(sType, PrePostExtension.OPERATION_SET, identifier, false, several ? "0" : "-1");
18971888
emit("generatedSetterHelperImpl(" + safeVarName + ", " + setIdentifier + ", 0, " +
1898-
"org.apache.xmlbeans.impl.values.XmlObjectBase.KIND_SETTERHELPER_SINGLETON);");
1889+
"org.apache.xmlbeans.impl.values.XmlObjectBase.KIND_SETTERHELPER_SINGLETON);");
18991890
emitPost(sType, PrePostExtension.OPERATION_SET, identifier, false, several ? "0" : "-1");
19001891
} else {
19011892
emitImplementationPreamble();
@@ -2034,13 +2025,13 @@ void printSetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap, Map<QNa
20342025
emit("org.apache.xmlbeans.SimpleValue[] dests = arraySetterHelper(" + safeVarName + "Array.length" + ", " + identifier + ");");
20352026
emit("for ( int i = 0 ; i < dests.length ; i++ ) {");
20362027
emit(" " + getUserTypeStaticHandlerMethod(true, (SchemaTypeImpl) prop.getType())
2037-
+ "(" + safeVarName + "Array[i], dests[i]);");
2028+
+ "(" + safeVarName + "Array[i], dests[i]);");
20382029
emit("}");
20392030
} else {
20402031
emit("org.apache.xmlbeans.SimpleValue[] dests = arraySetterHelper(" + safeVarName + "Array.length" + ", " + identifier + ", " + setIdentifier + ");");
20412032
emit("for ( int i = 0 ; i < dests.length ; i++ ) {");
20422033
emit(" " + getUserTypeStaticHandlerMethod(true, (SchemaTypeImpl) prop.getType())
2043-
+ "(" + safeVarName + "Array[i], dests[i]);");
2034+
+ "(" + safeVarName + "Array[i], dests[i]);");
20442035
emit("}");
20452036
}
20462037
} else {
@@ -2067,7 +2058,7 @@ void printSetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap, Map<QNa
20672058
if (xmltype && !isSubstGroup) {
20682059
emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, "i");
20692060
emit("generatedSetterHelperImpl(" + safeVarName + ", " + setIdentifier + ", i, " +
2070-
"org.apache.xmlbeans.impl.values.XmlObjectBase.KIND_SETTERHELPER_ARRAYITEM);");
2061+
"org.apache.xmlbeans.impl.values.XmlObjectBase.KIND_SETTERHELPER_ARRAYITEM);");
20712062
emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, "i");
20722063
} else {
20732064
emitImplementationPreamble();
@@ -2143,7 +2134,7 @@ void printSetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap, Map<QNa
21432134
} else // This is a subst group case
21442135
{
21452136
emit("(" + jtargetType + ")get_store().insert_element_user(" + setIdentifier + ", " +
2146-
identifier + ", i);");
2137+
identifier + ", i);");
21472138
}
21482139
outdent();
21492140
printJSetValue(javaType, safeVarName, (SchemaTypeImpl) prop.getType());
@@ -2184,7 +2175,7 @@ void printSetterImpls(SchemaProperty prop, Map<QName, Integer> qnameMap, Map<QNa
21842175
} else // This is a subst group case
21852176
{
21862177
emit("target = (" + xtype + ")get_store().insert_element_user(" +
2187-
setIdentifier + ", " + identifier + ", i);");
2178+
setIdentifier + ", " + identifier + ", i);");
21882179
}
21892180
emitPost(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr, "i");
21902181
emit("return target;");
@@ -2264,7 +2255,7 @@ SchemaProperty[] getSchemaProperties(SchemaType sType) {
22642255
}
22652256

22662257
void printInnerTypeImpl(
2267-
SchemaType sType, SchemaTypeSystem system, boolean isInner) throws IOException {
2258+
SchemaType sType, SchemaTypeSystem system, boolean isInner) throws IOException {
22682259
String shortName = sType.getShortJavaImplName();
22692260

22702261
printInnerTypeJavaDoc(sType);
@@ -2277,15 +2268,14 @@ void printInnerTypeImpl(
22772268

22782269
if (!sType.isSimpleType()) {
22792270
SchemaProperty[] properties = getSchemaProperties(sType);
2280-
Map<QName, Integer> qnameMap = new HashMap<>();
2281-
Map<QName, Integer> qsetMap = new HashMap<>();
2282-
printStaticFields(properties, qnameMap, qsetMap);
2271+
Map<SchemaProperty, Identifier> propMap = new HashMap<>();
2272+
printStaticFields(properties, propMap);
22832273

22842274
for (SchemaProperty prop : properties) {
2285-
printGetterImpls(prop, qnameMap, qsetMap);
2275+
printGetterImpls(prop, propMap);
22862276

22872277
if (!prop.isReadOnly()) {
2288-
printSetterImpls(prop, qnameMap, qsetMap, sType);
2278+
printSetterImpls(prop, propMap, sType);
22892279
}
22902280
}
22912281
}
@@ -2398,7 +2388,7 @@ void printInterfaceMethodImpl(String handler, InterfaceExtension.MethodSignature
23982388

23992389
void printNestedTypeImpls(SchemaType sType, SchemaTypeSystem system) throws IOException {
24002390
boolean redefinition = sType.getName() != null &&
2401-
sType.getName().equals(sType.getBaseType().getName());
2391+
sType.getName().equals(sType.getBaseType().getName());
24022392
while (sType != null) {
24032393
SchemaType[] anonTypes = sType.getAnonymousTypes();
24042394
for (SchemaType anonType : anonTypes) {
@@ -2411,7 +2401,7 @@ void printNestedTypeImpls(SchemaType sType, SchemaTypeSystem system) throws IOEx
24112401
// For redefinition by extension, go ahead and print the anonymous
24122402
// types in the base
24132403
if (!redefinition ||
2414-
(sType.getDerivationType() != SchemaType.DT_EXTENSION && !sType.isSimpleType())) {
2404+
(sType.getDerivationType() != SchemaType.DT_EXTENSION && !sType.isSimpleType())) {
24152405
break;
24162406
}
24172407
sType = sType.getBaseType();
@@ -2441,4 +2431,25 @@ public void printHolder(Writer writer, SchemaTypeSystem system, XmlOptions opt,
24412431
outdent();
24422432
emit("}");
24432433
}
2434+
2435+
private static class Identifier {
2436+
private final int getindex;
2437+
private Integer setindex = null;
2438+
2439+
private Identifier(int index) {
2440+
this.getindex = index;
2441+
}
2442+
2443+
public String getIdentifier() {
2444+
return "PROPERTY_QNAME[" + getindex + "]";
2445+
}
2446+
2447+
public String getSetIdentifier() {
2448+
return setindex == null ? getIdentifier() : "PROPERTY_QSET["+ setindex + "]";
2449+
}
2450+
2451+
public void setSetIndex(int setindex) {
2452+
this.setindex = setindex;
2453+
}
2454+
}
24442455
}

src/test/java/compile/scomp/detailed/SchemaCompilerTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,12 @@ void testNoExt() {
146146
SchemaCompiler.compile(params);
147147
assertFalse(hasSevereError(errors), "testNoExt(): failure when executing scomp");
148148
}
149+
150+
@Test
151+
void testXmlBeans644() {
152+
File[] xsdFiles = { new File(scompTestFilesRoot + "xmlbeans644.xsd") };
153+
String outputDirName = "xmlbeans644";
154+
String testname = "testXmlBeans644";
155+
_testCompile(xsdFiles, outputDirName, testname);
156+
}
149157
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Copyright 2004 The Apache Software Foundation
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License. -->
15+
16+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
17+
elementFormDefault="qualified" attributeFormDefault="qualified">
18+
<xs:element name="Object">
19+
<xs:complexType>
20+
<xs:sequence>
21+
<xs:element name="Name" type="xs:string" minOccurs="0"/>
22+
<xs:element name="Code" type="xs:string" minOccurs="0"/>
23+
<xs:element name="Value" type="xs:string" minOccurs="0"/>
24+
</xs:sequence>
25+
<xs:attribute name="Code" type="xs:string"/>
26+
<xs:attribute name="Attr" type="xs:string"/>
27+
</xs:complexType>
28+
</xs:element>
29+
</xs:schema>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Copyright 2004 The Apache Software Foundation
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License. -->
15+
16+
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">
17+
<xb:namespace uri="##any">
18+
<xb:package>example.schema1</xb:package>
19+
</xb:namespace>
20+
<xb:namespace uriprefix="Schema1">
21+
<xb:prefix>Schema1</xb:prefix>
22+
</xb:namespace>
23+
</xb:config>

0 commit comments

Comments
 (0)