diff --git a/modello-plugins/modello-plugin-jdom/src/main/java/org/codehaus/modello/plugin/jdom/JDOMWriterGenerator.java b/modello-plugins/modello-plugin-jdom/src/main/java/org/codehaus/modello/plugin/jdom/JDOMWriterGenerator.java index 1985e4f26..4a94d2788 100644 --- a/modello-plugins/modello-plugin-jdom/src/main/java/org/codehaus/modello/plugin/jdom/JDOMWriterGenerator.java +++ b/modello-plugins/modello-plugin-jdom/src/main/java/org/codehaus/modello/plugin/jdom/JDOMWriterGenerator.java @@ -538,11 +538,25 @@ private void updateClass(ModelClass clazz, JClass jClass, List alway ModelClass toClass = association.getToClass(); if (association.isOneMultiplicity()) { - sc.add(getValueChecker(type, value, field)); - sc.add("{"); - sc.addIndented("update" + capitalise(field.getType()) + "( " + value + ", \"" + fieldTagName - + "\", innerCount, rootElement );"); - sc.add("}"); + // Check if the association type is a class in the model or a simple type + boolean inModel = isClassInModel( + association.getTo(), field.getModelClass().getModel()); + + if (inModel) { + // It's a model class, call the update method for that class + sc.add(getValueChecker(type, value, field)); + sc.add("{"); + sc.addIndented("update" + capitalise(field.getType()) + "( " + value + ", \"" + fieldTagName + + "\", innerCount, rootElement );"); + sc.add("}"); + } else { + // It's a simple type (like String), update it directly + sc.add(getValueChecker(type, value, field)); + sc.add("{"); + sc.addIndented("updateElement( innerCount, root, \"" + fieldTagName + "\" ).setText( " + + getValue(association.getTo(), value, xmlFieldMetadata) + " );"); + sc.add("}"); + } } else { // MANY_MULTIPLICITY diff --git a/modello-plugins/modello-plugin-snakeyaml/src/main/java/org/codehaus/modello/plugin/snakeyaml/SnakeYamlReaderGenerator.java b/modello-plugins/modello-plugin-snakeyaml/src/main/java/org/codehaus/modello/plugin/snakeyaml/SnakeYamlReaderGenerator.java index 532afe6eb..c10d43879 100644 --- a/modello-plugins/modello-plugin-snakeyaml/src/main/java/org/codehaus/modello/plugin/snakeyaml/SnakeYamlReaderGenerator.java +++ b/modello-plugins/modello-plugin-snakeyaml/src/main/java/org/codehaus/modello/plugin/snakeyaml/SnakeYamlReaderGenerator.java @@ -520,24 +520,44 @@ private void processField( String associationName = association.getName(); if (association.isOneMultiplicity()) { - sc.add(tagComparison); + // Check if the association type is a class in the model or a simple type + boolean inModel = isClassInModel( + association.getTo(), field.getModelClass().getModel()); - sc.add("{"); - sc.indent(); + if (inModel) { + // It's a model class, call the parse method for that class + sc.add(tagComparison); + + sc.add("{"); + sc.indent(); - // sc.add( "// consume current key" ); - // sc.add( "parser.getEvent();" ); - sc.add(objectName - + ".set" - + capFieldName - + "( parse" - + association.getTo() - + "( parser, strict" - + trackingArgs - + " ) );"); + // sc.add( "// consume current key" ); + // sc.add( "parser.getEvent();" ); + sc.add(objectName + ".set" + capFieldName + "( parse" + association.getTo() + "( parser, strict" + + trackingArgs + " ) );"); - sc.unindent(); - sc.add("}"); + sc.unindent(); + sc.add("}"); + } else { + // It's a simple type (like String), use primitive field handling + sc.add(tagComparison); + + sc.add("{"); + sc.indent(); + + writePrimitiveField( + field, + association.getTo(), + objectName, + objectName, + "\"" + field.getName() + "\"", + "set" + capFieldName, + sc, + false); + + sc.unindent(); + sc.add("}"); + } } else { // MANY_MULTIPLICITY diff --git a/modello-plugins/modello-plugin-snakeyaml/src/main/java/org/codehaus/modello/plugin/snakeyaml/SnakeYamlWriterGenerator.java b/modello-plugins/modello-plugin-snakeyaml/src/main/java/org/codehaus/modello/plugin/snakeyaml/SnakeYamlWriterGenerator.java index 00da62ff8..830015634 100644 --- a/modello-plugins/modello-plugin-snakeyaml/src/main/java/org/codehaus/modello/plugin/snakeyaml/SnakeYamlWriterGenerator.java +++ b/modello-plugins/modello-plugin-snakeyaml/src/main/java/org/codehaus/modello/plugin/snakeyaml/SnakeYamlWriterGenerator.java @@ -264,17 +264,36 @@ private void writeClass(ModelClass modelClass, JClass jClass) throws ModelloExce ModelAssociation association = (ModelAssociation) field; if (association.isOneMultiplicity()) { - sc.add(getValueChecker(type, value, association)); + // Check if the association type is a class in the model or a simple type + boolean inModel = isClassInModel( + association.getTo(), field.getModelClass().getModel()); - sc.add("{"); - sc.indent(); + if (inModel) { + // It's a model class, call the write method for that class + sc.add(getValueChecker(type, value, association)); + + sc.add("{"); + sc.indent(); + + writeScalarKey(sc, fieldTagName); + sc.add("write" + association.getTo() + "( (" + association.getTo() + ") " + value + + ", generator );"); + + sc.unindent(); + sc.add("}"); + } else { + // It's a simple type (like String), write it directly as a scalar + sc.add(getValueChecker(type, value, association)); - writeScalarKey(sc, fieldTagName); - sc.add("write" + association.getTo() + "( (" + association.getTo() + ") " + value - + ", generator );"); + sc.add("{"); + sc.indent(); - sc.unindent(); - sc.add("}"); + writeScalarKey(sc, fieldTagName); + writeScalar(sc, getValue(association.getTo(), value, xmlFieldMetadata)); + + sc.unindent(); + sc.add("}"); + } } else { // MANY_MULTIPLICITY diff --git a/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxReaderGenerator.java b/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxReaderGenerator.java index 736d77eb1..deeb2a143 100644 --- a/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxReaderGenerator.java +++ b/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxReaderGenerator.java @@ -988,28 +988,46 @@ private void processField( String associationName = association.getName(); if (association.isOneMultiplicity()) { - sc.add(tagComparison); + // Check if the association type is a class in the model or a simple type + boolean inModel = isClassInModel( + association.getTo(), field.getModelClass().getModel()); - sc.add("{"); - sc.indent(); + if (inModel) { + // It's a model class, process as usual + sc.add(tagComparison); - ModelField referenceIdentifierField = getReferenceIdentifierField(association); + sc.add("{"); + sc.indent(); - if (referenceIdentifierField != null) { - addCodeToAddReferences(association, jClass, sc, referenceIdentifierField, objectName); + ModelField referenceIdentifierField = getReferenceIdentifierField(association); - // gobble the rest of the tag - sc.add("while ( xmlStreamReader.getEventType() != XMLStreamConstants.END_ELEMENT )"); - sc.add("{"); - sc.addIndented("xmlStreamReader.next();"); + if (referenceIdentifierField != null) { + addCodeToAddReferences(association, jClass, sc, referenceIdentifierField, objectName); + + // gobble the rest of the tag + sc.add("while ( xmlStreamReader.getEventType() != XMLStreamConstants.END_ELEMENT )"); + sc.add("{"); + sc.addIndented("xmlStreamReader.next();"); + sc.add("}"); + } else { + sc.add(objectName + ".set" + capFieldName + "( parse" + association.getTo() + + "( xmlStreamReader, strict ) );"); + } + + sc.unindent(); sc.add("}"); } else { - sc.add(objectName + ".set" + capFieldName + "( parse" + association.getTo() - + "( xmlStreamReader, strict ) );"); - } + // It's a simple type (like String), use primitive field handling + sc.add(tagComparison); - sc.unindent(); - sc.add("}"); + sc.add("{"); + sc.indent(); + + writePrimitiveField(field, association.getTo(), objectName, "set" + capFieldName, sc); + + sc.unindent(); + sc.add("}"); + } } else { // MANY_MULTIPLICITY diff --git a/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxWriterGenerator.java b/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxWriterGenerator.java index aa0314acb..6793b1003 100644 --- a/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxWriterGenerator.java +++ b/modello-plugins/modello-plugin-stax/src/main/java/org/codehaus/modello/plugin/stax/StaxWriterGenerator.java @@ -349,25 +349,45 @@ private void writeClass(ModelClass modelClass, JClass jClass) throws ModelloExce ModelField referenceIdentifierField = getReferenceIdentifierField(association); if (association.isOneMultiplicity()) { - sc.add(getValueChecker(type, value, association)); - sc.add("{"); - sc.indent(); + // Check if the association type is a class in the model or a simple type + boolean inModel = isClassInModel( + association.getTo(), field.getModelClass().getModel()); - if (referenceIdentifierField != null) { - // if xml.reference, then store as a reference instead + if (inModel) { + // It's a model class, process as usual + sc.add(getValueChecker(type, value, association)); + sc.add("{"); + sc.indent(); - sc.add("serializer.writeStartElement( \"" + fieldTagName + "\" );"); + if (referenceIdentifierField != null) { + // if xml.reference, then store as a reference instead - writeElementAttribute(sc, referenceIdentifierField, value); + sc.add("serializer.writeStartElement( \"" + fieldTagName + "\" );"); - sc.add("serializer.writeEndElement();"); + writeElementAttribute(sc, referenceIdentifierField, value); + + sc.add("serializer.writeEndElement();"); + } else { + sc.add("write" + association.getTo() + "( (" + association.getTo() + ") " + value + ", \"" + + fieldTagName + "\", serializer );"); + } + + sc.unindent(); + sc.add("}"); } else { - sc.add("write" + association.getTo() + "( (" + association.getTo() + ") " + value + ", \"" - + fieldTagName + "\", serializer );"); - } + // It's a simple type (like String), write it directly + sc.add(getValueChecker(type, value, association)); + sc.add("{"); + sc.indent(); - sc.unindent(); - sc.add("}"); + sc.add("serializer.writeStartElement( " + "\"" + fieldTagName + "\" );"); + sc.add("serializer.writeCharacters( " + getValue(association.getTo(), value, xmlFieldMetadata) + + " );"); + sc.add("serializer.writeEndElement();"); + + sc.unindent(); + sc.add("}"); + } } else { // MANY_MULTIPLICITY diff --git a/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3ReaderGenerator.java b/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3ReaderGenerator.java index 6e229ca87..b44766438 100644 --- a/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3ReaderGenerator.java +++ b/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3ReaderGenerator.java @@ -656,12 +656,37 @@ private void processField( String associationName = association.getName(); if (association.isOneMultiplicity()) { - sc.add(tagComparison); + // Check if the association type is a class in the model or a simple type + boolean inModel = isClassInModel( + association.getTo(), field.getModelClass().getModel()); - sc.add("{"); - sc.addIndented(objectName + ".set" + capFieldName + "( parse" + association.getTo() + "( parser, strict" - + trackingArgs + " ) );"); - sc.add("}"); + if (inModel) { + // It's a model class, call the parse method for that class + sc.add(tagComparison); + + sc.add("{"); + sc.addIndented(objectName + ".set" + capFieldName + "( parse" + association.getTo() + + "( parser, strict" + trackingArgs + " ) );"); + sc.add("}"); + } else { + // It's a simple type (like String), use primitive field handling + sc.add(tagComparison); + + sc.add("{"); + sc.indent(); + + writePrimitiveField( + field, + association.getTo(), + objectName, + objectName, + "\"" + field.getName() + "\"", + "set" + capFieldName, + sc); + + sc.unindent(); + sc.add("}"); + } } else { // MANY_MULTIPLICITY diff --git a/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3WriterGenerator.java b/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3WriterGenerator.java index 5bfa472cc..c2ceee835 100644 --- a/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3WriterGenerator.java +++ b/modello-plugins/modello-plugin-xpp3/src/main/java/org/codehaus/modello/plugin/xpp3/Xpp3WriterGenerator.java @@ -329,12 +329,31 @@ private void writeClass(ModelClass modelClass, JClass jClass) throws ModelloExce String associationName = association.getName(); if (association.isOneMultiplicity()) { - sc.add(getValueChecker(type, value, association)); + // Check if the association type is a class in the model or a simple type + boolean inModel = isClassInModel( + association.getTo(), field.getModelClass().getModel()); - sc.add("{"); - sc.addIndented("write" + association.getTo() + "( (" + association.getTo() + ") " + value + ", \"" - + fieldTagName + "\", serializer );"); - sc.add("}"); + if (inModel) { + // It's a model class, call the write method for that class + sc.add(getValueChecker(type, value, association)); + + sc.add("{"); + sc.addIndented("write" + association.getTo() + "( (" + association.getTo() + ") " + value + + ", \"" + fieldTagName + "\", serializer );"); + sc.add("}"); + } else { + // It's a simple type (like String), write it directly as a tag + sc.add(getValueChecker(type, value, association)); + + sc.add("{"); + sc.addIndented("serializer.startTag( NAMESPACE, " + "\"" + fieldTagName + "\" ).text( " + + getValue(association.getTo(), value, xmlFieldMetadata) + " ).endTag( NAMESPACE, " + + "\"" + fieldTagName + "\" );"); + sc.indent(); + writeLocationTracking(sc, uncapClassName, '"' + fieldTagName + '"'); + sc.unindent(); + sc.add("}"); + } } else { // MANY_MULTIPLICITY diff --git a/modello-plugins/modello-plugin-xpp3/src/test/java/org/codehaus/modello/generator/xml/xpp3/SimpleAssociationXpp3GeneratorTest.java b/modello-plugins/modello-plugin-xpp3/src/test/java/org/codehaus/modello/generator/xml/xpp3/SimpleAssociationXpp3GeneratorTest.java new file mode 100644 index 000000000..213851302 --- /dev/null +++ b/modello-plugins/modello-plugin-xpp3/src/test/java/org/codehaus/modello/generator/xml/xpp3/SimpleAssociationXpp3GeneratorTest.java @@ -0,0 +1,46 @@ +package org.codehaus.modello.generator.xml.xpp3; + +/* + * 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; + +public class SimpleAssociationXpp3GeneratorTest extends AbstractModelloJavaGeneratorTest { + public SimpleAssociationXpp3GeneratorTest() { + super("testSimpleAssociation"); + } + + public void testSimpleAssociation() throws Throwable { + ModelloCore modello = (ModelloCore) lookup(ModelloCore.ROLE); + + Model model = modello.loadModel(getXmlResourceReader("/simple-association.mdo")); + + Map parameters = getModelloParameters("1.0.0", 8); + + modello.generate(model, "java", parameters); + modello.generate(model, "xpp3-reader", parameters); + modello.generate(model, "xpp3-writer", parameters); + + compileGeneratedSources(8); + } +} diff --git a/modello-plugins/modello-plugin-xpp3/src/test/resources/simple-association.mdo b/modello-plugins/modello-plugin-xpp3/src/test/resources/simple-association.mdo new file mode 100644 index 000000000..1f637d72c --- /dev/null +++ b/modello-plugins/modello-plugin-xpp3/src/test/resources/simple-association.mdo @@ -0,0 +1,30 @@ + + + test + SimpleAssociationTest + + + package + org.codehaus.modello.generator.xml.xpp3.test.simpleassociation + + + + + Root + 1.0.0+ + + + stringField + 1.0.0+ + + String + 1 + + + + + +