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());
}
}
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);
}
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);
}
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);
}
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 
 being added
content = content.replace("\r\n", "\n");
OMText omText = OM_FACTORY.createOMText(content);
return new BXMLItem(omText);
}
Aggregations