if (!property.isXmlAny()) {
// create attribute block
JBlock block = builder.expectAttribute(property.getXmlName());
// add comment for readability
block.add(new JLineComment(property.getXmlStyle() + ": " + property.getName()));
// create collection var if necessary
JVar collectionVar = handleCollection(builder, property, beanVar);
// get the value to evaluate
JExpression value;
if (!property.isCollection()) {
value = builder.getAttributeVar().invoke("getValue");
} else {
JForEach forEach = block.forEach(context.toJClass(String.class), builder.getReadVariableManager().createId(property.getName() + "Item"), builder.getAttributeVar().invoke("getXmlListValue"));
block = forEach.body();
value = forEach.var();
}
// read and set
JExpression toSet = handleAttribute(builder, block, property, value);
doSet(builder, block, property, beanVar, toSet, collectionVar);
} else {
handleAnyAttribute(builder, property, beanVar);
}
}
break;
case ELEMENT:
case ELEMENT_REF: {
JAXBObjectBuilder elementBuilder = builder;
JVar parentVar = beanVar;
if (property.getXmlName() != null && !property.isXmlList()) {
elementBuilder = builder.expectWrapperElement(property.getXmlName(), beanVar, property.getName(), property.isMixed());
}
// create collection var if necessary
JVar collectionVar = handleCollection(elementBuilder, property, parentVar);
for (ElementMapping mapping : property.getElementMappings()) {
// create element block
JBlock block = elementBuilder.expectElement(mapping.getXmlName());
// add comment for readability
block.add(new JLineComment(property.getXmlStyle() + ": " + property.getName()));
// get the value to evaluate
JVar xsrVar = builder.getChildElementVar();
if (!property.isXmlList()) {
// read and set
JExpression toSet = handleElement(builder, xsrVar, block, property, mapping.isNillable(), mapping.getComponentType());
doSet(builder, block, property, parentVar, toSet, collectionVar);
} else {
JForEach forEach = block.forEach(context.toJClass(String.class), builder.getReadVariableManager().createId(property.getName() + "Item"), xsrVar.invoke("getElementAsXmlList"));
block = forEach.body();
JExpression value = forEach.var();
// read and set
String propertyName = property.getName();
if (property.isCollection()) propertyName += "Item";
propertyName = builder.getReadVariableManager().createId(propertyName);
JExpression toSet;
if (property.isIdref() || property.getAdapterType() == null) {
toSet = coerce(builder, xsrVar, value, toClass(mapping.getComponentType()));
} else {
// adapted type
JVar adapterVar = builder.getAdapter(property.getAdapterType());
block.add(new JBlankLine());
// convert raw value into bound type
Class targetType = toClass(mapping.getComponentType());
JVar valueVar = block.decl(context.toJClass(targetType), propertyName);
JTryBlock tryBlock = block._try();
tryBlock.body().assign(valueVar, adapterVar.invoke("unmarshal").arg(value));
JCatchBlock catchException = tryBlock._catch(context.toJClass(Exception.class));
JBlock catchBody = catchException.body();
catchBody.invoke(builder.getReadContextVar(), "xmlAdapterError")
.arg(xsrVar)
.arg(context.dotclass(property.getAdapterType()))
.arg(context.dotclass(targetType))
.arg(context.dotclass(targetType))
.arg(catchException.param("e"));
catchBody._continue();
block.add(new JBlankLine());
toSet = valueVar;
}
// JaxB refs need to be wrapped with a JAXBElement
if (toClass(property.getComponentType()).equals(JAXBElement.class)) {
toSet = newJaxBElement(xsrVar, toClass(mapping.getComponentType()), toSet);
}
doSet(builder, block, property, parentVar, toSet, collectionVar);
}
}
if (property.isXmlAny()) {
// create element block
JBlock block = elementBuilder.expectAnyElement();
// add comment for readability
block.add(new JLineComment(property.getXmlStyle() + ": " + property.getName()));
// read and set
JInvocation toSet = builder.getReadContextVar().invoke("readXmlAny")
.arg(builder.getChildElementVar())
.arg(context.dotclass(property.getComponentType()))
.arg(property.isLax() ? JExpr.TRUE : JExpr.FALSE);
doSet(builder, block, property, parentVar, toSet, collectionVar);
}
if (property.isMixed()) {
// create element block
JBlock block = elementBuilder.expectMixedElement();
// add comment for readability
block.add(new JLineComment(property.getXmlStyle() + " (Mixed): " + property.getName()));
// read and set
JInvocation toSet = builder.getChildElementVar().invoke("getText");
doSet(builder, block, property, parentVar, toSet, collectionVar);
}
}
break;
case VALUE: {
// value is read in class block
JBlock block = builder.expectValue();
// add comment for readability
block.add(new JBlankLine());
block.add(new JLineComment(property.getXmlStyle() + ": " + property.getName()));
// read and set value
handleValue(builder, builder.getXSR(), block, property, beanVar);
}
break;