Example usage for org.w3c.dom Element setAttribute

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

Introduction

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

Prototype

public void setAttribute(String name, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

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());
            }//www. j a va 2 s .c o m
        }
    }
}

From source file:sep.gaia.resources.poi.POILoaderWorker.java

/**
 * Generates a Overpass API-request in XML-format. The request complies to the limitations and the
 * bounding box in <code>query</code> and is designed to retrieve both nodes and recursed ways.
 * @param query The query to generate XML for.
 * @return The generated XML-query./*from www.j av  a2 s .co  m*/
 */
private static String generateQueryXML(POIQuery query) {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.newDocument();

        // The root of a OSM-query is always osm-script:
        Element script = doc.createElement("osm-script");
        doc.appendChild(script);

        // First element is the union containing the queries:
        Element unionElement = doc.createElement("union");
        script.appendChild(unionElement);

        // Second element says that the recused union of the prior results should be formed:
        Element recurseUnion = doc.createElement("union");
        Element itemElement = doc.createElement("item");
        recurseUnion.appendChild(itemElement);
        Element recurseElement = doc.createElement("recurse");
        recurseElement.setAttribute("type", "down");
        recurseUnion.appendChild(recurseElement);

        script.appendChild(recurseUnion);

        // The last element means, that the results (of the recursed union)
        // should be written as response:
        Element printElement = doc.createElement("print");
        script.appendChild(printElement);

        // First query (in the query union) askes for nodes conforming the given attributes:
        Element queryNodeElement = doc.createElement("query");
        queryNodeElement.setAttribute("type", "node");

        // The second element does the same for ways:
        Element queryWayElement = doc.createElement("query");
        queryWayElement.setAttribute("type", "way");

        // Add them to the first union:
        unionElement.appendChild(queryNodeElement);
        unionElement.appendChild(queryWayElement);

        // Now iterate all key-value-pairs and add "has-kv"-pairs to both queries:
        POIFilter filter = query.getLimitations();
        Map<String, String> attributes = filter.getLimitations();

        for (String key : attributes.keySet()) {
            String value = attributes.get(key);

            // The values returned by POIFilter are regular expressions, so use regv instead of v:
            Element currentKVNode = doc.createElement("has-kv");
            currentKVNode.setAttribute("k", key);
            currentKVNode.setAttribute("regv", value);
            queryNodeElement.appendChild(currentKVNode);

            Element currentKVWay = doc.createElement("has-kv");
            currentKVWay.setAttribute("k", key);
            currentKVWay.setAttribute("regv", value);
            queryWayElement.appendChild(currentKVWay);

        }

        // We don't want the data of the whole earth, so add bounding-boxes to the queries:
        Element nodeBBoxElement = createBBoxElement(doc, query.getBoundingBox());
        queryNodeElement.appendChild(nodeBBoxElement);

        Element wayBBoxElement = createBBoxElement(doc, query.getBoundingBox());
        queryWayElement.appendChild(wayBBoxElement);

        // Now the XML-tree is built, so transform it to a string and return it:
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(stream);

        transformer.transform(source, result);

        return stream.toString();

    } catch (ParserConfigurationException | TransformerException e) {
        Logger.getInstance().error("Cannot write cache-index: " + e.getMessage());
        return null;
    }
}

From source file:Main.java

public static Element addAttributes(Element elem, boolean dropEmptyValues, Object... attributeData) {
    for (int i = 0; i < attributeData.length - 1; i += 2) {
        Object attribName = attributeData[i];
        Object attribValue = attributeData[i + 1];
        if (attribName != null && attribValue != null) {
            String value = attribValue.toString();
            if (!(dropEmptyValues && value.isEmpty())) {
                elem.setAttribute(attribName.toString(), value);
            }//from  w ww  .  ja  v a 2 s . co  m
        }
    }
    return elem;
}

From source file:Main.java

private static Element copyNode(Document destDocument, Element dest, Element src) {

    NamedNodeMap namedNodeMap = src.getAttributes();
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr attr = (Attr) namedNodeMap.item(i);
        dest.setAttribute(attr.getName(), attr.getValue());
    }/*ww  w  .j  a  v a 2 s  .c  o m*/
    NodeList childNodeList = src.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node child = childNodeList.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            Text text = destDocument.createTextNode(child.getTextContent());
            dest.appendChild(text);
        } else if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element element = destDocument.createElement(((Element) child).getTagName());
            element = copyNode(destDocument, element, (Element) child);
            dest.appendChild(element);
        }
    }

    return dest;
}

From source file:com.wavemaker.tools.pws.install.PwsInstall.java

private static Document insertEntryKey(Document doc, File[] runtimeJarFiles, File[] toolsJarFiles,
        String partnerName) throws IOException {

    NodeList beans_list = doc.getElementsByTagName("beans");
    Node beans_node = beans_list.item(0);

    // First delete old entries

    NodeList beansChildren = beans_node.getChildNodes();
    Node bean_node = null, prop_node = null, map_node = null;
    for (int i = 0; i < beansChildren.getLength(); i++) {
        if (!beansChildren.item(i).getNodeName().equals("bean")) {
            continue;
        }/*from ww w  .j a  v  a  2 s .co  m*/
        bean_node = beansChildren.item(i);
        NodeList beanChildren = bean_node.getChildNodes();
        for (int j = 0; j < beanChildren.getLength(); j++) {
            if (!beanChildren.item(j).getNodeName().equals("property")) {
                continue;
            }

            prop_node = beanChildren.item(j);
            break;
        }

        if (prop_node == null) {
            continue;
        }

        NodeList propChildren = prop_node.getChildNodes();
        for (int k = 0; k < propChildren.getLength(); k++) {
            if (!propChildren.item(k).getNodeName().equals("map")) {
                continue;
            }

            map_node = propChildren.item(k);
            break;
        }

        if (map_node == null) {
            continue;
        }

        NodeList mapChildren = map_node.getChildNodes();
        List<Node> oldEntryList = new ArrayList<Node>();

        for (int l = 0; l < mapChildren.getLength(); l++) {
            if (!mapChildren.item(l).getNodeName().equals("entry")) {
                continue;
            }
            Node target = mapChildren.item(l);
            NamedNodeMap entry_attributes = target.getAttributes();
            for (int m = 0; m < entry_attributes.getLength(); m++) {
                Node entryAttr = entry_attributes.item(m);
                if (entryAttr.getNodeName().equals("key") && entryAttr.getNodeValue().equals(partnerName)) {
                    oldEntryList.add(target);
                    break;
                }
            }
        }

        if (oldEntryList.size() > 0) {
            for (Node oldEntry : oldEntryList) {
                map_node.removeChild(oldEntry);
            }
        }

        // Now, add new entries

        NamedNodeMap bean_attributes = bean_node.getAttributes();
        for (int m = 0; m < bean_attributes.getLength(); m++) {
            Node beanAttr = bean_attributes.item(m);
            if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsLoginManagerBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, LOGIN_MANAGER)
                        || classExistsInJar(toolsJarFiles, partnerName, LOGIN_MANAGER)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + LOGIN_MANAGER);
                    map_node.appendChild(newEntry);
                }
                break;
            } else if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsRestImporterBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, REST_IMPORTER)
                        || classExistsInJar(toolsJarFiles, partnerName, REST_IMPORTER)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + REST_IMPORTER);
                    map_node.appendChild(newEntry);
                }
                break;
            } else if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsRestWsdlGeneratorBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, REST_WSDL_GENERATOR)
                        || classExistsInJar(toolsJarFiles, partnerName, REST_WSDL_GENERATOR)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + REST_WSDL_GENERATOR);
                    map_node.appendChild(newEntry);
                }
                break;
            } else if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsServiceModifierBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, SERVICE_MODIFIER)
                        || classExistsInJar(toolsJarFiles, partnerName, SERVICE_MODIFIER)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + SERVICE_MODIFIER);
                    map_node.appendChild(newEntry);
                }
                break;
            } else if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsRestServiceGeneratorBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, REST_SERVICE_IMPORTER)
                        || classExistsInJar(toolsJarFiles, partnerName, REST_SERVICE_IMPORTER)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + REST_SERVICE_IMPORTER);
                    map_node.appendChild(newEntry);
                }
                break;
            } else if (beanAttr.getNodeName().equals("id")
                    && beanAttr.getNodeValue().equals("pwsResponseProcessorBeanFactory")) {
                if (classExistsInJar(runtimeJarFiles, partnerName, RESPONSE_PROCESSOR)
                        || classExistsInJar(toolsJarFiles, partnerName, RESPONSE_PROCESSOR)) {
                    Element newEntry = doc.createElement("entry");
                    newEntry.setAttribute("key", partnerName);
                    newEntry.setAttribute("value-ref", partnerName + RESPONSE_PROCESSOR);
                    map_node.appendChild(newEntry);
                }
                break;
            }
        }
    }

    return doc;
}

From source file:Main.java

public static Element overrideXml(Element target, Element parent) {

    if (parent != null) {

        NamedNodeMap namedNodeMap = parent.getAttributes();
        for (int i = 0; i < namedNodeMap.getLength(); i++) {

            Node attributeNode = namedNodeMap.item(i);
            String parentAttributeName = attributeNode.getNodeName();
            String parentAttributeValue = attributeNode.getNodeValue();

            // attribute override
            if (!target.hasAttribute(parentAttributeName)) {
                target.setAttribute(parentAttributeName, parentAttributeValue);
            }//  w  w w . j a va2s  . c  o m

            // children override
            if (parent.getChildNodes().getLength() > 0) {
                if (target.getChildNodes().getLength() == 0) {
                    for (int j = 0; j < target.getChildNodes().getLength(); j++) {

                        target.appendChild(target.getChildNodes().item(j));
                    }
                }
            }

        }
    }

    return target;
}

From source file:Main.java

@SuppressWarnings("null")
public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;// w  w  w  . j a va  2s .c om
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                /*
                 if (domimpl && !attr.getSpecified()) {
                 ((Attr) element.getAttributeNode(attrName)).setSpecified(false);
                 }
                 */
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null && dest != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:com.git.original.common.config.XMLFileConfigDocument.java

/**
 * ??XML/*from   ww w . j a va  2 s  .c o m*/
 * 
 * @param cn
 *            ?
 * @return XML
 */
@SuppressWarnings("unchecked")
static Element convertConfigNode(Document doc, ConfigNode cn) {
    Element elem = doc.createElement(cn.getName());

    Map<String, String> attrMap = cn.attributes();
    if (attrMap != null) {
        for (Entry<String, String> entry : attrMap.entrySet()) {
            elem.setAttribute(entry.getKey(), entry.getValue());
        }
    }

    if (cn.hasChildren()) {
        for (Object child : cn.getAllChildren()) {
            if (child instanceof List) {
                List<ConfigNode> cnList = (List<ConfigNode>) child;
                for (ConfigNode node : cnList) {
                    elem.appendChild(convertConfigNode(doc, node));
                }
            } else {
                elem.appendChild(convertConfigNode(doc, (ConfigNode) child));
            }
        }

    } else if (cn.value != null) {
        elem.setTextContent(cn.value.toString());
    }

    return elem;
}

From source file:Main.java

public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;//from   www  . ja  va2s  .  c  om
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + place.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:Main.java

public static void createElementAndAppend(String name, Date value, Document doc, Element appendeeElement,
        String attributeName, String attributeValue) {
    Element newElement = null;
    if (value == null) {

        log.info("XMLUtil.createElementAndAppend()  value == null for name = " + name);
        newElement = doc.createElement(name);
        Text text = doc.createTextNode("");
        newElement.appendChild(text);/*from w ww  .  j  a  v  a 2 s .c o m*/
    } else {
        newElement = doc.createElement(name);
        Text text = doc.createTextNode(sdfObj.format(value));
        newElement.appendChild(text);
    }
    if (attributeName != null && !attributeName.equals("")) {
        newElement.setAttribute(attributeName, attributeValue);
    }
    appendeeElement.appendChild(newElement);
}