Search in sources :

Example 6 with BindTransform

use of com.abubusoft.kripton.processor.bind.transform.BindTransform in project kripton by xcesco.

the class BindTypeBuilder method generateParseOnXmlAttributes.

private static void generateParseOnXmlAttributes(BindTypeContext context, MethodSpec.Builder methodBuilder, BindEntity entity) {
    BindTransform bindTransform;
    int count = 0;
    // count property to manage
    {
        // for each elements
        for (BindProperty property : entity.getCollection()) {
            if (property.xmlInfo.xmlType != XmlType.ATTRIBUTE)
                continue;
            count++;
        }
    }
    if (count > 0) {
        // @formatter:off
        methodBuilder.addCode("\n// attributes \n");
        methodBuilder.addStatement("String attributeName = null");
        // methodBuilder.addStatement("String attributeValue = null");
        methodBuilder.addStatement("int attributesCount = xmlParser.getAttributeCount();");
        methodBuilder.beginControlFlow("for (int attributeIndex = 0; attributeIndex < attributesCount; attributeIndex++)");
        methodBuilder.addStatement("attributeName = xmlParser.getAttributeName(attributeIndex)");
        // methodBuilder.addStatement("attributeValue =
        // $T.unescapeXml(xmlParser.getAttributeValue(attributeIndex))",
        // StringEscapeUtils.class);
        methodBuilder.beginControlFlow("switch(attributeName)$>");
        for (BindProperty property : entity.getCollection()) {
            if (property.xmlInfo.xmlType != XmlType.ATTRIBUTE)
                continue;
            methodBuilder.addCode("case $S:\n$>", property.label);
            bindTransform = BindTransformer.lookup(property);
            methodBuilder.addCode("// field $L (mapped by $S)\n", property.getName(), property.label);
            bindTransform.generateParseOnXml(context, methodBuilder, "xmlParser", property.getPropertyType().getTypeName(), "instance", property);
            methodBuilder.addStatement("$<break");
        }
        methodBuilder.addCode("default:\n$>");
        methodBuilder.addStatement("$<break$<");
        methodBuilder.endControlFlow();
        methodBuilder.endControlFlow();
    // @formatter:on
    } else {
        methodBuilder.addCode("// No attributes found\n");
    }
}
Also used : BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty)

Example 7 with BindTransform

use of com.abubusoft.kripton.processor.bind.transform.BindTransform in project kripton by xcesco.

the class BindTypeBuilder method generateParseOnJacksonAsString.

private static void generateParseOnJacksonAsString(BindTypeContext context, BindEntity entity) {
    // @formatter:off
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("parseOnJacksonAsString").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.getCurrentToken() == null)");
    methodBuilder.addStatement("jacksonParser.nextToken()");
    methodBuilder.endControlFlow();
    methodBuilder.beginControlFlow("if (jacksonParser.getCurrentToken() != $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.generateParseOnJacksonAsString(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());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 8 with BindTransform

use of com.abubusoft.kripton.processor.bind.transform.BindTransform 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());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 9 with BindTransform

use of com.abubusoft.kripton.processor.bind.transform.BindTransform 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());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty)

Example 10 with BindTransform

use of com.abubusoft.kripton.processor.bind.transform.BindTransform 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 ");
    }
}
Also used : BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty)

Aggregations

BindTransform (com.abubusoft.kripton.processor.bind.transform.BindTransform)13 BindProperty (com.abubusoft.kripton.processor.bind.model.BindProperty)11 MethodSpec (com.squareup.javapoet.MethodSpec)9 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 Bind (com.abubusoft.kripton.annotation.Bind)1 BindAdapter (com.abubusoft.kripton.annotation.BindAdapter)1 BindDisabled (com.abubusoft.kripton.annotation.BindDisabled)1 BindType (com.abubusoft.kripton.annotation.BindType)1 BindXml (com.abubusoft.kripton.annotation.BindXml)1 BindEntity (com.abubusoft.kripton.processor.bind.model.BindEntity)1 ByteArrayBindTransform (com.abubusoft.kripton.processor.bind.transform.ByteArrayBindTransform)1 ObjectBindTransform (com.abubusoft.kripton.processor.bind.transform.ObjectBindTransform)1 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)1 IncompatibleAnnotationException (com.abubusoft.kripton.processor.exceptions.IncompatibleAnnotationException)1 IncompatibleAttributesInAnnotationException (com.abubusoft.kripton.processor.exceptions.IncompatibleAttributesInAnnotationException)1 InvalidDefinition (com.abubusoft.kripton.processor.exceptions.InvalidDefinition)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1 Elements (javax.lang.model.util.Elements)1