use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.
the class Target method insert.
/**
* Inserts the given object into the target specified by the current Target object.
* @param synCtx Message Context to be enriched with the object.
* @param object Object to be inserted.
*/
public void insert(MessageContext synCtx, Object object) {
if (value.getExpression() != null && value.getExpression() instanceof SynapseXPath) {
SynapseXPath expression = (SynapseXPath) value.getExpression();
Object targetObj = null;
try {
targetObj = expression.selectSingleNode(synCtx);
} catch (JaxenException e) {
handleException("Failed to select the target.", e);
}
if (targetObj instanceof OMText) {
Object targetParent = ((OMText) targetObj).getParent();
if (targetParent != null && targetParent instanceof OMElement) {
((OMElement) targetParent).setText(object == null ? "" : object.toString());
} else {
handleException("Invalid target is specified by the expression: " + expression);
}
} else {
handleException("Invalid target is specified by the expression: " + expression);
}
} else if (value.getKeyValue() != null) {
synCtx.setProperty(value.getKeyValue(), object);
} else {
handleException("Invalid target description. " + value);
}
}
use of org.apache.axiom.om.OMText 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.apache.axiom.om.OMText 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;
}
use of org.apache.axiom.om.OMText 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);
}
use of org.apache.axiom.om.OMText in project webservices-axiom by apache.
the class OMFactoryImpl method importChildNode.
private AxiomChildNode importChildNode(OMNode child) {
int type = child.getType();
switch(type) {
case OMNode.ELEMENT_NODE:
return importElement((OMElement) child, AxiomElement.class);
case OMNode.TEXT_NODE:
case OMNode.SPACE_NODE:
case OMNode.CDATA_SECTION_NODE:
{
OMText text = (OMText) child;
Object content;
if (text.isBinary()) {
content = new TextContent(text.getContentID(), text.getDataHandler(), text.isOptimized());
} else {
content = text.getText();
}
return createAxiomText(null, content, type);
}
case OMNode.PI_NODE:
{
OMProcessingInstruction pi = (OMProcessingInstruction) child;
AxiomProcessingInstruction importedPI = createNode(AxiomProcessingInstruction.class);
importedPI.setTarget(pi.getTarget());
importedPI.setValue(pi.getValue());
return importedPI;
}
case OMNode.COMMENT_NODE:
{
OMComment comment = (OMComment) child;
AxiomComment importedComment = createNode(AxiomComment.class);
importedComment.setValue(comment.getValue());
return importedComment;
}
case OMNode.DTD_NODE:
{
OMDocType docType = (OMDocType) child;
AxiomDocType importedDocType = createNode(AxiomDocType.class);
importedDocType.coreSetRootName(docType.getRootName());
importedDocType.coreSetPublicId(docType.getPublicId());
importedDocType.coreSetSystemId(docType.getSystemId());
importedDocType.coreSetInternalSubset(docType.getInternalSubset());
return importedDocType;
}
case OMNode.ENTITY_REFERENCE_NODE:
AxiomEntityReference importedEntityRef = createNode(AxiomEntityReference.class);
importedEntityRef.coreSetName(((OMEntityReference) child).getName());
return importedEntityRef;
default:
throw new IllegalArgumentException("Unsupported node type");
}
}
Aggregations