List of usage examples for org.dom4j Document setRootElement
void setRootElement(Element rootElement);
From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java
License:Open Source License
protected Document mergeXMLAttributes(Document doc1, Document doc2) { Element doc2Root = doc2.getRootElement(); Element doc1Root = doc1.getRootElement(); Element docRoot = doc2Root.createCopy(); docRoot.clearContent();// ww w . j ava 2s. c o m if (doc1Root != null) { Iterator<Object> attributesIterator = doc1Root.attributeIterator(); while (attributesIterator.hasNext()) { org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next(); if (attribute.getName().equals("extends")) { continue; } docRoot.addAttribute(attribute.getName(), attribute.getValue()); } Element descriptionElement = doc1Root.element("description"); if (descriptionElement != null) { docRoot.add(descriptionElement.createCopy()); } } DocumentFactory factory = SAXReaderUtil.getDocumentFactory(); Document doc = factory.createDocument(); doc.setRootElement(docRoot); List<Element> doc2Components = doc2Root.elements(_COMPONENT); for (Element doc2Component : doc2Components) { Element component = doc2Component.createCopy(); String name = doc2Component.attributeValue("name"); Element doc1Component = getComponentNode(doc1, name); if (doc1Component != null) { Element doc1ComponentDescriptionElement = doc1Component.element("description"); if (doc1ComponentDescriptionElement != null) { Element descriptionElement = component.element("description"); if (descriptionElement != null) { component.remove(descriptionElement); } component.add(doc1ComponentDescriptionElement.createCopy()); } Iterator<Object> attributesIterator = doc1Component.attributeIterator(); while (attributesIterator.hasNext()) { org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next(); component.addAttribute(attribute.getName(), attribute.getValue()); } Element doc1AttributesNode = doc1Component.element(_ATTRIBUTES); Element attributesNode = component.element(_ATTRIBUTES); if ((doc1AttributesNode != null) && (attributesNode != null)) { List<Element> doc1Attributes = doc1AttributesNode.elements(_ATTRIBUTE); List<Element> attributes = attributesNode.elements(_ATTRIBUTE); for (Element doc1Attribute : doc1Attributes) { Element attribute = getElementByName(attributes, doc1Attribute.elementText("name")); if (attribute != null) { attributesNode.remove(attribute); } attributesNode.add(doc1Attribute.createCopy()); } } Element doc1EventsNode = doc1Component.element(_EVENTS); Element eventsNode = component.element(_EVENTS); if ((doc1EventsNode != null) && (eventsNode != null)) { List<Element> doc1Events = doc1EventsNode.elements(_EVENT); List<Element> events = eventsNode.elements(_EVENT); for (Element doc1Event : doc1Events) { Element event = getElementByName(events, doc1Event.elementText("name")); if (event != null) { eventsNode.add(event); } eventsNode.add(doc1Event.createCopy()); } } } doc.getRootElement().add(component); } if (doc1Root != null) { List<Element> doc1Components = doc1Root.elements(_COMPONENT); for (Element doc1Component : doc1Components) { Element component = doc1Component.createCopy(); String name = doc1Component.attributeValue("name"); Element doc2Component = getComponentNode(doc2, name); if (doc2Component == null) { doc.getRootElement().add(component); } } } return doc; }
From source file:com.liferay.util.xml.XMLConverter.java
License:Open Source License
public static org.w3c.dom.Element toW3CElement(org.dom4j.Element dom4jEl) throws org.dom4j.DocumentException { org.dom4j.Document dom4jDoc = org.dom4j.DocumentFactory.getInstance().createDocument(); dom4jDoc.setRootElement(dom4jEl.createCopy()); org.w3c.dom.Document w3cDoc = toW3CDocument(dom4jDoc); return w3cDoc.getDocumentElement(); }
From source file:com.ltmonitor.jt809.app.GlobalConfig.java
public static void setModel(ParameterModel pm) { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("config"); doc.setRootElement(root); Element pe = root.addElement("parameter"); pe.addAttribute("platip", pm.getPlatformIP()); pe.addAttribute("idletime", String.valueOf(pm.getIdleTime())); pe.addAttribute("localport", String.valueOf(pm.getLocalPort())); pe.addAttribute("platid", String.valueOf(pm.getPlatformCenterId())); pe.addAttribute("platport", String.valueOf(pm.getPlatformPort())); pe.addAttribute("platuserid", String.valueOf(pm.getPlatformUserName())); pe.addAttribute("platpassword", String.valueOf(pm.getPlatformPass())); pe.addAttribute("miyaom", String.valueOf(pm.getMiyaoM())); pe.addAttribute("miyaoa", String.valueOf(pm.getMiyaoA())); pe.addAttribute("miyaoc", String.valueOf(pm.getMiyaoC())); pe.addAttribute("protocolver", pm.getProtocolVer()); pe.addAttribute("localip", pm.getLocalIp()); pe.addAttribute("licenseno", pm.getLicenseNo()); pe.addAttribute("username", pm.getUsername()); try {// w ww. j a v a 2 s.c o m FileWriter out = new FileWriter(CONFIG_PATH + "database.xml"); XmlUtils.printXml(doc, out); } catch (Exception ex) { //this.logger.warn("" + ex.getMessage()); } }
From source file:com.mg.framework.support.ui.UIProducer.java
License:Open Source License
public static Document performDocument(Document document, RuntimeMacrosLoader runtimeMacrosLoader) { logger.debug("Original form descriptor:\n".concat(document.asXML())); Document result = DocumentFactory.getInstance().createDocument(document.getXMLEncoding()); result.setDocType(document.getDocType()); result.setRootElement(copyElement(document.getRootElement(), runtimeMacrosLoader)); result.getRootElement().addNamespace(document.getRootElement().getNamespacePrefix(), document.getRootElement().getNamespaceURI()); try {//from w ww . jav a 2 s. c o m //? ?, ? ? namespaces result = DocumentHelper.parseText(result.asXML()); logger.debug("Finished form descriptor:\n".concat(result.asXML())); return result; } catch (DocumentException e) { throw new ApplicationException(e); } }
From source file:com.noterik.bart.fs.legacy.tools.XmlHelper.java
License:Open Source License
/** * This function returns a Document with the value of the property set by * path//from ww w . j a v a2 s .co m * * @param xml * String containing the property file * @param path * The path for the desired value * @return Document with the value of the property */ public static Document getValueOfProperty(String xml, String path) { String xpath = "/fsxml/" + path; Document doc = null; try { doc = DocumentHelper.parseText(xml); } catch (DocumentException e) { e.printStackTrace(); } Element elem = (Element) doc.selectSingleNode(xpath).clone(); Document newDoc = DocumentHelper.createDocument(); Element root = DocumentHelper.createElement("fsxml"); newDoc.setRootElement(root); Element fsxml = (Element) newDoc.selectSingleNode("/fsxml"); fsxml.add(elem); return newDoc; }
From source file:com.ostrichemulators.semtool.ui.components.playsheets.BrowserPlaySheet2.java
License:Open Source License
protected BufferedImage getExportImageFromSVGBlock() throws IOException { log.debug("Using SVG block to save image."); DOMReader rdr = new DOMReader(); Document doc = rdr.read(engine.getDocument()); Document svgdoc = null; File svgfile = null;// w ww. jav a2 s . co m try { Map<String, String> namespaceUris = new HashMap<>(); namespaceUris.put("svg", "http://www.w3.org/2000/svg"); namespaceUris.put("xhtml", "http://www.w3.org/1999/xhtml"); XPath xp = DocumentHelper.createXPath("//svg:svg"); xp.setNamespaceURIs(namespaceUris); // don't forget about the styles XPath stylexp = DocumentHelper.createXPath("//xhtml:style"); stylexp.setNamespaceURIs(namespaceUris); svgdoc = DocumentHelper.createDocument(); Element svg = null; List<?> theSVGElements = xp.selectNodes(doc); if (theSVGElements.size() == 1) { svg = Element.class.cast(theSVGElements.get(0)).createCopy(); } else { int currentTop = 0; int biggestSize = 0; for (int i = 0; i < theSVGElements.size(); i++) { Element thisElement = Element.class.cast(theSVGElements.get(i)).createCopy(); int thisSize = thisElement.asXML().length(); if (thisSize > biggestSize) { currentTop = i; biggestSize = thisSize; } } svg = Element.class.cast(theSVGElements.get(currentTop)).createCopy(); } svgdoc.setRootElement(svg); Element oldstyle = Element.class.cast(stylexp.selectSingleNode(doc)); if (null != oldstyle) { Element defs = svg.addElement("defs"); Element style = defs.addElement("style"); style.addAttribute("type", "text/css"); String styledata = oldstyle.getTextTrim(); style.addCDATA(styledata); // put the stylesheet definitions first List l = svg.elements(); l.remove(defs); l.add(0, defs); } // clean up the SVG a little... // d3 comes up with coords like // M360,27475.063247863247C450,27475.063247863247 450,27269.907692307694 540,27269.907692307694 XPath cleanxp1 = DocumentHelper.createXPath("//svg:path"); Pattern pat = Pattern.compile(",([0-9]+)\\.([0-9]{1,2})[0-9]+"); cleanxp1.setNamespaceURIs(namespaceUris); List<?> cleanups = cleanxp1.selectNodes(svgdoc); for (Object n : cleanups) { Element e = Element.class.cast(n); String dstr = e.attributeValue("d"); Matcher m = pat.matcher(dstr); dstr = m.replaceAll(",$1.$2 "); e.addAttribute("d", dstr.replaceAll("([0-9])C([0-9])", "$1 C$2").trim()); } XPath cleanxp2 = DocumentHelper.createXPath("//svg:g[@class='node']"); cleanxp2.setNamespaceURIs(namespaceUris); cleanups = cleanxp2.selectNodes(svgdoc); for (Object n : cleanups) { Element e = Element.class.cast(n); String dstr = e.attributeValue("transform"); Matcher m = pat.matcher(dstr); dstr = m.replaceAll(",$1.$2"); e.addAttribute("transform", dstr.trim()); } svgfile = File.createTempFile("graphviz-", ".svg"); try (Writer svgw = new BufferedWriter(new FileWriter(svgfile))) { OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter xmlw = new XMLWriter(svgw, format); xmlw.write(svgdoc); xmlw.close(); if (log.isDebugEnabled()) { FileUtils.copyFile(svgfile, new File(FileUtils.getTempDirectory(), "graphvisualization.svg")); } } try (Reader svgr = new BufferedReader(new FileReader(svgfile))) { TranscoderInput inputSvg = new TranscoderInput(svgr); ByteArrayOutputStream baos = new ByteArrayOutputStream((int) svgfile.length()); TranscoderOutput outputPng = new TranscoderOutput(baos); try { PNGTranscoder transcoder = new PNGTranscoder(); transcoder.addTranscodingHint(PNGTranscoder.KEY_INDEXED, 256); transcoder.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.WHITE); transcoder.transcode(inputSvg, outputPng); } catch (Throwable t) { log.error(t, t); } baos.flush(); baos.close(); return ImageIO.read(new ByteArrayInputStream(baos.toByteArray())); } } catch (InvalidXPathException e) { log.error(e); String msg = "Problem creating image"; if (null != svgdoc) { try { File errsvg = new File(FileUtils.getTempDirectory(), "graphvisualization.svg"); FileUtils.write(errsvg, svgdoc.asXML(), Charset.defaultCharset()); msg = "Could not create the image. SVG data store here: " + errsvg.getAbsolutePath(); } catch (IOException ex) { // don't care } } throw new IOException(msg, e); } finally { if (null != svgfile) { FileUtils.deleteQuietly(svgfile); } } }
From source file:com.poka.util.XmlSax.java
public Document load(String f, boolean isHiberConfig) { Document document = null; try {// w w w . ja v a 2 s .c o m SAXReader saxReader = new SAXReader(); //saxReader.setValidation(false); //saxReader.setFeature("http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd", false); //document = saxReader.read(f); //?XML,document if (isHiberConfig) { saxReader.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { // TODO Auto-generated method stub InputSource is = new InputSource(this.getClass().getClassLoader() .getResourceAsStream("com/poka/images/hibernate-configuration-3.0.dtd")); is.setPublicId(publicId); is.setSystemId(systemId); return is; } }); saxReader.setValidation(true); } File tFile = new File(f); if (!tFile.exists() && !isHiberConfig) { document = DocumentHelper.createDocument(); // document.addDocType("hibernate-configuration", "-//Hibernate/Hibernate Configuration DTD 3.0//EN", "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"); // Element root = DocumentHelper.createElement("root"); document.setRootElement(root); } else { document = saxReader.read(tFile); } } catch (DocumentException ex) { } return document; }
From source file:com.xpn.xwiki.objects.BaseCollection.java
License:Open Source License
public String toXMLString() { Document doc = new DOMDocument(); doc.setRootElement(toXML(null)); OutputFormat outputFormat = new OutputFormat("", true); StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, outputFormat); try {/*from w ww . j ava 2s.co m*/ writer.write(doc); return out.toString(); } catch (IOException e) { e.printStackTrace(); return ""; } }
From source file:com.xpn.xwiki.objects.BaseProperty.java
License:Open Source License
public String toXMLString() { Document doc = new DOMDocument(); doc.setRootElement(toXML()); OutputFormat outputFormat = new OutputFormat("", true); StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, outputFormat); try {/*w w w.ja v a 2 s .co m*/ writer.write(doc); return out.toString(); } catch (IOException e) { e.printStackTrace(); return ""; } }
From source file:com.xpn.xwiki.tool.xar.XarMojo.java
License:Open Source License
/** * Generate a DOM4J Document containing the generated XML. * /*from w w w .j a v a 2 s . c o m*/ * @param files the list of files that we want to include in the generated package XML file. * @return the DOM4J Document containing the generated XML */ private Document toXML(Collection<ArchiveEntry> files) { Document doc = new DOMDocument(); Element packageElement = new DOMElement("package"); doc.setRootElement(packageElement); Element infoElement = new DOMElement("infos"); packageElement.add(infoElement); addInfoElements(infoElement); Element filesElement = new DOMElement(FILES_TAG); packageElement.add(filesElement); addFileElements(files, filesElement); return doc; }