use of javax.xml.transform.stream.StreamResult in project hazelcast by hazelcast.
the class ConfigXmlGenerator method format.
private String format(final String input, int indent) {
if (!formatted) {
return input;
}
StreamResult xmlOutput = null;
try {
final Source xmlInput = new StreamSource(new StringReader(input));
xmlOutput = new StreamResult(new StringWriter());
TransformerFactory transformerFactory = TransformerFactory.newInstance();
/* Older versions of Xalan still use this method of setting indent values.
* Attempt to make this work but don't completely fail if it's a problem.
*/
try {
transformerFactory.setAttribute("indent-number", indent);
} catch (IllegalArgumentException e) {
if (LOGGER.isFinestEnabled()) {
LOGGER.finest("Failed to set indent-number attribute; cause: " + e.getMessage());
}
}
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
/* Newer versions of Xalan will look for a fully-qualified output property in order to specify amount of
* indentation to use. Attempt to make this work as well but again don't completely fail if it's a problem.
*/
try {
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
} catch (IllegalArgumentException e) {
if (LOGGER.isFinestEnabled()) {
LOGGER.finest("Failed to set indent-amount property; cause: " + e.getMessage());
}
}
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (Exception e) {
LOGGER.warning(e);
return input;
} finally {
if (xmlOutput != null) {
closeResource(xmlOutput.getWriter());
}
}
}
use of javax.xml.transform.stream.StreamResult in project gephi by gephi.
the class LayoutPresetPersistence method savePreset.
public void savePreset(String name, Layout layout) {
Preset preset = addPreset(new Preset(name, layout));
FileOutputStream fos = null;
try {
//Create file if dont exist
FileObject folder = FileUtil.getConfigFile("layoutpresets");
if (folder == null) {
folder = FileUtil.getConfigRoot().createFolder("layoutpresets");
}
File presetFile = new File(FileUtil.toFile(folder), name + ".xml");
//Create doc
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
final Document document = documentBuilder.newDocument();
document.setXmlVersion("1.0");
document.setXmlStandalone(true);
//Write doc
preset.writeXML(document);
//Write XML file
fos = new FileOutputStream(presetFile);
Source source = new DOMSource(document);
Result result = new StreamResult(fos);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
} catch (Exception e) {
Logger.getLogger("").log(Level.SEVERE, "Error while writing preset file", e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
}
}
}
}
use of javax.xml.transform.stream.StreamResult in project gephi by gephi.
the class ExporterGraphML method transform.
private void transform(Document document) throws TransformerConfigurationException, TransformerException {
Source source = new DOMSource(document);
Result result = new StreamResult(writer);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
}
use of javax.xml.transform.stream.StreamResult in project gocd by gocd.
the class XsltStylesheet method retryXsltTransformation.
private String retryXsltTransformation(ThreadContext context, IRubyObject[] args, DOMSource domSource, NokogiriXsltErrorListener elistener) throws TransformerException, IOException {
Templates templates = getTemplatesFromStreamSource();
Transformer transf = templates.newTransformer();
transf.setErrorListener(elistener);
if (args.length > 1) {
addParametersToTransformer(context, transf, args[1]);
}
PipedWriter pwriter = new PipedWriter();
PipedReader preader = new PipedReader();
pwriter.connect(preader);
StreamResult result = new StreamResult(pwriter);
transf.transform(domSource, result);
char[] cbuf = new char[1024];
int len = preader.read(cbuf, 0, 1024);
StringBuilder builder = new StringBuilder();
builder.append(CharBuffer.wrap(cbuf, 0, len));
// judge from the first chunk
htmlish = isHtml(builder.toString());
while (len == 1024) {
len = preader.read(cbuf, 0, 1024);
if (len > 0) {
builder.append(CharBuffer.wrap(cbuf, 0, len));
}
}
preader.close();
pwriter.close();
return builder.toString();
}
use of javax.xml.transform.stream.StreamResult in project gradle by gradle.
the class XslTransformer method main.
public static void main(String[] args) throws TransformerException, IOException {
if (args.length < 3 || args.length > 4) {
throw new IllegalArgumentException("USAGE: <style-sheet> <source-file> <dest-file> [dest-dir]");
}
File stylesheet = new File(args[0]);
File source = new File(args[1]);
File dest = new File(args[2]);
String destDir = "";
if (args.length > 3) {
destDir = args[3];
}
System.out.format("=> stylesheet %s%n", stylesheet);
System.out.format("=> source %s%n", source);
System.out.format("=> dest %s%n", dest);
System.out.format("=> destDir %s%n", destDir);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
System.out.format("=> transformer %s (%s)%n", transformer, transformer.getClass().getName());
if (destDir.length() > 0) {
transformer.setParameter("base.dir", destDir + "/");
}
FileOutputStream outstr = new FileOutputStream(dest);
try {
transformer.transform(new StreamSource(source), new StreamResult(outstr));
} finally {
outstr.close();
}
}
Aggregations