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 logger by orhanobut.
the class LoggerPrinter method xml.
/**
* Formats the json content and print it
*
* @param xml the xml content
*/
@Override
public void xml(String xml) {
if (Helper.isEmpty(xml)) {
d("Empty/Null xml content");
return;
}
try {
Source xmlInput = new StreamSource(new StringReader(xml));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
d(xmlOutput.getWriter().toString().replaceFirst(">", ">\n"));
} catch (TransformerException e) {
e("Invalid xml");
}
}
use of javax.xml.transform.Source in project robovm by robovm.
the class ProcessorInclude method parse.
/**
* Set off a new parse for an included or imported stylesheet. This will
* set the {@link StylesheetHandler} to a new state, and recurse in with
* a new set of parse events. Once this function returns, the state of
* the StylesheetHandler should be restored.
*
* @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
* @param uri The Namespace URI, which should be the XSLT namespace.
* @param localName The local name (without prefix), which should be "include" or "import".
* @param rawName The qualified name (with prefix).
* @param attributes The list of attributes on the xsl:include or xsl:import element.
*
* @throws org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
*/
protected void parse(StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) throws org.xml.sax.SAXException {
TransformerFactoryImpl processor = handler.getStylesheetProcessor();
URIResolver uriresolver = processor.getURIResolver();
try {
Source source = null;
if (null != uriresolver) {
// There is a user provided URI resolver.
// At the startElement() call we would
// have tried to obtain a Source from it
// which we now retrieve
source = handler.peekSourceFromURIResolver();
if (null != source && source instanceof DOMSource) {
Node node = ((DOMSource) source).getNode();
// There is a user provided URI resolver.
// At the startElement() call we would
// have already pushed the system ID, obtained
// from either the source.getSystemId(), if non-null
// or from SystemIDResolver.getAbsoluteURI() as a backup
// which we now retrieve.
String systemId = handler.peekImportURL();
// stylesheet module onto the stack.
if (systemId != null)
handler.pushBaseIndentifier(systemId);
TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(), systemId);
try {
walker.traverse(node);
} catch (org.xml.sax.SAXException se) {
throw new TransformerException(se);
}
if (systemId != null)
handler.popBaseIndentifier();
return;
}
}
if (null == source) {
String absURL = SystemIDResolver.getAbsoluteURI(getHref(), handler.getBaseIdentifier());
source = new StreamSource(absURL);
}
// possible callback to a class that over-rides this method.
source = processSource(handler, source);
XMLReader reader = null;
if (source instanceof SAXSource) {
SAXSource saxSource = (SAXSource) source;
// may be null
reader = saxSource.getXMLReader();
}
InputSource inputSource = SAXSource.sourceToInputSource(source);
if (null == reader) {
// Use JAXP1.1 ( if possible )
try {
javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
if (handler.getStylesheetProcessor().isSecureProcessing()) {
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (org.xml.sax.SAXException se) {
}
}
javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
reader = jaxpParser.getXMLReader();
} catch (javax.xml.parsers.ParserConfigurationException ex) {
throw new org.xml.sax.SAXException(ex);
} catch (javax.xml.parsers.FactoryConfigurationError ex1) {
throw new org.xml.sax.SAXException(ex1.toString());
} catch (NoSuchMethodError ex2) {
} catch (AbstractMethodError ame) {
}
}
if (null == reader)
reader = XMLReaderFactory.createXMLReader();
if (null != reader) {
reader.setContentHandler(handler);
// Push the absolute URI of the included/imported
// stylesheet module onto the stack.
handler.pushBaseIndentifier(inputSource.getSystemId());
try {
reader.parse(inputSource);
} finally {
handler.popBaseIndentifier();
}
}
} catch (IOException ioe) {
handler.error(XSLTErrorResources.ER_IOEXCEPTION, new Object[] { getHref() }, ioe);
} catch (TransformerException te) {
handler.error(te.getMessage(), te);
}
}
use of javax.xml.transform.Source in project robovm by robovm.
the class SourceTreeManager method resolveURI.
/**
* This will be called by the processor when it encounters
* an xsl:include, xsl:import, or document() function.
*
* @param base The base URI that should be used.
* @param urlString Value from an xsl:import or xsl:include's href attribute,
* or a URI specified in the document() function.
*
* @return a Source that can be used to process the resource.
*
* @throws IOException
* @throws TransformerException
*/
public Source resolveURI(String base, String urlString, SourceLocator locator) throws TransformerException, IOException {
Source source = null;
if (null != m_uriResolver) {
source = m_uriResolver.resolve(urlString, base);
}
if (null == source) {
String uri = SystemIDResolver.getAbsoluteURI(urlString, base);
source = new StreamSource(uri);
}
return source;
}
use of javax.xml.transform.Source in project spring-framework by spring-projects.
the class StaxResultTests method eventWriterSource.
@Test
public void eventWriterSource() throws Exception {
StringWriter stringWriter = new StringWriter();
XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter);
Reader reader = new StringReader(XML);
Source source = new StreamSource(reader);
StaxResult result = new StaxResult(eventWriter);
assertEquals("Invalid eventWriter returned", eventWriter, result.getXMLEventWriter());
assertNull("StreamWriter returned", result.getXMLStreamWriter());
transformer.transform(source, result);
assertThat("Invalid result", stringWriter.toString(), isSimilarTo(XML));
}
Aggregations