Search in sources :

Example 6 with BXMLSequence

use of org.ballerinalang.model.values.BXMLSequence in project ballerina by ballerina-lang.

the class CPU method execXMLCreationOpcodes.

private static void execXMLCreationOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
    int i;
    int j;
    int k;
    int l;
    BXML<?> xmlVal;
    switch(opcode) {
        case InstructionCodes.NEWXMLELEMENT:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            l = operands[3];
            BXMLQName startTagName = (BXMLQName) sf.refRegs[j];
            BXMLQName endTagName = (BXMLQName) sf.refRegs[k];
            try {
                sf.refRegs[i] = XMLUtils.createXMLElement(startTagName, endTagName, sf.stringRegs[l]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.NEWXMLCOMMENT:
            i = operands[0];
            j = operands[1];
            try {
                sf.refRegs[i] = XMLUtils.createXMLComment(sf.stringRegs[j]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.NEWXMLTEXT:
            i = operands[0];
            j = operands[1];
            try {
                sf.refRegs[i] = XMLUtils.createXMLText(sf.stringRegs[j]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.NEWXMLPI:
            i = operands[0];
            j = operands[1];
            k = operands[2];
            try {
                sf.refRegs[i] = XMLUtils.createXMLProcessingInstruction(sf.stringRegs[j], sf.stringRegs[k]);
            } catch (Exception e) {
                ctx.setError(BLangVMErrors.createError(ctx, e.getMessage()));
                handleError(ctx);
            }
            break;
        case InstructionCodes.XMLSEQSTORE:
            i = operands[0];
            j = operands[1];
            xmlVal = (BXML<?>) sf.refRegs[i];
            BXML<?> child = (BXML<?>) sf.refRegs[j];
            xmlVal.addChildren(child);
            break;
        case InstructionCodes.NEWXMLSEQ:
            i = operands[0];
            sf.refRegs[i] = new BXMLSequence();
            break;
    }
}
Also used : BXMLQName(org.ballerinalang.model.values.BXMLQName) BXML(org.ballerinalang.model.values.BXML) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 7 with BXMLSequence

use of org.ballerinalang.model.values.BXMLSequence in project ballerina by ballerina-lang.

the class JSONUtils method convertToXML.

/**
 * Converts given json object to the corresponding xml.
 *
 * @param json JSON object to get the corresponding xml
 * @param attributePrefix String prefix used for attributes
 * @param arrayEntryTag String used as the tag in the arrays
 * @return BXML XML representation of the given json object
 */
@SuppressWarnings("rawtypes")
public static BXML convertToXML(BJSON json, String attributePrefix, String arrayEntryTag) {
    BXML xml;
    JsonNode jsonNode = json.value();
    List<BXML> omElementArrayList = traverseTree(jsonNode, attributePrefix, arrayEntryTag);
    if (omElementArrayList.size() == 1) {
        xml = omElementArrayList.get(0);
    } else {
        // There is a multi rooted node and create xml sequence from it
        BRefValueArray elementsSeq = new BRefValueArray();
        int count = omElementArrayList.size();
        for (int i = 0; i < count; i++) {
            elementsSeq.add(i, omElementArrayList.get(i));
        }
        xml = new BXMLSequence(elementsSeq);
    }
    return xml;
}
Also used : BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BXML(org.ballerinalang.model.values.BXML) BXMLSequence(org.ballerinalang.model.values.BXMLSequence)

Example 8 with BXMLSequence

use of org.ballerinalang.model.values.BXMLSequence in project ballerina by ballerina-lang.

the class XMLUtils method concatenate.

/**
 * Concatenate two XML sequences and produce a single sequence.
 *
 * @param firstSeq First XML sequence
 * @param secondSeq Second XML sequence
 * @return Concatenated XML sequence
 */
public static BXML<?> concatenate(BXML<?> firstSeq, BXML<?> secondSeq) {
    BRefValueArray concatSeq = new BRefValueArray();
    int j = 0;
    // Load the content fully before concat the two
    firstSeq.build();
    secondSeq.build();
    // Add all the items in the first sequence
    if (firstSeq.getNodeType() == XMLNodeType.SEQUENCE) {
        BRefValueArray seq = ((BXMLSequence) firstSeq).value();
        for (int i = 0; i < seq.size(); i++) {
            concatSeq.add(j++, seq.get(i));
        }
    } else {
        concatSeq.add(j++, firstSeq);
    }
    // Add all the items in the second sequence
    if (secondSeq.getNodeType() == XMLNodeType.SEQUENCE) {
        BRefValueArray seq = ((BXMLSequence) secondSeq).value();
        for (int i = 0; i < seq.size(); i++) {
            concatSeq.add(j++, seq.get(i));
        }
    } else {
        concatSeq.add(j++, secondSeq);
    }
    return new BXMLSequence(concatSeq);
}
Also used : BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BXMLSequence(org.ballerinalang.model.values.BXMLSequence)

Example 9 with BXMLSequence

use of org.ballerinalang.model.values.BXMLSequence in project ballerina by ballerina-lang.

the class XMLUtils method parse.

/**
 * Create a XML sequence from string inputstream.
 *
 * @param xmlStream XML imput stream
 * @return  XML Sequence
 */
@SuppressWarnings("unchecked")
public static BXML<?> parse(InputStream xmlStream) {
    BRefValueArray elementsSeq = new BRefValueArray();
    OMDocument doc;
    try {
        doc = OMXMLBuilderFactory.createOMBuilder(xmlStream).getDocument();
        Iterator<OMNode> docChildItr = doc.getChildren();
        int i = 0;
        while (docChildItr.hasNext()) {
            elementsSeq.add(i++, new BXMLItem(docChildItr.next()));
        }
    } catch (DeferredParsingException e) {
        throw new BallerinaException(e.getCause().getMessage());
    } catch (Throwable e) {
        throw new BallerinaException("failed to create xml: " + e.getMessage());
    }
    return new BXMLSequence(elementsSeq);
}
Also used : DeferredParsingException(org.apache.axiom.om.DeferredParsingException) OMNode(org.apache.axiom.om.OMNode) BXMLItem(org.ballerinalang.model.values.BXMLItem) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) OMDocument(org.apache.axiom.om.OMDocument)

Example 10 with BXMLSequence

use of org.ballerinalang.model.values.BXMLSequence in project ballerina by ballerina-lang.

the class XMLNativeFunctionTest method testSetChildrenWithDefaultNamespace.

@Test
public void testSetChildrenWithDefaultNamespace() {
    BValue[] returns = BRunUtil.invoke(result, "testSetChildrenDefaultNamespace");
    Assert.assertEquals(returns.length, 5);
    Assert.assertTrue(returns[0] instanceof BXML);
    Assert.assertEquals(returns[0].stringValue(), "<name xmlns=\"http://sample.com/test\"><fname>supun</fname>" + "<lname>setunga</lname><residency citizen=\"true\">true</residency></name>");
    // is children seq is empty?
    Assert.assertSame(returns[1].getClass(), BBoolean.class);
    Assert.assertEquals(((BBoolean) returns[1]).booleanValue(), false);
    // is children seq is singleton?
    Assert.assertSame(returns[2].getClass(), BBoolean.class);
    Assert.assertEquals(((BBoolean) returns[2]).booleanValue(), true);
    // Check children
    Assert.assertTrue(returns[3] instanceof BXML);
    BRefValueArray children = ((BXMLSequence) returns[3]).value();
    Assert.assertEquals(children.size(), 3);
    Assert.assertEquals(children.get(0).stringValue(), "<fname xmlns=\"http://sample.com/test\">supun</fname>");
    Assert.assertEquals(children.get(1).stringValue(), "<lname xmlns=\"http://sample.com/test\">setunga</lname>");
    Assert.assertEquals(children.get(2).stringValue(), "<residency xmlns=\"http://sample.com/test\" citizen=\"true\">true</residency>");
    // Check attribute value
    Assert.assertSame(returns[4].getClass(), BString.class);
    Assert.assertEquals(((BString) returns[4]).stringValue(), "true");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BXML(org.ballerinalang.model.values.BXML) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) Test(org.testng.annotations.Test)

Aggregations

BXMLSequence (org.ballerinalang.model.values.BXMLSequence)14 BXML (org.ballerinalang.model.values.BXML)11 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)9 BValue (org.ballerinalang.model.values.BValue)9 Test (org.testng.annotations.Test)9 BXMLItem (org.ballerinalang.model.values.BXMLItem)3 OMNode (org.apache.axiom.om.OMNode)2 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)2 DeferredParsingException (org.apache.axiom.om.DeferredParsingException)1 OMDocument (org.apache.axiom.om.OMDocument)1 OMElement (org.apache.axiom.om.OMElement)1 BJSON (org.ballerinalang.model.values.BJSON)1 BXMLQName (org.ballerinalang.model.values.BXMLQName)1