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.silverpeas.util.xml.transform.XPathTransformer.java
/** * Transform the DOM tree using the configuration. * * @param configuration the transformation configuration. * @param doc the DOM tree to be updated. *//*w ww. j av a2 s . com*/ protected void applyTransformation(XmlConfiguration configuration, Document doc) { List<Parameter> parameters = configuration.getParameters(); for (Parameter parameter : parameters) { console.printMessage('\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) { newNode.setTextContent(value.getValue()); rootXpathNode.replaceChild(newNode, oldNode); } else { ((Attr) newNode).setValue(value.getValue()); rootXpathNode.getAttributes().setNamedItem(newNode); } } break; } } } } } }
From source file:org.wso2.carbon.registry.samples.reporting.ServiceReportGenerator.java
public ByteArrayOutputStream execute(String template, String type) throws IOException { try {//from ww w . j a va2s .c o m Registry registry = getRegistry(); Registry gov = GovernanceUtils.getGovernanceUserRegistry(registry, "admin"); if (registry == null) { throw new RuntimeException("Registry is null"); } if (!registry.resourceExists(template)) { throw new FileNotFoundException("Template is not found"); } // Read Template String templateContent = RegistryUtils.decodeBytes((byte[]) registry.get(template).getContent()); //Read html file if (HTML.equalsIgnoreCase(type)) { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(templateContent))); //Find images and embed them NodeList nodes = doc.getElementsByTagName(IMAGE); for (int i = 0; i < nodes.getLength(); i++) { Node image = nodes.item(i); NodeList list = image.getChildNodes(); for (int j = 0; j != list.getLength(); ++j) { Node child = list.item(j); if (IMAGE_EXPRESSION.equals(child.getNodeName()) && child.getTextContent() != null) { String imgUrlStr = child.getTextContent().trim().replaceAll("^\"|\"$", ""); //Get image extension String imageExtension = imgUrlStr.substring(imgUrlStr.lastIndexOf(".") + 1); byte[] imageBytes = convertInputStream(imgUrlStr); Element imgExp = doc.createElement(IMAGE_EXPRESSION); String strBuilder = "\"data:image/" + imageExtension + ";base64," + new String(Base64.encodeBase64(imageBytes)) + "\""; imgExp.appendChild(doc.createCDATASection(strBuilder)); imgExp.setAttribute(CLASS, String.class.getName()); image.replaceChild(imgExp, child); } } } Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMSource source = new DOMSource(doc); StringWriter outWriter = new StringWriter(); StreamResult result = new StreamResult(outWriter); transformer.transform(source, result); StringBuffer stringBuffer = outWriter.getBuffer(); templateContent = stringBuffer.toString(); stringBuffer.delete(0, stringBuffer.length()); } GovernanceUtils.loadGovernanceArtifacts((UserRegistry) gov); // Create Report Bean Collection GenericArtifactManager genericArtifactManager = new GenericArtifactManager(gov, "restservice"); List<ReportBean> beanList = new LinkedList<ReportBean>(); for (GenericArtifact genericArtifact : genericArtifactManager.getAllGenericArtifacts()) { beanList.add(new ReportBean(genericArtifact.getAttribute("overview_name"), genericArtifact.getAttribute("overview_version"), genericArtifact.getAttribute("overview_description"))); } // Return Report Stream as a ByteArrayOutputStream return new ReportStream().getReportStream(new JasperPrintProvider().createJasperPrint( new JRBeanCollectionDataSource(beanList), templateContent, new ReportParamMap[0]), type); } catch (RuntimeException e) { throw new IOException("Failed to get input stream", e); } catch (RegistryException e) { throw new IOException("Failed to find report template", e); } catch (TransformerException e) { throw new IOException("Failed to transform file", e); } catch (JRException e) { throw new IOException("Failed to create jasperprint", e); } catch (ReportingException e) { throw new IOException("Failed to create jasperprint", e); } catch (ParserConfigurationException e) { throw new IOException("Failed to create DocumentBuilder", e); } catch (SAXException e) { throw new IOException("Failed to parse inputSource", e); } }
From source file:xmlconverter.controller.logic.GetFileCount.java
/** * This method is/are going to update the site information. The statements * are based on flags that are passed. This method should detect if files * content has changed and also update thous changed if node count has * changed./*from w w w .j a v a 2s . c om*/ * * @param path * @param pathXml * @throws DOMException * @throws IOException * @throws ParserConfigurationException * @throws SAXException * @throws TransformerException */ private void updateSite(Document newDoc, File pathXml) throws TransformerException, DOMException, ParserConfigurationException, IOException, SAXException { //System.out.println("I updated: " + pathXml.getName()); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document oldDoc = docBuilder.parse(pathXml); //Old root & NodeList Node root = oldDoc.getFirstChild(); NodeList staff = root.getChildNodes(); ArrayList<Node> oldNodes = new ArrayList<>(); for (int i = 0; i < staff.getLength(); i++) { oldNodes.add(staff.item(i)); } //New root & NodeList Node rootNew = newDoc.getFirstChild(); NodeList staffNew = rootNew.getChildNodes(); ArrayList<Node> newNodes = new ArrayList<>(); for (int i = 0; i < staffNew.getLength(); i++) { newNodes.add(staffNew.item(i)); } // * All extra nodes will be removed // * All not maching "id's" will be replaced if (oldNodes.size() > newNodes.size()) { for (int i = 0; i < oldNodes.size(); i++) { if (i >= newNodes.size()) { root.removeChild(oldNodes.get(i));//remove extra old nodes } else if (!oldNodes.get(i).getAttributes().getNamedItem("id").getNodeValue() .equals(newNodes.get(i).getAttributes().getNamedItem("id").getNodeValue())) { root.replaceChild(oldDoc.adoptNode(newNodes.get(i).cloneNode(true)), oldNodes.get(i)); //replace new node with old node } else { //System.out.println(i + "equal"); } } // * All not maching "id's" will be replaced } else if (oldNodes.size() == newNodes.size()) { for (int i = 0; i < oldNodes.size(); i++) { if (!oldNodes.get(i).getAttributes().getNamedItem("id").getNodeValue() .equals(newNodes.get(i).getAttributes().getNamedItem("id").getNodeValue())) { root.replaceChild(oldDoc.adoptNode(newNodes.get(i).cloneNode(true)), oldNodes.get(i));// replace old with new node } else { //System.out.println(i + " equal"); } } // * All extra nodes will be apended // * All not maching "id's" will be replaced } else if (oldNodes.size() < newNodes.size()) { for (int i = 0; i < newNodes.size(); ++i) { if (i >= oldNodes.size()) { root.appendChild(oldDoc.adoptNode(newNodes.get(i).cloneNode(true))); //append extra new node } else if (!newNodes.get(i).getAttributes().getNamedItem("id").getNodeValue() .equals(oldNodes.get(i).getAttributes().getNamedItem("id").getNodeValue())) { root.replaceChild(oldDoc.adoptNode(newNodes.get(i).cloneNode(true)), oldNodes.get(i));//replace old with new node } else { //System.out.println(i + "equal"); } } } writeDocument(oldDoc, pathXml); }