Example usage for org.w3c.dom Node getOwnerDocument

List of usage examples for org.w3c.dom Node getOwnerDocument

Introduction

In this page you can find the example usage for org.w3c.dom Node getOwnerDocument.

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:org.apache.ode.bpel.rtrep.v2.AssignHelper.java

/**
 * isInsert flag desginates this as an 'element' type insertion, which
 * requires insert the actual element value, rather than it's children
 * (madars.vitolins _at gmail.com - 2009.04.17 - moved from ASSIGN here)
 * @return//ww  w  . j av  a 2s. c  o  m
 * @throws FaultException
 */
public Node replaceContent(Node lvalue, Node lvaluePtr, String rvalue) throws FaultException {
    Document d = lvaluePtr.getOwnerDocument();

    if (__log.isDebugEnabled()) {
        __log.debug("lvaluePtr type " + lvaluePtr.getNodeType());
        __log.debug("lvaluePtr " + DOMUtils.domToString(lvaluePtr));
        __log.debug("lvalue " + lvalue);
        __log.debug("rvalue " + rvalue);
    }

    switch (lvaluePtr.getNodeType()) {
    case Node.ELEMENT_NODE:

        // Remove all the children.
        while (lvaluePtr.hasChildNodes())
            lvaluePtr.removeChild(lvaluePtr.getFirstChild());

        // Append a new text node.
        lvaluePtr.appendChild(d.createTextNode(rvalue));

        // If lvalue is a text, removing all lvaluePtr children had just removed it
        // so we need to rebuild it as a child of lvaluePtr
        if (lvalue instanceof Text)
            lvalue = lvaluePtr.getFirstChild();
        break;

    case Node.TEXT_NODE:

        Node newval = d.createTextNode(rvalue);
        // Replace ourselves .
        lvaluePtr.getParentNode().replaceChild(newval, lvaluePtr);

        // A little kludge, let our caller know that the root element has changed.
        // (used for assignment to a simple typed variable)
        if (lvalue.getNodeType() == Node.ELEMENT_NODE) {
            // No children, adding an empty text children to point to
            if (lvalue.getFirstChild() == null) {
                Text txt = lvalue.getOwnerDocument().createTextNode("");
                lvalue.appendChild(txt);
            }
            if (lvalue.getFirstChild().getNodeType() == Node.TEXT_NODE)
                lvalue = lvalue.getFirstChild();
        }
        if (lvalue.getNodeType() == Node.TEXT_NODE
                && ((Text) lvalue).getWholeText().equals(((Text) lvaluePtr).getWholeText()))
            lvalue = lvaluePtr = newval;
        break;

    case Node.ATTRIBUTE_NODE:

        ((Attr) lvaluePtr).setValue(rvalue);
        break;

    default:
        // This could occur if the expression language selects something
        // like
        // a PI or a CDATA.
        String msg = __msgs.msgInvalidLValue();
        if (__log.isDebugEnabled())
            __log.debug(lvaluePtr + ": " + msg);
        throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
    }

    return lvalue;
}

From source file:org.apache.ode.bpel.rtrep.v2.AssignHelper.java

/**
 * madars.vitolins _at gmail.com - 2009.04.17 moved from ASSIGN here
 * @param data/*from  w w  w  . j  a  v  a2  s . c  om*/
 * @param part
 * @param expression
 * @param ec
 * @return
 * @throws FaultException
 */
private Node evalQuery(Node data, OMessageVarType.Part part, OExpression expression, EvaluationContext ec)
        throws FaultException {
    assert data != null;

    if (part != null) {
        QName partName = new QName(null, part.name);
        Node qualLVal = DOMUtils.findChildByName((Element) data, partName);
        if (part.type instanceof OElementVarType) {
            QName elName = ((OElementVarType) part.type).elementType;
            qualLVal = DOMUtils.findChildByName((Element) qualLVal, elName);
        } else if (part.type == null) {
            // Special case of header parts never referenced in the WSDL def
            if (qualLVal != null && qualLVal.getNodeType() == Node.ELEMENT_NODE
                    && ((Element) qualLVal).getAttribute("headerPart") != null
                    && DOMUtils.getTextContent(qualLVal) == null)
                qualLVal = DOMUtils.getFirstChildElement((Element) qualLVal);
            // The needed part isn't there, dynamically creating it
            if (qualLVal == null) {
                qualLVal = data.getOwnerDocument().createElementNS(null, part.name);
                ((Element) qualLVal).setAttribute("headerPart", "true");
                data.appendChild(qualLVal);
            }
        }
        data = qualLVal;
    }

    if (expression != null) {
        // Neat little trick....
        data = ec.evaluateQuery(data, expression);
    }
    return data;
}

From source file:org.apache.ode.bpel.runtime.ASSIGN.java

private void replaceEndpointRefence(PartnerLinkInstance plval, Node rvalue) throws FaultException {
    if (rvalue.getNodeType() == Node.ATTRIBUTE_NODE) {
        rvalue = rvalue.getOwnerDocument().createTextNode(((Attr) rvalue).getValue());
    }// w  w w . j av  a2  s.c  om
    // Eventually wrapping with service-ref element if we've been directly assigned some
    // value that isn't wrapped.
    if (rvalue.getNodeType() == Node.TEXT_NODE
            || (rvalue.getNodeType() == Node.ELEMENT_NODE && !rvalue.getLocalName().equals("service-ref"))) {
        Document doc = DOMUtils.newDocument();
        Element serviceRef = doc.createElementNS(Namespaces.WSBPEL2_0_FINAL_SERVREF, "service-ref");
        doc.appendChild(serviceRef);
        serviceRef.appendChild(doc.importNode(rvalue, true));
        rvalue = serviceRef;
    }

    getBpelRuntimeContext().writeEndpointReference(plval, (Element) rvalue);
}

From source file:org.apache.ode.bpel.runtime.ASSIGN.java

private Node evalQuery(Node data, OMessageVarType.Part part, OExpression expression, EvaluationContext ec)
        throws FaultException {
    assert data != null;

    if (part != null) {
        QName partName = new QName(null, part.name);
        Node qualLVal = DOMUtils.findChildByName((Element) data, partName);
        if (part.type instanceof OElementVarType) {
            QName elName = ((OElementVarType) part.type).elementType;
            qualLVal = DOMUtils.findChildByName((Element) qualLVal, elName);
        } else if (part.type == null) {
            // Special case of header parts never referenced in the WSDL def
            if (qualLVal != null && qualLVal.getNodeType() == Node.ELEMENT_NODE
                    && ((Element) qualLVal).getAttribute("headerPart") != null
                    && DOMUtils.getTextContent(qualLVal) == null)
                qualLVal = DOMUtils.getFirstChildElement((Element) qualLVal);
            // The needed part isn't there, dynamically creating it
            if (qualLVal == null) {
                qualLVal = data.getOwnerDocument().createElementNS(null, part.name);
                ((Element) qualLVal).setAttribute("headerPart", "true");
                data.appendChild(qualLVal);
            }//from w w w .  ja  v a2  s  .  c om
        }
        data = qualLVal;
    }

    if (expression != null) {
        // Neat little trick....
        try {
            data = ec.evaluateQuery(data, expression);
        } catch (EvaluationException e) {
            String msg = __msgs.msgEvalException(expression.toString(), e.getMessage());
            if (__log.isDebugEnabled())
                __log.debug(expression + ": " + msg);
            if (e.getCause() instanceof FaultException)
                throw (FaultException) e.getCause();
            throw new FaultException(getOAsssign().getOwner().constants.qnSubLanguageExecutionFault, msg);
        }
    }

    return data;
}

From source file:org.apache.ode.bpel.runtime.AssignHelper.java

/**
 * isInsert flag desginates this as an 'element' type insertion, which
 * requires insert the actual element value, rather than it's children
 *
 * @return//from  w w  w . java 2 s .c om
 * @throws FaultException
 */
private Node replaceContent(Node lvalue, Node lvaluePtr, String rvalue) throws FaultException {
    Document d = lvaluePtr.getOwnerDocument();

    if (__log.isDebugEnabled()) {
        __log.debug("lvaluePtr type " + lvaluePtr.getNodeType());
        __log.debug("lvaluePtr " + DOMUtils.domToString(lvaluePtr));
        __log.debug("lvalue " + lvalue);
        __log.debug("rvalue " + rvalue);
    }

    switch (lvaluePtr.getNodeType()) {
    case Node.ELEMENT_NODE:

        // Remove all the children.
        while (lvaluePtr.hasChildNodes())
            lvaluePtr.removeChild(lvaluePtr.getFirstChild());

        // Append a new text node.
        lvaluePtr.appendChild(d.createTextNode(rvalue));

        // If lvalue is a text, removing all lvaluePtr children had just removed it
        // so we need to rebuild it as a child of lvaluePtr
        if (lvalue instanceof Text)
            lvalue = lvaluePtr.getFirstChild();
        break;

    case Node.TEXT_NODE:

        Node newval = d.createTextNode(rvalue);
        // Replace ourselves .
        lvaluePtr.getParentNode().replaceChild(newval, lvaluePtr);

        // A little kludge, let our caller know that the root element has changed.
        // (used for assignment to a simple typed variable)
        if (lvalue.getNodeType() == Node.ELEMENT_NODE) {
            // No children, adding an empty text children to point to
            if (lvalue.getFirstChild() == null) {
                Text txt = lvalue.getOwnerDocument().createTextNode("");
                lvalue.appendChild(txt);
            }
            if (lvalue.getFirstChild().getNodeType() == Node.TEXT_NODE)
                lvalue = lvalue.getFirstChild();
        }
        if (lvalue.getNodeType() == Node.TEXT_NODE
                && ((Text) lvalue).getWholeText().equals(((Text) lvaluePtr).getWholeText()))
            lvalue = lvaluePtr = newval;
        break;

    case Node.ATTRIBUTE_NODE:

        ((Attr) lvaluePtr).setValue(rvalue);
        break;

    default:
        // This could occur if the expression language selects something
        // like
        // a PI or a CDATA.
        String msg = __msgs.msgInvalidLValue();
        if (__log.isDebugEnabled())
            __log.debug(lvaluePtr + ": " + msg);
        throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
    }

    return lvalue;
}

From source file:org.apache.ode.bpel.runtime.AssignHelper.java

private Node evalQuery(Node data, OMessageVarType.Part part, OExpression expression, EvaluationContext ec)
        throws FaultException {
    assert data != null;

    if (part != null) {
        QName partName = new QName(null, part.name);
        Node qualLVal = DOMUtils.findChildByName((Element) data, partName);
        if (part.type instanceof OElementVarType) {
            QName elName = ((OElementVarType) part.type).elementType;
            qualLVal = DOMUtils.findChildByName((Element) qualLVal, elName);
        } else if (part.type == null) {
            // Special case of header parts never referenced in the WSDL def
            if (qualLVal != null && qualLVal.getNodeType() == Node.ELEMENT_NODE
                    && ((Element) qualLVal).getAttribute("headerPart") != null
                    && DOMUtils.getTextContent(qualLVal) == null)
                qualLVal = DOMUtils.getFirstChildElement((Element) qualLVal);
            // The needed part isn't there, dynamically creating it
            if (qualLVal == null) {
                qualLVal = data.getOwnerDocument().createElementNS(null, part.name);
                ((Element) qualLVal).setAttribute("headerPart", "true");
                data.appendChild(qualLVal);
            }//  www  .  j av a 2 s . c  o  m
        }
        data = qualLVal;
    }

    if (expression != null) {
        // Neat little trick....
        try {
            data = ec.evaluateQuery(data, expression);
        } catch (EvaluationException e) {
            String msg = __msgs.msgEvalException(expression.toString(), e.getMessage());
            if (__log.isDebugEnabled())
                __log.debug(expression + ": " + msg);
            if (e.getCause() instanceof FaultException)
                throw (FaultException) e.getCause();
            throw new FaultException(getOActivity().getOwner().constants.qnSubLanguageExecutionFault, msg);
        }
    }

    return data;
}

From source file:org.apache.ode.utils.DOMUtils.java

/**
 * Convert a DOM node to a stringified XML representation.
 *//*from   www  .j a v a2s  .  co m*/
static public String domToString(Node node) {
    if (node == null) {
        throw new IllegalArgumentException("Cannot stringify null Node!");
    }

    String value = null;
    short nodeType = node.getNodeType();
    if (nodeType == Node.ELEMENT_NODE || nodeType == Node.DOCUMENT_NODE
            || nodeType == Node.DOCUMENT_FRAGMENT_NODE) {
        // serializer doesn't handle Node type well, only Element
        DOMSerializerImpl ser = new DOMSerializerImpl();
        ser.setParameter(Constants.DOM_NAMESPACES, Boolean.TRUE);
        ser.setParameter(Constants.DOM_WELLFORMED, Boolean.FALSE);
        ser.setParameter(Constants.DOM_VALIDATE, Boolean.FALSE);

        // create a proper XML encoding header based on the input document;
        // default to UTF-8 if the parent document's encoding is not accessible
        String usedEncoding = "UTF-8";
        Document parent = node.getOwnerDocument();
        if (parent != null) {
            String parentEncoding = parent.getXmlEncoding();
            if (parentEncoding != null) {
                usedEncoding = parentEncoding;
            }
        }

        // the receiver of the DOM
        DOMOutputImpl out = new DOMOutputImpl();
        out.setEncoding(usedEncoding);

        // we write into a String
        StringWriter writer = new StringWriter(4096);
        out.setCharacterStream(writer);

        // out, ye characters!
        ser.write(node, out);
        writer.flush();

        // finally get the String
        value = writer.toString();
    } else {
        value = node.getNodeValue();
    }
    return value;
}

From source file:org.apache.ode.utils.DOMUtils.java

public static Document getDocument(Node contextNode) {
    return (contextNode == null) ? DOMUtils.newDocument() : contextNode.getOwnerDocument();
}

From source file:org.apache.rahas.impl.util.CommonUtil.java

/**
 * Decrypts the EncryptedKey element and returns the secret that was used.
 * @param callbackHandler Callback handler to pass to WSS4J framework.
 * @param crypto To get private key information.
 * @param encryptedKeyElement The encrypted Key element.
 * @return The secret as a byte stream.//from w w w  . j a v a  2  s . co  m
 * @throws WSSecurityException If an error is occurred while decrypting the element.
 */
public static byte[] getDecryptedBytes(CallbackHandler callbackHandler, Crypto crypto, Node encryptedKeyElement)
        throws WSSecurityException {

    EncryptedKeyProcessor encryptedKeyProcessor = new EncryptedKeyProcessor();

    RequestData requestData = new RequestData();
    requestData.setCallbackHandler(callbackHandler);
    requestData.setDecCrypto(crypto);

    final WSSConfig cfg = WSSConfig.getNewInstance();
    requestData.setWssConfig(cfg);

    WSDocInfo docInfo = new WSDocInfo(encryptedKeyElement.getOwnerDocument());

    List<WSSecurityEngineResult> resultList;

    resultList = encryptedKeyProcessor.handleToken((Element) encryptedKeyElement, requestData, docInfo);

    WSSecurityEngineResult wsSecurityEngineResult = resultList.get(0);

    return (byte[]) wsSecurityEngineResult.get(WSSecurityEngineResult.TAG_SECRET);
}

From source file:org.apache.servicemix.jbi.deployer.utils.ManagementSupport.java

private static Element createChild(Node parent, String name, String text) {
    Document doc = parent instanceof Document ? (Document) parent : parent.getOwnerDocument();
    Element child = doc.createElementNS(HTTP_JAVA_SUN_COM_XML_NS_JBI_MANAGEMENT_MESSAGE, name);
    if (text != null) {
        child.appendChild(doc.createTextNode(text));
    }//  w w  w .ja va  2  s.  com
    parent.appendChild(child);
    return child;
}