Example usage for org.w3c.dom Element setAttributeNodeNS

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

Introduction

In this page you can find the example usage for org.w3c.dom Element setAttributeNodeNS.

Prototype

public Attr setAttributeNodeNS(Attr newAttr) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:org.apache.ode.bpel.rtrep.v1.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 ww w. j a v  a  2  s .  c  om

    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.rtrep.v2.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  ww w .  ja va2  s. co  m

    Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getLocalName());
    if (ptr.getPrefix() != null) {
        replacement.setPrefix(ptr.getPrefix());
    }
    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.rtrep.v2.AssignHelper.java

/**
 * madars.vitolins _at gmail.com - 2009.04.17 - moved from ASSIGN here
 *///from   w  w  w . j a  v  a2 s. c  om
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

private void copyAttributes(Document doc, Element original, Element replacement) {
    NamedNodeMap attrs = original.getAttributes();
    for (int i = 0; i < attrs.getLength(); ++i) {
        Attr attr = (Attr) attrs.item(i);
        replacement.setAttributeNodeNS((Attr) doc.importNode(attr, true));
    }/*from   w w w .j  a v a  2  s .c o  m*/
}

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   ww w . jav  a2  s.  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.il.OMUtils.java

@SuppressWarnings("unchecked")
public static Element toDOM(OMElement element, Document doc, boolean deepNS) {
    ///* w ww .j ava2  s  .com*/
    //  Fix regarding lost qnames on response of invoke activity:
    //    * copy an element including its prefix.
    //    * add all namespase attributes.
    //
    String domElementNsUri = element.getQName().getNamespaceURI();
    String domElementQName;
    if (element.getQName().getPrefix() == null || element.getQName().getPrefix().trim().length() == 0) {
        domElementQName = element.getQName().getLocalPart();
    } else {
        domElementQName = element.getQName().getPrefix() + ":" + element.getQName().getLocalPart();
    }
    if (__log.isTraceEnabled())
        __log.trace("toDOM: creating element with nsUri=" + domElementNsUri + " qname=" + domElementQName
                + " from omElement, name=" + element.getLocalName());

    final Element domElement = doc.createElementNS(domElementNsUri, domElementQName);

    if (deepNS) {
        NSContext nscontext = new NSContext();
        buildNScontext(nscontext, element);
        DOMUtils.injectNamespacesWithAllPrefixes(domElement, nscontext);
    } else {
        if (element.getAllDeclaredNamespaces() != null) {
            for (Iterator<OMNamespace> i = element.getAllDeclaredNamespaces(); i.hasNext();) {
                OMNamespace omns = i.next();
                if (omns.getPrefix().equals(""))
                    domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns",
                            omns.getNamespaceURI() == null ? "" : omns.getNamespaceURI());
                else
                    domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + omns.getPrefix(),
                            omns.getNamespaceURI());
            }

        }
    }
    if (__log.isTraceEnabled())
        __log.trace("toDOM: created root element (deepNS=" + deepNS + "): " + DOMUtils.domToString(domElement));

    for (Iterator i = element.getAllAttributes(); i.hasNext();) {
        final OMAttribute attr = (OMAttribute) i.next();
        Attr newAttr;
        if (attr.getNamespace() != null)
            newAttr = doc.createAttributeNS(attr.getNamespace().getNamespaceURI(), attr.getLocalName());
        else
            newAttr = doc.createAttributeNS(null, attr.getLocalName());

        newAttr.appendChild(doc.createTextNode(attr.getAttributeValue()));
        domElement.setAttributeNodeNS(newAttr);

        // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually...
        int colonIdx = attr.getAttributeValue().indexOf(":");
        if (colonIdx > 0) {
            OMNamespace attrValNs = element.findNamespaceURI(attr.getAttributeValue().substring(0, colonIdx));
            if (attrValNs != null)
                domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + attrValNs.getPrefix(),
                        attrValNs.getNamespaceURI());
        }
    }

    for (Iterator<OMNode> i = element.getChildren(); i.hasNext();) {
        OMNode omn = i.next();

        switch (omn.getType()) {
        case OMNode.CDATA_SECTION_NODE:
            domElement.appendChild(doc.createCDATASection(((OMText) omn).getText()));
            break;
        case OMNode.TEXT_NODE:
            domElement.appendChild(doc.createTextNode(((OMText) omn).getText()));
            break;
        case OMNode.ELEMENT_NODE:
            domElement.appendChild(toDOM((OMElement) omn, doc, false));
            break;
        }

    }

    return domElement;

}

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

/**
 * @param el/*from   w w  w. ja  v a2  s.  c  om*/
 */
public static void pancakeNamespaces(Element el) {
    Map ns = getParentNamespaces(el);
    Document d = el.getOwnerDocument();
    assert d != null;
    Iterator it = ns.keySet().iterator();
    while (it.hasNext()) {
        String key = (String) it.next();
        String uri = (String) ns.get(key);
        Attr a = d.createAttributeNS(NS_URI_XMLNS, (key.length() != 0) ? ("xmlns:" + key) : ("xmlns"));
        a.setValue(uri);
        el.setAttributeNodeNS(a);
    }
}

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

/**
 * Drop the attributes from an element, except possibly an <code>xmlns</code>
 * attribute that declares its namespace.
 * @param target the element whose attributes will be removed.
 * @param flag preserve namespace declaration
 *//*from w ww.ja v a 2 s .  c  o m*/
public static void removeAttributes(Element target, boolean flag) {
    if (!target.hasAttributes()) {
        return;
    }
    String prefix = target.getPrefix();
    NamedNodeMap nnm = target.getAttributes();
    Attr toPutBack = null;
    if (flag) {
        if (prefix == null) {
            toPutBack = target.getAttributeNodeNS(NS_URI_XMLNS, "xmlns");
        } else {
            toPutBack = target.getAttributeNodeNS(NS_URI_XMLNS, "xmlns:" + prefix);
        }

    }
    while (nnm.getLength() != 0) {
        target.removeAttributeNode((Attr) nnm.item(0));
    }
    if (toPutBack != null) {
        target.setAttributeNodeNS(toPutBack);
    }
}

From source file:org.jbpm.bpel.integration.soap.SoapUtil.java

public static void copyAttributes(Element target, SOAPElement source) {
    // easy way out: no attributes to copy
    if (!source.hasAttributes())
        return;/*  ww w  .  j a  va  2 s .  co m*/
    // traverse attributes
    Iterator attrNameIt = source.getAllAttributes();
    while (attrNameIt.hasNext()) {
        Name attrName = (Name) attrNameIt.next();
        String namespaceURI = attrName.getURI();

        // isn't the attribute a namespace declaration?
        if (BpelConstants.NS_XMLNS.equals(namespaceURI))
            continue;

        // unqualified?
        if (namespaceURI == null || namespaceURI.length() == 0) {
            String localName = attrName.getLocalName();
            target.setAttribute(localName, source.getAttributeValue(attrName));
            if (traceEnabled)
                log.trace("set attribute: " + localName);
        }
        // qualified
        else {
            Attr attr = target.getOwnerDocument().createAttributeNS(namespaceURI, attrName.getQualifiedName());
            attr.setValue(source.getAttributeValue(attrName));
            target.setAttributeNodeNS(attr);
            XmlUtil.ensureNamespaceDeclared(attr, namespaceURI, attrName.getPrefix());
            if (traceEnabled)
                log.trace("set attribute: {" + namespaceURI + '}' + attrName.getQualifiedName());
        }
    }
}

From source file:org.jbpm.bpel.sublang.xpath.XPathEvaluator.java

private static Attr createAttribute(Step step, Context context, Element contextElem) {
    if (!step.getPredicates().isEmpty()) {
        log.error("cannot create attribute for step with predicates: " + step);
        throw new BpelFaultException(BpelConstants.FAULT_SELECTION_FAILURE);
    }/* ww  w  .j  a  v a  2 s.c  o  m*/

    if (!(step instanceof NameStep)) {
        log.error("cannot create attribute for non-name node test: " + step);
        throw new BpelFaultException(BpelConstants.FAULT_SELECTION_FAILURE);
    }

    NameStep nameStep = (NameStep) step;
    String localName = nameStep.getLocalName();

    if ("*".equals(localName)) {
        log.error("cannot create attribute for any-name node test: " + nameStep);
        throw new BpelFaultException(BpelConstants.FAULT_SELECTION_FAILURE);
    }

    // BPEL-191: preserve source prefix
    String prefix = nameStep.getPrefix();
    String namespaceURI = context.translateNamespacePrefixToUri(prefix);

    Attr attribute = contextElem.getOwnerDocument().createAttributeNS(namespaceURI,
            namespaceURI != null ? prefix + ':' + localName : localName);
    contextElem.setAttributeNodeNS(attribute);

    return attribute;
}