Skip to content
Merged
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 @@ -538,11 +538,25 @@ private void updateClass(ModelClass clazz, JClass jClass, List<ModelClass> 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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);
}
}
Loading