use of javax.xml.transform.Source in project cloudstack by apache.
the class PaloAltoResource method prettyFormat.
// pretty printing of xml strings
private String prettyFormat(String input) {
int indent = 4;
try {
Source xmlInput = new StreamSource(new StringReader(input));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", indent);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (Throwable e) {
try {
Source xmlInput = new StreamSource(new StringReader(input));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (Throwable t) {
return input;
}
}
}
use of javax.xml.transform.Source in project jackrabbit by apache.
the class ClientSession method exportDocumentView.
/**
* Exports the XML document view of the specified repository location
* to the given XML content handler. This method first requests the
* raw XML data from the remote session, and then uses an identity
* transformation to feed the data to the given XML content handler.
* Possible IO and transformer exceptions are thrown as SAXExceptions.
*
* {@inheritDoc}
*/
public void exportDocumentView(String path, ContentHandler handler, boolean binaryAsLink, boolean noRecurse) throws SAXException, RepositoryException {
try {
byte[] xml = remote.exportDocumentView(path, binaryAsLink, noRecurse);
Source source = new StreamSource(new ByteArrayInputStream(xml));
Result result = new SAXResult(handler);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
} catch (RemoteException ex) {
throw new RemoteRepositoryException(ex);
} catch (IOException ex) {
throw new SAXException(ex);
} catch (TransformerConfigurationException ex) {
throw new SAXException(ex);
} catch (TransformerException ex) {
throw new SAXException(ex);
}
}
use of javax.xml.transform.Source in project jackrabbit by apache.
the class AbstractImportXmlTest method serialize.
public void serialize(Document document) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
try {
// disable pretty printing/default line wrapping!
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.METHOD, "xml");
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
t.setOutputProperty(OutputKeys.INDENT, "no");
Source s = new DOMSource(document);
Result r = new StreamResult(bos);
t.transform(s, r);
} catch (TransformerException te) {
throw (IOException) new IOException(te.getMessage()).initCause(te);
} finally {
bos.close();
}
}
use of javax.xml.transform.Source in project jena by apache.
the class DOM2Model method load.
/**
* Parse a DOM Node with the RDF/XML parser, loading the triples into the
* associated Model. Known not to work with Java 1.4.1.
*
* @param document
*/
public void load(Node document) {
Source input = new DOMSource(document);
// Make a SAXResult object using this handler
SAXResult output = new SAXResult(this);
output.setLexicalHandler(this);
// Run transform
TransformerFactory xformFactory = TransformerFactory.newInstance();
try {
Transformer idTransform = xformFactory.newTransformer();
idTransform.transform(input, output);
} catch (FatalParsingErrorException e) {
// Old code ignored this,
// given difficult bug report, don't be silent.
logger.error("Unexpected exception in DOM2Model", e);
} catch (RuntimeException rte) {
throw rte;
} catch (Exception nrte) {
throw new JenaException(nrte);
} finally {
close();
}
}
use of javax.xml.transform.Source in project logging-log4j2 by apache.
the class XmlCompactFileAsyncAppenderValidationTest method validateXmlSchema.
private void validateXmlSchema(final File file) throws SAXException, IOException {
final URL schemaFile = this.getClass().getClassLoader().getResource("Log4j-events.xsd");
final Source xmlFile = new StreamSource(file);
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Schema schema = schemaFactory.newSchema(schemaFile);
final Validator validator = schema.newValidator();
validator.validate(xmlFile);
}
Aggregations