use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJsonXMLWithSingleElementAttributesNamespace.
@Test
public void testToJsonXMLWithSingleElementAttributesNamespace() {
String xmlStr = "<ns0:name test=\"5\" xmlns:ns0=\"http://sample0.com/test\">Jack</ns0:name>";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"ns0:name\":{\"@xmlns:ns0\":\"http://sample0.com/test\"," + "\"@test\":\"5\",\"#text\":\"Jack\"}}");
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJsonForEmptySingleElement.
@Test
public void testToJsonForEmptySingleElement() {
String xmlStr = "<key/>";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"key\":\"\"}");
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLNativeFunctionTest method testToJsonForXMLWithTwoLevels.
@Test
public void testToJsonForXMLWithTwoLevels() {
String xmlStr = "<persons><person><name>Jack</name><address>wso2</address></person></persons>";
BValue[] args = { new BXMLItem(xmlStr) };
BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
Assert.assertTrue(returns[0] instanceof BJSON);
Assert.assertEquals(returns[0].stringValue(), "{\"persons\":{\"person\":{\"name\":\"Jack\",\"address\":\"wso2\"}}}");
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class JSONUtils method traverseJsonNode.
/**
* Traverse a JSON node ad produces the corresponding xml items.
*
* @param node {@link JsonNode} to be traversed
* @param nodeName name of the current traversing node
* @param parentElement parent element of the current node
* @param omElementArrayList List of xml iterms generated
* @param attributePrefix String prefix used for attributes
* @param arrayEntryTag String used as the tag in the arrays
* @return List of xml items generated during the traversal.
*/
@SuppressWarnings("rawtypes")
private static OMElement traverseJsonNode(JsonNode node, String nodeName, OMElement parentElement, List<BXML> omElementArrayList, String attributePrefix, String arrayEntryTag) {
OMElement currentRoot = null;
if (nodeName != null) {
// Extract attributes and set to the immediate parent.
if (nodeName.startsWith(attributePrefix)) {
if (!node.isValueNode()) {
throw new BallerinaException("attribute cannot be an object or array");
}
if (parentElement != null) {
String attributeKey = nodeName.substring(1);
// Validate whether the attribute name is an XML supported qualified name, according to the XML
// recommendation.
XMLValidationUtils.validateXMLName(attributeKey);
parentElement.addAttribute(attributeKey, node.asText(), null);
}
return parentElement;
}
// Validate whether the tag name is an XML supported qualified name, according to the XML recommendation.
XMLValidationUtils.validateXMLName(nodeName);
currentRoot = OM_FACTORY.createOMElement(nodeName, null);
}
if (node.isObject()) {
Iterator<Entry<String, JsonNode>> nodeIterator = node.fields();
while (nodeIterator.hasNext()) {
Entry<String, JsonNode> nodeEntry = nodeIterator.next();
JsonNode objectNode = nodeEntry.getValue();
currentRoot = traverseJsonNode(objectNode, nodeEntry.getKey(), currentRoot, omElementArrayList, attributePrefix, arrayEntryTag);
if (nodeName == null) {
// Outermost object
omElementArrayList.add(new BXMLItem(currentRoot));
currentRoot = null;
}
}
} else if (node.isArray()) {
Iterator<JsonNode> arrayItemsIterator = node.elements();
while (arrayItemsIterator.hasNext()) {
JsonNode arrayNode = arrayItemsIterator.next();
currentRoot = traverseJsonNode(arrayNode, arrayEntryTag, currentRoot, omElementArrayList, attributePrefix, arrayEntryTag);
if (nodeName == null) {
// Outermost array
omElementArrayList.add(new BXMLItem(currentRoot));
currentRoot = null;
}
}
} else if (node.isValueNode() && currentRoot != null) {
if (node.isNull()) {
OMNamespace xsiNameSpace = OM_FACTORY.createOMNamespace(XSI_NAMESPACE, XSI_PREFIX);
currentRoot.addAttribute(NIL, "true", xsiNameSpace);
} else {
OMText txt1 = OM_FACTORY.createOMText(currentRoot, node.asText());
currentRoot.addChild(txt1);
}
} else {
throw new BallerinaException("error in converting json to xml");
}
// Set the current constructed root the parent element
if (parentElement != null) {
parentElement.addChild(currentRoot);
currentRoot = parentElement;
}
return currentRoot;
}
use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.
the class XMLUtils method traverseXMLSequence.
/**
* Converts given xml sequence to the corresponding json.
*
* @param xmlSequence XML sequence to traverse
* @param attributePrefix Prefix to use in attributes
* @param preserveNamespaces preserve the namespaces when converting
* @return JsonNode Json node corresponding to the given xml sequence
*/
private static JsonNode traverseXMLSequence(BXMLSequence xmlSequence, String attributePrefix, boolean preserveNamespaces) {
JsonNode jsonNode = null;
BRefValueArray sequence = xmlSequence.value();
long count = sequence.size();
ArrayList<OMElement> childArray = new ArrayList<>();
ArrayList<OMText> textArray = new ArrayList<>();
for (long i = 0; i < count; ++i) {
BXMLItem xmlItem = (BXMLItem) sequence.get(i);
OMNode omNode = xmlItem.value();
if (OMNode.ELEMENT_NODE == omNode.getType()) {
childArray.add((OMElement) omNode);
} else if (OMNode.TEXT_NODE == omNode.getType()) {
textArray.add((OMText) omNode);
}
}
JsonNode textArrayNode = null;
if (textArray.size() > 0) {
// Text nodes are converted into json array
textArrayNode = processTextArray(textArray);
}
if (childArray.size() > 0) {
jsonNode = new JsonNode(Type.OBJECT);
processChildelements(jsonNode, childArray, attributePrefix, preserveNamespaces);
if (textArrayNode != null) {
// When text nodes and elements are mixed, they will set into an array
textArrayNode.add(jsonNode);
}
}
if (textArrayNode != null) {
jsonNode = textArrayNode;
}
return jsonNode;
}
Aggregations