use of org.dom4j.io.OutputFormat in project tmdm-studio-se by Talend.
the class XmlUtil method write.
public static void write(Document document, String filePath, String printMode, String encoding) throws IOException {
OutputFormat format = null;
if (printMode.toLowerCase().equals("pretty")) {
// $NON-NLS-1$
// Pretty print the document
format = OutputFormat.createPrettyPrint();
} else if (printMode.toLowerCase().equals("compact")) {
// $NON-NLS-1$
// Compact format
format = OutputFormat.createCompactFormat();
}
format.setEncoding(encoding);
// lets write to a file
XMLWriter writer = new XMLWriter(new FileOutputStream(filePath), format);
// XMLWriter logger = new XMLWriter( System.out, format );
writer.write(document);
logger.info(Messages.bind(Messages.XmlUtil_Loginfo1, filePath));
// logger.write( document );
// logger.close();
writer.close();
}
use of org.dom4j.io.OutputFormat in project xwiki-platform by xwiki.
the class AbstractXMLSerializer method serialize.
@Override
public InputStream serialize(final R object) throws IOException {
// This puts everything on the heap for now.
// if size becomes a major problem, the alternitive is to fork over another thread
// and use a PipedInputStream.
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final XMLWriter writer;
try {
// This should always be UTF-8 because it is a property of the serializer.
final OutputFormat of = new OutputFormat(" ", true, "UTF-8");
writer = new XMLWriter(baos, of);
writer.startDocument();
} catch (SAXException e) {
throw new IOException("Could not open the XML writer.");
}
this.serialize(object, writer);
try {
writer.endDocument();
} catch (SAXException e) {
throw new IOException("Could not close the XML writer.");
}
return new ByteArrayInputStream(baos.toByteArray());
}
use of org.dom4j.io.OutputFormat in project xwiki-platform by xwiki.
the class XMLWriterTest method testWriteStream.
@Test
public void testWriteStream() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final OutputFormat of = new OutputFormat(" ", true, "UTF-8");
this.writer = new XMLWriter(baos, of);
this.writer.startDocument();
this.writer.writeOpen(new DOMElement("root"));
this.writer.write(new DOMElement("prop"), new StringReader("Hello World!"));
this.writer.writeClose(new DOMElement("root"));
this.writer.endDocument();
Assert.assertEquals("Incorrect response from testWriteStream.", WRITE_STREAM_TEST, new String(baos.toByteArray(), "UTF-8"));
}
use of org.dom4j.io.OutputFormat in project xwiki-platform by xwiki.
the class Package method toXML.
/**
* Write the package.xml file to an OutputStream
*
* @param out the OutputStream to write to
* @param context curent XWikiContext
* @throws IOException when an error occurs during streaming operation
* @since 2.3M2
*/
public void toXML(OutputStream out, XWikiContext context) throws IOException {
XMLWriter wr = new XMLWriter(out, new OutputFormat("", true, context.getWiki().getEncoding()));
Document doc = new DOMDocument();
wr.writeDocumentStart(doc);
toXML(wr);
wr.writeDocumentEnd(doc);
}
use of org.dom4j.io.OutputFormat in project xwiki-platform by xwiki.
the class PdfExportImpl method applyCSS.
/**
* Apply a CSS style sheet to an XHTML document and return the document with the resulting style properties inlined
* in <tt>style</tt> attributes.
*
* @param html the valid XHTML document to style
* @param css the style sheet to apply
* @param context the current request context
* @return the document with inlined style
*/
String applyCSS(String html, String css, XWikiContext context) {
LOGGER.debug("Applying the following CSS [{}] to HTML [{}]", css, html);
try {
// System.setProperty("org.w3c.css.sac.parser", "org.apache.batik.css.parser.Parser");
// Prepare the input
Reader re = new StringReader(html);
InputSource source = new InputSource(re);
SAXReader reader = new SAXReader(XHTMLDocumentFactory.getInstance());
reader.setEntityResolver(new DefaultEntityResolver());
XHTMLDocument document = (XHTMLDocument) reader.read(source);
// Set the base URL so that CSS4J can resolve URLs in CSS. Use the current document in the XWiki Context
document.setBaseURL(new URL(context.getDoc().getExternalURL("view", context)));
// Apply the style sheet
document.addStyleSheet(new org.w3c.css.sac.InputSource(new StringReader(css)));
applyInlineStyle(document.getRootElement());
OutputFormat outputFormat = new OutputFormat("", false);
if ((context == null) || (context.getWiki() == null)) {
outputFormat.setEncoding("UTF-8");
} else {
outputFormat.setEncoding(context.getWiki().getEncoding());
}
StringWriter out = new StringWriter();
XMLWriter writer = new XMLWriter(out, outputFormat);
writer.write(document);
String result = out.toString();
// Debug output
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("HTML with CSS applied [{}]", result);
}
return result;
} catch (Exception e) {
LOGGER.warn("Failed to apply CSS [{}] to HTML [{}]", css, html, e);
return html;
}
}
Aggregations