Example usage for org.w3c.dom Element getOwnerDocument

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

Introduction

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

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:Main.java

/**
 * Sets data to be the CDATA content of element
 *
 * @param element the parent element./*ww  w  .  j  a  va2 s.  c  o  m*/
 * @param data the data to set.
 */
public static void setCData(Element element, String data) {
    if (data == null)
        return;

    CDATASection txt = getCDataNode(element);
    if (txt != null)
        txt.setData(data);
    else {
        txt = element.getOwnerDocument().createCDATASection(data);
        element.appendChild(txt);
    }
}

From source file:Main.java

public static String getNamespaceURI(Element el, String prefix) {
    if ((prefix == null) || (prefix.length() < 1)) {
        return "";
    }//from  w  w  w  .j  a v  a  2 s.com
    prefix = prefix.trim();
    try {
        NamedNodeMap map = el.getOwnerDocument().getDocumentElement().getAttributes();
        for (int j = 0; j < map.getLength(); j++) {
            Node n = map.item(j);
            String attrName = ((Attr) n).getName();
            if (attrName != null) {
                if (attrName.trim().equals("xmlns:" + prefix)) {
                    return ((Attr) n).getValue();
                }
            }
        }
    } catch (Exception e) {
    }

    return "";
}

From source file:com.evolveum.midpoint.prism.util.PrismUtil.java

private static void fortifyNamespaceDeclarations(Element definitionElement, Element childElement) {
    Document doc = definitionElement.getOwnerDocument();
    NamedNodeMap attributes = childElement.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        if (DOMUtil.isNamespaceDefinition(attr)) {
            String prefix = DOMUtil.getNamespaceDeclarationPrefix(attr);
            String namespace = DOMUtil.getNamespaceDeclarationNamespace(attr);
            Element namespaceElement = doc.createElementNS(PrismConstants.A_NAMESPACE.getNamespaceURI(),
                    PrismConstants.A_NAMESPACE.getLocalPart());
            namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_PREFIX, prefix);
            namespaceElement.setAttribute(PrismConstants.A_NAMESPACE_URL, namespace);
            definitionElement.insertBefore(namespaceElement, childElement);
        }//w  ww  . j  a va 2 s  . c  o  m
    }
}

From source file:cz.incad.kramerius.utils.solr.SolrUtils.java

public static String disectPid(Element topElem) throws XPathExpressionException {
    synchronized (topElem.getOwnerDocument()) {
        Element foundElement = XMLUtils.findElement(topElem, new XMLUtils.ElementsFilter() {

            @Override/*  w w  w  .  ja  v  a  2  s. c  o m*/
            public boolean acceptElement(Element element) {
                return (element.getNodeName().equals("str") && element.getAttribute("name") != null
                        && element.getAttribute("name").equals("PID"));
            }

        });
        if (foundElement != null) {
            return foundElement.getTextContent().trim();
        } else
            return null;
    }
}

From source file:Main.java

/** @param text <code>null</code> removes 'subElement' completely */
public static Text setTextField(Element node, String subElement, String text) {
    NodeList nl = node.getElementsByTagName(subElement);
    if (nl == null || nl.getLength() == 0) {
        if (text == null)
            return null;
        Document d = node.getOwnerDocument();
        Element e = d.createElement(subElement);
        Text ans = d.createTextNode(text);
        e.appendChild(ans);/*from w ww . ja va2  s .c om*/
        node.appendChild(e);
        return ans;
    } else if (text != null) {
        return setText(nl.item(0), text);
    } else {
        node.removeChild(nl.item(0));
        return null;
    }
}

From source file:Main.java

public static void setText(Element element, String Text) {
    Node node;// w ww.ja  va 2 s  .  c  om
    node = element.getFirstChild();
    while (null != node) {
        if (Node.TEXT_NODE == node.getNodeType()) {
            Text text = (Text) node;
            text.setData(Text);
            return;
        }
    }
    Text text = element.getOwnerDocument().createTextNode(Text);
    element.appendChild(text);
}

From source file:com.msopentech.odatajclient.engine.data.atom.AtomSerializer.java

private static void setLinks(final Element entry, final List<AtomLink> links)
        throws ParserConfigurationException {
    for (AtomLink link : links) {
        final Element linkElem = entry.getOwnerDocument().createElement(ODataConstants.ATOM_ELEM_LINK);

        linkElem.setAttribute(ODataConstants.ATTR_REL, link.getRel());
        linkElem.setAttribute(ODataConstants.ATTR_TITLE, link.getTitle());
        linkElem.setAttribute(ODataConstants.ATTR_HREF, link.getHref());

        if (StringUtils.isNotBlank(link.getType())) {
            linkElem.setAttribute(ODataConstants.ATTR_TYPE, link.getType());
        }//  w  w w  . ja  v  a2s. c  om

        if (link.getInlineEntry() != null || link.getInlineFeed() != null) {
            final Element inline = entry.getOwnerDocument().createElement(ODataConstants.ATOM_ELEM_INLINE);
            linkElem.appendChild(inline);

            if (link.getInlineEntry() != null) {
                inline.appendChild(
                        entry.getOwnerDocument().importNode(entry((AtomEntry) link.getInlineEntry()), true));
            }
            if (link.getInlineFeed() != null) {
                inline.appendChild(
                        entry.getOwnerDocument().importNode(feed((AtomFeed) link.getInlineFeed()), true));
            }
        }

        entry.appendChild(linkElem);
    }
}

From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java

public static Element createElementAndText(Element parent, String elementName, String text) {
    if (text == null) {
        throw new NullPointerException("null text");
    }/*from  ww  w .  ja v a  2s. c om*/
    Element el = parent.getOwnerDocument().createElement(elementName);
    parent.appendChild(el);
    Text txt = el.getOwnerDocument().createTextNode(text);
    el.appendChild(txt);
    return el;
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static <T> T value(final Element doc, final String attributeName, Class<T> clz) {
    if (doc == null) {
        throw new IllegalArgumentException("element must not be null");
    }/*from w w w. j a  v  a  2 s  .  c o m*/
    synchronized (doc.getOwnerDocument()) {
        final String expectedTypeName = SOLR_TYPE_NAMES.get(clz);
        List<Element> elms = XMLUtils.getElements(doc, new XMLUtils.ElementsFilter() {

            @Override
            public boolean acceptElement(Element element) {
                return (element.getNodeName().equals(expectedTypeName) && element.hasAttribute("name")
                        && element.getAttribute("name").equals(attributeName));
            }
        });
        Object obj = elms.isEmpty() ? null : elms.get(0).getTextContent();
        if (obj != null)
            return value(obj.toString(), clz);
        else
            return null;
    }
}

From source file:Main.java

public static String setInnerText(Element node, String value) {
    StringBuilder sb = new StringBuilder();
    while (node.hasChildNodes()) {
        Node tn = node.getFirstChild();
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        }/*from www  . j  a v  a 2  s  .  c o m*/
        node.removeChild(tn);
    }
    node.appendChild(node.getOwnerDocument().createTextNode(value));
    return sb.toString();
}