List of usage examples for org.dom4j.io OutputFormat setSuppressDeclaration
public void setSuppressDeclaration(boolean suppressDeclaration)
This will set whether the XML declaration (<?xml version="1.0" encoding="UTF-8"?>
) is included or not.
From source file:org.craftercms.cstudio.alfresco.transform.TaxonomyRendition.java
License:Open Source License
/** * Write renditionResult along with the flattened XML file to WCM * /*from w w w .j a v a 2s .c o m*/ * @param articleRef */ public InputStream generateOutputFile(NodeRef articleRef) throws ServiceException { InputStream retStream = null; try { Document document = retreiveXml(articleRef); String encoding = document.getXMLEncoding(); if (encoding == null) { encoding = "UTF-16"; } Writer stringOutputWriter = new StringWriter(); OutputFormat opf = OutputFormat.createCompactFormat(); opf.setSuppressDeclaration(true); XMLWriter writer = new XMLWriter(stringOutputWriter, opf); writer.write(document); writer.close(); retStream = new ByteArrayInputStream(stringOutputWriter.toString().getBytes(encoding)); } catch (ServiceException e) { if (LOGGER.isErrorEnabled()) { LOGGER.error("Cannot create output XML Document: " + e.getMessage(), e); } throw new ServiceException("Cannot create output XML Document: " + e.getMessage(), e); } catch (IOException e) { if (LOGGER.isErrorEnabled()) { LOGGER.error("Cannot create output XML Document: " + e.getMessage(), e); } throw new ServiceException("Cannot create output XML Document: " + e.getMessage(), e); } return retStream; }
From source file:org.dentaku.gentaku.tools.cgen.plugin.GenGenPlugin.java
License:Apache License
private void prettyPrint(Document document, Writer writer) throws IOException { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(encoding);/*from w ww . j a va 2 s . c o m*/ format.setSuppressDeclaration(false); format.setExpandEmptyElements(false); XMLWriter xmlWriter = new XMLWriter(writer, format); xmlWriter.write(document); xmlWriter.flush(); }
From source file:org.dentaku.gentaku.tools.cgen.visitor.JellyTemplateGeneratingVisitor.java
License:Apache License
public void handleElement(Element element) throws VisitorException { if (element.getParent().getName().equals("schema")) { QName rootName = DocumentFactory.getInstance().createQName("jelly", "j", "jelly:core"); Branch parent = DocumentHelper.createDocument().addElement(rootName).addNamespace("x", "jelly:xml"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(getEncoding()); format.setSuppressDeclaration(false); format.setExpandEmptyElements(false); pushParent(parent.addElement(element.getName())); for (Iterator it = element.elementIterator(); it.hasNext();) { visit((Element) it.next()); }//w w w .j a va 2 s . c om popParent(); try { String filename = element.getName() + ".jelly"; Writer out = new FileWriter(new File(getRootDir(), filename)); final XMLWriter xmlWriter = new XMLWriter(out, format); xmlWriter.setEscapeText(false); xmlWriter.write(parent); xmlWriter.flush(); xmlWriter.close(); } catch (Exception e) { throw new VisitorException("Exception occurred when running Jelly", e); } topLevelElements.put(element.getName(), element); } else { String refName = element.attributeValue("ref"); if (refName != null) { Branch parent = getCurrentParent(); // create an include QName qName = DocumentFactory.getInstance().createQName("import", "j", "jelly:core"); parent.addElement(qName).addAttribute("uri", refName + ".jelly").addAttribute("inherit", "true"); } } }
From source file:org.dentaku.gentaku.tools.cgen.xmi.XMIGenTask.java
License:Apache License
private void writeFile(Branch document, File file) throws IOException { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(System.getProperty("file.encoding")); format.setSuppressDeclaration(false); format.setExpandEmptyElements(false); Writer out = new FileWriter(file); final XMLWriter xmlWriter = new XMLWriter(out, format); xmlWriter.setEscapeText(false);//from ww w.j a v a 2s .c o m xmlWriter.write(document); xmlWriter.flush(); xmlWriter.close(); }
From source file:org.eclipse.ecr.core.io.impl.plugins.XMLDocumentTreeWriter.java
License:Open Source License
protected XMLWriter initWriter() { if (writer == null) { try {/* w ww. j a v a2s . c o m*/ OutputFormat format = AbstractDocumentWriter.createCompactFormat(); format.setSuppressDeclaration(true); writer = new XMLWriter(out, format); } catch (UnsupportedEncodingException e) { // XXX } } return writer; }
From source file:org.gbif.portal.util.mhf.message.impl.xml.XMLMessage.java
License:Open Source License
/** * @see org.gbif.portal.util.mhf.Message#getRawData() *//*from w w w .j ava2s . c o m*/ public String getRawData() throws MessageAccessException { if (rawXml != null) return rawXml.toString(); else { // build a short string StringWriter writer = new StringWriter(); OutputFormat outformat = OutputFormat.createCompactFormat(); outformat.setSuppressDeclaration(true); XMLWriter xmlWriter = new XMLWriter(writer, outformat); try { xmlWriter.write(getDocument()); xmlWriter.flush(); String rawXml = writer.toString(); setRawData(rawXml); return rawXml; } catch (IOException e) { throw new MessageAccessException("Error writing the XML Document", e); } finally { try { xmlWriter.close(); } catch (IOException e) { } try { writer.close(); } catch (IOException e) { } } } }
From source file:org.gbif.portal.util.mhf.message.impl.xml.XMLMessage.java
License:Open Source License
/** * @see org.gbif.portal.util.mhf.message.impl.xml.BeautifiedData#getBeautifiedData() *//*from w w w .ja va 2s. c o m*/ public String getLoggableData() throws MessageAccessException { // build a beautiful string StringWriter writer = new StringWriter(); OutputFormat outformat = OutputFormat.createPrettyPrint(); outformat.setSuppressDeclaration(true); XMLWriter xmlWriter = new XMLWriter(writer, outformat); try { xmlWriter.write(getDocument()); xmlWriter.flush(); String rawXml = writer.toString(); setRawData(rawXml); return rawXml.trim(); } catch (IOException e) { throw new MessageAccessException("Error writing the XML Document", e); } finally { try { xmlWriter.close(); } catch (IOException e) { } try { writer.close(); } catch (IOException e) { } } }
From source file:org.infoglue.cms.applications.common.actions.SimpleXmlServiceAction.java
License:Open Source License
protected String getFormattedDocument(Document doc, boolean compact, boolean supressDecl) { OutputFormat format = compact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint(); format.setSuppressDeclaration(supressDecl); format.setEncoding(ENCODING);/*from w w w. ja v a2 s . c o m*/ format.setExpandEmptyElements(false); StringWriter stringWriter = new StringWriter(); XMLWriter writer = new XMLWriter(stringWriter, format); try { writer.write(doc); } catch (IOException e) { e.printStackTrace(); } return stringWriter.toString(); }
From source file:org.infoglue.cms.util.dom.DOMBuilder.java
License:Open Source License
public String getFormattedDocument(Document doc, boolean compact, boolean supressDecl, String encoding) { OutputFormat format = compact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint(); format.setSuppressDeclaration(supressDecl); format.setEncoding(encoding);// w w w . j a va 2s. c o m format.setExpandEmptyElements(false); StringWriter stringWriter = new StringWriter(); XMLWriter writer = new XMLWriter(stringWriter, format); try { writer.write(doc); } catch (IOException e) { e.printStackTrace(); } return stringWriter.toString(); }
From source file:org.openadaptor.util.XmlUtils.java
License:Open Source License
/** * Uses a Dom4j SAXReader to apply formatting to the supplied XML fragment. In this case we use a prettyprinter to * indent the tags./*w ww. j a v a2s . c o m*/ * * @param xml * The XML to be formatted (can be just a fragment) * @param isFragment * If you supplied a XML fragment then this must be set to true so that the writer doesn't output an XML * declaration * * @return the formatted XML * * @throws IOException * if there was a problem applying the formatting * @throws DocumentException * if there was a problem with the XML */ public static String format(String xml, boolean isFragment) throws IOException, DocumentException { OutputFormat format = OutputFormat.createPrettyPrint(); format.setSuppressDeclaration(isFragment); format.setIndent("\t"); SAXReader reader = new SAXReader(); Reader r = new StringReader(xml); org.dom4j.Document document = reader.read(r); Writer w = new StringWriter(); XMLWriter out = new XMLWriter(w, format); out.write(document); out.close(); return w.toString(); }