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>");
}
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>");
}
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>");
}
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);
}
Aggregations