Example usage for org.w3c.dom Element setAttributeNS

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

Introduction

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

Prototype

public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:Main.java

/**
 * Returns a namespaced root element of a document
 * Useful to create a namespace holder element
 * @param namespace/*from w  ww. jav a 2 s  .c  o m*/
 * @return the root Element
 */
public static Element getRootElement(String elementName, String namespace, String prefix)
        throws TransformerException {
    Element rootNS = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();
        Document namespaceHolder = impl.createDocument(namespace,
                (prefix == null ? "" : prefix + ":") + elementName, null);
        rootNS = namespaceHolder.getDocumentElement();
        rootNS.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace);
    } catch (Exception e) {
        String err = "Error creating a namespace holder document: " + e.getLocalizedMessage();
        throw new TransformerException(err);
    }
    return rootNS;
}

From source file:net.sourceforge.dita4publishers.word2dita.Word2DitaValidationHelper.java

/**
 * @param documentDom/*from w w w  .j  ava 2 s.  co m*/
 * @param commentId
 * @param xpath
 * @throws XPathExpressionException
 */
static void addCommentRefToParaForXPath(Document documentDom, String commentId, String xpath)
        throws XPathExpressionException {
    /**
    <w:r>
     <w:rPr>
       <w:rStyle
         w:val="CommentReference"/>
     </w:rPr>
     <w:commentReference
       w:id="14"/>
    </w:r>
             
     */
    Node node = getWordParaForXPath(documentDom, xpath);

    Element p = (Element) node;
    Element commentRef = documentDom.createElementNS(wNs, "w:r");
    Element elem = (Element) commentRef.appendChild(documentDom.createElementNS(wNs, "w:rPr"));
    elem = (Element) elem.appendChild(documentDom.createElementNS(wNs, "w:rStyle"));
    elem.setAttributeNS(wNs, "w:val", "CommentReference");
    elem = (Element) commentRef.appendChild(documentDom.createElementNS(wNs, "w:commentReference"));
    elem.setAttributeNS(wNs, "w:id", commentId);
    p.appendChild(commentRef);
}

From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtilsV4.java

public static void buildSubtree(final Element parent, final JsonNode node) {
    final String v4AnnotationPrefix = "@";
    final Iterator<String> fieldNameItor = node.fieldNames();
    final Iterator<JsonNode> nodeItor = node.elements();
    while (nodeItor.hasNext()) {
        final JsonNode child = nodeItor.next();
        final String name = fieldNameItor.hasNext() ? fieldNameItor.next() : "";

        // no name? array item
        if (name.isEmpty()) {
            final Element element = parent.getOwnerDocument().createElementNS(ODataConstants.NS_DATASERVICES,
                    ODataConstants.PREFIX_DATASERVICES + ODataConstants.ELEM_ELEMENT);
            parent.appendChild(element);

            if (child.isValueNode()) {
                if (child.isNull()) {
                    element.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_NULL,
                            Boolean.toString(true));
                } else {
                    element.appendChild(parent.getOwnerDocument().createTextNode(child.asText()));
                }/*from ww  w.j av  a 2  s  .c om*/
            }

            if (child.isContainerNode()) {
                buildSubtree(element, child);
            }
        } else if (!name.contains("@") && !ODataConstants.JSON_TYPE.equals(name)) {
            final Element property = parent.getOwnerDocument().createElementNS(ODataConstants.NS_DATASERVICES,
                    ODataConstants.PREFIX_DATASERVICES + name);
            parent.appendChild(property);

            boolean typeSet = false;
            if (node.hasNonNull(name + "@" + ODataConstants.JSON_TYPE)) {
                property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                        node.get(name + "@" + ODataConstants.JSON_TYPE).textValue());
                typeSet = true;
            }

            if (child.isNull()) {
                property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_NULL,
                        Boolean.toString(true));
            } else if (child.isValueNode()) {
                if (!typeSet) {
                    if (child.isInt()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.Int32.toString());
                    }
                    if (child.isLong()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.Int64.toString());
                    }
                    if (child.isBigDecimal()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.Decimal.toString());
                    }
                    if (child.isDouble()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.Double.toString());
                    }
                    if (child.isBoolean()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.Boolean.toString());
                    }
                    if (child.isTextual()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.String.toString());
                    }
                }

                property.appendChild(parent.getOwnerDocument().createTextNode(child.asText()));
            } else if (child.isContainerNode()) {
                if (!typeSet && child.hasNonNull(v4AnnotationPrefix + ODataConstants.JSON_TYPE)) {
                    property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                            child.get(v4AnnotationPrefix + ODataConstants.JSON_TYPE).textValue());
                }

                final String type = property.getAttribute(ODataConstants.ATTR_M_TYPE);
                if (StringUtils.isNotBlank(type) && EdmSimpleType.isGeospatial(type)) {
                    if (EdmSimpleType.Geography.toString().equals(type)
                            || EdmSimpleType.Geometry.toString().equals(type)) {

                        final String geoType = child.get(ODataConstants.ATTR_TYPE).textValue();
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                geoType.startsWith("Geo") ? EdmSimpleType.namespace() + "." + geoType
                                        : type + geoType);
                    }

                    if (child.has(ODataConstants.JSON_COORDINATES)
                            || child.has(ODataConstants.JSON_GEOMETRIES)) {
                        GeospatialJSONHandler.deserialize(child, property,
                                property.getAttribute(ODataConstants.ATTR_M_TYPE));
                    }
                } else {
                    buildSubtree(property, child);
                }
            }
        }
    }
}

From source file:be.fedict.eid.applet.service.signer.facets.XAdESXLSignatureFacet.java

public static Element createNamespaceElement(Node documentNode) {
    Document document = documentNode.getOwnerDocument();
    Element nsElement = document.createElement("nsElement");
    nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:ds", Constants.SignatureSpecNS);
    nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:xades", XADES_NAMESPACE);
    return nsElement;
}

From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtils.java

/**
 * Recursively builds DOM content out of JSON subtree rooted at given node.
 *
 * @param document root of the DOM document being built
 * @param parent parent of the nodes being generated during this step
 * @param node JSON node to be used as source for DOM elements
 *///from   ww  w .  j a v  a2s .  com
public static void buildSubtree(final Element parent, final JsonNode node) {
    final Iterator<String> fieldNameItor = node.fieldNames();
    final Iterator<JsonNode> nodeItor = node.elements();
    while (nodeItor.hasNext()) {
        final JsonNode child = nodeItor.next();
        final String name = fieldNameItor.hasNext() ? fieldNameItor.next() : "";

        // no name? array item
        if (name.isEmpty()) {
            final Element element = parent.getOwnerDocument().createElementNS(ODataConstants.NS_DATASERVICES,
                    ODataConstants.PREFIX_DATASERVICES + ODataConstants.ELEM_ELEMENT);
            parent.appendChild(element);

            if (child.isValueNode()) {
                if (child.isNull()) {
                    element.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_NULL,
                            Boolean.toString(true));
                } else {
                    element.appendChild(parent.getOwnerDocument().createTextNode(child.asText()));
                }
            }

            if (child.isContainerNode()) {
                buildSubtree(element, child);
            }
        } else if (!name.contains("@") && !ODataConstants.JSON_TYPE.equals(name)) {
            final Element property = parent.getOwnerDocument().createElementNS(ODataConstants.NS_DATASERVICES,
                    ODataConstants.PREFIX_DATASERVICES + name);
            parent.appendChild(property);

            boolean typeSet = false;
            if (node.hasNonNull(name + "@" + ODataConstants.JSON_TYPE)) {
                property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                        node.get(name + "@" + ODataConstants.JSON_TYPE).textValue());
                typeSet = true;
            }

            if (child.isNull()) {
                property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_NULL,
                        Boolean.toString(true));
            } else if (child.isValueNode()) {
                if (!typeSet) {
                    if (child.isInt()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.Int32.toString());
                    }
                    if (child.isLong()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.Int64.toString());
                    }
                    if (child.isBigDecimal()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.Decimal.toString());
                    }
                    if (child.isDouble()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.Double.toString());
                    }
                    if (child.isBoolean()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.Boolean.toString());
                    }
                    if (child.isTextual()) {
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                EdmSimpleType.String.toString());
                    }
                }

                property.appendChild(parent.getOwnerDocument().createTextNode(child.asText()));
            } else if (child.isContainerNode()) {
                if (!typeSet && child.hasNonNull(ODataConstants.JSON_TYPE)) {
                    property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                            child.get(ODataConstants.JSON_TYPE).textValue());
                }

                final String type = property.getAttribute(ODataConstants.ATTR_M_TYPE);
                if (StringUtils.isNotBlank(type) && EdmSimpleType.isGeospatial(type)) {
                    if (EdmSimpleType.Geography.toString().equals(type)
                            || EdmSimpleType.Geometry.toString().equals(type)) {

                        final String geoType = child.get(ODataConstants.ATTR_TYPE).textValue();
                        property.setAttributeNS(ODataConstants.NS_METADATA, ODataConstants.ATTR_M_TYPE,
                                geoType.startsWith("Geo") ? EdmSimpleType.namespace() + "." + geoType
                                        : type + geoType);
                    }

                    if (child.has(ODataConstants.JSON_COORDINATES)
                            || child.has(ODataConstants.JSON_GEOMETRIES)) {
                        GeospatialJSONHandler.deserialize(child, property,
                                property.getAttribute(ODataConstants.ATTR_M_TYPE));
                    }
                } else {
                    buildSubtree(property, child);
                }
            }
        }
    }
}

From source file:Main.java

private static void fixupAttrsSingle(Element e) throws DOMException {
    removeXmlBase(e);// w  w w  .  jav a 2 s.  c  om
    Map<String, String> replace = new HashMap<String, String>();
    NamedNodeMap attrs = e.getAttributes();
    for (int j = 0; j < attrs.getLength(); j++) {
        Attr attr = (Attr) attrs.item(j);
        if (attr.getNamespaceURI() == null && !attr.getName().equals("xmlns")) { // NOI18N
            replace.put(attr.getName(), attr.getValue());
        }
    }
    for (Map.Entry<String, String> entry : replace.entrySet()) {
        e.removeAttribute(entry.getKey());
        e.setAttributeNS(null, entry.getKey(), entry.getValue());
    }
}

From source file:de.betterform.xml.xforms.model.constraints.RelevanceSelector.java

private static void addAttributes(Element relevantElement, Node instanceNode) {
    NamedNodeMap instanceAttributes = instanceNode.getAttributes();

    for (int index = 0; index < instanceAttributes.getLength(); index++) {
        Node instanceAttr = (Node) instanceAttributes.item(index);

        if (isEnabled(instanceAttr)) {
            if (instanceAttr.getNamespaceURI() == null) {
                relevantElement.setAttribute(instanceAttr.getNodeName(), instanceAttr.getNodeValue());
            } else {
                relevantElement.setAttributeNS(instanceAttr.getNamespaceURI(), instanceAttr.getNodeName(),
                        instanceAttr.getNodeValue());
            }//from   w  ww  .  j  a  va 2s  .c o  m
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.impl.JSONDOMTreeUtils.java

/**
 * Recursively builds DOM content out of JSON subtree rooted at given node.
 *
 * @param client OData client.//from  ww  w .j  a v  a2  s.  c  om
 * @param document root of the DOM document being built
 * @param parent parent of the nodes being generated during this step
 * @param node JSON node to be used as source for DOM elements
 */
public static void buildSubtree(final ODataClient client, final Element parent, final JsonNode node) {
    final Iterator<String> fieldNameItor = node.fieldNames();
    final Iterator<JsonNode> nodeItor = node.elements();
    while (nodeItor.hasNext()) {
        final JsonNode child = nodeItor.next();
        final String name = fieldNameItor.hasNext() ? fieldNameItor.next() : "";

        // no name? array item
        if (name.isEmpty()) {
            final Element element = parent.getOwnerDocument().createElementNS(
                    client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_DATASERVICES),
                    ODataConstants.PREFIX_DATASERVICES + ODataConstants.ELEM_ELEMENT);
            parent.appendChild(element);

            if (child.isValueNode()) {
                if (child.isNull()) {
                    element.setAttributeNS(
                            client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                            ODataConstants.ATTR_NULL, Boolean.toString(true));
                } else {
                    element.appendChild(parent.getOwnerDocument().createTextNode(child.asText()));
                }
            }

            if (child.isContainerNode()) {
                buildSubtree(client, element, child);
            }
        } else if (!name.contains("@") && !ODataConstants.JSON_TYPE.equals(name)) {
            final Element property = parent.getOwnerDocument().createElementNS(
                    client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_DATASERVICES),
                    ODataConstants.PREFIX_DATASERVICES + name);
            parent.appendChild(property);

            boolean typeSet = false;
            if (node.hasNonNull(name + "@" + ODataConstants.JSON_TYPE)) {
                property.setAttributeNS(
                        client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                        ODataConstants.ATTR_M_TYPE,
                        node.get(name + "@" + ODataConstants.JSON_TYPE).textValue());
                typeSet = true;
            }

            if (child.isNull()) {
                property.setAttributeNS(
                        client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                        ODataConstants.ATTR_NULL, Boolean.toString(true));
            } else if (child.isValueNode()) {
                if (!typeSet) {
                    if (child.isInt()) {
                        property.setAttributeNS(
                                client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                                ODataConstants.ATTR_M_TYPE, EdmSimpleType.Int32.toString());
                    }
                    if (child.isLong()) {
                        property.setAttributeNS(
                                client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                                ODataConstants.ATTR_M_TYPE, EdmSimpleType.Int64.toString());
                    }
                    if (child.isBigDecimal()) {
                        property.setAttributeNS(
                                client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                                ODataConstants.ATTR_M_TYPE, EdmSimpleType.Decimal.toString());
                    }
                    if (child.isDouble()) {
                        property.setAttributeNS(
                                client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                                ODataConstants.ATTR_M_TYPE, EdmSimpleType.Double.toString());
                    }
                    if (child.isBoolean()) {
                        property.setAttributeNS(
                                client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                                ODataConstants.ATTR_M_TYPE, EdmSimpleType.Boolean.toString());
                    }
                    if (child.isTextual()) {
                        property.setAttributeNS(
                                client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                                ODataConstants.ATTR_M_TYPE, EdmSimpleType.String.toString());
                    }
                }

                property.appendChild(parent.getOwnerDocument().createTextNode(child.asText()));
            } else if (child.isContainerNode()) {
                if (!typeSet && child.hasNonNull(ODataConstants.JSON_TYPE)) {
                    property.setAttributeNS(
                            client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                            ODataConstants.ATTR_M_TYPE, child.get(ODataConstants.JSON_TYPE).textValue());
                }

                final String type = property.getAttribute(ODataConstants.ATTR_M_TYPE);
                if (StringUtils.isNotBlank(type) && EdmSimpleType.isGeospatial(type)) {
                    if (EdmSimpleType.Geography.toString().equals(type)
                            || EdmSimpleType.Geometry.toString().equals(type)) {

                        final String geoType = child.get(ODataConstants.ATTR_TYPE).textValue();
                        property.setAttributeNS(
                                client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA),
                                ODataConstants.ATTR_M_TYPE,
                                geoType.startsWith("Geo") ? EdmSimpleType.namespace() + "." + geoType
                                        : type + geoType);
                    }

                    if (child.has(ODataConstants.JSON_COORDINATES)
                            || child.has(ODataConstants.JSON_GEOMETRIES)) {
                        GeospatialJSONHandler.deserialize(child, property,
                                property.getAttribute(ODataConstants.ATTR_M_TYPE));
                    }
                } else {
                    buildSubtree(client, property, child);
                }
            }
        }
    }
}

From source file:com.microsoft.tfs.core.clients.build.internal.utils.XamlHelper.java

public static String updateProperties(final String originalXaml, final Properties properties) {
    final ArrayList keys = new ArrayList(properties.keySet());

    final Document document = DOMCreateUtils.parseString(originalXaml);
    final Element root = document.getDocumentElement();

    // first update any properties that we already have
    final NodeList nodes = root.getElementsByTagName("x:String"); //$NON-NLS-1$
    for (int i = 0; i < nodes.getLength(); i++) {
        final Element element = (Element) nodes.item(i);
        final String key = element.getAttribute("x:Key"); //$NON-NLS-1$
        element.getFirstChild().getNodeValue();

        if (properties.containsKey(key)) {
            keys.remove(key);/* w w w.ja va  2  s.co m*/
            element.getFirstChild().setNodeValue(properties.getProperty(key));
        }
    }

    // now add any new properties to the xaml
    for (final Iterator it = keys.iterator(); it.hasNext();) {
        final String key = (String) it.next();
        final Element element = DOMUtils.appendChild(root, "x:String"); //$NON-NLS-1$
        element.setAttributeNS(XAML_NAMESPACE, "x:Key", key); //$NON-NLS-1$
        element.setAttributeNS(XML_NAMESPACE, "xml:space", "preserve"); //$NON-NLS-1$ //$NON-NLS-2$
        DOMUtils.appendText(element, properties.getProperty(key));
    }

    return DOMSerializeUtils.toString(root, DOMSerializeUtils.INDENT).trim();
}

From source file:com.amalto.webapp.core.util.Util.java

/**
 * Returns a namespaced root element of a document Useful to create a namespace holder element
 * //from  ww w.  j a  v  a 2  s . c o m
 * @return the root Element
 */
public static Element getRootElement(String elementName, String namespace, String prefix) throws Exception {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();
        Document namespaceHolder = impl.createDocument(namespace,
                (prefix == null ? "" : prefix + ":") + elementName, null); //$NON-NLS-1$ //$NON-NLS-2$
        Element rootNS = namespaceHolder.getDocumentElement();
        rootNS.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace); //$NON-NLS-1$ //$NON-NLS-2$
        return rootNS;
    } catch (Exception e) {
        String err = "Error creating a namespace holder document: " + e.getLocalizedMessage(); //$NON-NLS-1$
        throw new Exception(err);
    }
}