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.jbpm.bpel.xml.util.XmlUtil.java

public static void copyAttributes(Element target, Element source) {
    // easy way out: no attributes
    if (!source.hasAttributes())
        return;//from w  ww  .j  a  v  a  2  s . c  o  m
    // traverse attributes
    NamedNodeMap attributes = source.getAttributes();
    for (int i = 0, n = attributes.getLength(); i < n; i++) {
        Node sourceAttr = attributes.item(i);
        String namespaceURI = sourceAttr.getNamespaceURI();
        String name = sourceAttr.getNodeName();
        // namespace declaration?
        if (BpelConstants.NS_XMLNS.equals(namespaceURI))
            continue;
        // unqualified?
        if (namespaceURI == null || namespaceURI.length() == 0) {
            target.setAttribute(name, sourceAttr.getNodeValue());
            if (traceEnabled)
                log.trace("set attribute: " + name);
        }
        // qualified
        else {
            Attr targetAttr = target.getOwnerDocument().createAttributeNS(namespaceURI, name);
            targetAttr.setValue(sourceAttr.getNodeValue());
            target.setAttributeNodeNS(targetAttr);
            ensureNamespaceDeclared(targetAttr, namespaceURI, sourceAttr.getPrefix());
            if (traceEnabled)
                log.trace("set attribute: {" + namespaceURI + '}' + name);
        }
    }
}

From source file:org.osaf.cosmo.xml.DomReader.java

private static Element readElement(Document d, XMLStreamReader reader) throws XMLStreamException {
    Element e = null;

    String local = reader.getLocalName();
    String ns = reader.getNamespaceURI();
    if (ns != null) {
        String prefix = reader.getPrefix();
        String qualified = prefix != null ? prefix + ":" + local : local;
        e = d.createElementNS(ns, qualified);
    } else {/*w ww  .j a v a  2 s  .c o  m*/
        e = d.createElement(local);
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading element " + e.getTagName());

    for (int i = 0; i < reader.getAttributeCount(); i++) {
        Attr a = readAttribute(i, d, reader);
        if (a.getNamespaceURI() != null)
            e.setAttributeNodeNS(a);
        else
            e.setAttributeNode(a);
    }

    return e;
}

From source file:org.unitedinternet.cosmo.util.DomReader.java

private static Element readElement(Document d, XMLStreamReader reader) throws XMLStreamException {
    Element e = null;

    String local = reader.getLocalName();
    String ns = reader.getNamespaceURI();
    if (ns != null && !ns.equals("")) {
        String prefix = reader.getPrefix();
        String qualified = prefix != null && !prefix.isEmpty() ? prefix + ":" + local : local;
        e = d.createElementNS(ns, qualified);
    } else {/* ww w . ja  v a  2 s .  c  o m*/
        e = d.createElement(local);
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading element " + e.getTagName());

    for (int i = 0; i < reader.getAttributeCount(); i++) {
        Attr a = readAttribute(i, d, reader);
        if (a.getNamespaceURI() != null) {
            e.setAttributeNodeNS(a);
        } else {
            e.setAttributeNode(a);
        }
    }

    return e;
}

From source file:org.wso2.carbon.humantask.core.engine.runtime.xpath.XPathExpressionRuntime.java

private Element replaceElement(Element lval, Element src) {
    Document doc = lval.getOwnerDocument();
    NodeList nl = src.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i) {
        lval.appendChild(doc.importNode(nl.item(i), true));
    }/*  ww  w  .  j  a v  a 2  s .  c  o  m*/
    NamedNodeMap attrs = src.getAttributes();
    for (int i = 0; i < attrs.getLength(); ++i) {
        Attr attr = (Attr) attrs.item(i);
        if (!attr.getName().startsWith("xmlns")) {
            lval.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) {
                    lval.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs);
                }
            }
        }
    }

    return lval;
}

From source file:sf.net.experimaestro.utils.JSUtils.java

public static Object toDOM(Scriptable scope, Object object, OptionalDocument document) {
    // Unwrap if needed (if this is not a JSBaseObject)
    if (object instanceof Wrapper && !(object instanceof JSBaseObject))
        object = ((Wrapper) object).unwrap();

    // It is already a DOM node
    if (object instanceof Node)
        return object;

    if (object instanceof XMLObject) {
        final XMLObject xmlObject = (XMLObject) object;
        String className = xmlObject.getClassName();

        if (className.equals("XMLList")) {
            LOGGER.debug("Transforming from XMLList [%s]", object);
            final Object[] ids = xmlObject.getIds();
            if (ids.length == 1)
                return toDOM(scope, xmlObject.get((Integer) ids[0], xmlObject), document);

            Document doc = XMLUtils.newDocument();
            DocumentFragment fragment = doc.createDocumentFragment();

            for (int i = 0; i < ids.length; i++) {
                Node node = (Node) toDOM(scope, xmlObject.get((Integer) ids[i], xmlObject), document);
                if (node instanceof Document)
                    node = ((Document) node).getDocumentElement();
                fragment.appendChild(doc.adoptNode(node));
            }//from  w  w w .  jav a2  s.  com

            return fragment;
        }

        // XML node
        if (className.equals("XML")) {
            // FIXME: this strips all whitespaces!
            Node node = XMLLibImpl.toDomNode(object);
            LOGGER.debug("Got node from JavaScript [%s / %s] from [%s]", node.getClass(),
                    XMLUtils.toStringObject(node), object.toString());

            if (node instanceof Document)
                node = ((Document) node).getDocumentElement();

            node = document.get().adoptNode(node.cloneNode(true));
            return node;
        }

        throw new RuntimeException(format("Not implemented: convert %s to XML", className));

    }

    if (object instanceof NativeArray) {
        NativeArray array = (NativeArray) object;
        ArrayNodeList list = new ArrayNodeList();
        for (Object x : array) {
            Object o = toDOM(scope, x, document);
            if (o instanceof Node)
                list.add(document.cloneAndAdopt((Node) o));
            else {
                for (Node node : XMLUtils.iterable((NodeList) o)) {
                    list.add(document.cloneAndAdopt(node));
                }
            }
        }
        return list;
    }

    if (object instanceof NativeObject) {
        // JSON case: each key of the JS object is an XML element
        NativeObject json = (NativeObject) object;
        ArrayNodeList list = new ArrayNodeList();

        for (Object _id : json.getIds()) {

            String jsQName = JSUtils.toString(_id);

            if (jsQName.length() == 0) {
                final Object seq = toDOM(scope, json.get(jsQName, json), document);
                for (Node node : XMLUtils.iterable(seq)) {
                    if (node instanceof Document)
                        node = ((Document) node).getDocumentElement();
                    list.add(document.cloneAndAdopt(node));
                }
            } else if (jsQName.charAt(0) == '@') {
                final QName qname = QName.parse(jsQName.substring(1), null, new JSNamespaceBinder(scope));
                Attr attribute = document.get().createAttributeNS(qname.getNamespaceURI(),
                        qname.getLocalPart());
                StringBuilder sb = new StringBuilder();
                for (Node node : XMLUtils.iterable(toDOM(scope, json.get(jsQName, json), document))) {
                    sb.append(node.getTextContent());
                }

                attribute.setTextContent(sb.toString());
                list.add(attribute);
            } else {
                final QName qname = QName.parse(jsQName, null, new JSNamespaceBinder(scope));
                Element element = qname.hasNamespace()
                        ? document.get().createElementNS(qname.getNamespaceURI(), qname.getLocalPart())
                        : document.get().createElement(qname.getLocalPart());

                list.add(element);

                final Object seq = toDOM(scope, json.get(jsQName, json), document);
                for (Node node : XMLUtils.iterable(seq)) {
                    if (node instanceof Document)
                        node = ((Document) node).getDocumentElement();
                    node = document.get().adoptNode(node.cloneNode(true));
                    if (node.getNodeType() == Node.ATTRIBUTE_NODE)
                        element.setAttributeNodeNS((Attr) node);
                    else
                        element.appendChild(node);
                }
            }
        }

        return list;
    }

    if (object instanceof Double) {
        // Wrap a double
        final Double x = (Double) object;
        if (x.longValue() == x.doubleValue())
            return document.get().createTextNode(Long.toString(x.longValue()));
        return document.get().createTextNode(Double.toString(x));
    }

    if (object instanceof Integer) {
        return document.get().createTextNode(Integer.toString((Integer) object));
    }

    if (object instanceof CharSequence) {
        return document.get().createTextNode(object.toString());
    }

    if (object instanceof UniqueTag)
        throw new XPMRuntimeException("Undefined cannot be converted to XML", object.getClass());

    if (object instanceof JSNode) {
        Node node = ((JSNode) object).getNode();
        if (document.has()) {
            if (node instanceof Document)
                node = ((Document) node).getDocumentElement();
            return document.get().adoptNode(node);
        }
        return node;
    }

    if (object instanceof JSNodeList) {
        return ((JSNodeList) object).getList();
    }

    if (object instanceof Scriptable) {
        ((Scriptable) object).getDefaultValue(String.class);
    }

    // By default, convert to string
    return document.get().createTextNode(object.toString());
}