Example usage for org.w3c.dom Attr setNodeValue

List of usage examples for org.w3c.dom Attr setNodeValue

Introduction

In this page you can find the example usage for org.w3c.dom Attr setNodeValue.

Prototype

public void setNodeValue(String nodeValue) throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:Main.java

/**
 * Identify variables in attribute values and CDATA contents and replace
 * with their values as defined in the variableDefs map. Variables without
 * definitions are left alone.//  w ww  .  j a  v a 2 s . c o m
 *
 * @param node the node to do variable replacement in
 * @param variableDefs map from variable names to values.
 */
public static void replaceVariables(final Node node, Map<String, String> variableDefs) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        final Element element = (Element) node;
        final NamedNodeMap atts = element.getAttributes();
        for (int i = 0; i < atts.getLength(); i++) {
            final Attr attr = (Attr) atts.item(i);
            attr.setNodeValue(replaceVariablesInString(attr.getValue(), variableDefs));
        }
        break;

    case Node.CDATA_SECTION_NODE:
        String content = node.getTextContent();
        node.setNodeValue(replaceVariablesInString(content, variableDefs));
        break;

    default:
        break;
    }

    // process children
    final NodeList children = node.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); childIndex++)
        replaceVariables(children.item(childIndex), variableDefs);
}

From source file:Main.java

/**
 * Clones the given DOM node into the given DOM document.
 * /*from w ww .j a v a  2  s  .com*/
 * @param node
 *            The DOM node to clone.
 * @param doc
 *            The target DOM document.
 * @return The cloned node in the target DOM document.
 */
public static Node cloneNode(Node node, Document doc) {
    Node clone = null;
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        clone = doc.createElement(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attrNode = attrs.item(i);
            Attr attrClone = doc.createAttribute(attrNode.getNodeName());
            attrClone.setNodeValue(attrNode.getNodeValue());
            ((Element) clone).setAttributeNode(attrClone);
        }

        // Iterate through each child nodes.
        NodeList childNodes = node.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node childNode = childNodes.item(i);
                if (childNode == null) {
                    continue;
                }
                Node childClone = cloneNode(childNode, doc);
                if (childClone == null) {
                    continue;
                }
                clone.appendChild(childClone);
            }
        }
        break;

    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        clone = doc.createTextNode(node.getNodeName());
        clone.setNodeValue(node.getNodeValue());
        break;
    }
    return clone;
}

From source file:Main.java

public static final Node copyElement(Node e) throws UnsupportedDataTypeException {
    Node result = null;/*from ww w.j a  va  2  s  .  c o m*/

    switch (e.getNodeType()) {
    case Node.ELEMENT_NODE:
        result = document.createElement(e.getNodeName());

        for (int i = 0; i < e.getAttributes().getLength(); i++) {
            Attr attr = document.createAttribute(e.getAttributes().item(i).getNodeName());
            attr.setNodeValue(e.getAttributes().item(i).getNodeValue());
            result.getAttributes().setNamedItem(attr);
        }
        break;

    case Node.CDATA_SECTION_NODE:
        result = document.createCDATASection(((CDATASection) e).getData());
        break;
    case Node.TEXT_NODE:
        if (((Text) e).getTextContent().replaceAll("\t", "").trim() != "")
            result = document.createTextNode(((Text) e).getTextContent().replaceAll("\t", "").trim());
        break;

    default:
        throw new UnsupportedDataTypeException(new StringBuilder(e.getNodeType()).toString());
    }

    for (int i = 0; i < e.getChildNodes().getLength(); i++)
        result.appendChild(copyElement(e.getChildNodes().item(i)));

    return result;
}

From source file:Main.java

/**
 * Append attribute.//from  w w  w.j  a  v a 2  s . co m
 *
 * @param baseNode
 *            the base node
 * @param attributeName
 *            the attribute name
 * @param attributeValue
 *            the attribute value
 */
public static void appendAttribute(Element baseNode, String attributeName, String attributeValue) {
    Attr typeAttribute = baseNode.getOwnerDocument().createAttribute(attributeName);
    if (attributeValue == null) {
        typeAttribute.setNodeValue("<null>");
    } else {
        typeAttribute.setNodeValue(attributeValue);
    }
    baseNode.setAttributeNode(typeAttribute);
}

From source file:Main.java

public static void setTTestSetSampleSignificane(Node sampleNode, boolean different, double level) {
    Attr attr = sampleNode.getOwnerDocument().createAttribute("ttest");
    Attr attrLevel = sampleNode.getOwnerDocument().createAttribute("ttest-level");
    if (different)
        attr.setNodeValue("H1");
    else//from w ww  .  j av  a 2  s.co  m
        attr.setNodeValue("H0");
    // System.out.println(attr.getNodeValue());
    attrLevel.setNodeValue(level + "");
    Element e = (Element) sampleNode;
    e.setAttributeNode(attr);
    e.setAttributeNode(attrLevel);
}

From source file:de.elbe5.base.data.XmlData.java

public void addAttribute(Element node, String key, String value) {
    Attr attr = doc.createAttribute(key);
    attr.setNodeValue(value == null ? "" : value);
    node.setAttributeNode(attr);//from  w  ww .j a  va  2  s .  co m
}

From source file:de.fhg.iais.cortex.services.ingest.worker.BinaryResolutionWorker.java

private CortexModel replaceLocalUrisInEDM(CortexModel model, String itemId) {
    if ((model == null) || (model.getAny().size() < 1)) {
        return model;
    }//from  w  w  w.  j  a  va2  s  .  c  o m

    Element rdf = model.getAny().get(0);
    if (rdf != null) {
        //replace in edm model
        Element aggregation = (Element) rdf
                .getElementsByTagNameNS(GlobalConstants.ORE_NAMESPACE_URI, "Aggregation").item(0);
        if (aggregation != null) {
            Element thumbnail = (Element) aggregation
                    .getElementsByTagNameNS(GlobalConstants.EDM_NAMESPACE_URI, "object").item(0);
            if (thumbnail != null) {
                if (thumbnail.getAttributeNS(GlobalConstants.RDF_NAMESPACE_URI, Parser.RESOURCE) != null) {
                    Attr attr = thumbnail.getAttributeNodeNS(GlobalConstants.RDF_NAMESPACE_URI,
                            Parser.RESOURCE);
                    String href = attr.getValue();
                    String value = amendRelativeUris(itemId, href);
                    attr.setNodeValue(value);
                }
            }
        } else {
            throw new DbcException(
                    "Ingest failed: " + itemId + " has no ore:Aggregation. Please check your Mappings.");
        }
    }

    return model;
}

From source file:org.ajax4jsf.webapp.tidy.TidyParser.java

private org.w3c.dom.Node importNode(Document document, org.w3c.dom.Node node, boolean recursive) {

    switch (node.getNodeType()) {
    case org.w3c.dom.Node.ELEMENT_NODE:
        Element element = document.createElement(node.getNodeName());

        NamedNodeMap attributes = node.getAttributes();
        if (attributes != null) {
            int length = attributes.getLength();
            for (int i = 0; i < length; i++) {
                element.setAttributeNode((Attr) importNode(document, attributes.item(i), recursive));
            }/* w w w  .  j a  v a  2  s .  co m*/
        }

        if (recursive) {
            NodeList childNodes = node.getChildNodes();
            if (childNodes != null) {
                int length = childNodes.getLength();
                for (int i = 0; i < length; i++) {
                    element.appendChild(importNode(document, childNodes.item(i), recursive));
                }
            }
        }

        return element;

    case org.w3c.dom.Node.ATTRIBUTE_NODE:
        Attr attr = document.createAttribute(node.getNodeName());
        attr.setNodeValue(node.getNodeValue());

        return attr;

    case org.w3c.dom.Node.TEXT_NODE:
        String charData = ((CharacterData) node).getData();

        return document.createTextNode(charData);

    case org.w3c.dom.Node.CDATA_SECTION_NODE:
        charData = ((CharacterData) node).getData();

        return document.createCDATASection(charData);
    case org.w3c.dom.Node.COMMENT_NODE:
        charData = ((CharacterData) node).getData();

        return document.createComment(charData);
    case org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE:
    case org.w3c.dom.Node.DOCUMENT_NODE:
    case org.w3c.dom.Node.ENTITY_NODE:
    case org.w3c.dom.Node.ENTITY_REFERENCE_NODE:
    case org.w3c.dom.Node.NOTATION_NODE:
    case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:
    default:
        throw new IllegalArgumentException("Unsupported node type: " + node.getNodeType());
    }
}

From source file:org.deegree.portal.context.WebMapContextFactory.java

/**
 * creates an instance of a class encapsulating the access the configuration of Module
 * //from   w w  w .  j a v  a  2  s  . co  m
 * @param element
 * @param xml
 * 
 * @return instance of <tt>ModuleConfiguration</tt>
 * 
 * @throws XMLParsingException
 * @throws MalformedURLException
 */
private static ModuleConfiguration createModuleConfiguration(Element element, XMLFragment xml)
        throws XMLParsingException, MalformedURLException {

    ModuleConfiguration mc = null;
    if (element != null) {
        Element elem = XMLTools.getRequiredElement(element, "cntxt:OnlineResource",
                CommonNamespaces.getNamespaceContext());
        Attr attr = elem.getAttributeNodeNS(CommonNamespaces.XLNNS.toASCIIString(), "href");
        String url = attr.getValue();
        URL u = xml.resolve(url);
        url = u.toExternalForm();
        if (url.endsWith("?")) {
            url = url.substring(0, url.length() - 1);
        }
        attr.setNodeValue(url);
        URL onlineResource = createOnlineResource(elem);
        mc = new ModuleConfiguration(onlineResource);
    }

    return mc;
}

From source file:org.dita.dost.AbstractIntegrationTest.java

private Document rewriteIds(final Document doc, final Map<String, Pattern> patterns) {
    final Map<String, String> idMap = new HashMap<>();
    AtomicInteger counter = new AtomicInteger();
    final NodeList ns = doc.getElementsByTagName("*");
    for (int i = 0; i < ns.getLength(); i++) {
        final Element e = (Element) ns.item(i);
        for (Map.Entry<String, Pattern> p : patterns.entrySet()) {
            final Attr id = e.getAttributeNode(p.getKey());
            if (id != null) {
                if (p.getKey().equals("headers")) {// split value
                    final List<String> res = new ArrayList<>();
                    for (final String v : id.getValue().trim().split("\\s+")) {
                        rewriteId(v, idMap, counter, p.getValue());
                        res.add(idMap.getOrDefault(v, v));
                    }/* w w w.ja v  a  2 s. c  o  m*/
                    id.setNodeValue(res.stream().collect(Collectors.joining(" ")));

                } else {
                    final String v = id.getValue();
                    rewriteId(v, idMap, counter, p.getValue());
                    if (idMap.containsKey(v)) {
                        id.setNodeValue(idMap.get(v));
                    }
                }
            }
        }
    }
    return doc;
}