Example usage for org.w3c.dom Document getElementById

List of usage examples for org.w3c.dom Document getElementById

Introduction

In this page you can find the example usage for org.w3c.dom Document getElementById.

Prototype

public Element getElementById(String elementId);

Source Link

Document

Returns the Element that has an ID attribute with the given value.

Usage

From source file:Main.java

public static void findByID(Document doc, String idName) {
        Element name = doc.getElementById(idName);
        if (name == null) {
            System.out.println("There is no element with the ID " + idName);
        } else {/*from w  w w.  ja va  2 s .com*/
            Text text = (Text) name.getFirstChild();
            System.out.println("The ID " + idName + " locates the name " + text.getData());
        }
    }

From source file:DOMEdit.java

public static void findByID(Document doc, String idName) {
    Element name = doc.getElementById(idName);
    if (name == null) {
        System.out.println("There is no element with the ID " + idName);
    } else {/*from   w  ww  . j  ava  2s  .  c o m*/
        Text text = (Text) name.getFirstChild();
        System.out.println("The ID " + idName + " locates the name " + text.getData());
    }
}

From source file:be.e_contract.mycarenet.xkms.ProofOfPossessionSignatureSOAPHandler.java

private void prepareDocument(Document xkmsDocument) {
    Element prototypeElement = xkmsDocument.getElementById(this.prototypeKeyBindingId);
    if (null == prototypeElement) {
        LOG.warn("Prototype element not found via Id");
        prototypeElement = (Element) xkmsDocument.getElementsByTagNameNS(XKMS_NAMESPACE, "Prototype").item(0);
        prototypeElement.setIdAttribute("Id", true);
    }//from  ww w. j a  v a2  s. c om
}

From source file:be.e_contract.mycarenet.xkms2.ProofOfPossessionSignatureSOAPHandler.java

private void prepareDocument(Document xkmsDocument) {
    Element prototypeElement = xkmsDocument.getElementById(this.prototypeKeyBindingId);
    if (null == prototypeElement) {
        LOG.warn("Prototype element not found via Id");
        prototypeElement = (Element) xkmsDocument
                .getElementsByTagNameNS(XKMS2ServiceFactory.XKMS2_NAMESPACE, "PrototypeKeyBinding").item(0);
        prototypeElement.setIdAttribute("Id", true);
    }/*  ww w.ja v  a 2  s .  c  o m*/
}

From source file:de.betterform.connector.xmlrpc.XMLRPCURIResolver.java

/**
 * Decodes the <code>xmlrpc</code> URI, runs the indicated function
 * and returns the result as a DOM document.
 *
 * @return a DOM node parsed from the <code>xmlrpc</code> URI.
 * @throws XFormsException if any error occurred.
 *//*from  w  w  w  .  ja  v a  2  s  .c  o  m*/
public Object resolve() throws XFormsException {
    try {

        URI uri = new URI(getURI());
        log.info("Getting URI: '" + uri + "'");

        Vector v = parseURI(uri);
        String rpcURL = (String) v.get(0);
        String function = (String) v.get(1);
        Hashtable params = (Hashtable) v.get(2);

        de.betterform.connector.xmlrpc.RPCClient rpc = new de.betterform.connector.xmlrpc.RPCClient(rpcURL);

        Document document = rpc.getDocument(function, params);

        if (uri.getFragment() != null) {
            return document.getElementById(uri.getFragment());
        }

        return document;
    } catch (Exception e) {
        throw new XFormsException(e);
    }
}

From source file:be.e_contract.mycarenet.xkms2.KeyBindingAuthenticationSignatureSOAPHandler.java

private void prepareDocument(Document xkmsDocument) {
    Element prototypeElement = xkmsDocument.getElementById(this.prototypeKeyBindingId);
    if (null == prototypeElement) {
        LOG.warn("Prototype element not found via Id");
        prototypeElement = (Element) xkmsDocument
                .getElementsByTagNameNS(XKMS2ServiceFactory.XKMS2_NAMESPACE, "PrototypeKeyBinding").item(0);
        if (null == prototypeElement) {
            prototypeElement = (Element) xkmsDocument
                    .getElementsByTagNameNS(XKMS2ServiceFactory.XKMS2_NAMESPACE, "RevokeKeyBinding").item(0);
        }/* w ww  . ja  va2s  .c  o m*/
        prototypeElement.setIdAttribute("Id", true);
    }
}

From source file:eu.europa.esig.dss.xades.signature.EnvelopedSignatureBuilder.java

/**
 * Preconditions:/*from w  w  w.  j a va 2  s  .c  o m*/
 * - The reference data is XML
 * - The last transformation is canonicalization.
 *
 * @param reference {@code DSSReference} to be transformed
 * @return {@code DSSDocument} containing transformed reference's data
 */
@Override
protected DSSDocument transformReference(final DSSReference reference) {

    DSSDocument dssDocument = reference.getContents();
    final List<DSSTransform> transforms = reference.getTransforms();
    if (shouldPerformTransformations(transforms)) {
        return dssDocument;
    }
    // In the case of ENVELOPED signature the document to sign is an XML. However one of the references can point to another document this test case is not taken into account!

    Node nodeToTransform = null;
    final String uri = reference.getUri();
    // Check if the reference is related to the whole document
    if (StringUtils.isNotBlank(uri) && uri.startsWith("#") && !isXPointer(uri)) {

        final Document document = DSSXMLUtils.buildDOM(dssDocument);
        DSSXMLUtils.recursiveIdBrowse(document.getDocumentElement());
        final String uri_id = uri.substring(1);
        nodeToTransform = document.getElementById(uri_id);
    }
    byte[] transformedReferenceBytes = null;
    if (CollectionUtils.isEmpty(transforms)) {
        transformedReferenceBytes = DSSXMLUtils.serializeNode(nodeToTransform);
    } else {
        transformedReferenceBytes = applyTransformations(dssDocument, transforms, nodeToTransform,
                transformedReferenceBytes);
    }
    return new InMemoryDocument(transformedReferenceBytes);
}

From source file:com.rapidminer.gui.OperatorDocLoader.java

/**
 * /*  w w w. ja v  a  2  s  . c  o  m*/
 * @param operatorWikiName
 * @param opDesc
 * @return The parsed <tt>Document</tt> (not finally parsed) of the selected operator.
 * @throws MalformedURLException
 * @throws ParserConfigurationException
 */
private static Document parseDocumentForOperator(String operatorWikiName, OperatorDescription opDesc)
        throws MalformedURLException, ParserConfigurationException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setIgnoringComments(true);
    builderFactory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
    documentBuilder.setEntityResolver(new XHTMLEntityResolver());

    Document document = null;
    URL url = new URL(WIKI_PREFIX_FOR_OPERATORS + operatorWikiName);
    if (url != null) {
        try {
            document = documentBuilder.parse(WebServiceTools.openStreamFromURL(url));
        } catch (IOException e) {
            logger.warning("Could not open " + url.toExternalForm() + ": " + e.getMessage());
        } catch (SAXException e) {
            logger.warning("Could not parse operator documentation: " + e.getMessage());
        }

        int i = 0;

        if (document != null) {
            Element contentElement = document.getElementById("content");

            // removing content element from document
            if (contentElement != null) {
                contentElement.getParentNode().removeChild(contentElement);
            }

            // removing everything from body
            NodeList bodies = document.getElementsByTagName("body");
            for (int k = 0; k < bodies.getLength(); k++) {
                Node body = bodies.item(k);
                while (body.hasChildNodes()) {
                    body.removeChild(body.getFirstChild());
                }

                // read content element to body
                if (contentElement != null && k == 0) {
                    body.appendChild(contentElement);
                }
            }

            // removing everything from head
            NodeList heads = document.getElementsByTagName("head");
            for (int k = 0; k < heads.getLength(); k++) {
                Node head = heads.item(k);
                while (head.hasChildNodes()) {
                    head.removeChild(head.getFirstChild());
                }
            }
            // removing...<head/> from document
            if (heads != null) {
                while (i < heads.getLength()) {
                    Node head = heads.item(i);
                    head.getParentNode().removeChild(head);
                }
            }

            // removing jump-to-nav element from document
            Element jumpToNavElement = document.getElementById("jump-to-nav");
            if (jumpToNavElement != null) {
                jumpToNavElement.getParentNode().removeChild(jumpToNavElement);
            }

            // removing mw-normal-catlinks element from document
            Element mwNormalCatlinksElement = document.getElementById("mw-normal-catlinks");
            if (mwNormalCatlinksElement != null) {
                mwNormalCatlinksElement.getParentNode().removeChild(mwNormalCatlinksElement);
            }

            // removing complete link navigation
            Element tocElement = document.getElementById("toc");
            if (tocElement != null) {
                tocElement.getParentNode().removeChild(tocElement);
            }

            // removing everything from class printfooter
            NodeList nodeListDiv = document.getElementsByTagName("div");
            for (int k = 0; k < nodeListDiv.getLength(); k++) {
                Element div = (Element) nodeListDiv.item(k);
                if (div.getAttribute("class").equals("printfooter")) {
                    div.getParentNode().removeChild(div);
                }
            }

            // removing everything from class editsection
            NodeList spanList = document.getElementsByTagName("span");
            for (int k = 0; k < spanList.getLength(); k++) {
                Element span = (Element) spanList.item(k);
                if (span.getAttribute("class").equals("editsection")) {
                    span.getParentNode().removeChild(span);
                }
            }

            // Synopsis Header
            boolean doIt = true;
            NodeList pList = document.getElementsByTagName("p");
            for (int k = 0; k < pList.getLength(); k++) {

                if (doIt) {
                    Node p = pList.item(k);
                    NodeList pChildList = p.getChildNodes();

                    for (int j = 0; j < pChildList.getLength(); j++) {

                        Node pChild = pChildList.item(j);
                        if (pChild.getNodeType() == Node.TEXT_NODE && pChild.getNodeValue() != null
                                && StringUtils.isNotBlank(pChild.getNodeValue())
                                && StringUtils.isNotEmpty(pChild.getNodeValue())) {

                            String pChildString = pChild.getNodeValue();
                            Element newPWithoutSpaces = document.createElement("p");
                            newPWithoutSpaces.setTextContent(pChildString);

                            Node synopsis = document.createTextNode("Synopsis");

                            Element span = document.createElement("span");
                            span.setAttribute("class", "mw-headline");
                            span.setAttribute("id", "Synopsis");
                            span.appendChild(synopsis);

                            Element h2 = document.createElement("h2");
                            h2.appendChild(span);

                            Element div = document.createElement("div");
                            div.setAttribute("id", "synopsis");
                            div.appendChild(h2);
                            div.appendChild(newPWithoutSpaces);

                            Node pChildParentParent = pChild.getParentNode().getParentNode();
                            Node pChildParent = pChild.getParentNode();

                            pChildParentParent.replaceChild(div, pChildParent);
                            doIt = false;
                            break;
                        }
                    }
                } else {
                    break;
                }
            }

            // removing all <br...>-Tags
            NodeList brList = document.getElementsByTagName("br");

            while (i < brList.getLength()) {
                Node br = brList.item(i);
                Node parentBrNode = br.getParentNode();
                parentBrNode.removeChild(br);
            }

            // removing everything from script
            NodeList scriptList = document.getElementsByTagName("script");
            while (i < scriptList.getLength()) {
                Node scriptNode = scriptList.item(i);
                Node parentNode = scriptNode.getParentNode();
                parentNode.removeChild(scriptNode);
            }

            // removing all empty <p...>-Tags
            NodeList pList2 = document.getElementsByTagName("p");
            int ccc = 0;
            while (ccc < pList2.getLength()) {
                Node p = pList2.item(ccc);
                NodeList pChilds = p.getChildNodes();

                int kk = 0;

                while (kk < pChilds.getLength()) {
                    Node pChild = pChilds.item(kk);
                    if (pChild.getNodeType() == Node.TEXT_NODE) {
                        String pNodeValue = pChild.getNodeValue();
                        if (pNodeValue == null || StringUtils.isBlank(pNodeValue)
                                || StringUtils.isEmpty(pNodeValue)) {
                            kk++;
                        } else {
                            ccc++;
                            break;
                        }
                    } else {
                        ccc++;
                        break;
                    }
                    if (kk == pChilds.getLength()) {
                        Node parentBrNode = p.getParentNode();
                        parentBrNode.removeChild(p);
                    }
                }
            }

            // removing firstHeading element from document
            Element firstHeadingElement = document.getElementById("firstHeading");
            if (firstHeadingElement != null) {
                CURRENT_OPERATOR_NAME_READ_FROM_RAPIDWIKI = firstHeadingElement.getFirstChild().getNodeValue()
                        .replaceFirst(".*:", "");
                firstHeadingElement.getParentNode().removeChild(firstHeadingElement);
            }

            // setting operator plugin name
            if (opDesc != null && opDesc.getProvider() != null) {
                CURRENT_OPERATOR_PLUGIN_NAME = opDesc.getProvider().getName();
            }

            // removing sitesub element from document
            Element siteSubElement = document.getElementById("siteSub");
            if (siteSubElement != null) {
                siteSubElement.getParentNode().removeChild(siteSubElement);
            }

            // removing contentSub element from document
            Element contentSubElement = document.getElementById("contentSub");
            if (contentSubElement != null) {
                contentSubElement.getParentNode().removeChild(contentSubElement);
            }

            // removing catlinks element from document
            Element catlinksElement = document.getElementById("catlinks");
            if (catlinksElement != null) {
                catlinksElement.getParentNode().removeChild(catlinksElement);
            }

            // removing <a...> element from document, if they are empty
            NodeList aList = document.getElementsByTagName("a");
            if (aList != null) {
                int k = 0;
                while (k < aList.getLength()) {
                    Node a = aList.item(k);
                    Element aElement = (Element) a;
                    if (aElement.getAttribute("class").equals("internal")) {
                        a.getParentNode().removeChild(a);
                    } else {
                        Node aChild = a.getFirstChild();
                        if (aChild != null
                                && (aChild.getNodeValue() != null && aChild.getNodeType() == Node.TEXT_NODE
                                        && StringUtils.isNotBlank(aChild.getNodeValue())
                                        && StringUtils.isNotEmpty(aChild.getNodeValue())
                                        || aChild.getNodeName() != null)) {
                            Element aChildElement = null;
                            if (aChild.getNodeName().startsWith("img")) {
                                aChildElement = (Element) aChild;

                                Element imgElement = document.createElement("img");
                                imgElement.setAttribute("alt", aChildElement.getAttribute("alt"));
                                imgElement.setAttribute("class", aChildElement.getAttribute("class"));
                                imgElement.setAttribute("height", aChildElement.getAttribute("height"));
                                imgElement.setAttribute("src",
                                        WIKI_PREFIX_FOR_IMAGES + aChildElement.getAttribute("src"));
                                imgElement.setAttribute("width", aChildElement.getAttribute("width"));
                                imgElement.setAttribute("border", "1");

                                Node aParent = a.getParentNode();
                                aParent.replaceChild(imgElement, a);
                            } else {
                                k++;
                            }
                        } else {
                            a.getParentNode().removeChild(a);
                        }
                    }
                }
            }

        }
    }
    return document;
}

From source file:de.betterform.connector.file.FileURIResolver.java

/**
 * Performs link traversal of the <code>file</code> URI and returns the
 * result as a DOM document./*from   w  w  w  .ja v  a 2s.c  o m*/
 *
 * @return a DOM node parsed from the <code>file</code> URI.
 * @throws XFormsException if any error occurred during link traversal.
 */
public Object resolve() throws XFormsException {
    try {
        // create uri
        URI uri = new URI(getURI());

        // use scheme specific part in order to handle UNC names
        String fileName = uri.getSchemeSpecificPart();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("loading file '" + fileName + "'");
        }

        // create file
        File file = new File(fileName);

        // check for directory
        if (file.isDirectory()) {
            return FileURIResolver.buildDirectoryListing(file);
        }

        // parse file
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(false);
        Document document = factory.newDocumentBuilder().parse(file);

        /*
                    Document document = CacheManager.getDocument(file);
                    if(document == null) {
            document = DOMResource.newDocumentBuilder().parse(file);
            CacheManager.putIntoFileCache(file, document);
                    }
        */

        // check for fragment identifier
        if (uri.getFragment() != null) {
            return document.getElementById(uri.getFragment());
        }

        return document;
    } catch (Exception e) {
        //todo: improve error handling as files might fail due to missing DTDs or Schemas - this won't be detected very well
        throw new XFormsException(e);
    }
}

From source file:net.chaosserver.timelord.swingui.Timelord.java

/**
 * Check if there is a newer version of the timelord application
 * available and if so, prompt the user to download the new version.
 * /*  w w  w.j  a v a2 s .c  o  m*/
 * @return if the user wants to download a new version
 */
public boolean isUpgradeRequested() {
    boolean result = false;
    Properties appProperties = getAppProperties();

    if (appProperties != null) {
        String pomUrlString = appProperties.getProperty("pomurl");
        String appVersion = appProperties.getProperty("implementation.version");

        if (pomUrlString != null) {
            InputStream pomStream = null;

            try {
                URL pomUrl = new URL(pomUrlString);
                pomStream = pomUrl.openStream();

                if (log.isDebugEnabled()) {
                    log.debug("Opened URL [" + pomUrl + "] with result ["
                            + (pomStream != null ? pomStream.available() : "null") + "]");
                }

                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setNamespaceAware(true);
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse(pomStream);

                if (log.isTraceEnabled()) {
                    log.trace("Loaded document: " + doc.getDocumentElement());
                }

                Element pomversionElement = doc.getElementById("pomversion");
                String pomVersion = "";
                if (pomversionElement != null) {
                    pomVersion = pomversionElement.getNodeValue();
                }

                if (appVersion == null) {
                    appVersion = "";
                }

                if (log.isDebugEnabled()) {
                    log.debug("Testinv version of app [" + appVersion + "] against version of pom ["
                            + pomVersion + "]");
                }

                if (!pomVersion.equals(appVersion)) {

                }

            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (pomStream != null) {
                    try {
                        pomStream.close();
                    } catch (IOException e) {
                        if (log.isInfoEnabled()) {
                            log.info("failed to close pomstream", e);
                        }
                    }
                }
            }

        } else {
            if (log.isWarnEnabled()) {
                log.warn("Got back blank pomurl, so not checking " + "for upgrade.");
            }
        }
    } else {
        if (log.isWarnEnabled()) {
            log.warn("Got back blank properties, so not checking " + "for upgrade.");
        }
    }

    return result;
}