Example usage for org.w3c.dom Node hasAttributes

List of usage examples for org.w3c.dom Node hasAttributes

Introduction

In this page you can find the example usage for org.w3c.dom Node hasAttributes.

Prototype

public boolean hasAttributes();

Source Link

Document

Returns whether this node (if it is an element) has any attributes.

Usage

From source file:esg.security.yadis.XrdsDoc.java

protected int getPriority(Node node) {
    if (node.hasAttributes()) {
        Node priority = node.getAttributes().getNamedItem(XRD_ATTR_PRIORITY);
        if (priority != null)
            return Integer.parseInt(priority.getNodeValue());
        else/*  w w w .ja v  a2 s.c  o m*/
            return XrdsServiceElem.LOWEST_PRIORITY;
    }

    return 0;
}

From source file:com.nanocrawler.contentparser.HtmlContentParser.java

private void getLinks(List<ExtractedUrlAnchorPair> outgoingUrls, Document doc, String elementName,
        String attribName, boolean getAnchorText) {
    if (doc.getElementsByTagName(elementName).getLength() > 0) {
        NodeList nl = doc.getElementsByTagName(elementName);
        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);
            if (n.hasAttributes() && n.getAttributes().getNamedItem(attribName) != null) {
                ExtractedUrlAnchorPair newUrl = new ExtractedUrlAnchorPair();
                newUrl.setHref(n.getAttributes().getNamedItem(attribName).getNodeValue().trim());
                if (getAnchorText) {
                    newUrl.setAnchor(n.getTextContent().trim());
                } else {
                    newUrl.setAnchor("");
                }/* w  ww  .  ja  va 2 s . c  o  m*/

                if (newUrl.getHref().trim().length() > 0) {
                    outgoingUrls.add(newUrl);
                }
            }
        }
    }
}

From source file:de.mpg.mpdl.inge.xmltransforming.TestBase.java

/**
 * Return the text value of the selected attribute.
 * /*from w  ww.  j  a  va 2  s. co  m*/
 * @param node The node.
 * @param xPath The xpath to select the node containint the attribute,
 * @param attributeName The name of the attribute.
 * @return The text value of the selected attribute.
 * @throws Exception If anything fails.
 */
public static String getAttributeValue(final Node node, final String xPath, final String attributeName)
        throws Exception {
    if (node == null) {
        throw new IllegalArgumentException(TestBase.class.getSimpleName() + ":getAttributeValue:node is null");
    }
    if (xPath == null) {
        throw new IllegalArgumentException(TestBase.class.getSimpleName() + ":getAttributeValue:xPath is null");
    }
    if (attributeName == null) {
        throw new IllegalArgumentException(
                TestBase.class.getSimpleName() + ":getAttributeValue:attributeName is null");
    }
    String result = null;
    Node attribute = selectSingleNode(node, xPath);
    if (attribute.hasAttributes()) {
        result = attribute.getAttributes().getNamedItem(attributeName).getTextContent();
    }
    return result;
}

From source file:com.nanocrawler.contentparser.HtmlContentParser.java

public List<ExtractedUrlAnchorPair> getOutgoingUrls(Document doc) {
    List<ExtractedUrlAnchorPair> outgoingUrls = new ArrayList<>();

    getLinks(outgoingUrls, doc, LINK_ELEMENT, HREF_ATTRIB, false);
    getLinks(outgoingUrls, doc, A_ELEMENT, HREF_ATTRIB, true);
    getLinks(outgoingUrls, doc, IFRAME_ELEMENT, SRC_ATTRIB, false);
    getLinks(outgoingUrls, doc, FRAME_ELEMENT, SRC_ATTRIB, false);
    getLinks(outgoingUrls, doc, EMBED_ELEMENT, SRC_ATTRIB, false);

    NodeList metaNodes = doc.getElementsByTagName(META_ELEMENT);
    if (metaNodes != null) {
        for (int i = 0; i < metaNodes.getLength(); i++) {
            Node n = metaNodes.item(i);
            if (n.hasAttributes()) {
                String equiv = n.getAttributes().getNamedItem("http-equiv") != null
                        ? n.getAttributes().getNamedItem("http-equiv").getNodeValue().trim()
                        : null;/*  w  w  w. ja va  2  s .  co m*/
                String content = n.getAttributes().getNamedItem("content") != null
                        ? n.getAttributes().getNamedItem("content").getNodeValue().trim()
                        : null;
                if (equiv != null && content != null) {
                    equiv = equiv.toLowerCase();

                    // http-equiv="refresh" content="0;URL=http://foo.bar/..."
                    if (equiv.equalsIgnoreCase("refresh")) {
                        String metaRefresh = "";
                        int pos = content.toLowerCase().indexOf("url=");
                        if (pos != -1) {
                            metaRefresh = content.substring(pos + 4);
                        }

                        if (metaRefresh.length() > 0) {
                            ExtractedUrlAnchorPair newUrl = new ExtractedUrlAnchorPair();
                            newUrl.setHref(metaRefresh);
                            newUrl.setAnchor("");
                            outgoingUrls.add(newUrl);
                        }
                    }

                    // http-equiv="location" content="http://foo.bar/..."
                    if (equiv.equalsIgnoreCase("location")) {
                        if (content.length() > 0) {
                            ExtractedUrlAnchorPair newUrl = new ExtractedUrlAnchorPair();
                            newUrl.setHref(content);
                            outgoingUrls.add(newUrl);
                        }
                    }
                }
            }
        }
    }

    return outgoingUrls;
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Find first page pid.//  w  ww.j  a  v a  2 s.  co m
 * 
 * @param pid
 *        the pid
 * @return the string
 */
public static String findFirstPagePid(String pid) {

    ArrayList<String> pids = new ArrayList<String>();
    try {
        String command = configuration.getFedoraHost() + "/get/" + pid + "/RELS-EXT";
        InputStream is = RESTHelper.get(command, configuration.getFedoraLogin(),
                configuration.getFedoraPassword(), true);
        Document contentDom = XMLUtils.parseDocument(is);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("/RDF/Description/*");
        NodeList nodes = (NodeList) expr.evaluate(contentDom, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node childnode = nodes.item(i);
            String nodeName = childnode.getNodeName();
            if (nodeName.contains(FedoraRelationship.hasPage.getStringRepresentation())
                    || nodeName.contains(FedoraRelationship.isOnPage.getStringRepresentation())) {
                return childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("uuid:")[1];
            } else if (!nodeName.contains("hasModel") && childnode.hasAttributes()
                    && childnode.getAttributes().getNamedItem("rdf:resource") != null) {
                pids.add(childnode.getAttributes().getNamedItem("rdf:resource").getNodeValue().split("/")[1]);
            }
        }
        for (String relpid : pids) {
            return FedoraUtils.findFirstPagePid(relpid);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}

From source file:com.swdouglass.joid.consumer.Discoverer.java

private String nodeToString(Node inNode) {
    StringBuilder sb = new StringBuilder();
    sb.append("[Node: name=");
    sb.append(inNode.getNodeName());/*from  w  ww .  j ava  2 s . c  o  m*/
    if (inNode.hasAttributes()) {
        sb.append(", attributes={");
        NamedNodeMap nnmap = inNode.getAttributes();
        for (int i = 0; i < nnmap.getLength(); i++) {
            sb.append(nnmap.item(i).getNodeName());
            sb.append("=");
            sb.append(nnmap.item(i).getNodeValue());
            sb.append(" ");
        }
    }
    sb.append("}]");
    return sb.toString();
}

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

public String getStringAttribute(Node node, String key) {
    if (node.hasAttributes()) {
        NamedNodeMap attrMap = node.getAttributes();
        Node attr = attrMap.getNamedItem(key);
        if (attr != null) {
            return attr.getNodeValue();
        }/*from  w  w w  .j  a  v  a2 s .  c  o  m*/
    }
    return "";
}

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

public int getIntAttribute(Node node, String key) {
    int result = -1;
    if (node.hasAttributes()) {
        NamedNodeMap attrMap = node.getAttributes();
        Node attr = attrMap.getNamedItem(key);
        if (attr != null) {
            try {
                result = Integer.parseInt(attr.getNodeValue());
            } catch (Exception ignored) {
            }// www  . ja v a2 s .  c  o m
        }
    }
    return result;
}

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

public long getLongAttribute(Node node, String key) {
    long result = -1;
    if (node.hasAttributes()) {
        NamedNodeMap attrMap = node.getAttributes();
        Node attr = attrMap.getNamedItem(key);
        if (attr != null) {
            try {
                result = Long.parseLong(attr.getNodeValue());
            } catch (Exception ignored) {
            }//ww  w .j  av a2 s  .  c  o  m
        }
    }
    return result;
}

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

public boolean getBooleanAttribute(Node node, String key) {
    boolean result = false;
    if (node.hasAttributes()) {
        NamedNodeMap attrMap = node.getAttributes();
        Node attr = attrMap.getNamedItem(key);
        if (attr != null) {
            try {
                result = Boolean.parseBoolean(attr.getNodeValue());
            } catch (Exception ignored) {
            }//from w w w.j  a v  a  2s .  c  om
        }
    }
    return result;
}