Search in sources :

Example 31 with OMText

use of org.apache.axiom.om.OMText in project webservices-axiom by apache.

the class TestInsertSiblingBeforeOnOrphan method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory fac = metaFactory.getOMFactory();
    OMText text1 = fac.createOMText("text1");
    OMText text2 = fac.createOMText("text2");
    try {
        text1.insertSiblingAfter(text2);
        fail("Expected OMException because node has no parent");
    } catch (OMException ex) {
    // Expected
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMText(org.apache.axiom.om.OMText) OMException(org.apache.axiom.om.OMException)

Example 32 with OMText

use of org.apache.axiom.om.OMText in project wso2-axis2-transports by wso2.

the class Echo method echoMTOMtoBase64.

public OMElement echoMTOMtoBase64(OMElement omEle) {
    OMText omText = (OMText) (omEle.getFirstElement()).getFirstOMChild();
    omText.setOptimize(false);
    return omEle;
}
Also used : OMText(org.apache.axiom.om.OMText)

Example 33 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class Target method insert.

public void insert(MessageContext synContext, ArrayList<OMNode> sourceNodeList, SynapseLog synLog) throws JaxenException {
    if (targetType == EnrichMediator.CUSTOM) {
        assert xpath != null : "Xpath cannot be null for CUSTOM";
        if (sourceNodeList.isEmpty()) {
            synLog.error("Cannot Enrich message from an empty source.");
            return;
        }
        Object targetObj = xpath.selectSingleNode(synContext);
        // if the type custom is used to enrich a property, It'll be handled in a different method
        if (xpath.getExpression().startsWith(SynapseXPathConstants.GET_PROPERTY_FUNCTION)) {
            this.handleProperty(xpath, synContext, sourceNodeList, synLog);
        } else {
            if (targetObj instanceof SOAPHeaderImpl) {
                OMElement targetElem = (OMElement) targetObj;
                ArrayList<OMNode> headerSourceNodeList = new ArrayList<>();
                for (OMNode o : sourceNodeList) {
                    OMElement ins = ((OMElement) o).cloneOMElement();
                    SOAPFactory fac = (SOAPFactory) synContext.getEnvelope().getOMFactory();
                    try {
                        headerSourceNodeList.add(ElementHelper.toSOAPHeaderBlock(ins, fac));
                    } catch (Exception e) {
                        log.error("Error occurred while transforming the OMElement to SOAPHeaderBlock ", e);
                        throw new JaxenException(e);
                    }
                }
                insertElement(headerSourceNodeList, targetElem, synLog);
            } else if (targetObj instanceof OMElement) {
                OMElement targetElem = (OMElement) targetObj;
                insertElement(sourceNodeList, targetElem, synLog);
            } else if (targetObj instanceof OMText) {
                OMText targetText = (OMText) targetObj;
                if (sourceNodeList.get(0) instanceof OMText) {
                    if (targetText.getParent() != null) {
                        Object parent = targetText.getParent();
                        if (parent instanceof OMElement) {
                            ((OMElement) parent).setText(((OMText) sourceNodeList.get(0)).getText());
                        }
                    }
                } else if (sourceNodeList.get(0) instanceof OMElement) {
                    Object targetParent = targetText.getParent();
                    if (targetParent instanceof OMElement) {
                        targetText.detach();
                        synchronized (sourceNodeList.get(0)) {
                            ((OMElement) targetParent).addChild(sourceNodeList.get(0));
                        }
                    }
                }
            } else if (targetObj instanceof OMAttribute) {
                OMAttribute attribute = (OMAttribute) targetObj;
                attribute.setAttributeValue(((OMText) sourceNodeList.get(0)).getText());
            } else {
                synLog.error("Invalid Target object to be enrich.");
                throw new SynapseException("Invalid Target object to be enrich.");
            }
        }
    } else if (targetType == EnrichMediator.BODY) {
        SOAPEnvelope env = synContext.getEnvelope();
        SOAPBody body = env.getBody();
        OMElement e = body.getFirstElement();
        if (e != null) {
            insertElement(sourceNodeList, e, synLog);
        } else {
            // if the body is empty just add as a child
            for (OMNode elem : sourceNodeList) {
                if (elem instanceof OMElement) {
                    synchronized (elem) {
                        body.addChild(elem);
                    }
                } else {
                    synLog.error("Invalid Object type to be inserted into message body");
                }
            }
        }
    } else if (targetType == EnrichMediator.ENVELOPE) {
        OMNode node = sourceNodeList.get(0);
        if (node instanceof SOAPEnvelope) {
            try {
                synContext.setEnvelope((SOAPEnvelope) node);
            } catch (AxisFault axisFault) {
                synLog.error("Failed to set the SOAP Envelope");
                throw new SynapseException("Failed to set the SOAP Envelope");
            }
        } else {
            synLog.error("SOAPEnvelope is expected");
            throw new SynapseException("A SOAPEnvelope is expected");
        }
    } else if (targetType == EnrichMediator.PROPERTY) {
        assert property != null : "Property cannot be null for PROPERTY type";
        if (action != null && property != null) {
            Object propertyObj = synContext.getProperty(property);
            OMElement documentElement = null;
            try {
                if (isOMElement(propertyObj)) {
                    documentElement = (OMElement) propertyObj;
                } else {
                    documentElement = AXIOMUtil.stringToOM((String) propertyObj);
                }
            } catch (Exception e1) {
            // just ignoring the phaser error
            }
            if (documentElement != null && action.equals(ACTION_ADD_CHILD)) {
                // logic should valid only when adding child elements, and other cases
                // such as sibling and replacement using the else condition
                insertElement(sourceNodeList, documentElement, synLog);
                if (isOMElement(propertyObj)) {
                    synContext.setProperty(property, documentElement);
                } else {
                    synContext.setProperty(property, documentElement.getText());
                }
            } else {
                synContext.setProperty(property, sourceNodeList);
            }
        } else {
            synContext.setProperty(property, sourceNodeList);
        }
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) SynapseException(org.apache.synapse.SynapseException) ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPHeaderImpl(org.apache.axiom.soap.impl.llom.SOAPHeaderImpl) SOAPFactory(org.apache.axiom.soap.SOAPFactory) JaxenException(org.jaxen.JaxenException) SynapseException(org.apache.synapse.SynapseException) OMNode(org.apache.axiom.om.OMNode) SOAPBody(org.apache.axiom.soap.SOAPBody) JaxenException(org.jaxen.JaxenException) OMText(org.apache.axiom.om.OMText) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 34 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class PayloadHelper method getTextPayload.

// Text payload is carried in a wrapper element with QName TEXTELT
public static String getTextPayload(SOAPEnvelope envelope) {
    OMElement el = getXMLPayload(envelope);
    if (el == null)
        return null;
    if (!el.getQName().equals(TEXTELT)) {
        log.error("Wrong QName " + el.getQName());
        return null;
    }
    OMNode textNode = el.getFirstOMChild();
    if (textNode.getType() != OMNode.TEXT_NODE) {
        log.error("Text Node not found");
        return null;
    }
    OMText text = (OMText) textNode;
    return text.getText();
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement)

Example 35 with OMText

use of org.apache.axiom.om.OMText in project wso2-synapse by wso2.

the class MessageInjector method removeIndentations.

/**
 * Helper function to remove indentations.
 * @param element
 */
private void removeIndentations(OMElement element) {
    List<OMText> removables = new ArrayList<OMText>();
    removeIndentations(element, removables);
    for (OMText node : removables) {
        node.detach();
    }
}
Also used : OMText(org.apache.axiom.om.OMText) ArrayList(java.util.ArrayList)

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