Search in sources :

Example 41 with BXMLItem

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

the class XMLUtils method parse.

/**
 * Create a XML item from string literal.
 *
 * @param xmlStr String representation of the XML
 * @return XML sequence
 */
@SuppressWarnings("unchecked")
public static BXML<?> parse(String xmlStr) {
    try {
        if (xmlStr.isEmpty()) {
            return new BXMLItem(new TextImpl());
        }
        // If this is an XML document, parse it and return an element type XML.
        if (xmlStr.trim().startsWith(XML_DCLR_START)) {
            return new BXMLItem(xmlStr);
        }
        // Here we add a dummy enclosing tag, and send to AXIOM to parse the XML.
        // This is to overcome the issue of axiom not allowing to parse xml-comments,
        // xml-text nodes, and pi nodes, without having an enclosing xml-element node.
        OMElement omElement = AXIOMUtil.stringToOM("<root>" + xmlStr + "</root>");
        Iterator<OMNode> children = omElement.getChildren();
        OMNode omNode = null;
        if (children.hasNext()) {
            omNode = children.next();
        }
        if (children.hasNext()) {
            throw new BallerinaException("xml item must be one of the types: 'element', 'comment', 'text', 'pi'");
        }
        // Here the node is detached from the dummy root, and added to a
        // document element. This is to get the xpath working correctly
        omNode = omNode.detach();
        OMDocument doc = OM_FACTORY.createOMDocument();
        doc.addChild(omNode);
        return new BXMLItem(omNode);
    } catch (BallerinaException e) {
        throw e;
    } catch (OMException | XMLStreamException e) {
        Throwable cause = e.getCause() == null ? e : e.getCause();
        throw new BallerinaException(cause.getMessage());
    } catch (Throwable e) {
        throw new BallerinaException("failed to parse xml: " + e.getMessage());
    }
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) OMNode(org.apache.axiom.om.OMNode) XMLStreamException(javax.xml.stream.XMLStreamException) OMElement(org.apache.axiom.om.OMElement) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) OMException(org.apache.axiom.om.OMException) TextImpl(org.apache.axiom.om.impl.dom.TextImpl) OMDocument(org.apache.axiom.om.OMDocument)

Example 42 with BXMLItem

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

the class XMLUtils method tableToXML.

/**
 * Converts a {@link BTable} to {@link BXML}.
 *
 * @param table {@link BTable} to convert
 * @param isInTransaction   Within a transaction or not
 * @return converted {@link BXML}
 */
@SuppressWarnings("rawtypes")
public static BXML tableToXML(BTable table, boolean isInTransaction) {
    OMSourcedElementImpl omSourcedElement = new OMSourcedElementImpl();
    omSourcedElement.init(new TableOMDataSource(table, null, null, isInTransaction));
    return new BXMLItem(omSourcedElement);
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) TableOMDataSource(org.ballerinalang.model.TableOMDataSource) OMSourcedElementImpl(org.apache.axiom.om.impl.llom.OMSourcedElementImpl)

Example 43 with BXMLItem

use of org.ballerinalang.model.values.BXMLItem 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)

Example 44 with BXMLItem

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

the class XMLUtils method createXMLElement.

/**
 * Create an element type BXML.
 *
 * @param startTagName Name of the start tag of the element
 * @param endTagName Name of the end tag of element
 * @param defaultNsUri Default namespace URI
 * @return BXML Element type BXML
 */
public static BXML<?> createXMLElement(BXMLQName startTagName, BXMLQName endTagName, String defaultNsUri) {
    if (!StringUtils.isEqual(startTagName.getLocalName(), endTagName.getLocalName()) || !StringUtils.isEqual(startTagName.getUri(), endTagName.getUri()) || !StringUtils.isEqual(startTagName.getPrefix(), endTagName.getPrefix())) {
        throw new BallerinaException("start and end tag names mismatch: '" + startTagName + "' and '" + endTagName + "'");
    }
    // Validate whether the tag names are XML supported qualified names, according to the XML recommendation.
    XMLValidationUtils.validateXMLQName(startTagName);
    String nsUri = startTagName.getUri();
    OMElement omElement;
    if (defaultNsUri == null) {
        defaultNsUri = XMLConstants.NULL_NS_URI;
    }
    String prefix = startTagName.getPrefix() == null ? XMLConstants.DEFAULT_NS_PREFIX : startTagName.getPrefix();
    if (nsUri == null) {
        omElement = OM_FACTORY.createOMElement(startTagName.getLocalName(), defaultNsUri, prefix);
    } else if (nsUri.isEmpty()) {
        omElement = OM_FACTORY.createOMElement(startTagName.getLocalName(), nsUri, prefix);
    } else if (nsUri.equals(defaultNsUri)) {
        omElement = OM_FACTORY.createOMElement(startTagName.getLocalName(), defaultNsUri, prefix);
    } else {
        QName qname = getQName(startTagName.getLocalName(), nsUri, prefix);
        omElement = OM_FACTORY.createOMElement(qname);
        if (!defaultNsUri.isEmpty()) {
            omElement.declareDefaultNamespace(defaultNsUri);
        }
    }
    return new BXMLItem(omElement);
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BXMLQName(org.ballerinalang.model.values.BXMLQName) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 45 with BXMLItem

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

the class XMLUtils method createXMLText.

/**
 * Create a comment type BXML.
 *
 * @param content Text content
 * @return BXML Text type BXML
 */
public static BXML<?> createXMLText(String content) {
    // Remove carriage return on windows environments to eliminate additional &#xd; being added
    content = content.replace("\r\n", "\n");
    OMText omText = OM_FACTORY.createOMText(content);
    return new BXMLItem(omText);
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) OMText(org.apache.axiom.om.OMText)

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