Search in sources :

Example 36 with BXMLItem

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\"}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 37 with BXMLItem

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\":\"\"}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 38 with BXMLItem

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\"}}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 39 with BXMLItem

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;
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) Entry(java.util.Map.Entry) OMNamespace(org.apache.axiom.om.OMNamespace) Iterator(java.util.Iterator) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BString(org.ballerinalang.model.values.BString)

Example 40 with BXMLItem

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;
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) OMNode(org.apache.axiom.om.OMNode) ArrayList(java.util.ArrayList) OMText(org.apache.axiom.om.OMText) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) OMElement(org.apache.axiom.om.OMElement)

Aggregations

BXMLItem (org.ballerinalang.model.values.BXMLItem)56 Test (org.testng.annotations.Test)46 BValue (org.ballerinalang.model.values.BValue)42 BJSON (org.ballerinalang.model.values.BJSON)36 BString (org.ballerinalang.model.values.BString)26 OMNode (org.apache.axiom.om.OMNode)18 BXML (org.ballerinalang.model.values.BXML)17 BStruct (org.ballerinalang.model.values.BStruct)6 OMElement (org.apache.axiom.om.OMElement)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)5 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)4 ArrayList (java.util.ArrayList)3 OMText (org.apache.axiom.om.OMText)3 BXMLSequence (org.ballerinalang.model.values.BXMLSequence)3 OMDocument (org.apache.axiom.om.OMDocument)2 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)2 BFloatArray (org.ballerinalang.model.values.BFloatArray)2 BIntArray (org.ballerinalang.model.values.BIntArray)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)2