Example usage for org.w3c.dom Element getOwnerDocument

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

Introduction

In this page you can find the example usage for org.w3c.dom Element 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

/**
 * Get the r-value. There are several possibilities:
 * <ul>//from w w  w .java2s. c  om
 * <li>a message is selected - an element representing the whole message is
 * returned.</li>
 * <li>a (element) message part is selected - the element is returned.
 * </li>
 * <li>a (typed) message part is select - a wrapper element is returned.
 * </li>
 * <li>an attribute is selected - an attribute node is returned. </li>
 * <li>a text node/string expression is selected - a text node is returned.
 * </li>
 * </ul>
 * (madars.vitolins _at gmail.com - 2009.04.17 - moved from ASSIGN here)
 * @param from
 *
 * @return Either {@link Element}, {@link org.w3c.dom.Text}, or
 *         {@link org.w3c.dom.Attr} node representing the r-value.
 *
 * @throws FaultException
 *             DOCUMENTME
 * @throws UnsupportedOperationException
 *             DOCUMENTME
 * @throws IllegalStateException
 *             DOCUMENTME
 */
public Node evalRValue(OAssign.RValue from) throws FaultException, ExternalVariableModuleException {
    if (__log.isDebugEnabled())
        __log.debug("Evaluating FROM expression \"" + from + "\".");

    Node retVal;
    if (from instanceof OAssign.DirectRef) {
        OAssign.DirectRef dref = (OAssign.DirectRef) from;
        sendVariableReadEvent(_scopeFrame.resolve(dref.variable));
        Node data = fetchVariableData(_scopeFrame.resolve(dref.variable), false);
        retVal = DOMUtils.findChildByName((Element) data, dref.elName);
    } else if (from instanceof OAssign.VariableRef) {
        OAssign.VariableRef varRef = (OAssign.VariableRef) from;
        sendVariableReadEvent(_scopeFrame.resolve(varRef.variable));
        Node data = fetchVariableData(_scopeFrame.resolve(varRef.variable), false);
        retVal = evalQuery(data, varRef.part != null ? varRef.part : varRef.headerPart, varRef.location,
                getEvaluationContext());
    } else if (from instanceof OAssign.PropertyRef) {
        OAssign.PropertyRef propRef = (OAssign.PropertyRef) from;
        sendVariableReadEvent(_scopeFrame.resolve(propRef.variable));
        Node data = fetchVariableData(_scopeFrame.resolve(propRef.variable), false);
        retVal = evalQuery(data, propRef.propertyAlias.part, propRef.propertyAlias.location,
                getEvaluationContext());
    } else if (from instanceof OAssign.PartnerLinkRef) {
        OAssign.PartnerLinkRef pLinkRef = (OAssign.PartnerLinkRef) from;
        PartnerLinkInstance pLink = _scopeFrame.resolve(pLinkRef.partnerLink);
        Node tempVal = pLinkRef.isMyEndpointReference ? getBpelRuntime().fetchMyRoleEndpointReferenceData(pLink)
                : getBpelRuntime().fetchPartnerRoleEndpointReferenceData(pLink);
        if (__log.isDebugEnabled())
            __log.debug("RValue is a partner link, corresponding endpoint " + tempVal.getClass().getName()
                    + " has value " + DOMUtils.domToString(tempVal));
        retVal = tempVal;
    } else if (from instanceof OAssign.ContextRef) {
        OAssign.ContextRef ctxRef = (OAssign.ContextRef) from;
        ContextData cdata = getBpelRuntime().fetchContextData(_scopeFrame.resolve(ctxRef.partnerLink));
        retVal = evalQuery(cdata.toXML(ctxRef.contexts), null, ctxRef.location, getEvaluationContext());
    } else if (from instanceof OAssign.Expression) {
        OExpression expr = ((OAssign.Expression) from).expression;
        List<Node> l = getBpelRuntime().getExpLangRuntime().evaluate(expr, getEvaluationContext());

        if (l.size() == 0) {
            String msg = __msgs.msgRValueNoNodesSelected(expr.toString());
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg,
                    new Throwable("ignoreMissingFromData"));
        } else if (l.size() > 1) {
            String msg = __msgs.msgRValueMultipleNodesSelected(expr.toString());
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
        }
        retVal = l.get(0);
    } else if (from instanceof OAssign.Literal) {
        Element literalRoot = ((OAssign.Literal) from).getXmlLiteral().getDocumentElement();
        assert literalRoot.getLocalName().equals("literal");
        // We'd like a single text node...

        literalRoot.normalize();
        retVal = literalRoot.getFirstChild();

        // Adjust for whitespace before an element.
        if (retVal != null && retVal.getNodeType() == Node.TEXT_NODE
                && retVal.getTextContent().trim().length() == 0 && retVal.getNextSibling() != null) {
            retVal = retVal.getNextSibling();
        }

        if (retVal == null) {
            // Special case, no children --> empty TII
            retVal = literalRoot.getOwnerDocument().createTextNode("");
        } else if (retVal.getNodeType() == Node.ELEMENT_NODE) {
            // Make sure there is no more elements.
            Node x = retVal.getNextSibling();
            while (x != null) {
                if (x.getNodeType() == Node.ELEMENT_NODE) {
                    String msg = __msgs.msgLiteralContainsMultipleEIIs();
                    if (__log.isDebugEnabled())
                        __log.debug(from + ": " + msg);
                    throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);

                }
                x = x.getNextSibling();
            }
        } else if (retVal.getNodeType() == Node.TEXT_NODE) {
            // Make sure there are no elements following this text node.
            Node x = retVal.getNextSibling();
            while (x != null) {
                if (x.getNodeType() == Node.ELEMENT_NODE) {
                    String msg = __msgs.msgLiteralContainsMixedContent();
                    if (__log.isDebugEnabled())
                        __log.debug(from + ": " + msg);
                    throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);

                }
                x = x.getNextSibling();
            }

        }

        if (retVal == null) {
            String msg = __msgs.msgLiteralMustContainTIIorEII();
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
        }
    } else {
        String msg = __msgs.msgInternalError("Unknown RVALUE type: " + from);
        if (__log.isErrorEnabled())
            __log.error(from + ": " + msg);
        throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
    }

    // Now verify we got something.
    if (retVal == null) {
        String msg = __msgs.msgEmptyRValue();
        if (__log.isDebugEnabled())
            __log.debug(from + ": " + msg);
        throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
    }

    // Now check that we got the right thing.
    switch (retVal.getNodeType()) {
    case Node.TEXT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ELEMENT_NODE:
    case Node.CDATA_SECTION_NODE:
        break;
    default:
        String msg = __msgs.msgInvalidRValue();
        if (__log.isDebugEnabled())
            __log.debug(from + ": " + msg);

        throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);

    }

    return retVal;
}

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

/**
 * madars.vitolins _at gmail.com - 2009.04.17 - moved from ASSIGN here
 *//*w  w  w. j a  v  a2 s  .  co m*/
public Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) {
    Document doc = ptr.getOwnerDocument();
    Node parent = ptr.getParentNode();
    if (keepSrcElement) {
        Element replacement = (Element) doc.importNode(src, true);
        parent.replaceChild(replacement, ptr);
        return (lval == ptr) ? replacement : lval;
    }

    Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getLocalName());
    NodeList nl = src.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i)
        replacement.appendChild(doc.importNode(nl.item(i), true));
    NamedNodeMap attrs = src.getAttributes();
    for (int i = 0; i < attrs.getLength(); ++i) {
        Attr attr = (Attr) attrs.item(i);
        if (!attr.getName().startsWith("xmlns")) {
            replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true));
            // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually
            int colonIdx = attr.getValue().indexOf(":");
            if (colonIdx > 0) {
                String prefix = attr.getValue().substring(0, colonIdx);
                String attrValNs = src.lookupPrefix(prefix);
                if (attrValNs != null)
                    replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs);
            }
        }
    }
    parent.replaceChild(replacement, ptr);
    DOMUtils.copyNSContext(ptr, replacement);

    return (lval == ptr) ? replacement : lval;
}

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

/**
 * Get the r-value. There are several possibilities:
 * <ul>//from   w ww  .j a va  2 s  . co  m
 * <li>a message is selected - an element representing the whole message is
 * returned.</li>
 * <li>a (element) message part is selected - the element is returned.
 * </li>
 * <li>a (typed) message part is select - a wrapper element is returned.
 * </li>
 * <li>an attribute is selected - an attribute node is returned. </li>
 * <li>a text node/string expression is selected - a text node is returned.
 * </li>
 * </ul>
 *
 * @param from
 *
 * @return Either {@link Element}, {@link org.w3c.dom.Text}, or
 *         {@link org.w3c.dom.Attr} node representing the r-value.
 *
 * @throws FaultException
 *             DOCUMENTME
 * @throws UnsupportedOperationException
 *             DOCUMENTME
 * @throws IllegalStateException
 *             DOCUMENTME
 */
private Node evalRValue(OAssign.RValue from) throws FaultException, ExternalVariableModuleException {
    if (__log.isDebugEnabled())
        __log.debug("Evaluating FROM expression \"" + from + "\".");

    Node retVal;
    if (from instanceof DirectRef) {
        OAssign.DirectRef dref = (OAssign.DirectRef) from;
        sendVariableReadEvent(_scopeFrame.resolve(dref.variable));
        Node data = fetchVariableData(_scopeFrame.resolve(dref.variable), false);
        retVal = DOMUtils.findChildByName((Element) data, dref.elName);
    } else if (from instanceof OAssign.VariableRef) {
        OAssign.VariableRef varRef = (OAssign.VariableRef) from;
        sendVariableReadEvent(_scopeFrame.resolve(varRef.variable));
        Node data = fetchVariableData(_scopeFrame.resolve(varRef.variable), false);
        retVal = evalQuery(data, varRef.part != null ? varRef.part : varRef.headerPart, varRef.location,
                getEvaluationContext());
    } else if (from instanceof OAssign.PropertyRef) {
        OAssign.PropertyRef propRef = (OAssign.PropertyRef) from;
        sendVariableReadEvent(_scopeFrame.resolve(propRef.variable));
        Node data = fetchVariableData(_scopeFrame.resolve(propRef.variable), false);
        retVal = evalQuery(data, propRef.propertyAlias.part, propRef.propertyAlias.location,
                getEvaluationContext());
    } else if (from instanceof OAssign.PartnerLinkRef) {
        OAssign.PartnerLinkRef pLinkRef = (OAssign.PartnerLinkRef) from;
        PartnerLinkInstance pLink = _scopeFrame.resolve(pLinkRef.partnerLink);
        Node tempVal = pLinkRef.isMyEndpointReference
                ? getBpelRuntimeContext().fetchMyRoleEndpointReferenceData(pLink)
                : getBpelRuntimeContext().fetchPartnerRoleEndpointReferenceData(pLink);
        if (__log.isDebugEnabled())
            __log.debug("RValue is a partner link, corresponding endpoint " + tempVal.getClass().getName()
                    + " has value " + DOMUtils.domToString(tempVal));
        retVal = tempVal;
    } else if (from instanceof OAssign.Expression) {
        List<Node> l;
        OExpression expr = ((OAssign.Expression) from).expression;
        try {
            l = getBpelRuntimeContext().getExpLangRuntime().evaluate(expr, getEvaluationContext());
            if (l.size() == 0 || l.get(0) == null || !(l.get(0) instanceof Element)) {
                if (__log.isTraceEnabled()) {
                    __log.trace("evalRValue: OAssign.Expression: eval reult not Element or node=null");
                }
            } else {
                Element element = (Element) l.get(0);
                for (Map.Entry<String, String> entry : DOMUtils.getMyNSContext(element).toMap().entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    if (entry.getKey() == null || entry.getKey().length() == 0) {
                        element.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns", value);
                    } else {
                        element.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + key, value);
                    }
                }
            }
        } catch (EvaluationException e) {
            String msg = __msgs.msgEvalException(from.toString(), e.getMessage());
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            if (e.getCause() instanceof FaultException)
                throw (FaultException) e.getCause();
            throw new FaultException(getOAsssign().getOwner().constants.qnSelectionFailure, msg);
        }
        if (l.size() == 0) {
            String msg = __msgs.msgRValueNoNodesSelected(expr.toString());
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            throw new FaultException(getOAsssign().getOwner().constants.qnSelectionFailure, msg,
                    new Throwable("ignoreMissingFromData"));
        } else if (l.size() > 1) {
            String msg = __msgs.msgRValueMultipleNodesSelected(expr.toString());
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            throw new FaultException(getOAsssign().getOwner().constants.qnSelectionFailure, msg);
        }
        retVal = (Node) l.get(0);
    } else if (from instanceof OAssign.Literal) {
        String literal = ((OAssign.Literal) from).getXmlLiteral();
        Element literalRoot;
        try {
            literalRoot = DOMUtils.stringToDOM(literal);
        } catch (Exception e) {
            throw new RuntimeException("XML literal parsing failed " + literal, e);
        }
        assert literalRoot.getLocalName().equals("literal");
        // We'd like a single text node...

        literalRoot.normalize();
        retVal = literalRoot.getFirstChild();

        // Adjust for whitespace before an element.
        if (retVal != null && retVal.getNodeType() == Node.TEXT_NODE
                && retVal.getTextContent().trim().length() == 0 && retVal.getNextSibling() != null) {
            retVal = retVal.getNextSibling();
        }

        if (retVal == null) {
            // Special case, no children --> empty TII
            retVal = literalRoot.getOwnerDocument().createTextNode("");
        } else if (retVal.getNodeType() == Node.ELEMENT_NODE) {
            // Make sure there is no more elements.
            Node x = retVal.getNextSibling();
            while (x != null) {
                if (x.getNodeType() == Node.ELEMENT_NODE) {
                    String msg = __msgs.msgLiteralContainsMultipleEIIs();
                    if (__log.isDebugEnabled())
                        __log.debug(from + ": " + msg);
                    throw new FaultException(getOAsssign().getOwner().constants.qnSelectionFailure, msg);

                }
                x = x.getNextSibling();
            }
        } else if (retVal.getNodeType() == Node.TEXT_NODE) {
            // Make sure there are no elements following this text node.
            Node x = retVal.getNextSibling();
            while (x != null) {
                if (x.getNodeType() == Node.ELEMENT_NODE) {
                    String msg = __msgs.msgLiteralContainsMixedContent();
                    if (__log.isDebugEnabled())
                        __log.debug(from + ": " + msg);
                    throw new FaultException(getOAsssign().getOwner().constants.qnSelectionFailure, msg);

                }
                x = x.getNextSibling();
            }

        }

        if (retVal == null) {
            String msg = __msgs.msgLiteralMustContainTIIorEII();
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            throw new FaultException(getOAsssign().getOwner().constants.qnSelectionFailure, msg);
        }
    } else {
        String msg = __msgs.msgInternalError("Unknown RVALUE type: " + from);
        if (__log.isErrorEnabled())
            __log.error(from + ": " + msg);
        throw new FaultException(getOAsssign().getOwner().constants.qnSelectionFailure, msg);
    }

    // Now verify we got something.
    if (retVal == null) {
        String msg = __msgs.msgEmptyRValue();
        if (__log.isDebugEnabled())
            __log.debug(from + ": " + msg);
        throw new FaultException(getOAsssign().getOwner().constants.qnSelectionFailure, msg);
    }

    // Now check that we got the right thing.
    switch (retVal.getNodeType()) {
    case Node.TEXT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ELEMENT_NODE:
    case Node.CDATA_SECTION_NODE:
        break;
    default:
        String msg = __msgs.msgInvalidRValue();
        if (__log.isDebugEnabled())
            __log.debug(from + ": " + msg);

        throw new FaultException(getOAsssign().getOwner().constants.qnSelectionFailure, msg);

    }

    return retVal;
}

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

private void copy(OAssign.Copy ocopy) throws FaultException, ExternalVariableModuleException {

    if (__log.isDebugEnabled())
        __log.debug("Assign.copy(" + ocopy + ")");

    ScopeEvent se;/*w  w w. j av  a 2 s .c  o m*/

    // Check for message to message - copy, we can do this efficiently in
    // the database.
    if ((ocopy.to instanceof VariableRef && ((VariableRef) ocopy.to).isMessageRef())
            || (ocopy.from instanceof VariableRef && ((VariableRef) ocopy.from).isMessageRef())) {

        if ((ocopy.to instanceof VariableRef && ((VariableRef) ocopy.to).isMessageRef())
                && ocopy.from instanceof VariableRef && ((VariableRef) ocopy.from).isMessageRef()) {

            final VariableInstance lval = _scopeFrame.resolve(ocopy.to.getVariable());
            final VariableInstance rval = _scopeFrame.resolve(((VariableRef) ocopy.from).getVariable());
            Element lvalue = (Element) fetchVariableData(rval, false);
            initializeVariable(lval, lvalue);
            se = new VariableModificationEvent(lval.declaration.name);
            ((VariableModificationEvent) se).setNewValue(lvalue);
        } else {
            // This really should have been caught by the compiler.
            __log.fatal("Message/Non-Message Assignment, should be caught by compiler:" + ocopy);
            throw new FaultException(ocopy.getOwner().constants.qnSelectionFailure,
                    "Message/Non-Message Assignment:  " + ocopy);
        }
    } else {
        // Conventional Assignment logic.
        Node rvalue = evalRValue(ocopy.from);
        Node lvalue = evalLValue(ocopy.to);
        if (__log.isDebugEnabled()) {
            __log.debug("lvalue after eval " + lvalue);
            if (lvalue != null)
                __log.debug("content " + DOMUtils.domToString(lvalue));
        }

        // Get a pointer within the lvalue.
        Node lvaluePtr = lvalue;
        boolean headerAssign = false;
        if (ocopy.to instanceof OAssign.DirectRef) {
            DirectRef dref = ((DirectRef) ocopy.to);
            Element el = DOMUtils.findChildByName((Element) lvalue, dref.elName);
            if (el == null) {
                el = (Element) ((Element) lvalue).appendChild(lvalue.getOwnerDocument()
                        .createElementNS(dref.elName.getNamespaceURI(), dref.elName.getLocalPart()));
            }
            lvaluePtr = el;
        } else if (ocopy.to instanceof OAssign.VariableRef) {
            VariableRef varRef = ((VariableRef) ocopy.to);
            if (varRef.headerPart != null)
                headerAssign = true;
            lvaluePtr = evalQuery(lvalue, varRef.part != null ? varRef.part : varRef.headerPart,
                    varRef.location, new EvaluationContextProxy(varRef.getVariable(), lvalue));
        } else if (ocopy.to instanceof OAssign.PropertyRef) {
            PropertyRef propRef = ((PropertyRef) ocopy.to);
            lvaluePtr = evalQuery(lvalue, propRef.propertyAlias.part, propRef.propertyAlias.location,
                    new EvaluationContextProxy(propRef.getVariable(), lvalue));
        } else if (ocopy.to instanceof OAssign.LValueExpression) {
            LValueExpression lexpr = (LValueExpression) ocopy.to;
            lexpr.setInsertMissingToData(ocopy.insertMissingToData);
            lvaluePtr = evalQuery(lvalue, null, lexpr.expression,
                    new EvaluationContextProxy(lexpr.getVariable(), lvalue));
            if (__log.isDebugEnabled())
                __log.debug("lvaluePtr expr res " + lvaluePtr);
        }

        // For partner link assignmenent, the whole content is assigned.
        if (ocopy.to instanceof OAssign.PartnerLinkRef) {
            OAssign.PartnerLinkRef pLinkRef = ((OAssign.PartnerLinkRef) ocopy.to);
            PartnerLinkInstance plval = _scopeFrame.resolve(pLinkRef.partnerLink);
            replaceEndpointRefence(plval, rvalue);
            se = new PartnerLinkModificationEvent(((OAssign.PartnerLinkRef) ocopy.to).partnerLink.getName());
        } else {
            // Sneakily converting the EPR if it's not the format expected by the lvalue
            if (ocopy.from instanceof OAssign.PartnerLinkRef) {
                rvalue = getBpelRuntimeContext().convertEndpointReference((Element) rvalue, lvaluePtr);
                if (rvalue.getNodeType() == Node.DOCUMENT_NODE)
                    rvalue = ((Document) rvalue).getDocumentElement();
            }

            Node parentNode = lvaluePtr.getParentNode();
            if (headerAssign && parentNode != null && "message".equals(parentNode.getNodeName())
                    && rvalue.getNodeType() == Node.ELEMENT_NODE) {
                lvalue = copyInto((Element) lvalue, (Element) lvaluePtr, (Element) rvalue);
            } else if (rvalue.getNodeType() == Node.ELEMENT_NODE
                    && lvaluePtr.getNodeType() == Node.ELEMENT_NODE) {
                lvalue = replaceElement((Element) lvalue, (Element) lvaluePtr, (Element) rvalue,
                        ocopy.keepSrcElementName);
            } else {
                lvalue = replaceContent(lvalue, lvaluePtr, rvalue.getTextContent());
            }
            final VariableInstance lval = _scopeFrame.resolve(ocopy.to.getVariable());
            if (__log.isDebugEnabled())
                __log.debug("ASSIGN Writing variable '" + lval.declaration.name + "' value '"
                        + DOMUtils.domToString(lvalue) + "'");
            commitChanges(lval, lvalue);
            se = new VariableModificationEvent(lval.declaration.name);
            ((VariableModificationEvent) se).setNewValue(lvalue);
        }
    }

    if (ocopy.debugInfo != null)
        se.setLineNo(ocopy.debugInfo.startLine);
    sendEvent(se);
}

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

private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) {
    Document doc = ptr.getOwnerDocument();
    Node parent = ptr.getParentNode();
    if (keepSrcElement) {
        Element replacement = (Element) doc.importNode(src, true);
        parent.replaceChild(replacement, ptr);
        return (lval == ptr) ? replacement : lval;
    }//from   w ww .j av a 2  s. co  m

    Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getTagName());
    NodeList nl = src.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i)
        replacement.appendChild(doc.importNode(nl.item(i), true));
    copyAttributes(doc, ptr, replacement);
    copyAttributes(doc, src, replacement);
    parent.replaceChild(replacement, ptr);
    DOMUtils.copyNSContext(ptr, replacement);

    return (lval == ptr) ? replacement : lval;
}

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

/**
 * Get the r-value. There are several possibilities:
 * <ul>/*ww  w . java 2s  .  com*/
 * <li>a message is selected - an element representing the whole message is
 * returned.</li>
 * <li>a (element) message part is selected - the element is returned.
 * </li>
 * <li>a (typed) message part is select - a wrapper element is returned.
 * </li>
 * <li>an attribute is selected - an attribute node is returned. </li>
 * <li>a text node/string expression is selected - a text node is returned.
 * </li>
 * </ul>
 *
 * @param from
 *
 * @return Either {@link Element}, {@link org.w3c.dom.Text}, or
 *         {@link org.w3c.dom.Attr} node representing the r-value.
 *
 * @throws FaultException
 *             DOCUMENTME
 * @throws UnsupportedOperationException
 *             DOCUMENTME
 * @throws IllegalStateException
 *             DOCUMENTME
 */
private Node evalRValue(OAssign.RValue from) throws FaultException, ExternalVariableModuleException {
    if (__log.isDebugEnabled())
        __log.debug("Evaluating FROM expression \"" + from + "\".");

    Node retVal;
    if (from instanceof DirectRef) {
        OAssign.DirectRef dref = (OAssign.DirectRef) from;
        sendVariableReadEvent(_scopeFrame.resolve(dref.variable));
        Node data = fetchVariableData(_scopeFrame.resolve(dref.variable), false);
        retVal = DOMUtils.findChildByName((Element) data, dref.elName);
    } else if (from instanceof OAssign.VariableRef) {
        OAssign.VariableRef varRef = (OAssign.VariableRef) from;
        sendVariableReadEvent(_scopeFrame.resolve(varRef.variable));
        Node data = fetchVariableData(_scopeFrame.resolve(varRef.variable), false);
        retVal = evalQuery(data, varRef.part != null ? varRef.part : varRef.headerPart, varRef.location,
                getEvaluationContext());
    } else if (from instanceof OAssign.PropertyRef) {
        OAssign.PropertyRef propRef = (OAssign.PropertyRef) from;
        sendVariableReadEvent(_scopeFrame.resolve(propRef.variable));
        Node data = fetchVariableData(_scopeFrame.resolve(propRef.variable), false);
        retVal = evalQuery(data, propRef.propertyAlias.part, propRef.propertyAlias.location,
                getEvaluationContext());
    } else if (from instanceof OAssign.PartnerLinkRef) {
        OAssign.PartnerLinkRef pLinkRef = (OAssign.PartnerLinkRef) from;
        PartnerLinkInstance pLink = _scopeFrame.resolve(pLinkRef.partnerLink);
        Node tempVal = pLinkRef.isMyEndpointReference
                ? getBpelRuntimeContext().fetchMyRoleEndpointReferenceData(pLink)
                : getBpelRuntimeContext().fetchPartnerRoleEndpointReferenceData(pLink);
        if (__log.isDebugEnabled())
            __log.debug("RValue is a partner link, corresponding endpoint " + tempVal.getClass().getName()
                    + " has value " + DOMUtils.domToString(tempVal));
        retVal = tempVal;
    } else if (from instanceof OAssign.Expression) {
        List<Node> l;
        OExpression expr = ((OAssign.Expression) from).expression;
        try {
            l = getBpelRuntimeContext().getExpLangRuntime().evaluate(expr, getEvaluationContext());
        } catch (EvaluationException e) {
            String msg = __msgs.msgEvalException(from.toString(), e.getMessage());
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            if (e.getCause() instanceof FaultException)
                throw (FaultException) e.getCause();
            throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
        }
        if (l.size() == 0) {
            String msg = __msgs.msgRValueNoNodesSelected(expr.toString());
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg,
                    new Throwable("ignoreMissingFromData"));
        } else if (l.size() > 1) {
            String msg = __msgs.msgRValueMultipleNodesSelected(expr.toString());
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
        }
        retVal = (Node) l.get(0);
    } else if (from instanceof OAssign.Literal) {
        String literal = ((OAssign.Literal) from).getXmlLiteral();
        Element literalRoot;
        try {
            literalRoot = DOMUtils.stringToDOM(literal);
        } catch (Exception e) {
            throw new RuntimeException("XML literal parsing failed " + literal, e);
        }
        assert literalRoot.getLocalName().equals("literal");
        // We'd like a single text node...

        literalRoot.normalize();
        retVal = literalRoot.getFirstChild();

        // Adjust for whitespace before an element.
        if (retVal != null && retVal.getNodeType() == Node.TEXT_NODE
                && retVal.getTextContent().trim().length() == 0 && retVal.getNextSibling() != null) {
            retVal = retVal.getNextSibling();
        }

        if (retVal == null) {
            // Special case, no children --> empty TII
            retVal = literalRoot.getOwnerDocument().createTextNode("");
        } else if (retVal.getNodeType() == Node.ELEMENT_NODE) {
            // Make sure there is no more elements.
            Node x = retVal.getNextSibling();
            while (x != null) {
                if (x.getNodeType() == Node.ELEMENT_NODE) {
                    String msg = __msgs.msgLiteralContainsMultipleEIIs();
                    if (__log.isDebugEnabled())
                        __log.debug(from + ": " + msg);
                    throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);

                }
                x = x.getNextSibling();
            }
        } else if (retVal.getNodeType() == Node.TEXT_NODE) {
            // Make sure there are no elements following this text node.
            Node x = retVal.getNextSibling();
            while (x != null) {
                if (x.getNodeType() == Node.ELEMENT_NODE) {
                    String msg = __msgs.msgLiteralContainsMixedContent();
                    if (__log.isDebugEnabled())
                        __log.debug(from + ": " + msg);
                    throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);

                }
                x = x.getNextSibling();
            }

        }

        if (retVal == null) {
            String msg = __msgs.msgLiteralMustContainTIIorEII();
            if (__log.isDebugEnabled())
                __log.debug(from + ": " + msg);
            throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
        }
    } else {
        String msg = __msgs.msgInternalError("Unknown RVALUE type: " + from);
        if (__log.isErrorEnabled())
            __log.error(from + ": " + msg);
        throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
    }

    // Now verify we got something.
    if (retVal == null) {
        String msg = __msgs.msgEmptyRValue();
        if (__log.isDebugEnabled())
            __log.debug(from + ": " + msg);
        throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
    }

    // Now check that we got the right thing.
    switch (retVal.getNodeType()) {
    case Node.TEXT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ELEMENT_NODE:
    case Node.CDATA_SECTION_NODE:
        break;
    default:
        String msg = __msgs.msgInvalidRValue();
        if (__log.isDebugEnabled())
            __log.debug(from + ": " + msg);

        throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);

    }

    return retVal;
}

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

protected void copy(OAssign.Copy ocopy) throws FaultException, ExternalVariableModuleException {

    if (__log.isDebugEnabled())
        __log.debug("Assign.copy(" + ocopy + ")");

    ScopeEvent se;//  w  w w  .j  a  va2s  .co m

    // Check for message to message - copy, we can do this efficiently in
    // the database.
    if ((ocopy.to instanceof VariableRef && ((VariableRef) ocopy.to).isMessageRef())
            || (ocopy.from instanceof VariableRef && ((VariableRef) ocopy.from).isMessageRef())) {

        if ((ocopy.to instanceof VariableRef && ((VariableRef) ocopy.to).isMessageRef())
                && ocopy.from instanceof VariableRef && ((VariableRef) ocopy.from).isMessageRef()) {

            final VariableInstance lval = _scopeFrame.resolve(ocopy.to.getVariable());
            final VariableInstance rval = _scopeFrame.resolve(((VariableRef) ocopy.from).getVariable());
            Element lvalue = (Element) fetchVariableData(rval, false);
            initializeVariable(lval, lvalue);
            se = new VariableModificationEvent(lval.declaration.name);
            ((VariableModificationEvent) se).setNewValue(lvalue);
        } else {
            // This really should have been caught by the compiler.
            __log.fatal("Message/Non-Message Assignment, should be caught by compiler:" + ocopy);
            throw new FaultException(ocopy.getOwner().constants.qnSelectionFailure,
                    "Message/Non-Message Assignment:  " + ocopy);
        }
    } else {
        // Conventional Assignment logic.
        Node rvalue = evalRValue(ocopy.from);
        Node lvalue = evalLValue(ocopy.to);
        if (__log.isDebugEnabled()) {
            __log.debug("lvalue after eval " + lvalue);
            if (lvalue != null)
                __log.debug("content " + DOMUtils.domToString(lvalue));
        }

        // Get a pointer within the lvalue.
        Node lvaluePtr = lvalue;
        boolean headerAssign = false;
        if (ocopy.to instanceof OAssign.DirectRef) {
            DirectRef dref = ((DirectRef) ocopy.to);
            Element el = DOMUtils.findChildByName((Element) lvalue, dref.elName);
            if (el == null) {
                el = (Element) ((Element) lvalue).appendChild(lvalue.getOwnerDocument()
                        .createElementNS(dref.elName.getNamespaceURI(), dref.elName.getLocalPart()));
            }
            lvaluePtr = el;
        } else if (ocopy.to instanceof OAssign.VariableRef) {
            VariableRef varRef = ((VariableRef) ocopy.to);
            if (varRef.headerPart != null)
                headerAssign = true;
            lvaluePtr = evalQuery(lvalue, varRef.part != null ? varRef.part : varRef.headerPart,
                    varRef.location, new EvaluationContextProxy(varRef.getVariable(), lvalue));
        } else if (ocopy.to instanceof OAssign.PropertyRef) {
            PropertyRef propRef = ((PropertyRef) ocopy.to);
            lvaluePtr = evalQuery(lvalue, propRef.propertyAlias.part, propRef.propertyAlias.location,
                    new EvaluationContextProxy(propRef.getVariable(), lvalue));
        } else if (ocopy.to instanceof OAssign.LValueExpression) {
            LValueExpression lexpr = (LValueExpression) ocopy.to;
            lexpr.setInsertMissingToData(ocopy.insertMissingToData);
            lvaluePtr = evalQuery(lvalue, null, lexpr.expression,
                    new EvaluationContextProxy(lexpr.getVariable(), lvalue));
            if (__log.isDebugEnabled())
                __log.debug("lvaluePtr expr res " + lvaluePtr);
        }

        // For partner link assignmenent, the whole content is assigned.
        if (ocopy.to instanceof OAssign.PartnerLinkRef) {
            OAssign.PartnerLinkRef pLinkRef = ((OAssign.PartnerLinkRef) ocopy.to);
            PartnerLinkInstance plval = _scopeFrame.resolve(pLinkRef.partnerLink);
            replaceEndpointRefence(plval, rvalue);
            se = new PartnerLinkModificationEvent(((OAssign.PartnerLinkRef) ocopy.to).partnerLink.getName());
        } else {
            // Sneakily converting the EPR if it's not the format expected by the lvalue
            if (ocopy.from instanceof OAssign.PartnerLinkRef) {
                rvalue = getBpelRuntimeContext().convertEndpointReference((Element) rvalue, lvaluePtr);
                if (rvalue.getNodeType() == Node.DOCUMENT_NODE)
                    rvalue = ((Document) rvalue).getDocumentElement();
            }

            Node parentNode = lvaluePtr.getParentNode();
            if (headerAssign && parentNode != null && "message".equals(parentNode.getNodeName())
                    && rvalue.getNodeType() == Node.ELEMENT_NODE) {
                lvalue = copyInto((Element) lvalue, (Element) lvaluePtr, (Element) rvalue);
            } else if (rvalue.getNodeType() == Node.ELEMENT_NODE
                    && lvaluePtr.getNodeType() == Node.ELEMENT_NODE) {
                lvalue = replaceElement((Element) lvalue, (Element) lvaluePtr, (Element) rvalue,
                        ocopy.keepSrcElementName);
            } else {
                lvalue = replaceContent(lvalue, lvaluePtr, rvalue.getTextContent());
            }
            final VariableInstance lval = _scopeFrame.resolve(ocopy.to.getVariable());
            if (__log.isDebugEnabled())
                __log.debug("ASSIGN Writing variable '" + lval.declaration.name + "' value '"
                        + DOMUtils.domToString(lvalue) + "'");
            commitChanges(lval, lvalue);
            se = new VariableModificationEvent(lval.declaration.name);
            ((VariableModificationEvent) se).setNewValue(lvalue);
        }
    }

    if (ocopy.debugInfo != null)
        se.setLineNo(ocopy.debugInfo.startLine);
    sendEvent(se);
}

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

private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) {
    Document doc = ptr.getOwnerDocument();
    Node parent = ptr.getParentNode();
    if (keepSrcElement) {
        Element replacement = (Element) doc.importNode(src, true);
        parent.replaceChild(replacement, ptr);
        return (lval == ptr) ? replacement : lval;
    }/*from   w  w  w.j a  va2s  . c o  m*/

    Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getTagName());
    NodeList nl = src.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i)
        replacement.appendChild(doc.importNode(nl.item(i), true));
    NamedNodeMap attrs = src.getAttributes();
    for (int i = 0; i < attrs.getLength(); ++i) {
        Attr attr = (Attr) attrs.item(i);
        if (!attr.getName().startsWith("xmlns")) {
            replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true));
            // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually
            int colonIdx = attr.getValue().indexOf(":");
            if (colonIdx > 0) {
                String prefix = attr.getValue().substring(0, colonIdx);
                String attrValNs = src.lookupPrefix(prefix);
                if (attrValNs != null)
                    replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs);
            }
        }
    }
    parent.replaceChild(replacement, ptr);
    DOMUtils.copyNSContext(ptr, replacement);

    return (lval == ptr) ? replacement : lval;
}

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

/**
 * @deprecated relies on XMLSerializer which is a deprecated Xerces class, use domToString instead
 *//*from   ww w  . j  ava2 s.c  o m*/
static public String prettyPrint(Element e) throws IOException {
    OutputFormat format = new OutputFormat(e.getOwnerDocument());
    format.setLineWidth(65);
    format.setIndenting(true);
    format.setIndent(2);
    StringWriter out = new StringWriter();
    XMLSerializer serializer = new XMLSerializer(out, format);
    serializer.serialize(e);

    return out.toString();
}

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

public static void serialize(Element elmt, OutputStream ostr) {
    String usedEncoding = "UTF-8";
    Document parent = elmt.getOwnerDocument();
    if (parent != null) {
        String parentEncoding = parent.getXmlEncoding();
        if (parentEncoding != null) {
            usedEncoding = parentEncoding;
        }// ww w .  j ava 2 s . com
    }

    DOMOutputImpl out = new DOMOutputImpl();
    out.setEncoding(usedEncoding);

    DOMSerializerImpl ser = new DOMSerializerImpl();
    out.setByteStream(ostr);
    ser.write(elmt, out);
}