use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class AbstractCollectionBindTransform method generateSerializeOnXml.
@Override
public void generateSerializeOnXml(BindTypeContext context, MethodSpec.Builder methodBuilder, String serializerName, TypeName beanClass, String beanName, BindProperty property) {
TypeName elementTypeName = extractTypeParameterName(property);
// @formatter:off
methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property));
switch(collectionType) {
case LIST:
methodBuilder.addStatement("int n=$L.size()", getter(beanName, beanClass, property));
methodBuilder.addStatement("$T item", elementTypeName);
break;
case ARRAY:
methodBuilder.addStatement("int n=$L.length", getter(beanName, beanClass, property));
methodBuilder.addStatement("$T item", elementTypeName);
break;
case SET:
methodBuilder.addStatement("int n=$L.size()", getter(beanName, beanClass, property));
break;
}
if (property.xmlInfo.isWrappedCollection()) {
methodBuilder.addCode("// write wrapper tag\n");
methodBuilder.addStatement("$L.writeStartElement($S)", serializerName, property.label);
}
BindTransform transform = BindTransformer.lookup(elementTypeName);
BindProperty elementProperty = BindProperty.builder(elementTypeName, property).inCollection(true).elementName(property.xmlInfo.labelItem).build();
switch(collectionType) {
case SET:
methodBuilder.beginControlFlow("for ($T item: $L)", elementTypeName, getter(beanName, beanClass, property));
break;
case LIST:
methodBuilder.beginControlFlow("for (int i=0; i<n; i++)");
methodBuilder.addStatement("item=$L.get(i)", getter(beanName, beanClass, property));
break;
case ARRAY:
methodBuilder.beginControlFlow("for (int i=0; i<n; i++)");
methodBuilder.addStatement("item=$L[i]", getter(beanName, beanClass, property));
break;
}
if (!TypeUtility.isTypePrimitive(elementTypeName)) {
methodBuilder.beginControlFlow("if (item==null)");
methodBuilder.addStatement("$L.writeEmptyElement($S)", serializerName, property.xmlInfo.labelItem);
methodBuilder.nextControlFlow("else");
transform.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item", elementProperty);
methodBuilder.endControlFlow();
} else {
transform.generateSerializeOnXml(context, methodBuilder, serializerName, null, "item", elementProperty);
}
methodBuilder.endControlFlow();
if (property.xmlInfo.isWrappedCollection()) {
methodBuilder.addStatement("$L.writeEndElement()", serializerName);
} else {
// if there's no wrap tag, we need to assure that empty collection will be writed.
// to distinguish between first empty element and empty collection, we write an attribute emptyCollection to
// say: this collection is empty
methodBuilder.addCode("// to distinguish between first empty element and empty collection, we write an attribute emptyCollection\n");
methodBuilder.beginControlFlow("if (n==0)");
methodBuilder.addStatement("$L.writeStartElement($S)", serializerName, property.xmlInfo.labelItem);
methodBuilder.addStatement("$L.writeAttribute($S, $S)", serializerName, EMPTY_COLLECTION_ATTRIBUTE_NAME, "true");
methodBuilder.addStatement("$L.writeEndElement()", serializerName);
methodBuilder.endControlFlow();
}
methodBuilder.endControlFlow();
// @formatter:on
}
use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class BindTypeBuilder method generateParseOnJackson.
private static void generateParseOnJackson(BindTypeContext context, BindEntity entity) {
// @formatter:off
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("parseOnJackson").addJavadoc("parse with jackson\n").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(typeName(JsonParser.class), "jacksonParser").returns(typeName(entity.getElement())).addException(Exception.class);
// @formatter:on
// methodBuilder.beginControlFlow("try");
// methodBuilder.addStatement("$T jacksonParser =
// wrapper.jacksonParser", JsonParser.class);
methodBuilder.addStatement("$T instance = new $T()", entity.getElement(), entity.getElement());
methodBuilder.addStatement("String fieldName");
methodBuilder.beginControlFlow("if (jacksonParser.currentToken() == null)");
methodBuilder.addStatement("jacksonParser.nextToken()");
methodBuilder.endControlFlow();
methodBuilder.beginControlFlow("if (jacksonParser.currentToken() != $T.START_OBJECT)", JsonToken.class);
methodBuilder.addStatement("jacksonParser.skipChildren()");
methodBuilder.addStatement("return instance");
methodBuilder.endControlFlow();
methodBuilder.beginControlFlow("while (jacksonParser.nextToken() != $T.END_OBJECT)", JsonToken.class);
methodBuilder.addStatement("fieldName = jacksonParser.getCurrentName()");
methodBuilder.addStatement("jacksonParser.nextToken()");
if (entity.getCollection().size() > 0) {
methodBuilder.addCode("\n// Parse fields:\n");
methodBuilder.beginControlFlow("switch (fieldName)$>");
BindTransform bindTransform;
for (BindProperty item : entity.getCollection()) {
bindTransform = BindTransformer.lookup(item);
methodBuilder.addCode("case $S:\n$>", item.label);
methodBuilder.addCode("// field $L (mapped with $S)\n", item.getName(), item.label);
bindTransform.generateParseOnJackson(context, methodBuilder, "jacksonParser", item.getPropertyType().getTypeName(), "instance", item);
methodBuilder.addCode("$<break;\n");
}
methodBuilder.addCode("default:$>\n");
methodBuilder.addStatement("jacksonParser.skipChildren()");
methodBuilder.addCode("$<break;");
methodBuilder.addCode("$<");
methodBuilder.endControlFlow();
} else {
methodBuilder.addCode("\n// No field to parse\n");
}
methodBuilder.endControlFlow();
methodBuilder.addStatement("return instance");
// methodBuilder.nextControlFlow("catch ($T e)", Exception.class);
// methodBuilder.addStatement("e.printStackTrace()");
// methodBuilder.addStatement("throw new $T(e)",
// KriptonRuntimeException.class);
// methodBuilder.endControlFlow();
context.builder.addMethod(methodBuilder.build());
}
use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class BindTypeBuilder method generateSerializeOnJacksonAsString.
private static void generateSerializeOnJacksonAsString(BindTypeContext context, BindEntity entity) {
// @formatter:off
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("serializeOnJacksonAsString").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(typeName(entity.getElement()), "object").addParameter(typeName(JsonGenerator.class), "jacksonSerializer").returns(Integer.TYPE).addException(Exception.class);
// @formatter:on
// methodBuilder.beginControlFlow("try");
// methodBuilder.addStatement("$T jacksonSerializer =
// wrapper.jacksonGenerator", className(JsonGenerator.class));
methodBuilder.addStatement("jacksonSerializer.writeStartObject()");
methodBuilder.addStatement("int fieldCount=0");
BindTransform bindTransform;
methodBuilder.addCode("\n");
// fields
methodBuilder.addCode("// Serialized Field:\n\n");
for (BindProperty item : entity.getCollection()) {
bindTransform = BindTransformer.lookup(item);
methodBuilder.addCode("// field $L (mapped with $S)\n", item.getName(), item.label);
bindTransform.generateSerializeOnJacksonAsString(context, methodBuilder, "jacksonSerializer", item.getPropertyType().getTypeName(), "object", item);
methodBuilder.addCode("\n");
}
methodBuilder.addStatement("jacksonSerializer.writeEndObject()");
methodBuilder.addStatement("return fieldCount");
// methodBuilder.nextControlFlow("catch($T e)",
// typeName(Exception.class));
// methodBuilder.addStatement("e.printStackTrace()");
// methodBuilder.addStatement("throw (new $T(e))",
// typeName(KriptonRuntimeException.class));
// methodBuilder.endControlFlow();
context.builder.addMethod(methodBuilder.build());
}
use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class BindTypeBuilder method generate.
/**
* @return typeName of generated class
*
* @throws IOException
*/
public static String generate(Filer filer, BindEntity item) throws IOException {
Elements elementUtils = BaseProcessor.elementUtils;
String beanClassName = item.getSimpleName().toString();
boolean needSuffix = true;
if (beanClassName.endsWith(SUFFIX)) {
needSuffix = false;
}
String className = PREFIX + beanClassName + (needSuffix ? SUFFIX : "");
PackageElement pkg = elementUtils.getPackageOf(item.getElement());
String packageName = pkg.isUnnamed() ? "" : pkg.getQualifiedName().toString();
AnnotationProcessorUtilis.infoOnGeneratedClasses(BindType.class, packageName, className);
// @formatter:off
TypeSpec.Builder builder = TypeSpec.classBuilder(className).addAnnotation(AnnotationSpec.builder(BindMap.class).addMember("value", "$T.class", typeName(item.getElement().asType())).build()).addModifiers(Modifier.PUBLIC).superclass(TypeUtility.parameterizedTypeName(className(AbstractMapper.class), typeName(item.getElement().asType())));
BindTypeContext context = new BindTypeContext(builder, TypeUtility.typeName(packageName, className), Modifier.PRIVATE);
// @formatter:on
builder.addJavadoc("This class is binder map for $T\n\n", item.getElement());
JavadocUtility.generateJavadocGeneratedBy(builder);
builder.addJavadoc("@see $T\n", item.getElement());
// order item by order, property typeName
Collections.sort(item.getCollection(), new Comparator<BindProperty>() {
@Override
public int compare(BindProperty lhs, BindProperty rhs) {
int c1 = lhs.order - rhs.order;
if (c1 != 0)
return c1;
return lhs.getName().compareTo(rhs.getName());
}
});
// generate serializeOnJackson
generateSerializeOnJackson(context, item);
// generate serializeOnJacksonAsString
generateSerializeOnJacksonAsString(context, item);
// order item by type (attribute, element, value), order, xmlName
Collections.sort(item.getCollection(), new Comparator<BindProperty>() {
@Override
public int compare(BindProperty lhs, BindProperty rhs) {
int c1 = lhs.xmlInfo.xmlType.ordinal() - rhs.xmlInfo.xmlType.ordinal();
if (c1 != 0)
return c1;
c1 = lhs.order - rhs.order;
if (c1 != 0)
return c1;
return lhs.label.compareTo(rhs.label);
}
});
// generate serializeOnXml
generateSerializeOnXml(context, item);
// generate parseOnJackson
generateParseOnJackson(context, item);
// generate parseOnJacksonAsString
generateParseOnJacksonAsString(context, item);
// generate parseOnXml
generateParseOnXml(context, item);
TypeSpec typeSpec = builder.build();
JavaWriterHelper.writeJava2File(filer, packageName, typeSpec);
return className;
}
use of com.abubusoft.kripton.processor.bind.model.BindProperty in project kripton by xcesco.
the class BindTypeBuilder method generateParserOnXmlCharacters.
/**
* Parse entity properties and write code to read only CData Text fields
*
* @param methodBuilder
* @param entity
*/
private static void generateParserOnXmlCharacters(BindTypeContext context, MethodSpec.Builder methodBuilder, String instanceName, String parserName, BindEntity entity) {
BindTransform bindTransform;
int count = 0;
for (BindProperty property : entity.getCollection()) {
if (property.xmlInfo.xmlType != XmlType.VALUE && property.xmlInfo.xmlType != XmlType.VALUE_CDATA)
continue;
count++;
methodBuilder.beginControlFlow("if (elementName!=null && $L.hasText())", parserName);
methodBuilder.addCode("// property $L\n", property.getName());
bindTransform = BindTransformer.lookup(property);
bindTransform.generateParseOnXml(context, methodBuilder, parserName, property.getPropertyType().getTypeName(), "instance", property);
methodBuilder.endControlFlow();
}
if (count == 0) {
methodBuilder.addCode("// no property is binded to VALUE o CDATA ");
}
}
Aggregations