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:com.rockagen.commons.util.XmlUtil.java
License:Apache License
/** * Format xml {@link OutputFormat#createCompactFormat()} * * @param xmlStr xml String * @param enc encoding * @param isSuppressDeclaration set supperss declaration * @return Compact xml String/*from ww w.j av a2 s.com*/ */ public static String formatCompact(String xmlStr, String enc, boolean isSuppressDeclaration) { if (CommUtil.isBlank(xmlStr)) return xmlStr; if (enc == null) enc = ENCODING; OutputFormat formater = OutputFormat.createCompactFormat(); formater.setEncoding(enc); formater.setSuppressDeclaration(isSuppressDeclaration); return format(xmlStr, formater); }
From source file:de.codecentric.multitool.xml.XmlLibrary.java
License:Apache License
/** * Konvertiert ein XML-Document in einen XML-String * //from w ww. j a v a 2s. c om * | ${xmlString} = | Get Xml From String | ${xmlDoc} | */ public String getStringFromXml(Document document) throws IOException { OutputFormat format = OutputFormat.createCompactFormat(); format.setSuppressDeclaration(true); StringWriter writer = new StringWriter(); XMLWriter xmlWriter = new XMLWriter(writer, format); xmlWriter.write(document); return writer.toString(); }
From source file:de.innovationgate.wgpublisher.WGACore.java
License:Open Source License
private void initDefaultSerializer() { Dom4JDriver driver = new Dom4JDriver(); OutputFormat format = OutputFormat.createCompactFormat(); format.setSuppressDeclaration(true); driver.setOutputFormat(format);/* w w w . ja va 2 s .c o m*/ _defaultSerializer = new XStream(driver); _defaultSerializer.setClassLoader(getLibraryLoader()); _defaultSerializer.alias("tmloption", TMLOption.class); _defaultSerializer.alias("version", Version.class); _defaultSerializer.alias("portletState", TMLPortletState.class); _defaultSerializer.registerConverter(ExpressionEngineFactory.getTMLScriptEngine()); // DOM4J Objects _defaultSerializer.registerConverter(new SingleValueConverter() { public boolean canConvert(Class arg0) { return org.dom4j.Element.class.isAssignableFrom(arg0); } public Object fromString(String arg0) { try { org.dom4j.Document doc = DocumentHelper.parseText(arg0); return doc.getRootElement(); } catch (DocumentException e) { Logger.getLogger("wga.ajax") .error("Exception deserializing DOM4J Element from TMLPortlet.SessionContext", e); return ""; } } public String toString(Object arg0) { Element elem = (Element) arg0; return elem.asXML(); } }); // Serialize TMLContext to their context path. All other info is dropped. Deserialize to a TMLContext.Serialized. _defaultSerializer.registerConverter(new SingleValueConverter() { @SuppressWarnings("rawtypes") @Override public boolean canConvert(Class arg0) { return TMLContext.class.isAssignableFrom(arg0); } @Override public String toString(Object arg0) { try { TMLContext cx = (TMLContext) arg0; return cx.getpath(); } catch (WGAPIException e) { throw new RuntimeException("Exception serializing TMLContext", e); } } @Override public Object fromString(String arg0) { return new TMLContext.Serialized(arg0); } }); // Version object _defaultSerializer.registerConverter(new SingleValueConverter() { public boolean canConvert(Class arg0) { return Version.class.isAssignableFrom(arg0); } public Object fromString(String arg0) { return new Version(arg0); } public String toString(Object arg0) { return ((Version) arg0).toString(); } }); // GSON data structures _defaultSerializer.registerConverter(new SingleValueConverter() { private Gson _gson = new GsonBuilder().create(); @Override public boolean canConvert(Class arg0) { return JsonElement.class.isAssignableFrom(arg0); } @Override public Object fromString(String arg0) { return new JsonParser().parse(arg0); } @Override public String toString(Object arg0) { return _gson.toJson((JsonElement) arg0); } }); // Serialize types not meant to be serialized to empty string, deserialize to null _defaultSerializer.registerConverter(new SingleValueConverter() { public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) { return DEFAULT_SERIALIZER_NONSERIALIZABLE_TYPES.contains(clazz); } public Object fromString(String arg0) { return null; } public String toString(Object arg0) { return ""; } }); }
From source file:eml.studio.server.oozie.workflow.WFGraph.java
License:Open Source License
/** * Transform the Graph into an workflow xml definition * @param jobname the job name of Oozie job, can't be null * @return workflow xml//w w w . jav a2 s . co m */ public String toWorkflow(String jobname) { Namespace xmlns = new Namespace("", "uri:oozie:workflow:0.4"); // root namespace uri QName qName = QName.get("workflow-app", xmlns); // your root element's name Element workflow = DocumentHelper.createElement(qName); Document xmldoc = DocumentHelper.createDocument(workflow); // Create workflow root workflow.addAttribute("xmlns", "uri:oozie:workflow:0.4"); // <workflow-app name='xxx'></workflow-app> if (jobname == null || "".equals(jobname)) workflow.addAttribute("name", "Not specified"); else workflow.addAttribute("name", jobname); Queue<NodeDef> que = new LinkedList<NodeDef>(); que.add(start); while (!que.isEmpty()) { NodeDef cur = que.remove(); cur.append2XML(workflow); for (NodeDef toNode : cur.getOutNodes()) { toNode.delInNode(cur); if (toNode.getInDegree() == 0) que.add(toNode); } } // Set XML document format OutputFormat outputFormat = OutputFormat.createPrettyPrint(); // Set XML encoding, use the specified encoding to save the XML document to the string, it can be specified GBK or ISO8859-1 outputFormat.setEncoding("UTF-8"); outputFormat.setSuppressDeclaration(true); // Whether generate xml header outputFormat.setIndent(true); // Whether set indentation outputFormat.setIndent(" "); // Implement indentation with four spaces outputFormat.setNewlines(true); // Set whether to wrap try { // stringWriter is used to save xml document StringWriter stringWriter = new StringWriter(); // xmlWriter is used to write XML document to string(tool) XMLWriter xmlWriter = new XMLWriter(stringWriter, outputFormat); // Write the created XML document into the string xmlWriter.write(xmldoc); xmlWriter.close(); System.out.println(stringWriter.toString().trim()); // Print the string, that is, the XML document return stringWriter.toString().trim(); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:gr.abiss.calipso.util.XmlUtils.java
License:Open Source License
/** * Override that accepts an XML DOM Document * @param document XML as DOM Document/* w w w . j av a 2 s . co m*/ */ public static String getAsPrettyXml(Document document) { OutputFormat format = new OutputFormat(" ", true); format.setSuppressDeclaration(true); StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, format); try { try { writer.write(document); } finally { writer.close(); } } catch (IOException ioe) { throw new RuntimeException(ioe); } return out.toString().trim(); }
From source file:it.unibz.inf.xmlssd.metadator.helpers.UIHelper.java
License:Apache License
/** * Method which return a pretty formated string. If it fails the untouched * string is given back./* w w w. j a va 2s . com*/ * @param str * @return */ public static String prettyPrintString(String str) { try { Document document = DocumentHelper.parseText(str); OutputFormat format = OutputFormat.createPrettyPrint(); // remove <?xml version="1.0" encoding="UTF-8"?> format.setSuppressDeclaration(true); StringWriter strWriter = new StringWriter(); XMLWriter writer = new XMLWriter(strWriter, format); writer.write(document); return strWriter.toString(); } catch (IOException e) { } catch (DocumentException e) { } return str; }
From source file:org.apache.commons.jelly.tags.core.FileTag.java
License:Apache License
/** * A Factory method to create a new XMLOutput from the given Writer. *//*www . java 2 s. co m*/ protected XMLOutput createXMLOutput(Writer writer) { OutputFormat format = null; if (prettyPrint) { format = OutputFormat.createPrettyPrint(); } else { format = new OutputFormat(); } if (encoding != null) { format.setEncoding(encoding); } if (omitXmlDeclaration) { format.setSuppressDeclaration(true); } boolean isHtml = outputMode != null && outputMode.equalsIgnoreCase("html"); final XMLWriter xmlWriter = (isHtml) ? new HTMLWriter(writer, format) : new XMLWriter(writer, format); xmlWriter.setEscapeText(isEscapeText()); XMLOutput answer = new XMLOutput() { public void close() throws IOException { xmlWriter.close(); } }; answer.setContentHandler(xmlWriter); answer.setLexicalHandler(xmlWriter); return answer; }
From source file:org.apache.cxf.jaxrs.provider.dom4j.DOM4JProvider.java
License:Apache License
public void writeTo(org.dom4j.Document doc, Class<?> cls, Type type, Annotation[] anns, MediaType mt, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException, WebApplicationException { if (!convertAlways && mt.getSubtype().contains("xml")) { XMLWriter writer;/* w ww .ja v a2 s .c o m*/ if (MessageUtils.getContextualBoolean(getCurrentMessage(), SUPPRESS_XML_DECLARATION, false)) { OutputFormat format = new org.dom4j.io.OutputFormat(); format.setSuppressDeclaration(true); writer = new org.dom4j.io.XMLWriter(os, format); } else { writer = new org.dom4j.io.XMLWriter(os); } writer.write(doc); writer.flush(); } else { org.w3c.dom.Document domDoc = convertToDOM(doc); MessageBodyWriter<org.w3c.dom.Document> writer = providers.getMessageBodyWriter(DOM_DOC_CLS, DOM_DOC_CLS, anns, mt); if (writer == null) { throw ExceptionUtils.toNotAcceptableException(null, null); } writer.writeTo(domDoc, DOM_DOC_CLS, DOM_DOC_CLS, anns, mt, headers, os); } }
From source file:org.bedework.appcommon.NotifyResource.java
License:Apache License
/** * @return formatted xml without the header *//*from w w w.jav a 2s. c o m*/ public String getXmlFragment() { try { OutputFormat format = OutputFormat.createPrettyPrint(); format.setTrimText(false); format.setSuppressDeclaration(true); StringWriter sw = new StringWriter(); XMLWriter writer = new XMLWriter(sw, format); writer.write(DocumentHelper.parseText(content)); return sw.toString(); } catch (Throwable t) { return "<error>" + t.getLocalizedMessage() + "</error>"; } }
From source file:org.craftercms.core.util.xml.marshalling.xstream.CrafterXStreamMarshaller.java
License:Open Source License
/** * Just as super(), but instead of a {@link com.thoughtworks.xstream.io.xml.CompactWriter} creates a {@link * EscapingCompactWriter}./*ww w. j a v a2s. com*/ * Also if the object graph is a Dom4j document, the document is written directly instead of using XStream. */ @Override public void marshalWriter(Object graph, Writer writer, DataHolder dataHolder) throws XmlMappingException, IOException { if (graph instanceof Document) { OutputFormat outputFormat = OutputFormat.createCompactFormat(); outputFormat.setSuppressDeclaration(suppressXmlDeclaration); XMLWriter xmlWriter = new XMLWriter(writer, outputFormat); try { xmlWriter.write((Document) graph); } finally { try { xmlWriter.flush(); } catch (Exception ex) { logger.debug("Could not flush XMLWriter", ex); } } } else { if (!suppressXmlDeclaration) { writer.write(XML_DECLARATION); } HierarchicalStreamWriter streamWriter = new EscapingCompactWriter(writer); try { getXStream().marshal(graph, streamWriter, dataHolder); } catch (Exception ex) { throw convertXStreamException(ex, true); } finally { try { streamWriter.flush(); } catch (Exception ex) { logger.debug("Could not flush HierarchicalStreamWriter", ex); } } } }