Package com.sun.codemodel

Examples of com.sun.codemodel.JBlock


    public void write() {
        if (written) return;
       
        written = true;
       
        JBlock b = method.body();

        if (!valueType &&
            (elements.size() > 0
            || buildContext.getGlobalElements().size() > 0
            || xsiTypes.size() > 0
            || attributes.size() > 0
            || unexpectedXsiType != null
            || anyAttribute != null
            || anyElement != null
            || mixedElement != null)) {
            writeMainLoop();
        } else {
            b.add(removeBraces(codeBlock));
            codeBlock.add(removeBraces(preElementBlock));
        }
       
        for (ElementParserBuilderImpl e : states) {
            e.write();
        }
       
        if(!tailBlock.getContents().isEmpty()) {
            preElementBlock.add(removeBraces(tailBlock));
        }

        // Add return statement to the end of the block
        if (returnType != null && _return != null) {
            preElementBlock.add(new JBlankLine());
            setReturnType(returnType);
            preElementBlock._return(_return);
        }
       
        if (root && returnType == null) {
            b.add(new JBlankLine());
            b._return(JExpr._null());
        }

        if (root) {
            writeReadAsType();
            if (baseClass != null) {
View Full Code Here


        m.param(XoXMLStreamReader.class, "reader");
        m.param(buildContext.getMarshalContextClass(), "context");
        JVar typeVar = m.param(QName.class, "type");
        m._throws(XMLStreamException.class);
       
        JBlock block = m.body();
        writeXsiChecks(block, typeVar);
       
        block.add(new JBlankLine());
        block._return(JExpr._null());
    }
View Full Code Here

    /**
     * Write out a loop which will read in a sequence of elements.
     *
     */
    private void writeMainLoop() {
        JBlock b = method.body();
       
        // Add XSI checks
        if (returnType != null || !valueType && depth > 1) {
            writeXsiChecks(b);
        }
       
        // Add the user constructed codeblock and continue from there
        b.add(codeBlock);
        b = codeBlock;

        writeAttributeReader(b);

        // Add the user constructed codeblock and continue from there
        b.add(preElementBlock);
        b = preElementBlock;

        if (!elements.isEmpty() || !xsiTypes.isEmpty() || anyElement != null || mixedElement != null || allowUnknown ) {
            b.add(new JBlankLine());
            b.add(new JLineComment("Read elements"));

            if (forEachChildElement != null && !allowUnknown) {
                b.add(forEachChildElement);
                writeElementReader(elements, forEachChildElement.body(), forEachChildElement.var(), false);
            } else {
                // declare variables used during element reading
                JVar targetDepthVar = b.decl(model._ref(int.class), "targetDepth", xsrVar.invoke("getDepth").plus(JExpr.lit(1)));
                JVar event = b.decl(model._ref(int.class), "event", xsrVar.invoke("nextTagIgnoreAll"));
                JVar depthVar = b.decl(model._ref(int.class), "depth", xsrVar.invoke("getDepth"));

//            JClass sysType = (JClass) model._ref(System.class);
//            if (depth != 1)
//                b.add(sysType.staticRef("out").invoke("println").arg(JExpr.lit("TD ").plus(targetDepthVar)
//                         .plus(JExpr.lit(" Depth: ")).plus(depthVar)
//                         .plus(JExpr.lit(" Name: " + name).plus(JExpr.lit(" Current: "))
//                               .plus(xsrVar.invoke("getName")))));

                JBlock loop = b._while(depthVar.gte(targetDepthVar.minus(JExpr.lit(1)))).body();

                b = loop._if(event.eq(JExpr.lit(XMLStreamConstants.START_ELEMENT)))._then();


                JConditional ifDepth = b._if(depthVar.eq(targetDepthVar));

                Map<QName, ExpectedElement> globalAndLocalEls = new HashMap<QName, ExpectedElement>();
                globalAndLocalEls.putAll(elements);
                globalAndLocalEls.putAll(buildContext.getGlobalElements());
               
                writeElementReader(globalAndLocalEls, ifDepth._then(), xsrVar, false);

                if (allowUnknown) {
                    writeElementReader(buildContext.getGlobalElements(), ifDepth._else(), xsrVar, true);
                }

                JConditional ifHasNext = loop._if(xsrVar.invoke("hasNext"));
                ifHasNext._then().assign(event, xsrVar.invoke("next"));
                ifHasNext._then().assign(depthVar, xsrVar.invoke("getDepth"));
                ifHasNext._else()._break();
            }
        }
View Full Code Here

        for (Map.Entry<QName, ExpectedAttribute> e : attributes.entrySet()) {
            QName name = e.getKey();
            ExpectedAttribute expectedAttribute = e.getValue();

            JExpression qnameCompare = buildQNameCompare(name, attName, attNs);
            JBlock block = attributesBlock.addCondition(qnameCompare);

            List<JExpression> vars = null;
            if (expectedAttribute.getParserBuilder() != null) {
                AttributeParserBuilderImpl builder = expectedAttribute.getParserBuilder();

                // if we have a builder we need to pass the attribute value
                builder.getMethod().param(model._ref(String.class), "_attValue");
                vars = new ArrayList<JExpression>(builder.variables);
                vars.add(attValue);

                // todo why?
                builder.getMethod().body().add(builder.codeBlock);
            }

            writeReader(block, xsrVar, expectedAttribute, vars);
        }

        if (anyAttribute != null) {
            JBlock block = attributesBlock.addCondition(model.ref(XMLConstants.class).staticRef("W3C_XML_SCHEMA_INSTANCE_NS_URI").ne(attNs));
            writeReader(block, xsrVar, anyAttribute, null);
        }
    }
View Full Code Here

        for (Map.Entry<QName, ExpectedXsiType> entry : xsiTypes.entrySet()) {
            QName name = entry.getKey();
            ExpectedXsiType expectedXsiType = entry.getValue();

            JExpression qnameCompare = buildQNameCompare(name, xsiType.invoke("getLocalPart"), xsiType.invoke("getNamespaceURI"));
            JBlock xsiTypeBlock = xsiTypesBlock.addCondition(qnameCompare);

            // xsi:type elements are immediately returned to the caller
            writeReader(xsiTypeBlock, xsrVar, expectedXsiType, null);

            ElementParserBuilderImpl parserBuilder = expectedXsiType.getParserBuilder();
            if (parserBuilder != null && expectedXsiType.getParserBuilder().returnType == null) {
                xsiTypeBlock._return();
            }
        }

        if (unexpectedXsiType != null) {
            JBlock anyBlock = block;
            if (xmlType != null) {
                JExpression localInv = JExpr.lit(xmlType.getLocalPart()).ne(xsiType.invoke("getLocalPart"));
                JExpression nsInv = JExpr.lit(xmlType.getNamespaceURI()).ne(xsiType.invoke("getNamespaceURI"));
                if (xmlType.getNamespaceURI().equals("")) {
                    nsInv = nsInv.cand(xsiType.invoke("getNamespaceURI").ne(JExpr._null()));
View Full Code Here

        JIfElseBlock elementsBlock = new JIfElseBlock();
        block.add(elementsBlock);

        if (mixedElement != null) {
            JBlock stringBlock = elementsBlock.addCondition(xsrVar.invoke("isCharacters"));
            writeReader(stringBlock, xsrVar, mixedElement, null);
        }

        for (Map.Entry<QName, ExpectedElement> entry : elements.entrySet()) {
            QName name = entry.getKey();
            ExpectedElement expectedElement = entry.getValue();

            JExpression qnameCompare = buildQNameCompare(name, xsrVar.invoke("getLocalName"), xsrVar.invoke("getNamespaceURI"));
            JBlock elementBlock = elementsBlock.addCondition(qnameCompare);

            writeReader(elementBlock, xsrVar, expectedElement, expectedElement.getVars());
        }

        if (anyElement != null) {
            JBlock anyBlock = block;
            if (!elementsBlock.ifConditions().isEmpty()) {
                anyBlock = elementsBlock._else().block();
            }
           
            writeReader(anyBlock, xsrVar, anyElement, null);
View Full Code Here

            writeReader(anyBlock, xsrVar, anyElement, null);
        }
    }

    private void writeReader(JBlock block, JVar xsrVar, Expected expected, List<? extends JExpression> vars) {
        JBlock readBlock = expected.getReadBlock();
        AbstractParserBuilder builder = expected.getParserBuilder();
        if (builder != null) {
            JMethod readMethod = builder.getMethod();

            JInvocation invocation = JExpr.invoke(readMethod).arg(xsrVar).arg(rtContextVar);

            // Global reader methods don't have arguments
            if (vars == null) vars = builder.getVariables();
            if (vars != null) {
                for (JExpression var : vars) {
                    invocation.arg(var);
                }
            }

            if (readBlock != null) {
                if (expected.getReadVar() != null) {
                    JVar readVar = expected.getReadVar();
                    readVar.init(invocation);
                } else {
                    readBlock.add(invocation);
                }
                block.add(removeBraces(readBlock));
            } else if (root && builder.returnType != null) {
                block._return(invocation);
            } else {
View Full Code Here

    public List<IfCondition> ifConditions() {
        return ifConditions;
    }

    public JBlock _else() {
        if (elseBlock == null) elseBlock = new JBlock();
        return elseBlock;
    }
View Full Code Here

            return createVar(short.class, Short.class);
        } else if (cls.equals(byte.class) || cls.equals(Byte.class)) {
            return createVar(byte.class, Byte.class);
        } else if (cls.equals(boolean.class) || cls.equals(Boolean.class)) {
            JExpression var = JExpr.direct("_attValue");
            JBlock b = method.body();
            JVar retVar = method.body().decl(model._ref(boolean.class), "value");
           
            JConditional cond = b._if(JExpr.lit("1").invoke("equals").arg(var).cor(JExpr.lit("true").invoke("equals").arg(var)));
            JClass boolClass = (JClass) model._ref(Boolean.class);
           
            cond._then().assign(retVar, boolClass.staticRef("TRUE"));
            cond._else().assign(retVar, boolClass.staticRef("FALSE"));
           
View Full Code Here

        this.model = parent.model;
        currentBlock = method.body();
    }

    public void writeAs(Class cls) {
        JBlock block = currentBlock._if(objectVar.ne(JExpr._null()))._then();
       
        if (cls.equals(String.class)) {
            writeAs(block, objectVar);
        } else if (cls.equals(int.class) || cls.equals(Integer.class)) {
            JClass jc = (JClass) model._ref(Integer.class);
View Full Code Here

TOP

Related Classes of com.sun.codemodel.JBlock

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.