List of usage examples for org.w3c.dom Node getParentNode
public Node getParentNode();
From source file:edu.mayo.cts2.framework.core.xml.PatchedCastorMarshaller.java
protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException { Node node = domSource.getNode(); if (node != null && node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; Node parent = node.getParentNode(); while (parent != null) { NamedNodeMap atts = parent.getAttributes(); if (atts != null) { for (int i = 0, j = atts.getLength(); i < j; i++) { Attr att = (Attr) atts.item(i); if (XMLNS_NS.equals(att.getNamespaceURI())) { String name = att.getName(); String value = att.getValue(); if (!element.hasAttributeNS(XMLNS_NS, name)) { element.setAttributeNS(XMLNS_NS, name, value); }//from w w w . ja va 2 s . com } } } parent = parent.getParentNode(); } } return super.unmarshalDomSource(domSource); }
From source file:org.kuali.coeus.s2sgen.impl.validate.S2SValidatorServiceImpl.java
/** * //from w w w .j a v a2s . c om * This method receives a node, fetches its name, and recurses up to its parent node, until it reaches the document node, thus * creating the XPath of the node passed and returns it as String * * @param node for which Document node has to found. * @return String which represents XPath of the node */ protected String getXPath(Node node) { if (node == null || node.getNodeType() == Node.DOCUMENT_NODE) { return ""; } else { return getXPath(node.getParentNode()) + "/" + node.getNodeName(); } }
From source file:com.adaptris.util.text.xml.ReplaceNode.java
@Override public Document merge(Document original, Document newDoc) throws Exception { if (StringUtils.isEmpty(getXpathToNode())) { throw new Exception("No xpath node configured"); }//from ww w. ja v a 2 s . c o m Document resultDoc = original; Node node = createXPath().selectSingleNode(resultDoc, getXpathToNode()); if (node == null) { throw new Exception("Failed to resolve " + getXpathToNode()); } Node parent = node.getParentNode(); if (parent == null) { throw new Exception("Parent of " + getXpathToNode() + " is null"); } Node replacement = resultDoc.importNode(newDoc.getDocumentElement(), true); parent.replaceChild(replacement, node); return resultDoc; }
From source file:com.konakart.bl.modules.payment.globalcollect.GlobalCollectUtils.java
/** * Utility to get the XML path of a node * /* www. j av a 2 s . co m*/ * @param nodeIn * @param arrayIdx * @param arrayLocation * @return The XML path of the node */ protected String getXmlPath(Node nodeIn, int arrayIdx, String arrayLocation) { String path = ""; Node myNode = nodeIn; while (myNode != null) { myNode = myNode.getParentNode(); if (myNode != null) { String name = myNode.getNodeName(); // if (!name.equals("#document")) if (myNode.getParentNode() != null) { if (path.length() > 0) { path = name + "." + path; } else { path = name + path; } } } } if (arrayIdx >= 0 && arrayLocation != null && path.startsWith(arrayLocation)) { path = path + "." + arrayIdx; } return path; }
From source file:de.betterform.xml.xforms.model.Instance.java
public static ModelItem createModelItem(Node node) { String id = Model.generateModelItemId(); ModelItem modelItem;/* w w w.j a v a2s. c om*/ if (node.getNodeType() == Node.ELEMENT_NODE) { modelItem = new XercesElementImpl(id); } else { modelItem = new XercesNodeImpl(id); } modelItem.setNode(node); Node parentNode; if (node.getNodeType() == Node.ATTRIBUTE_NODE) { parentNode = ((Attr) node).getOwnerElement(); } else { parentNode = node.getParentNode(); } if (parentNode != null) { ModelItem parentItem = (ModelItem) parentNode.getUserData(""); if (parentItem == null) { parentItem = createModelItem(parentNode); } modelItem.setParent(parentItem); } node.setUserData("", modelItem, null); return modelItem; }
From source file:com.rapidminer.gui.OperatorDocLoader.java
/** * //from ww w .j a v a 2 s .com * @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:nl.surfnet.sab.SabResponseParser.java
public SabRoleHolder parse(InputStream inputStream) throws IOException { String organisation = null;/*from w ww. ja va 2s . c o m*/ List<String> roles = new ArrayList<String>(); XPath xpath = getXPath(); try { Document document = createDocument(inputStream); validateStatus(document, xpath); // Extract organisation XPathExpression organisationExpr = xpath.compile(XPATH_ORGANISATION); NodeList nodeList = (NodeList) organisationExpr.evaluate(document, XPathConstants.NODESET); for (int i = 0; nodeList != null && i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node != null) { organisation = StringUtils.trimWhitespace(node.getTextContent()); node.getParentNode().getTextContent(); } } // Extract roles XPathExpression rolesExpr = xpath.compile(XPATH_ROLES); NodeList rolesNodeList = (NodeList) rolesExpr.evaluate(document, XPathConstants.NODESET); for (int i = 0; rolesNodeList != null && i < rolesNodeList.getLength(); i++) { Node node = rolesNodeList.item(i); if (node != null) { roles.add(StringUtils.trimWhitespace(node.getTextContent())); } } } catch (XPathExpressionException e) { throw new RuntimeException(e); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (SAXException e) { throw new IOException(e); } return new SabRoleHolder(organisation, roles); }
From source file:gov.nih.nci.cagrid.opensaml.SAMLObject.java
/** * Installs the root node of this DOM as the document element * * @return The root node so planted/*from w w w . ja va 2 s .c o m*/ */ protected Node plantRoot() { if (root != null) { Node domroot = root; while (domroot.getParentNode() != null && domroot.getParentNode().getNodeType() != Node.DOCUMENT_NODE) domroot = domroot.getParentNode(); Element e = root.getOwnerDocument().getDocumentElement(); if (e != null && e != domroot) root.getOwnerDocument().replaceChild(domroot, e); else if (e == null) root.getOwnerDocument().appendChild(domroot); } return root; }
From source file:esg.security.yadis.XrdsDoc.java
protected Map extractElementsByParent(String ns, String elem, Set parents, Document document) { Map result = new HashMap(); NodeList nodes = document.getElementsByTagNameNS(ns, elem); Node node; for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i);/*from w ww . j a v a 2 s .c o m*/ if (node == null || !parents.contains(node.getParentNode())) continue; String localId = node.getFirstChild() != null && node.getFirstChild().getNodeType() == Node.TEXT_NODE ? node.getFirstChild().getNodeValue() : null; result.put(node.getParentNode(), localId); } return result; }