use of javax.xml.transform.Source in project che by eclipse.
the class BuildFileGenerator method documentToString.
/** Convert document to formatted XML string. */
private String documentToString(Document doc) throws TransformerException {
StringWriter writer = new StringWriter();
Source source = new DOMSource(doc);
Result result = new StreamResult(writer);
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute("indent-number", "4");
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
return writer.toString();
}
use of javax.xml.transform.Source in project head by mifos.
the class ChartOfAccountsConfig method load.
/**
* Factory method which loads the Chart of Accounts configuration from the
* given filename. Given XML filename will be validated against
* {@link FilePaths#CHART_OF_ACCOUNTS_SCHEMA}.
*
* @param chartOfAccountsXml
* a relative path to the Chart of Accounts configuration file.
*/
public static ChartOfAccountsConfig load(String chartOfAccountsXml) throws ConfigurationException {
ChartOfAccountsConfig instance = null;
Document document = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = dbf.newDocumentBuilder();
if (FilePaths.CHART_OF_ACCOUNTS_DEFAULT.equals(chartOfAccountsXml)) {
// default chart of accounts
document = parser.parse(MifosResourceUtil.getClassPathResourceAsStream(chartOfAccountsXml));
} else {
// custom chart of accounts
document = parser.parse(MifosResourceUtil.getFile(chartOfAccountsXml));
}
// create a SchemaFactory capable of understanding XML schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load an XML schema
ClassPathResource schemaFileResource = new ClassPathResource(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
Source schemaFile = null;
if (schemaFileResource.exists()) {
InputStream in = ChartOfAccountsConfig.class.getClassLoader().getResourceAsStream(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
schemaFile = new StreamSource(in);
} else {
schemaFile = new StreamSource(MifosResourceUtil.getFile(FilePaths.CHART_OF_ACCOUNTS_SCHEMA));
}
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance and validate document
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
} catch (IOException e) {
throw new ConfigurationException(e);
} catch (SAXException e) {
throw new ConfigurationException(e);
} catch (ParserConfigurationException e) {
throw new ConfigurationException(e);
}
instance = new ChartOfAccountsConfig();
instance.coaDocument = document;
return instance;
}
use of javax.xml.transform.Source in project OpenAttestation by OpenAttestation.
the class ConverterUtil method formateXMLString.
public static String formateXMLString(String inputXML) {
StreamResult xmlOutput = null;
try {
Source xmlInput = new StreamSource(new StringReader(inputXML));
StringWriter stringWriter = new StringWriter();
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", "2");
transformer.transform(xmlInput, xmlOutput);
} catch (Exception e) {
// simple exception handling, please review it
throw new RuntimeException(e);
}
return xmlOutput.getWriter().toString();
}
use of javax.xml.transform.Source in project jersey by jersey.
the class SourceEntityProviderTest method getSourceTest.
@Test
public void getSourceTest() throws Exception {
Response response = target().path("test").path("source").request().get();
assertEquals(Status.OK.getStatusCode(), response.getStatus());
String content = extractContent(response.readEntity(Source.class));
assertTrue(content.startsWith(prefix) || content.startsWith(xdkPrefix));
}
use of javax.xml.transform.Source in project sonarqube by SonarSource.
the class DebtModelXMLExporter method prettyFormatXml.
private static String prettyFormatXml(String xml) {
try {
Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT);
Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
StreamResult res = new StreamResult(new ByteArrayOutputStream());
serializer.transform(xmlSource, res);
return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(), StandardCharsets.UTF_8);
} catch (TransformerException ignored) {
// Ignore, raw XML will be returned
}
return xml;
}
Aggregations