Search in sources :

Example 86 with OMText

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);
    }
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) JaxenException(org.jaxen.JaxenException) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 87 with OMText

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;
}
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 88 with OMText

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;
}
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)

Example 89 with OMText

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 &#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)

Example 90 with 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");
    }
}
Also used : OMProcessingInstruction(org.apache.axiom.om.OMProcessingInstruction) OMDocType(org.apache.axiom.om.OMDocType) AxiomDocType(org.apache.axiom.om.impl.intf.AxiomDocType) AxiomProcessingInstruction(org.apache.axiom.om.impl.intf.AxiomProcessingInstruction) OMComment(org.apache.axiom.om.OMComment) AxiomEntityReference(org.apache.axiom.om.impl.intf.AxiomEntityReference) AxiomComment(org.apache.axiom.om.impl.intf.AxiomComment) OMText(org.apache.axiom.om.OMText) TextContent(org.apache.axiom.om.impl.intf.TextContent)

Aggregations

OMText (org.apache.axiom.om.OMText)92 OMElement (org.apache.axiom.om.OMElement)62 OMFactory (org.apache.axiom.om.OMFactory)39 DataHandler (javax.activation.DataHandler)26 OMNode (org.apache.axiom.om.OMNode)21 OMNamespace (org.apache.axiom.om.OMNamespace)10 QName (javax.xml.namespace.QName)8 OMException (org.apache.axiom.om.OMException)8 IOException (java.io.IOException)7 Iterator (java.util.Iterator)7 InputStream (java.io.InputStream)6 OMAttribute (org.apache.axiom.om.OMAttribute)6 Entry (org.apache.synapse.config.Entry)6 ArrayList (java.util.ArrayList)5 DataSource (javax.activation.DataSource)5 ByteArrayDataSource (org.apache.axiom.attachments.ByteArrayDataSource)5 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)5 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)5 OutputStream (java.io.OutputStream)4 StringReader (java.io.StringReader)4