List of usage examples for org.w3c.dom Node getParentNode
public Node getParentNode();
From source file:org.apache.ws.security.util.WSSecurityUtil.java
public static String getPrefixNS(String uri, Node e) { while (e != null && (e.getNodeType() == Element.ELEMENT_NODE)) { NamedNodeMap attrs = e.getAttributes(); for (int n = 0; n < attrs.getLength(); n++) { Attr a = (Attr) attrs.item(n); String name = a.getName(); if (name.startsWith("xmlns:") && a.getNodeValue().equals(uri)) { return name.substring(6); }/*from w w w . j a v a 2s .c o m*/ } e = e.getParentNode(); } return null; }
From source file:org.apache.ws.security.util.WSSecurityUtil.java
public static String getNamespace(String prefix, Node e) { while (e != null && (e.getNodeType() == Node.ELEMENT_NODE)) { Attr attr = null;/*from w w w . j a va2 s . com*/ if (prefix == null) { attr = ((Element) e).getAttributeNode("xmlns"); } else { attr = ((Element) e).getAttributeNodeNS(WSConstants.XMLNS_NS, prefix); } if (attr != null) { return attr.getValue(); } e = e.getParentNode(); } return null; }
From source file:org.apache.xml.security.utils.CachedXPathFuncHereAPI.java
/** * Method getStrFromNode/*from ww w .j a va2 s . c o m*/ * * @param xpathnode * @return the string for the node. */ public static String getStrFromNode(Node xpathnode) { if (xpathnode.getNodeType() == Node.TEXT_NODE) { // we iterate over all siblings of the context node because eventually, // the text is "polluted" with pi's or comments StringBuffer sb = new StringBuffer(); for (Node currentSibling = xpathnode.getParentNode() .getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) { if (currentSibling.getNodeType() == Node.TEXT_NODE) { sb.append(((Text) currentSibling).getData()); } } return sb.toString(); } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) { return ((Attr) xpathnode).getNodeValue(); } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { return ((ProcessingInstruction) xpathnode).getNodeValue(); } return null; }
From source file:org.apache.xml.security.utils.IdResolver.java
private static int getEl(Node currentNode, String id, Element[] els) { Node sibling = null;/* w w w .ja va 2s . c o m*/ Node parentNode = null; do { switch (currentNode.getNodeType()) { case Node.DOCUMENT_FRAGMENT_NODE: case Node.DOCUMENT_NODE: sibling = currentNode.getFirstChild(); break; case Node.ELEMENT_NODE: Element currentElement = (Element) currentNode; if (isElement(currentElement, id, els) == 1) { return 1; } sibling = currentNode.getFirstChild(); if (sibling == null) { if (parentNode != null) { sibling = currentNode.getNextSibling(); } } else { parentNode = currentElement; } break; } while (sibling == null && parentNode != null) { sibling = parentNode.getNextSibling(); parentNode = parentNode.getParentNode(); if (parentNode != null && Node.ELEMENT_NODE != parentNode.getNodeType()) { parentNode = null; } } if (sibling == null) { return 1; } currentNode = sibling; sibling = currentNode.getNextSibling(); } while (true); }
From source file:org.apache.xml.security.utils.XMLUtils.java
/** * This is the work horse for {@link #circumventBug2650}. * * @param node/* ww w. j a va 2s . c om*/ * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650"> * Namespace axis resolution is not XPath compliant </A> */ private static void circumventBug2650internal(Node node) { Node parent = null; Node sibling = null; final String namespaceNs = Constants.NamespaceSpecNS; do { switch (node.getNodeType()) { case Node.ELEMENT_NODE: Element element = (Element) node; if (!element.hasChildNodes()) { break; } if (element.hasAttributes()) { NamedNodeMap attributes = element.getAttributes(); int attributesLength = attributes.getLength(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() != Node.ELEMENT_NODE) { continue; } Element childElement = (Element) child; for (int i = 0; i < attributesLength; i++) { Attr currentAttr = (Attr) attributes.item(i); if (!namespaceNs.equals(currentAttr.getNamespaceURI())) continue; if (childElement.hasAttributeNS(namespaceNs, currentAttr.getLocalName())) { continue; } childElement.setAttributeNS(namespaceNs, currentAttr.getName(), currentAttr.getNodeValue()); } } } case Node.ENTITY_REFERENCE_NODE: parent = node; sibling = node.getFirstChild(); break; case Node.DOCUMENT_NODE: parent = node; sibling = node.getFirstChild(); break; } while ((sibling == null) && (parent != null)) { sibling = parent.getNextSibling(); parent = parent.getParentNode(); } if (sibling == null) { return; } node = sibling; sibling = node.getNextSibling(); } while (true); }
From source file:org.archive.crawler.migrate.MigrateH1to3Tool.java
/** * Given a Document, return a Map of all non-blank simple text * nodes, keyed by the pseudo-XPath to their parent element. * /*from w w w . j a v a 2s. c om*/ * @param h1order Document to extract Map * @return Map<String,String> Xpath-like-String -> non-blank text content * @throws XPathExpressionException */ public static Map<String, String> flattenH1Order(Document h1order) throws XPathExpressionException { Map<String, String> flattened = new LinkedHashMap<String, String>(); XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//text()"); NodeList nodes = (NodeList) xpath.evaluate(h1order, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (StringUtils.isNotBlank(node.getTextContent())) { String pseudoXPath = getPseudoXpath(node.getParentNode()); pseudoXPath = pseudoXPath.replaceFirst("/crawl-order", "/"); // System.out.println( // pseudoXPath // +" "+node.getTextContent()); flattened.put(pseudoXPath, node.getTextContent()); } } // System.err.println(flattened.size()); // System.err.println(flattened); return flattened; }
From source file:org.archive.crawler.migrate.MigrateH1to3Tool.java
/** * Given a node, give back an XPath-like string that addresses it. * (For our constrained order.xml files, it is a valid and unique * XPath, but the simple approach used here might not generate * unique XPaths on all XML.// w ww .j a va2 s . c o m * * @param node node to get pseudo-XPath * @return String pseudo-XPath */ protected static String getPseudoXpath(Node node) { String pseudoXpath = ""; Node currentNode = node; while (currentNode.getParentNode() != null) { String thisSegment = currentNode.getNodeName(); if (currentNode.getAttributes().getNamedItem("name") != null) { thisSegment = "*[@" + currentNode.getAttributes().getNamedItem("name") + "]"; } pseudoXpath = "/" + thisSegment + pseudoXpath; currentNode = currentNode.getParentNode(); } return pseudoXpath; }
From source file:org.asqatasun.crawler.util.HeritrixAttributeValueModifier.java
@Override public Document modifyDocument(Document document, String value) { if (value == null || value.isEmpty()) { LOGGER.debug(" value is empty " + this.getClass()); return document; }/*from w w w.ja va2 s . co m*/ try { Node parentNode = getNodeFromXpath(document); NamedNodeMap attr = parentNode.getAttributes(); Node nodeAttr = attr.getNamedItem(DEFAULT_ATTRIBUTE_NAME); if (StringUtils.isNotEmpty(value)) { nodeAttr.setTextContent(value); } else { parentNode.getParentNode().removeChild(parentNode); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Update " + getAttributeValue() + " attribute of bean " + getIdBeanParent() + " with value " + value); } } catch (XPathExpressionException ex) { LOGGER.warn(ex); } return document; }
From source file:org.asqatasun.crawler.util.HeritrixAttributeValueModifierAndEraser.java
@Override public Document modifyDocument(Document document, String value) { try {/*w w w. j a v a 2 s . c o m*/ Node parentNode = getNodeFromXpath(document); NamedNodeMap attr = parentNode.getAttributes(); Node nodeAttr = attr.getNamedItem(DEFAULT_ATTRIBUTE_NAME); if (StringUtils.isNotEmpty(value)) { nodeAttr.setTextContent(value); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Update " + getAttributeValue() + " attribute of bean " + getIdBeanParent() + " with value " + value); } } else { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Delete " + getAttributeValue() + " attribute of bean " + getIdBeanParent() + " because of null or empty value "); } parentNode.getParentNode().removeChild(parentNode); } } catch (XPathExpressionException ex) { Logger.getLogger(HeritrixParameterValueModifier.class.getName()).warn(ex); } return document; }
From source file:org.bibsonomy.scraper.url.kde.ieee.IEEEXploreStandardsScraper.java
public String ieeeStandardsScrape(ScrapingContext sc) throws ScrapingException { try {/*from www . ja v a2 s . com*/ //-- init all NodeLists and Node NodeList pres = null; Node currNode = null; NodeList temp = null; //-- init String map for bibtex entries String type = IEEE_STANDARDS; String url = sc.getUrl().toString(); String numpages = ""; String title = ""; String isbn = ""; String abstr = ""; String year = ""; //-- get the html doc and parse the DOM final Document document = XmlUtils.getDOM(sc.getPageContent()); /* -- get the spans to extract the title and abstract */ pres = null; pres = document.getElementsByTagName("span"); //get all <span>-Tags for (int i = 0; i < pres.getLength(); i++) { currNode = pres.item(i); if (currNode.hasAttributes()) { Element g = (Element) currNode; Attr own = g.getAttributeNode("class"); //-- Extract the title if ("headNavBlueXLarge2".equals(own.getValue())) { temp = currNode.getChildNodes(); title = temp.item(temp.getLength() - 1).getNodeValue().trim(); } //-- Extract the abstract if ("sectionHeaders".equals(own.getValue()) && "Abstract".equals(currNode.getFirstChild().getNodeValue())) { abstr = currNode.getParentNode().getLastChild().getNodeValue().trim(); } } } /*-- get all <p>-Tags to extract the standard informations * In every standard page the css-class "bodyCopyBlackLargeSpaced" * indicates the collection of all informations. * */ pres = null; pres = document.getElementsByTagName("p"); //get all <p>-Tags for (int i = 0; i < pres.getLength(); i++) { currNode = pres.item(i); if (currNode.hasAttributes()) { Element g = (Element) currNode; Attr own = g.getAttributeNode("class"); if ("bodyCopyBlackLargeSpaced".equals(own.getValue())) { temp = currNode.getChildNodes(); for (int j = 0; j < temp.getLength(); j++) { if (temp.item(j).getNodeValue().indexOf(CONST_DATE) != -1) { String date = temp.item(j).getNodeValue().substring(CONST_DATE.length()).trim(); year = date.substring(date.length() - 4).trim(); } if (temp.item(j).getNodeValue().indexOf(CONST_PAGE) != -1) { numpages = temp.item(j).getNodeValue().substring(CONST_PAGE.length()).trim(); } if (temp.item(j).getNodeValue().indexOf(CONST_EISBN) != -1) { isbn = temp.item(j).getNodeValue().substring(CONST_EISBN.length()).trim(); } } } } } //create valid bibtex snippet return type + " {," + "title = {" + title + "}, " + "year = {" + year + "}, " + "url = {" + url + "}, " + "pages = {" + numpages + "}, " + "abstract = {" + abstr + "}, " + "isbn = {" + isbn + "}}"; } catch (Exception e) { throw new InternalFailureException(e); } }