Search in sources :

Example 76 with Source

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);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) FOUserAgent(org.apache.fop.apps.FOUserAgent) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FopFactory(org.apache.fop.apps.FopFactory) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXResult(javax.xml.transform.sax.SAXResult) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 77 with Source

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);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) XmlSource(org.opennms.protocols.xml.config.XmlSource)

Example 78 with Source

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;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) JAXBSource(javax.xml.bind.util.JAXBSource)

Example 79 with 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);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 80 with Source

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");
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) StreamSource(javax.xml.transform.stream.StreamSource) BufferedOutputStream(java.io.BufferedOutputStream) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) FileInputStream(java.io.FileInputStream)

Aggregations

Source (javax.xml.transform.Source)238 StreamSource (javax.xml.transform.stream.StreamSource)161 DOMSource (javax.xml.transform.dom.DOMSource)108 Transformer (javax.xml.transform.Transformer)76 StreamResult (javax.xml.transform.stream.StreamResult)74 InputSource (org.xml.sax.InputSource)67 SAXSource (javax.xml.transform.sax.SAXSource)56 StringReader (java.io.StringReader)52 IOException (java.io.IOException)46 TransformerException (javax.xml.transform.TransformerException)45 Result (javax.xml.transform.Result)42 TransformerFactory (javax.xml.transform.TransformerFactory)41 Test (org.junit.Test)39 StringWriter (java.io.StringWriter)35 InputStream (java.io.InputStream)32 SAXException (org.xml.sax.SAXException)32 Schema (javax.xml.validation.Schema)29 Validator (javax.xml.validation.Validator)29 SchemaFactory (javax.xml.validation.SchemaFactory)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26