Search in sources :

Example 11 with BXMLSequence

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

the class XMLNativeFunctionTest method testCopy.

@Test
public void testCopy() {
    BValue[] returns = BRunUtil.invoke(result, "testCopy");
    Assert.assertEquals(returns.length, 4);
    Assert.assertTrue(returns[0] instanceof BXMLItem);
    Assert.assertEquals(returns[0].stringValue(), "<ns0:name xmlns:ns0=\"http://sample.com/test\"><newFname>" + "supun-new</newFname><newMname>thilina-new</newMname><newLname>setunga-new</newLname></ns0:name>");
    // Check children of the copied xml
    Assert.assertTrue(returns[3] instanceof BXML);
    BRefValueArray children = ((BXMLSequence) ((BXML) returns[0]).children()).value();
    Assert.assertEquals(children.size(), 3);
    Assert.assertEquals(children.get(0).stringValue(), "<newFname>supun-new</newFname>");
    Assert.assertEquals(children.get(1).stringValue(), "<newMname>thilina-new</newMname>");
    Assert.assertEquals(children.get(2).stringValue(), "<newLname>setunga-new</newLname>");
    // 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 of the original xml
    Assert.assertTrue(returns[3] instanceof BXML);
    BRefValueArray originalChildren = ((BXMLSequence) returns[3]).value();
    Assert.assertEquals(originalChildren.size(), 2);
    Assert.assertEquals(originalChildren.get(0).stringValue(), "<fname xmlns:ns0=\"http://sample.com/test\">supun</fname>");
    Assert.assertEquals(originalChildren.get(1).stringValue(), "<lname xmlns:ns0=\"http://sample.com/test\">setunga</lname>");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) 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)

Example 12 with BXMLSequence

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

the class XMLNativeFunctionTest method testSelectDescendantsFromSeq.

@Test
public void testSelectDescendantsFromSeq() {
    BValue[] returns = BRunUtil.invoke(result, "testSelectDescendantsFromSeq");
    Assert.assertTrue(returns[0] instanceof BXML);
    BXMLSequence seq = (BXMLSequence) returns[0];
    Assert.assertEquals(seq.value().size(), 3);
    Assert.assertEquals(seq.stringValue(), "<name xmlns=\"http://ballerinalang.org/\" " + "xmlns:ns0=\"http://ballerinalang.org/aaa\"><name>Supun</name><lname>Setunga</lname></name>" + "<name xmlns=\"http://ballerinalang.org/\" xmlns:ns0=\"http://ballerinalang.org/aaa\">John</name>" + "<name xmlns=\"http://ballerinalang.org/\" xmlns:ns0=\"http://ballerinalang.org/aaa\">Doe</name>");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BXML(org.ballerinalang.model.values.BXML) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) Test(org.testng.annotations.Test)

Example 13 with BXMLSequence

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

the class XMLNativeFunctionTest method testSelectDescendantsWithEmptyNs.

@Test
public void testSelectDescendantsWithEmptyNs() {
    BValue[] returns = BRunUtil.invoke(result, "testSelectDescendantsWithEmptyNs");
    Assert.assertTrue(returns[0] instanceof BXML);
    BXMLSequence seq = (BXMLSequence) returns[0];
    Assert.assertEquals(seq.value().size(), 2);
    Assert.assertEquals(seq.stringValue(), "<name xmlns:ns0=\"http://ballerinalang.org/aaa\"><name>Supun</name>" + "<lname>Setunga</lname></name><name xmlns:ns0=\"http://ballerinalang.org/aaa\">John</name>");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BXML(org.ballerinalang.model.values.BXML) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) Test(org.testng.annotations.Test)

Example 14 with BXMLSequence

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

the class XMLUtils method convertToJSON.

/**
 * Converts given xml object to the corresponding json.
 *
 * @param xml XML object to get the corresponding json
 * @param attributePrefix Prefix to use in attributes
 * @param preserveNamespaces preserve the namespaces when converting
 * @return BJSON JSON representation of the given xml object
 */
@SuppressWarnings("rawtypes")
public static BJSON convertToJSON(BXML xml, String attributePrefix, boolean preserveNamespaces) {
    JsonNode jsonNode = null;
    if (xml instanceof BXMLItem) {
        // Process xml item
        BXMLItem xmlItem = (BXMLItem) xml;
        OMNode omNode = xmlItem.value();
        if (OMNode.ELEMENT_NODE == omNode.getType()) {
            jsonNode = traverseXMLElement((OMElement) omNode, attributePrefix, preserveNamespaces);
        } else if (OMNode.TEXT_NODE == omNode.getType()) {
            jsonNode = JsonParser.parse("\"" + ((OMText) omNode).getText() + "\"");
        } else {
            jsonNode = new JsonNode(Type.OBJECT);
        }
    } else {
        // Process xml sequence
        BXMLSequence xmlSequence = (BXMLSequence) xml;
        if (xmlSequence.isEmpty().booleanValue()) {
            return new BJSON("[]");
        }
        jsonNode = traverseXMLSequence(xmlSequence, attributePrefix, preserveNamespaces);
    }
    return new BJSON(jsonNode);
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) OMNode(org.apache.axiom.om.OMNode) OMElement(org.apache.axiom.om.OMElement) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) BJSON(org.ballerinalang.model.values.BJSON)

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