Package com.envoisolutions.sxc.builder.impl

Examples of com.envoisolutions.sxc.builder.impl.JIfElseBlock$IfCondition


    private void addEnum(EnumInfo enumInfo) {
        JAXBEnumBuilder builder = context.createJAXBEnumBuilder(enumInfo.getType(), enumInfo.getRootElementName(), enumInfo.getSchemaTypeName());

        JMethod method = builder.getParseMethod();

        JIfElseBlock enumCond = new JIfElseBlock();
        method.body().add(enumCond);
        for (Map.Entry<Enum, String> entry : enumInfo.getEnumMap().entrySet()) {
            Enum enumValue = entry.getKey();
            String enumText = entry.getValue();

            JExpression textCompare = JExpr.lit(enumText).invoke("equals").arg(builder.getParseValue());
            JBlock block = enumCond.addCondition(textCompare);
            block._return(context.toJClass(enumInfo.getType()).staticRef(enumValue.name()));
        }

        JInvocation unexpectedInvoke = enumCond._else().invoke(builder.getParseContext(), "unexpectedEnumValue")
                .arg(builder.getParseXSR())
                .arg(context.toJClass(enumInfo.getType()).dotclass())
                .arg(builder.getParseValue());

        for (String expectedValue : enumInfo.getEnumMap().values()) {
            unexpectedInvoke.arg(expectedValue);
        }
        enumCond._else()._return(JExpr._null());

        enumBuilders.put(enumInfo.getType(), builder);
    }
View Full Code Here


    private void add(JAXBObjectBuilder builder, Bean bean) {
        JBlock block = builder.getWriteMethod().body();

        // perform instance checks
        JIfElseBlock ifElseBlock = new JIfElseBlock();
        block.add(ifElseBlock);
        JInvocation unexpectedSubclass = builder.getWriteContextVar().invoke("unexpectedSubclass").arg(builder.getXSW()).arg(builder.getWriteObject()).arg(context.dotclass(bean.getType()));
        for (Bean altBean : getSubstitutionTypes(builder.getType())) {
            if (bean == altBean) continue;

            // add condition
            JBlock altBlock = ifElseBlock.addCondition(context.dotclass(altBean.getType()).eq(builder.getWriteObject().invoke("getClass")));

            // write xsi:type
            QName typeName = altBean.getSchemaTypeName();
            altBlock.invoke(builder.getXSW(), "writeXsiType").arg(typeName.getNamespaceURI()).arg(typeName.getLocalPart());

            // call alternate marshaller
            writeClassWriter(builder, altBean, altBlock, JExpr.cast(context.toJClass(altBean.getType()), builder.getWriteObject()));
            altBlock._return();

            // add as expected subclass arg
            unexpectedSubclass.arg(context.dotclass(altBean.getType()));
        }

        // if the class isn't exactally this bean's type, then we have an unexpceted subclass
        JBlock unknownSubclassBlock = ifElseBlock.addCondition(context.dotclass(builder.getType()).ne(builder.getWriteObject().invoke("getClass")));
        unknownSubclassBlock.add(unexpectedSubclass);
        unknownSubclassBlock._return();

        block.add(new JBlankLine());
View Full Code Here

    private void addEnum(EnumInfo enumInfo) {
        JAXBEnumBuilder builder = context.createJAXBEnumBuilder(enumInfo.getType(), enumInfo.getRootElementName(), enumInfo.getSchemaTypeName());

        JMethod method = builder.getToStringMethod();

        JIfElseBlock enumSwitch = new JIfElseBlock();
        method.body().add(enumSwitch);
        for (Map.Entry<Enum, String> entry : enumInfo.getEnumMap().entrySet()) {
            Enum enumValue = entry.getKey();
            String enumText = entry.getValue();

            JBlock enumCase = enumSwitch.addCondition(context.toJClass(enumInfo.getType()).staticRef(enumValue.name()).eq(builder.getToStringValue()));
            enumCase._return(JExpr.lit(enumText));
        }

        JInvocation unexpectedInvoke = enumSwitch._else().invoke(builder.getToStringContext(), "unexpectedEnumConst")
                .arg(builder.getToStringBean())
                .arg(builder.getToStringParameterName())
                .arg(builder.getToStringValue());

        for (Enum expectedValue : enumInfo.getEnumMap().keySet()) {
            unexpectedInvoke.arg(context.toJClass(enumInfo.getType()).staticRef(expectedValue.name()));
        }
        enumSwitch._else()._return(JExpr._null());

        // switch statements don't seem to compile correctly
        // JSwitch enumSwitch = method.body()._switch(value);
        // for (Map.Entry<Enum, String> entry : bean.getEnumMap().entrySet()) {
        //     Enum enumValue = entry.getKey();
View Full Code Here

                        // 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;
                            }


                            // add instance of check
                            JExpression isInstance = outerVar._instanceof(context.toJClass(itemType));
                            JBlock block = conditional.addCondition(isInstance);

                            // 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);
                            }

                            // declare item variable
                            JVar itemVar;
                            if (toClass(property.getComponentType()) == itemType) {
                                itemVar = outerVar;
                            } else {
                                String itemName = builder.getWriteVariableManager().createId(itemType.getSimpleName());
                                itemVar = block.decl(context.toJClass(itemType), itemName, JExpr.cast(context.toJClass(itemType), outerVar));
                            }
                            writeElement(builder, block, mapping, itemVar, itemType, false, property.isXmlList());
                        }

                        // if item was null, write xsi:nil or report an error
                        JBlock nullBlock = conditional.addCondition(outerVar.eq(JExpr._null()));
                        if (nilMapping != null) {
                            // write start element
                            QName name = nilMapping.getXmlName();
                            nullBlock.add(builder.getWriteStartElement(name));

                            // write xsi:nil
                            nullBlock.add(builder.getXSW().invoke("writeXsiNil"));

                            // close element
                            nullBlock.add(builder.getXSW().invoke("writeEndElement"));
                        } else {
                            nullBlock.invoke(builder.getWriteContextVar(), "unexpectedNullValue").arg(builder.getWriteObject()).arg(property.getName());
                        }

                        // if not a recogonized type or null, report unknown type error
                        JInvocation unexpected = conditional._else().invoke(builder.getWriteContextVar(), "unexpectedElementType").arg(builder.getXSW()).arg(builder.getWriteObject()).arg(property.getName()).arg(outerVar);
                        for (Class expectedType : expectedTypes.keySet()) {
                            unexpected.arg(context.dotclass(expectedType));
                        }
                    }
                    break;
                case ELEMENT_REF:
                    JBlock block = builder.getWriteMethod().body();

                    JVar itemVar = propertyVar;
                    if (property.isCollection()) {
                        JBlock collectionNotNull = block._if(propertyVar.ne(JExpr._null()))._then();

                        JType itemType;
                        if (!toClass(property.getComponentType()).isPrimitive()) {
                            itemType = context.getGenericType(property.getComponentType());
                        } else {
                            itemType = context.toJType((Class<?>) toClass(property.getComponentType()));
                        }

                        String itemName = builder.getWriteVariableManager().createId( property.getName() + "Item");
                        JForEach each = collectionNotNull.forEach(itemType, itemName, propertyVar);

                        JBlock newBody = each.body();
                        block = newBody;
                        itemVar = each.var();
                    }

                    // process value through adapter
                    itemVar = writeAdapterConversion(builder, block, property, itemVar);

                    if (property.isMixed()) {
                        // add instance of check
                        JExpression isInstance = itemVar._instanceof(context.toJClass(String.class));
                        JConditional conditional = block._if(isInstance);

                        // declare item variable
                        JVar stringVar;
                        if (toClass(property.getComponentType()) == String.class) {
                            stringVar = itemVar;
                        } else {
                            String itemName = builder.getWriteVariableManager().createId("string");
                            stringVar = conditional._then().decl(context.toJClass(String.class), itemName, JExpr.cast(context.toJClass(String.class), itemVar));
                        }
                        writeSimpleTypeElement(builder, stringVar, String.class, conditional._then());

                        block = conditional._else();
                    }


                    if (!property.isXmlAny()) {
                        block.invoke(builder.getWriteContextVar(), "unexpectedElementRef").arg(builder.getXSW()).arg(builder.getWriteObject()).arg(property.getName()).arg(itemVar);
View Full Code Here

TOP

Related Classes of com.envoisolutions.sxc.builder.impl.JIfElseBlock$IfCondition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.