List of usage examples for org.w3c.dom Node replaceChild
public Node replaceChild(Node newChild, Node oldChild) throws DOMException;
oldChild
with newChild
in the list of children, and returns the oldChild
node. From source file:org.apache.ode.bpel.rtrep.v2.AssignHelper.java
/** * madars.vitolins _at gmail.com - 2009.04.17 - moved from ASSIGN here *//*from www. j a v a 2 s .c o m*/ public Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) { Document doc = ptr.getOwnerDocument(); Node parent = ptr.getParentNode(); if (keepSrcElement) { Element replacement = (Element) doc.importNode(src, true); parent.replaceChild(replacement, ptr); return (lval == ptr) ? replacement : lval; } Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getLocalName()); NodeList nl = src.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) replacement.appendChild(doc.importNode(nl.item(i), true)); NamedNodeMap attrs = src.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Attr attr = (Attr) attrs.item(i); if (!attr.getName().startsWith("xmlns")) { replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true)); // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually int colonIdx = attr.getValue().indexOf(":"); if (colonIdx > 0) { String prefix = attr.getValue().substring(0, colonIdx); String attrValNs = src.lookupPrefix(prefix); if (attrValNs != null) replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs); } } } parent.replaceChild(replacement, ptr); DOMUtils.copyNSContext(ptr, replacement); return (lval == ptr) ? replacement : lval; }
From source file:org.apache.ode.bpel.runtime.ASSIGN.java
private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) { Document doc = ptr.getOwnerDocument(); Node parent = ptr.getParentNode(); if (keepSrcElement) { Element replacement = (Element) doc.importNode(src, true); parent.replaceChild(replacement, ptr); return (lval == ptr) ? replacement : lval; }/*from w w w . j a v a2 s . c om*/ Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getTagName()); NodeList nl = src.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) replacement.appendChild(doc.importNode(nl.item(i), true)); copyAttributes(doc, ptr, replacement); copyAttributes(doc, src, replacement); parent.replaceChild(replacement, ptr); DOMUtils.copyNSContext(ptr, replacement); return (lval == ptr) ? replacement : lval; }
From source file:org.apache.ode.bpel.runtime.AssignHelper.java
private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) { Document doc = ptr.getOwnerDocument(); Node parent = ptr.getParentNode(); if (keepSrcElement) { Element replacement = (Element) doc.importNode(src, true); parent.replaceChild(replacement, ptr); return (lval == ptr) ? replacement : lval; }/* w w w.ja va 2s .c o m*/ Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getTagName()); NodeList nl = src.getChildNodes(); for (int i = 0; i < nl.getLength(); ++i) replacement.appendChild(doc.importNode(nl.item(i), true)); NamedNodeMap attrs = src.getAttributes(); for (int i = 0; i < attrs.getLength(); ++i) { Attr attr = (Attr) attrs.item(i); if (!attr.getName().startsWith("xmlns")) { replacement.setAttributeNodeNS((Attr) doc.importNode(attrs.item(i), true)); // Case of qualified attribute values, we're forced to add corresponding namespace declaration manually int colonIdx = attr.getValue().indexOf(":"); if (colonIdx > 0) { String prefix = attr.getValue().substring(0, colonIdx); String attrValNs = src.lookupPrefix(prefix); if (attrValNs != null) replacement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + prefix, attrValNs); } } } parent.replaceChild(replacement, ptr); DOMUtils.copyNSContext(ptr, replacement); return (lval == ptr) ? replacement : lval; }
From source file:org.apache.xml.security.encryption.XMLCipher.java
/** * Encrypts an <code>Element</code> and replaces it with its encrypted * counterpart in the context <code>Document</code>, that is, the * <code>Document</code> specified when one calls * {@link #getInstance(String) getInstance}. * * @param element the <code>Element</code> to encrypt. * @return the context <code>Document</code> with the encrypted * <code>Element</code> having replaced the source <code>Element</code>. * @throws Exception/*ww w . j av a 2 s. c om*/ */ private Document encryptElement(Element element) throws Exception { if (log.isDebugEnabled()) { log.debug("Encrypting element..."); } if (null == element) { log.error("Element unexpectedly null..."); } if (cipherMode != ENCRYPT_MODE && log.isDebugEnabled()) { log.debug("XMLCipher unexpectedly not in ENCRYPT_MODE..."); } if (algorithm == null) { throw new XMLEncryptionException("XMLCipher instance without transformation specified"); } encryptData(contextDocument, element, false); Element encryptedElement = factory.toElement(ed); Node sourceParent = element.getParentNode(); sourceParent.replaceChild(encryptedElement, element); return contextDocument; }
From source file:org.apache.xml.security.encryption.XMLCipher.java
/** * Decrypts <code>EncryptedData</code> in a single-part operation. * * @param element the <code>EncryptedData</code> to decrypt. * @return the <code>Node</code> as a result of the decrypt operation. * @throws XMLEncryptionException//from w ww. j a v a 2 s .c om */ private Document decryptElement(Element element) throws XMLEncryptionException { if (log.isDebugEnabled()) { log.debug("Decrypting element..."); } if (cipherMode != DECRYPT_MODE) { log.error("XMLCipher unexpectedly not in DECRYPT_MODE..."); } String octets; try { octets = new String(decryptToByteArray(element), "UTF-8"); } catch (UnsupportedEncodingException uee) { throw new XMLEncryptionException("empty", uee); } if (log.isDebugEnabled()) { log.debug("Decrypted octets:\n" + octets); } Node sourceParent = element.getParentNode(); Node decryptedNode = serializer.deserialize(octets, sourceParent); // The de-serialiser returns a node whose children we need to take on. if (sourceParent != null && Node.DOCUMENT_NODE == sourceParent.getNodeType()) { // If this is a content decryption, this may have problems contextDocument.removeChild(contextDocument.getDocumentElement()); contextDocument.appendChild(decryptedNode); } else if (sourceParent != null) { sourceParent.replaceChild(decryptedNode, element); } return contextDocument; }
From source file:org.dhatim.xml.DomUtils.java
/** * Replace one node with another node./*from www . j a v a2 s. c o m*/ * @param newNode New node - added in same location as oldNode. * @param oldNode Old node - removed. */ public static void replaceNode(Node newNode, Node oldNode) { AssertArgument.isNotNull(newNode, "newNode"); AssertArgument.isNotNull(oldNode, "oldNode"); Node parentNode = oldNode.getParentNode(); if (parentNode == null) { logger.debug("Cannot replace node [" + oldNode + "] with [" + newNode + "]. [" + oldNode + "] has no parent."); } else { parentNode.replaceChild(newNode, oldNode); } }
From source file:org.ebayopensource.turmeric.eclipse.resources.util.SOAIntfUtil.java
/** * The format should be the following//from w w w .j a v a 2 s . c o m * <wsdl:service name="CreativeService"> * <wsdl:documentation> * <version>1.0</version> * </wsdl:documentation> * ... * * @param project the project * @param newVersion the new version * @param monitor the monitor * @throws Exception the exception */ public static void modifyWsdlAppInfoVersion(final IProject project, final String newVersion, IProgressMonitor monitor) throws Exception { monitor.setTaskName("Modifying service WSDL..."); final String serviceName = project.getName(); final IFile wsdlFile = SOAServiceUtil.getWsdlFile(project, serviceName); InputStream ins = null; Definition wsdl = null; try { ins = wsdlFile.getContents(); wsdl = WSDLUtil.readWSDL(ins); } finally { IOUtils.closeQuietly(ins); } monitor.worked(10); if (wsdl == null) return; DOMParser domParser = new DOMParser(); Document doc = null; try { ins = wsdlFile.getContents(); domParser.parse(new InputSource(ins)); doc = domParser.getDocument(); } finally { IOUtils.closeQuietly(ins); } monitor.worked(10); if (doc == null) return; Node wsdlNode = doc.getFirstChild(); Node serviceNode = null; for (int i = wsdlNode.getChildNodes().getLength() - 1; i >= 0; i--) { Node node = wsdlNode.getChildNodes().item(i); if (node.getNodeType() == Node.ELEMENT_NODE && ELEM_NAME_SERVICE.equals(node.getLocalName())) { serviceNode = node; break; } } monitor.worked(10); if (serviceNode == null) return; Node documentationNode = null; for (int i = 0; i < serviceNode.getChildNodes().getLength(); i++) { Node node = serviceNode.getChildNodes().item(i); if (ELEM_NAME_DOCUMENTATION.equals(node.getLocalName())) { documentationNode = node; } } monitor.worked(10); boolean needUpdateWsdl = false; if (documentationNode != null) { //we found the documentation node Node verNode = null; for (int i = 0; i < documentationNode.getChildNodes().getLength(); i++) { Node node = documentationNode.getChildNodes().item(i); if (ELEM_NAME_VERSION_V2_CAMEL_CASE.equals(node.getLocalName()) || ELEM_NAME_VERSION_V3_CAMEL_CASE.equals(node.getLocalName())) { verNode = node; break; } } if (verNode == null) { // add version node to document node if there is no version // node. Element v3Version = doc.createElement("version"); Text versionText = doc.createTextNode(newVersion); v3Version.appendChild(versionText); documentationNode.appendChild(v3Version); needUpdateWsdl = true; } else { if (ELEM_NAME_VERSION_V2_CAMEL_CASE.equals(verNode.getLocalName())) { // if current version node is V2 format, replace it with V3 // format Element v3Version = doc.createElement("version"); Text versionText = doc.createTextNode(newVersion); v3Version.appendChild(versionText); documentationNode.replaceChild(v3Version, verNode); needUpdateWsdl = true; } else { // current version format is V3, update version value. for (int i = 0; i < verNode.getChildNodes().getLength(); i++) { Node node = verNode.getChildNodes().item(i); if (node.getNodeType() == Node.TEXT_NODE && newVersion.equals(node.getNodeValue()) == false) { logger.warning("Version defined in WSDL's service section->", node.getNodeValue(), " is older than the new version->", newVersion); node.setNodeValue(newVersion); needUpdateWsdl = true; break; } } } } } monitor.worked(10); if (needUpdateWsdl == true) { FileWriter writer = null; try { writer = new FileWriter(wsdlFile.getLocation().toFile()); XMLUtil.writeXML(wsdlNode, writer); } finally { IOUtils.closeQuietly(writer); wsdlFile.refreshLocal(IResource.DEPTH_ONE, monitor); } } else { logger.info("WSDL already have the correct version '", newVersion, "', skip the modification for WSDL->", wsdlFile.getLocation()); } monitor.worked(10); }
From source file:org.etudes.component.app.melete.SubSectionUtilImpl.java
public String moveAllDownSection(String sectionsSeqXML, String section_id) throws MeleteException { try {//w ww. j av a 2 s . c o m org.w3c.dom.Document subSectionW3CDOM = Xml.readDocumentFromString(sectionsSeqXML); org.w3c.dom.Element root = subSectionW3CDOM.getDocumentElement(); org.w3c.dom.Element moveDownThisElement = subSectionW3CDOM.getElementById(section_id); org.w3c.dom.Node cloneMoveElement = moveDownThisElement.cloneNode(true); org.w3c.dom.Node afterElementParent = moveDownThisElement.getParentNode(); org.w3c.dom.Node LastChildOfafterElementParent = afterElementParent.getLastChild(); org.w3c.dom.Node cloneLastChildElement = LastChildOfafterElementParent.cloneNode(true); if (!LastChildOfafterElementParent.equals(moveDownThisElement)) { afterElementParent.replaceChild(cloneMoveElement, LastChildOfafterElementParent); org.w3c.dom.Node newLastChild = afterElementParent.getLastChild(); afterElementParent.insertBefore(cloneLastChildElement, newLastChild); afterElementParent.removeChild(moveDownThisElement); } return writeDocumentToString(subSectionW3CDOM); } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug("some other error on moving down subsections xml string" + ex.toString()); ex.printStackTrace(); } throw new MeleteException("move_down_fail"); } }
From source file:org.phenotips.ncbieutils.internal.AbstractSpecializedNCBIEUtilsAccessService.java
@Override public List<Map<String, Object>> getSummaries(List<String> idList) { // response example at // http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=omim&id=190685,605298,604829,602917,601088,602523,602259 // response type: XML // return it// ww w. ja v a2s. co m String queryList = getSerializedList(idList); String url = composeURL(TERM_SUMMARY_QUERY_SCRIPT, TERM_SUMMARY_PARAM_NAME, queryList); try { Document response = readXML(url); NodeList nodes = response.getElementsByTagName("Item"); // OMIM titles are all UPPERCASE, try to fix this for (int i = 0; i < nodes.getLength(); ++i) { Node n = nodes.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { if (n.getFirstChild() != null) { n.replaceChild(response.createTextNode(fixCase(n.getTextContent())), n.getFirstChild()); } } } List<Map<String, Object>> result = new LinkedList<Map<String, Object>>(); nodes = response.getElementsByTagName("DocSum"); for (int i = 0; i < nodes.getLength(); ++i) { Element n = (Element) nodes.item(i); Map<String, Object> doc = new HashMap<String, Object>(); doc.put("id", n.getElementsByTagName("Id").item(0).getTextContent()); NodeList items = n.getElementsByTagName("Item"); for (int j = 0; j < items.getLength(); ++j) { Element item = (Element) items.item(j); if ("List".equals(item.getAttribute("Type"))) { NodeList subitems = item.getElementsByTagName("Item"); if (subitems.getLength() > 0) { List<String> values = new ArrayList<String>(subitems.getLength()); for (int k = 0; k < subitems.getLength(); ++k) { values.add(subitems.item(k).getTextContent()); } doc.put(item.getAttribute("Name"), values); } } else { String value = item.getTextContent(); if (StringUtils.isNotEmpty(value)) { doc.put(item.getAttribute("Name"), value); } } } result.add(doc); } return result; } catch (Exception ex) { this.logger.error("Error while trying to retrieve summaries for ids " + idList + " " + ex.getClass().getName() + " " + ex.getMessage(), ex); } return Collections.emptyList(); }
From source file:org.silverpeas.SilverpeasSettings.xml.transform.XPathTransformer.java
/** * Transform the DOM tree using the configuration. * @param configuration the transformation configuration. * @param doc the DOM tree to be updated. *//* ww w .ja v a2s . c o m*/ protected void applyTransformation(XmlConfiguration configuration, Document doc) { List<Parameter> parameters = configuration.getParameters(); for (Parameter parameter : parameters) { displayMessageln("\t" + parameter.getKey() + " (mode:" + parameter.getMode() + ")"); Node rootXpathNode = getMatchingNode(parameter.getKey(), doc); if (rootXpathNode != null) { for (Value value : parameter.getValues()) { switch (value.getMode()) { case XmlTreeHandler.MODE_INSERT: { createNewNode(doc, rootXpathNode, value); } break; case XmlTreeHandler.MODE_DELETE: { Node deletedNode = getMatchingNode(value.getLocation(), rootXpathNode); rootXpathNode.removeChild(deletedNode); } break; case XmlTreeHandler.MODE_UPDATE: { Node oldNode = getMatchingNode(value.getLocation(), rootXpathNode); if (oldNode == null) { createNewNode(doc, rootXpathNode, value); } else { if (rootXpathNode.equals(oldNode)) { rootXpathNode = rootXpathNode.getParentNode(); } Node newNode = oldNode.cloneNode(true); if (oldNode instanceof Element) { ((Element) newNode).setTextContent(value.getValue()); rootXpathNode.replaceChild(newNode, oldNode); } else { ((Attr) newNode).setValue(value.getValue()); rootXpathNode.getAttributes().setNamedItem(newNode); } } break; } } } } } }