expectedTypes.put(toClass(property.getType()), mapping);
}
}
if (expectedTypes.size() == 1 && !property.isMixed()) {
ElementMapping mapping = property.getElementMappings().iterator().next();
// null check for non-nillable elements
JBlock block = outerBlock;
JConditional nullCond = null;
if (!mapping.isNillable() && !propertyType.isPrimitive()) {
nullCond = outerBlock._if(outerVar.ne(JExpr._null()));
block = nullCond._then();
}
// add space (' ') separator for XmlList
if (property.isXmlList()) {
// if (fooFirst) {
// writer.writeCharacters(" ");
// }
// fooFirst = false;
block._if(firstVar.not())._then().add(builder.getXSW().invoke("writeCharacters").arg(" "));
block.assign(firstVar, JExpr.FALSE);
}
// write element
writeElement(builder, block, mapping, outerVar, propertyType, mapping.isNillable(), property.isXmlList());
// property is required and does not support nill, then an error is reported if the value was null
if (property.isRequired() && !mapping.isNillable() && nullCond != null) {
nullCond._else().invoke(builder.getWriteContextVar(), "unexpectedNullValue").arg(builder.getWriteObject()).arg(property.getName());
}
} else {
JIfElseBlock conditional = new JIfElseBlock();
outerBlock.add(conditional);
if (property.isMixed()) {
// add instance of check
JExpression isInstance = outerVar._instanceof(context.toJClass(String.class));
JBlock block = conditional.addCondition(isInstance);
// declare item variable
JVar itemVar;
if (toClass(property.getComponentType()) == String.class) {
itemVar = outerVar;
} else {
String itemName = builder.getWriteVariableManager().createId("string");
itemVar = block.decl(context.toJClass(String.class), itemName, JExpr.cast(context.toJClass(String.class), outerVar));
}
writeSimpleTypeElement(builder, itemVar, String.class, block);
}
ElementMapping nilMapping = null;
for (Map.Entry<Class, ElementMapping> entry : expectedTypes.entrySet()) {
Class itemType = entry.getKey();
ElementMapping mapping = entry.getValue();
if (mapping.isNillable()) {
if (nilMapping != null && nilMapping != mapping) {
throw new BuildException("Property " + property + " mappings " + mapping.getXmlName() + " and " + nilMapping + " are both nillable. Only one mapping may of an property may be nilable");
}
nilMapping = mapping;
}