use of javax.xml.transform.Source in project opennms by OpenNMS.
the class OnmsPdfViewResolver method resolveView.
@Override
public void resolveView(ServletRequest request, ServletResponse response, Preferences preferences, Object viewData) throws Exception {
InputStream is = new ByteArrayInputStream(((String) viewData).getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream out = new ByteArrayOutputStream();
FopFactory fopFactory = FopFactory.newInstance();
fopFactory.setStrictValidation(false);
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
TransformerFactory tfact = TransformerFactory.newInstance();
Transformer transformer = tfact.newTransformer();
Source src = new StreamSource(is);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
byte[] contents = out.toByteArray();
response.setContentLength(contents.length);
response.getOutputStream().write(contents);
}
use of javax.xml.transform.Source in project opennms by OpenNMS.
the class AbstractXmlCollectionHandler method applyXsltTransformation.
/**
* Apply XSLT transformation.
*
* @param request the request
* @param is the input stream
* @return the input stream
* @throws Exception the exception
*/
protected InputStream applyXsltTransformation(Request request, InputStream is) throws Exception {
if (request == null || is == null)
return is;
String xsltFilename = request.getParameter("xslt-source-file");
if (xsltFilename == null)
return is;
File xsltFile = new File(xsltFilename);
if (!xsltFile.exists())
return is;
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(xsltFile);
Transformer transformer = factory.newTransformer(xslt);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
transformer.transform(new StreamSource(is), new StreamResult(baos));
return new ByteArrayInputStream(baos.toByteArray());
} finally {
IOUtils.closeQuietly(is);
}
}
use of javax.xml.transform.Source in project voltdb by VoltDB.
the class JDBCSQLXML method getSource.
/**
* Returns a Source for reading the XML value designated by this SQLXML instance.
* Sources are used as inputs to XML parsers and XSLT transformers.
* <p>
* Sources for XML parsers will have namespace processing on by default.
* The systemID of the Source is implementation dependent.
* <p>
* The SQL XML object becomes not readable when this method is called and
* may also become not writable depending on implementation.
* <p>
* Note that SAX is a callback architecture, so a returned
* SAXSource should then be set with a content handler that will
* receive the SAX events from parsing. The content handler
* will receive callbacks based on the contents of the XML.
* <pre>
* SAXSource saxSource = sqlxml.getSource(SAXSource.class);
* XMLReader xmlReader = saxSource.getXMLReader();
* xmlReader.setContentHandler(myHandler);
* xmlReader.parse(saxSource.getInputSource());
* </pre>
*
* @param sourceClass The class of the source, or null.
* If the class is null, a vendor specifc Source implementation will be returned.
* The following classes are supported at a minimum:
* <pre>
* javax.xml.transform.dom.DOMSource - returns a DOMSource
* javax.xml.transform.sax.SAXSource - returns a SAXSource
* javax.xml.transform.stax.StAXSource - returns a StAXSource
* javax.xml.transform.stream.StreamSource - returns a StreamSource
* </pre>
* @return a Source for reading the XML value.
* @throws SQLException if there is an error processing the XML value
* or if this feature is not supported.
* The getCause() method of the exception may provide a more detailed exception, for example,
* if an XML parser exception occurs.
* An exception is thrown if the state is not readable.
* @exception SQLFeatureNotSupportedException if the JDBC driver does not support
* this method
* @since JDK 1.6 Build 79
*/
@SuppressWarnings("unchecked")
public synchronized <T extends Source> T getSource(Class<T> sourceClass) throws SQLException {
checkClosed();
checkReadable();
final Source source = getSourceImpl(sourceClass);
setReadable(false);
setWritable(false);
return (T) source;
}
use of javax.xml.transform.Source in project jangaroo-tools by CoreMedia.
the class ExmlValidator method setupSAXParser.
private SAXParser setupSAXParser() throws IOException {
try {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
ExmlSchemaResolver exmlSchemaResolver = new ExmlSchemaResolver();
schemaFactory.setResourceResolver(exmlSchemaResolver);
List<Source> schemas = new ArrayList<Source>();
schemas.add(new StreamSource(getClass().getResourceAsStream(Exmlc.EXML_SCHEMA_LOCATION), "exml"));
schemas.add(new StreamSource(getClass().getResourceAsStream(Exmlc.EXML_UNTYPED_SCHEMA_LOCATION), "untyped"));
Collection<ExmlSchemaSource> exmlSchemaSources = exmlSchemaSourceByNamespace.values();
for (ExmlSchemaSource exmlSchemaSource : exmlSchemaSources) {
schemas.add(exmlSchemaSource.newStreamSource());
}
Schema exmlSchema = schemaFactory.newSchema(schemas.toArray(new Source[schemas.size()]));
final SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setNamespaceAware(true);
saxFactory.setSchema(exmlSchema);
SAXParser saxParser = saxFactory.newSAXParser();
saxParser.getXMLReader().setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
//To change body of implemented methods use File | Settings | File Templates.
return null;
}
});
return saxParser;
} catch (ParserConfigurationException e) {
throw new IllegalStateException("A default dom builder should be provided.", e);
} catch (SAXParseException e) {
// SAX parser error while parsing EXML schemas: log only, will cause error or warning, depending on configuration:
logSAXParseException(null, e, true);
return null;
} catch (SAXException e) {
throw new IllegalStateException("SAX parser does not support validation.", e);
}
}
use of javax.xml.transform.Source in project enclojure by EricThorsen.
the class Processor method main.
public static void main(final String[] args) throws Exception {
if (args.length < 2) {
showUsage();
return;
}
int inRepresentation = getRepresentation(args[0]);
int outRepresentation = getRepresentation(args[1]);
InputStream is = System.in;
OutputStream os = new BufferedOutputStream(System.out);
Source xslt = null;
for (int i = 2; i < args.length; i++) {
if ("-in".equals(args[i])) {
is = new FileInputStream(args[++i]);
} else if ("-out".equals(args[i])) {
os = new BufferedOutputStream(new FileOutputStream(args[++i]));
} else if ("-xslt".equals(args[i])) {
xslt = new StreamSource(new FileInputStream(args[++i]));
// } else if( "-computemax".equals( args[ i].toLowerCase())) {
// computeMax = true;
} else {
showUsage();
return;
}
}
if (inRepresentation == 0 || outRepresentation == 0) {
showUsage();
return;
}
Processor m = new Processor(inRepresentation, outRepresentation, is, os, xslt);
long l1 = System.currentTimeMillis();
int n = m.process();
long l2 = System.currentTimeMillis();
System.err.println(n);
System.err.println((l2 - l1) + "ms " + 1000f * n / (l2 - l1) + " resources/sec");
}
Aggregations