// add list serializer method to class
String sername = LIST_SERIALIZE_PREFIX + propname;
MethodBuilder sermeth = builder.addMethod(sername, "java.lang.String");
sermeth.addParameter("values", (Type)builder.clone(fieldtype));
if (passctx) {
SingleVariableDeclaration decl = sermeth.addParameter("ictx", "org.jibx.runtime.IMarshallingContext");
decl.modifiers().add(decl.getAST().newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
sermeth.addThrows("org.jibx.runtime.JiBXException");
}
sermeth.setPublicStatic();
sermeth.addSourceComment("Serializer for " + descript + ' ' + term + '.');
sermeth.addSourceComment("");
sermeth.addSourceComment("@param", " values");
sermeth.addSourceComment("@return", " text");
// create a simple null return for null parameter string
BlockBuilder nullblock = builder.newBlock();
nullblock.addReturnNull();
// create block for actual serialization when parameter non-null
BlockBuilder serblock = builder.newBlock();
NewInstanceBuilder newbuff = builder.newInstance("java.lang.StringBuffer");
serblock.addLocalVariableDeclaration("java.lang.StringBuffer", "buff", newbuff);
// create body of loop to handle the conversion
BlockBuilder forblock = builder.newBlock();
// append space to buffer unless empty
InfixExpressionBuilder lengthexpr = builder.buildInfix(Operator.GREATER);
lengthexpr.addOperand(builder.createNormalMethodCall("buff", "length"));
lengthexpr.addNumberLiteralOperand("0");
InvocationBuilder appendcall = builder.createNormalMethodCall("buff", "append");
appendcall.addCharacterLiteralOperand(' ');
BlockBuilder spaceblock = builder.newBlock();
spaceblock.addExpressionStatement(appendcall);
forblock.addIfStatement(lengthexpr, spaceblock);
// load the current value from array
if (reptype == SchemaRootBase.REPEAT_TYPED) {
forblock.addLocalVariableDeclaration(type, "value", builder.createNormalMethodCall("iter", "next"));
} else if (reptype == SchemaRootBase.REPEAT_LIST) {
CastBuilder castexpr = builder.buildCast(type);
castexpr.addOperand(builder.createNormalMethodCall("iter", "next"));
forblock.addLocalVariableDeclaration(type, "value", castexpr);
} else {
forblock.addLocalVariableDeclaration(type, "value", builder.buildArrayIndexAccess("values", "index"));
}
// append the current value to the buffer
appendcall = builder.createNormalMethodCall("buff", "append");
if (valuename != null) {
appendcall.addOperand(builder.createNormalMethodCall("value", valuename));
} else if (valdesername != null) {
InvocationBuilder sercall = builder.createStaticMethodCall(valsername);
sercall.addVariableOperand("value");
if (passctx) {
sercall.addVariableOperand("ictx");
}
appendcall.addOperand(sercall);
} else {
appendcall.addVariableOperand("value");
}
forblock.addExpressionStatement(appendcall);
// build the for loop around the conversion
if (reptype == SchemaRootBase.REPEAT_TYPED) {
Type itertype = builder.createParameterizedType("java.util.Iterator", type);
serblock.addIteratedForStatement("iter", itertype,
builder.createNormalMethodCall("values", "iterator"), forblock);
} else if (reptype == SchemaRootBase.REPEAT_LIST) {
serblock.addIteratedForStatement("iter", builder.createType("java.util.Iterator"),
builder.createNormalMethodCall("values", "iterator"), forblock);
} else {
serblock.addIndexedForStatement("index", "values", forblock);
}
// finish non-null serialization block with buffer conversion
serblock.addReturnExpression(builder.createNormalMethodCall("buff", "toString"));
// finish with the if statement that decides which to execute
InfixExpressionBuilder iftest = builder.buildNameOp("values", Operator.EQUALS);
iftest.addNullOperand();
sermeth.createBlock().addIfElseStatement(iftest, nullblock, serblock);
// add list deserializer method to class
String desername = LIST_DESERIALIZE_PREFIX + propname;
MethodBuilder desermeth = builder.addMethod(desername, (Type)builder.clone(fieldtype));
desermeth.addParameter("text", "java.lang.String");
if (passctx) {
SingleVariableDeclaration decl = desermeth.addParameter("ictx",
"org.jibx.runtime.IUnmarshallingContext");
decl.modifiers().add(decl.getAST().newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
}
desermeth.setPublicStatic();
desermeth.addSourceComment("Deserializer for " + descript + ' ' + term + '.');
desermeth.addSourceComment("");
desermeth.addSourceComment("@param", " text");