List of usage examples for org.dom4j.io XMLWriter XMLWriter
public XMLWriter(OutputFormat format) throws UnsupportedEncodingException
From source file:org.alfresco.web.bean.dashboard.PageConfig.java
License:Open Source License
/** * Convert this config to an XML definition which can be serialized. * Example://from w ww .j a v a 2s.c o m * <code> * <?xml version="1.0"?> * <dashboard> * <page id="main" layout-id="narrow-left-2column"> * <column> * <dashlet idref="clock" /> * <dashlet idref="random-joke" /> * </column> * <column> * <dashlet idref="getting-started" /> * <dashlet idref="task-list" /> * <dashlet idref="my-checkedout-docs" /> * <dashlet idref="my-documents" /> * </column> * </page> * </dashboard> * </code> * * @return XML for this config */ public String toXML() { try { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement(ELEMENT_DASHBOARD); for (Page page : pages) { Element pageElement = root.addElement(ELEMENT_PAGE); pageElement.addAttribute(ATTR_ID, page.getId()); pageElement.addAttribute(ATTR_LAYOUTID, page.getLayoutDefinition().Id); for (Column column : page.getColumns()) { Element columnElement = pageElement.addElement(ELEMENT_COLUMN); for (DashletDefinition dashletDef : column.getDashlets()) { columnElement.addElement(ELEMENT_DASHLET).addAttribute(ATTR_REFID, dashletDef.Id); } } } StringWriter out = new StringWriter(512); XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); writer.setWriter(out); writer.write(doc); return out.toString(); } catch (Throwable err) { throw new AlfrescoRuntimeException( "Unable to serialize Dashboard PageConfig to XML: " + err.getMessage(), err); } }
From source file:org.alfresco.web.bean.search.SearchContext.java
License:Open Source License
/** * @return this SearchContext as XML/*from w ww. jav a2s . co m*/ * * Example: * <code> * <?xml version="1.0" encoding="UTF-8"?> * <search> * <text>CDATA</text> * <mode>int</mode> * <location>XPath</location> * <categories> * <category>XPath</category> * </categories> * <content-type>String</content-type> * <folder-type>String</folder-type> * <mimetype>String</mimetype> * <attributes> * <attribute name="String">String</attribute> * </attributes> * <ranges> * <range name="String"> * <lower>String</lower> * <upper>String</upper> * <inclusive>boolean</inclusive> * </range> * </ranges> * <fixed-values> * <value name="String">String</value> * </fixed-values> * <query>CDATA</query> * </search> * </code> */ public String toXML() { try { NamespaceService ns = Repository.getServiceRegistry(FacesContext.getCurrentInstance()) .getNamespaceService(); Document doc = DocumentHelper.createDocument(); Element root = doc.addElement(ELEMENT_SEARCH); root.addElement(ELEMENT_TEXT).addCDATA(this.text); root.addElement(ELEMENT_MODE).addText(Integer.toString(this.mode)); if (this.location != null) { root.addElement(ELEMENT_LOCATION).addText(this.location); } Element categories = root.addElement(ELEMENT_CATEGORIES); for (String path : this.categories) { categories.addElement(ELEMENT_CATEGORY).addText(path); } if (this.contentType != null) { root.addElement(ELEMENT_CONTENT_TYPE).addText(this.contentType); } if (this.folderType != null) { root.addElement(ELEMENT_FOLDER_TYPE).addText(this.folderType); } if (this.mimeType != null && this.mimeType.length() != 0) { root.addElement(ELEMENT_MIMETYPE).addText(this.mimeType); } Element attributes = root.addElement(ELEMENT_ATTRIBUTES); for (QName attrName : this.queryAttributes.keySet()) { attributes.addElement(ELEMENT_ATTRIBUTE).addAttribute(ELEMENT_NAME, attrName.toPrefixString(ns)) .addCDATA(this.queryAttributes.get(attrName)); } Element ranges = root.addElement(ELEMENT_RANGES); for (QName rangeName : this.rangeAttributes.keySet()) { RangeProperties rangeProps = this.rangeAttributes.get(rangeName); Element range = ranges.addElement(ELEMENT_RANGE); range.addAttribute(ELEMENT_NAME, rangeName.toPrefixString(ns)); range.addElement(ELEMENT_LOWER).addText(rangeProps.lower); range.addElement(ELEMENT_UPPER).addText(rangeProps.upper); range.addElement(ELEMENT_INCLUSIVE).addText(Boolean.toString(rangeProps.inclusive)); } Element values = root.addElement(ELEMENT_FIXED_VALUES); for (QName valueName : this.queryFixedValues.keySet()) { values.addElement(ELEMENT_VALUE).addAttribute(ELEMENT_NAME, valueName.toPrefixString(ns)) .addCDATA(this.queryFixedValues.get(valueName)); } // outputing the full lucene query may be useful for some situations Element query = root.addElement(ELEMENT_QUERY); String queryString = buildQuery(0); if (queryString != null) { query.addCDATA(queryString); } StringWriter out = new StringWriter(1024); XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); writer.setWriter(out); writer.write(doc); return out.toString(); } catch (Throwable err) { throw new AlfrescoRuntimeException("Failed to export SearchContext to XML.", err); } }
From source file:org.apache.commons.jelly.XMLOutput.java
License:Apache License
/** * Creates a text based XMLOutput which converts all XML events into * text and writes to the underlying Writer. * * @param writer is the writer to output to * @param escapeText is whether or not text output will be escaped. This must be true * if the underlying output is XML or could be false if the underlying output is textual. *//* w w w . ja v a 2 s. c om*/ public static XMLOutput createXMLOutput(Writer writer, boolean escapeText) { XMLWriter xmlWriter = new XMLWriter(writer); xmlWriter.setEscapeText(escapeText); return createXMLOutput(xmlWriter); }
From source file:org.apache.commons.jelly.XMLOutput.java
License:Apache License
/** * Creates a text based XMLOutput which converts all XML events into * text and writes to the underlying OutputStream. * * @param out is the output stream to write * @param escapeText is whether or not text output will be escaped. This must be true * if the underlying output is XML or could be false if the underlying output is textual. *///from w w w .j a v a 2 s . co m public static XMLOutput createXMLOutput(OutputStream out, boolean escapeText) throws UnsupportedEncodingException { XMLWriter xmlWriter = new XMLWriter(out); xmlWriter.setEscapeText(escapeText); return createXMLOutput(xmlWriter); }
From source file:org.apache.isis.core.runtime.services.memento.Dom4jUtil.java
License:Apache License
static String asString(final Document doc) { XMLWriter writer = null;// w w w . ja va2 s .c o m final StringWriter sw = new StringWriter(); try { // previously this code used pretty print. // however, that tripped up on strings with double spaces in them; the double space was normalized // to a single space! // OutputFormat outputFormat = OutputFormat.createPrettyPrint(); // writer = new XMLWriter(sw, outputFormat); writer = new XMLWriter(sw); writer.write(doc); return sw.toString(); } catch (IOException e) { throw new IsisException(e); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { // ignore } } } }
From source file:org.apache.maven.archetype.common.DefaultPomManager.java
License:Apache License
public void addModule(File pom, String artifactId) throws IOException, XmlPullParserException, DocumentException, InvalidPackaging { boolean found = false; StringWriter writer = new StringWriter(); Reader fileReader = null;/* w w w . ja va 2 s . c o m*/ try { fileReader = ReaderFactory.newXmlReader(pom); SAXReader reader = new SAXReader(); Document document = reader.read(fileReader); Element project = document.getRootElement(); String packaging = null; Element packagingElement = project.element("packaging"); if (packagingElement != null) { packaging = packagingElement.getStringValue(); } if (!"pom".equals(packaging)) { throw new InvalidPackaging( "Unable to add module to the current project as it is not of packaging type 'pom'"); } Element modules = project.element("modules"); if (modules == null) { modules = project.addText(" ").addElement("modules"); modules.setText("\n "); project.addText("\n"); } // TODO: change to while loop for (@SuppressWarnings("unchecked") Iterator<Element> i = modules.elementIterator("module"); i.hasNext() && !found;) { Element module = i.next(); if (module.getText().equals(artifactId)) { found = true; } } if (!found) { Node lastTextNode = null; for (@SuppressWarnings("unchecked") Iterator<Node> i = modules.nodeIterator(); i.hasNext();) { Node node = i.next(); if (node.getNodeType() == Node.ELEMENT_NODE) { lastTextNode = null; } else if (node.getNodeType() == Node.TEXT_NODE) { lastTextNode = node; } } if (lastTextNode != null) { modules.remove(lastTextNode); } modules.addText("\n "); modules.addElement("module").setText(artifactId); modules.addText("\n "); XMLWriter xmlWriter = new XMLWriter(writer); xmlWriter.write(document); FileUtils.fileWrite(pom.getAbsolutePath(), writer.toString()); } // end if } finally { IOUtil.close(fileReader); } }
From source file:org.apache.maven.archetype.old.DefaultOldArchetype.java
License:Apache License
static boolean addModuleToParentPom(String artifactId, Reader fileReader, Writer fileWriter) throws DocumentException, IOException, ArchetypeTemplateProcessingException { SAXReader reader = new SAXReader(); Document document = reader.read(fileReader); Element project = document.getRootElement(); String packaging = null;//from ww w.ja v a 2 s. co m Element packagingElement = project.element("packaging"); if (packagingElement != null) { packaging = packagingElement.getStringValue(); } if (!"pom".equals(packaging)) { throw new ArchetypeTemplateProcessingException( "Unable to add module to the current project as it is not of packaging type 'pom'"); } Element modules = project.element("modules"); if (modules == null) { modules = project.addText(" ").addElement("modules"); modules.setText("\n "); project.addText("\n"); } boolean found = false; for (Iterator<?> i = modules.elementIterator("module"); i.hasNext() && !found;) { Element module = (Element) i.next(); if (module.getText().equals(artifactId)) { found = true; } } if (!found) { Node lastTextNode = null; for (Iterator<?> i = modules.nodeIterator(); i.hasNext();) { Node node = (Node) i.next(); if (node.getNodeType() == Node.ELEMENT_NODE) { lastTextNode = null; } else if (node.getNodeType() == Node.TEXT_NODE) { lastTextNode = node; } } if (lastTextNode != null) { modules.remove(lastTextNode); } modules.addText("\n "); modules.addElement("module").setText(artifactId); modules.addText("\n "); XMLWriter writer = new XMLWriter(fileWriter); writer.write(document); } return !found; }
From source file:org.apache.openmeetings.core.documents.CreateLibraryPresentation.java
License:Apache License
public static ConverterProcessResult generateXMLDocument(File targetDirectory, String originalDocument, String pdfDocument, String swfDocument) { ConverterProcessResult returnMap = new ConverterProcessResult(); returnMap.setProcess("generateXMLDocument"); try {/*ww w .j a va 2s .c om*/ Document document = DocumentHelper.createDocument(); Element root = document.addElement("presentation"); File file = new File(targetDirectory, originalDocument); root.addElement("originalDocument").addAttribute("lastmod", (new Long(file.lastModified())).toString()) .addAttribute("size", (new Long(file.length())).toString()).addText(originalDocument); if (pdfDocument != null) { File pdfDocumentFile = new File(targetDirectory, pdfDocument); root.addElement("pdfDocument") .addAttribute("lastmod", (new Long(pdfDocumentFile.lastModified())).toString()) .addAttribute("size", (new Long(pdfDocumentFile.length())).toString()).addText(pdfDocument); } if (swfDocument != null) { File swfDocumentFile = new File(targetDirectory, originalDocument); root.addElement("swfDocument") .addAttribute("lastmod", (new Long(swfDocumentFile.lastModified())).toString()) .addAttribute("size", (new Long(swfDocumentFile.length())).toString()).addText(swfDocument); } Element thumbs = root.addElement("thumbs"); //Second get all Files of this Folder FilenameFilter ff = new FilenameFilter() { @Override public boolean accept(File b, String name) { File f = new File(b, name); return f.isFile(); } }; String[] allfiles = targetDirectory.list(ff); if (allfiles != null) { Arrays.sort(allfiles); for (int i = 0; i < allfiles.length; i++) { File thumbfile = new File(targetDirectory, allfiles[i]); if (allfiles[i].startsWith(thumbImagePrefix)) { thumbs.addElement("thumb") .addAttribute("lastmod", (new Long(thumbfile.lastModified())).toString()) .addAttribute("size", (new Long(thumbfile.length())).toString()) .addText(allfiles[i]); } } } // lets write to a file XMLWriter writer = new XMLWriter( new FileOutputStream(new File(targetDirectory, OmFileHelper.libraryFileName))); writer.write(document); writer.close(); returnMap.setExitValue("0"); return returnMap; } catch (Exception err) { log.error("Error while generateXMLDocument", err); returnMap.setError(err.getMessage()); returnMap.setExitValue("-1"); return returnMap; } }
From source file:org.apache.openmeetings.documents.CreateLibraryPresentation.java
License:Apache License
public static ConverterProcessResult generateXMLDocument(File targetDirectory, String originalDocument, String pdfDocument, String swfDocument) { ConverterProcessResult returnMap = new ConverterProcessResult(); returnMap.setProcess("generateXMLDocument"); try {/* w ww.ja v a 2 s . c o m*/ Document document = DocumentHelper.createDocument(); Element root = document.addElement("presentation"); File file = new File(targetDirectory, originalDocument); root.addElement("originalDocument").addAttribute("lastmod", (new Long(file.lastModified())).toString()) .addAttribute("size", (new Long(file.length())).toString()).addText(originalDocument); if (pdfDocument != null) { File pdfDocumentFile = new File(targetDirectory, pdfDocument); root.addElement("pdfDocument") .addAttribute("lastmod", (new Long(pdfDocumentFile.lastModified())).toString()) .addAttribute("size", (new Long(pdfDocumentFile.length())).toString()).addText(pdfDocument); } if (swfDocument != null) { File swfDocumentFile = new File(targetDirectory, originalDocument); root.addElement("swfDocument") .addAttribute("lastmod", (new Long(swfDocumentFile.lastModified())).toString()) .addAttribute("size", (new Long(swfDocumentFile.length())).toString()).addText(swfDocument); } Element thumbs = root.addElement("thumbs"); //Secoond get all Files of this Folder FilenameFilter ff = new FilenameFilter() { public boolean accept(File b, String name) { File f = new File(b, name); return f.isFile(); } }; String[] allfiles = targetDirectory.list(ff); if (allfiles != null) { Arrays.sort(allfiles); for (int i = 0; i < allfiles.length; i++) { File thumbfile = new File(targetDirectory, allfiles[i]); if (allfiles[i].startsWith("_thumb_")) { thumbs.addElement("thumb") .addAttribute("lastmod", (new Long(thumbfile.lastModified())).toString()) .addAttribute("size", (new Long(thumbfile.length())).toString()) .addText(allfiles[i]); } } } // lets write to a file XMLWriter writer = new XMLWriter( new FileOutputStream(new File(targetDirectory, OmFileHelper.libraryFileName))); writer.write(document); writer.close(); returnMap.setExitValue("0"); return returnMap; } catch (Exception err) { err.printStackTrace(); returnMap.setError(err.getMessage()); returnMap.setExitValue("-1"); return returnMap; } }
From source file:org.apache.openmeetings.documents.InstallationDocumentHandler.java
License:Apache License
public static void createDocument(Integer stepNo) throws Exception { Document document = DocumentHelper.createDocument(); Element root = document.addElement("install"); Element step = root.addElement("step"); step.addElement("stepnumber").addText(stepNo.toString()); step.addElement("stepname").addText("Step " + stepNo); XMLWriter writer = new XMLWriter(new FileWriter(OmFileHelper.getInstallFile())); writer.write(document);//from ww w .j a va 2s.c om writer.close(); }