Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -487,19 +487,19 @@ private void updateClass(ModelClass clazz, JClass jClass, List<ModelClass> alway
String namespace = xmlModelMetadata.getNamespace(getGeneratedVersion());

if (namespace != null) {
sc.add("Element root = factory.element( xmlTag, \"" + namespace + "\" );");
sc.add("Element rootElement = factory.element( xmlTag, \"" + namespace + "\" );");
if (xmlModelMetadata.getSchemaLocation() != null) {
String url = xmlModelMetadata.getSchemaLocation(getGeneratedVersion());
sc.add(
"Namespace xsins = Namespace.getNamespace( \"xsi\", \"http://www.w3.org/2001/XMLSchema-instance\" );");
sc.add("root.setAttribute( \"schemaLocation\", \"" + namespace + " " + url + "\", xsins );");
sc.add("rootElement.setAttribute( \"schemaLocation\", \"" + namespace + " " + url + "\", xsins );");
}
} else {
sc.add("Element root = factory.element( xmlTag );");
sc.add("Element rootElement = factory.element( xmlTag );");
}
sc.add("document.setRootElement( root );");
sc.add("document.setRootElement( rootElement );");
} else {
sc.add("Element root = updateElement( counter, element, xmlTag );");
sc.add("Element rootElement = updateElement( counter, element, xmlTag );");
}

sc.add("Counter innerCount = new Counter( counter.getDepth() + 1 );");
Expand Down Expand Up @@ -528,7 +528,7 @@ private void updateClass(ModelClass clazz, JClass jClass, List<ModelClass> alway
if (xmlFieldMetadata.isAttribute()) {
sc.add(getValueChecker(type, value, field));
sc.add("{");
sc.addIndented("root.setAttribute( \"" + fieldTagName + "\", "
sc.addIndented("rootElement.setAttribute( \"" + fieldTagName + "\", "
+ getValue(field.getType(), value, xmlFieldMetadata) + " );");
sc.add("}");
continue;
Expand All @@ -541,7 +541,7 @@ private void updateClass(ModelClass clazz, JClass jClass, List<ModelClass> alway
sc.add(getValueChecker(type, value, field));
sc.add("{");
sc.addIndented("update" + capitalise(field.getType()) + "( " + value + ", \"" + fieldTagName
+ "\", innerCount, root );");
+ "\", innerCount, rootElement );");
sc.add("}");
} else {
// MANY_MULTIPLICITY
Expand All @@ -559,26 +559,27 @@ private void updateClass(ModelClass clazz, JClass jClass, List<ModelClass> alway
String toType = association.getTo();
if (toClass != null) {
if (xmlAssociationMetadata.isWrappedItems()) {
sc.add("iterate" + capitalise(toType) + "( innerCount, root, " + value + ",\""
sc.add("iterate" + capitalise(toType) + "( innerCount, rootElement, " + value + ",\""
+ fieldTagName + "\",\"" + valuesTagName + "\" );");
createIterateMethod(field.getName(), toClass, singular(fieldTagName), jClass);
} else {
// assume flat..
sc.add("iterate2" + capitalise(toType) + "( innerCount, root, " + value + ", \""
sc.add("iterate2" + capitalise(toType) + "( innerCount, rootElement, " + value + ", \""
+ valuesTagName + "\" );");
createIterateMethod2(field.getName(), toClass, singular(fieldTagName), jClass);
}
} else {
// list of strings?
sc.add("findAndReplaceSimpleLists( innerCount, root, " + value + ", \"" + fieldTagName
+ "\", \"" + singular(fieldTagName) + "\" );");
sc.add("findAndReplaceSimpleLists( innerCount, rootElement, " + value + ", \""
+ fieldTagName + "\", \"" + singular(fieldTagName) + "\" );");
}
} else {
// Map or Properties
sc.add(getValueChecker(type, value, field));
sc.add("{");
sc.indent();
sc.add("Element listElement = updateElement( innerCount, root, \"" + fieldTagName + "\" );");
sc.add("Element listElement = updateElement( innerCount, rootElement, \"" + fieldTagName
+ "\" );");
sc.add("Iterator it = " + value + ".keySet().iterator();");
sc.add("Counter propertiesCounter = new Counter( innerCount.getDepth() + 1 );");
sc.add("while ( it.hasNext() )");
Expand All @@ -604,14 +605,14 @@ private void updateClass(ModelClass clazz, JClass jClass, List<ModelClass> alway
}
} else {
if ("DOM".equals(field.getType())) {
sc.add("findAndReplaceXpp3DOM( innerCount, root, \"" + fieldTagName + "\", (Xpp3Dom) " + value
+ " );");
sc.add("findAndReplaceXpp3DOM( innerCount, rootElement, \"" + fieldTagName + "\", (Xpp3Dom) "
+ value + " );");

requiresDomSupport = true;
} else {
sc.add(getValueChecker(type, value, field));
sc.add("{");
sc.addIndented("updateElement( innerCount, root, \"" + fieldTagName + "\" ).setText( "
sc.addIndented("updateElement( innerCount, rootElement, \"" + fieldTagName + "\" ).setText( "
+ getValue(field.getType(), value, xmlFieldMetadata) + " );");
sc.add("}");
}
Expand All @@ -620,7 +621,7 @@ private void updateClass(ModelClass clazz, JClass jClass, List<ModelClass> alway

if (contentField != null) {
XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) contentField.getMetadata(XmlFieldMetadata.ID);
sc.add("root.setText( " + getValue(contentField.getType(), contentValue, xmlFieldMetadata) + " );");
sc.add("rootElement.setText( " + getValue(contentField.getType(), contentValue, xmlFieldMetadata) + " );");
}

sc.unindent();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.codehaus.modello.plugin.jdom;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

import java.util.Map;

import org.codehaus.modello.AbstractModelloJavaGeneratorTest;
import org.codehaus.modello.core.ModelloCore;
import org.codehaus.modello.model.Model;

/**
* Test for issue where "Root" cannot be used as a class name in JDOM writer.
* The generated code would have a variable name conflict between the method parameter
* and a local variable both named "root".
*
* @author Copilot
*/
public class RootClassnameJDOMGeneratorTest extends AbstractModelloJavaGeneratorTest {
public RootClassnameJDOMGeneratorTest() {
super("root-classname");
}

public void testJavaGenerator() throws Throwable {
ModelloCore modello = (ModelloCore) lookup(ModelloCore.ROLE);

Model model = modello.loadModel(getXmlResourceReader("/root-classname.mdo"));

Map<String, Object> parameters = getModelloParameters("1.0.0");

modello.generate(model, "java", parameters);
modello.generate(model, "jdom-writer", parameters);

addDependency("org.jdom", "jdom");
compileGeneratedSources();

// If the code compiles successfully, the test passes
// The compilation would fail if there was a variable name conflict
}
}
56 changes: 56 additions & 0 deletions modello-test/src/main/resources/root-classname.mdo
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you 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.
-->

<model xmlns="http://codehaus-plexus.github.io/MODELLO/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://codehaus-plexus.github.io/MODELLO/2.0.0 https://codehaus-plexus.github.io/modello/xsd/modello-2.0.0.xsd"
xml.namespace="http://codehaus-plexus.github.io/ROOT-CLASSNAME/1.0.0">
<id>root-classname</id>
<name>RootClassname</name>
<description>Test model for issue where Root cannot be used as a class name</description>
<defaults>
<default>
<key>package</key>
<value>org.codehaus.modello.generator.xml.rootclassname</value>
</default>
</defaults>
<classes>
<class rootElement="true" xml.tagName="root">
<name>Root</name>
<version>1.0.0+</version>
<description>Root element class named Root to test variable naming conflict</description>
<fields>
<field>
<name>name</name>
<version>1.0.0+</version>
<type>String</type>
<description>Test field</description>
</field>
<field>
<name>version</name>
<version>1.0.0+</version>
<type>String</type>
<description>Another test field</description>
</field>
</fields>
</class>
</classes>
</model>