Search in sources :

Example 11 with SAXResult

use of javax.xml.transform.sax.SAXResult in project series-rest-api by 52North.

the class PDFReportGenerator method encodeAndWriteTo.

@Override
public void encodeAndWriteTo(DataCollection<QuantityData> data, OutputStream stream) throws IoParseException {
    try {
        generateOutput(data);
        DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
        Configuration cfg = cfgBuilder.build(document.newInputStream());
        FopFactory fopFactory = new FopFactoryBuilder(baseURI).setConfiguration(cfg).build();
        final String mimeType = MimeType.APPLICATION_PDF.getMimeType();
        Fop fop = fopFactory.newFop(mimeType, stream);
        //FopFactory fopFactory = FopFactory.newInstance(cfg);
        //Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        //FopFactory fopFactory = fopFactoryBuilder.build();
        //Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        // Create PDF via XSLT transformation
        TransformerFactory transFact = TransformerFactory.newInstance();
        StreamSource transformationRule = getTransforamtionRule();
        Transformer transformer = transFact.newTransformer(transformationRule);
        Source source = new StreamSource(document.newInputStream());
        Result result = new SAXResult(fop.getDefaultHandler());
        if (LOGGER.isDebugEnabled()) {
            try {
                File tempFile = File.createTempFile(TEMP_FILE_PREFIX, ".xml");
                StreamResult debugResult = new StreamResult(tempFile);
                transformer.transform(source, debugResult);
                String xslResult = XmlObject.Factory.parse(tempFile).xmlText();
                LOGGER.debug("xsl-fo input (locale '{}'): {}", i18n.getTwoDigitsLanguageCode(), xslResult);
            } catch (IOException | TransformerException | XmlException e) {
                LOGGER.error("Could not debug XSL result output!", e);
            }
        }
        // XXX debug, diagram is not embedded
        transformer.transform(source, result);
    } catch (FOPException e) {
        throw new IoParseException("Failed to create Formatting Object Processor (FOP)", e);
    } catch (SAXException | ConfigurationException | IOException e) {
        throw new IoParseException("Failed to read config for Formatting Object Processor (FOP)", e);
    } catch (TransformerConfigurationException e) {
        throw new IoParseException("Invalid transform configuration. Inspect xslt!", e);
    } catch (TransformerException e) {
        throw new IoParseException("Could not generate PDF report!", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) DefaultConfigurationBuilder(org.apache.avalon.framework.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.avalon.framework.configuration.Configuration) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) FopFactoryBuilder(org.apache.fop.apps.FopFactoryBuilder) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) ConfigurationException(org.apache.avalon.framework.configuration.ConfigurationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XmlException(org.apache.xmlbeans.XmlException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 12 with SAXResult

use of javax.xml.transform.sax.SAXResult 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);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) SAXResult(javax.xml.transform.sax.SAXResult) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteException(java.rmi.RemoteException) TransformerException(javax.xml.transform.TransformerException)

Example 13 with SAXResult

use of javax.xml.transform.sax.SAXResult 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();
    }
}
Also used : JenaException(org.apache.jena.shared.JenaException) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) SAXResult(javax.xml.transform.sax.SAXResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) SAXParseException(org.xml.sax.SAXParseException) JenaException(org.apache.jena.shared.JenaException)

Example 14 with SAXResult

use of javax.xml.transform.sax.SAXResult in project uPortal by Jasig.

the class XSLTComponent method getEventReader.

/* (non-Javadoc)
     * @see org.apereo.portal.rendering.StAXPipelineComponent#getXmlStreamReader(java.lang.Object, java.lang.Object)
     */
@Override
public PipelineEventReader<XMLEventReader, XMLEvent> getEventReader(HttpServletRequest request, HttpServletResponse response) {
    final PipelineEventReader<XMLEventReader, XMLEvent> pipelineEventReader = this.wrappedComponent.getEventReader(request, response);
    final Transformer transformer = this.transformerSource.getTransformer(request, response);
    //Setup a URIResolver based on the current resource loader
    transformer.setURIResolver(this.uriResolver);
    //Configure the Transformer via injected class
    if (this.xsltParameterSource != null) {
        final Map<String, Object> transformerParameters = this.xsltParameterSource.getParameters(request, response);
        if (transformerParameters != null) {
            this.logger.debug("{} - Setting Transformer Parameters: ", this.beanName, transformerParameters);
            for (final Map.Entry<String, Object> transformerParametersEntry : transformerParameters.entrySet()) {
                final String name = transformerParametersEntry.getKey();
                final Object value = transformerParametersEntry.getValue();
                if (value != null) {
                    transformer.setParameter(name, value);
                }
            }
        }
        final Properties outputProperties = this.xsltParameterSource.getOutputProperties(request, response);
        if (outputProperties != null) {
            this.logger.debug("{} - Setting Transformer Output Properties: ", this.beanName, outputProperties);
            transformer.setOutputProperties(outputProperties);
        }
    }
    //The event reader from the previous component in the pipeline
    final XMLEventReader eventReader = pipelineEventReader.getEventReader();
    //Wrap the event reader in a stream reader to avoid a JDK bug
    final XMLStreamReader streamReader;
    try {
        streamReader = new FixedXMLEventStreamReader(eventReader);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XMLStreamReader from XMLEventReader", e);
    }
    final Source xmlReaderSource = new StAXSource(streamReader);
    //Setup logging for the transform
    transformer.setErrorListener(this.errorListener);
    //Transform to a SAX ContentHandler to avoid JDK bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6775588
    final XMLEventBufferWriter eventWriterBuffer = new XMLEventBufferWriter();
    final ContentHandler contentHandler = StaxUtils.createLexicalContentHandler(eventWriterBuffer);
    contentHandler.setDocumentLocator(new LocatorImpl());
    final SAXResult outputTarget = new SAXResult(contentHandler);
    try {
        this.logger.debug("{} - Begining XML Transformation", this.beanName);
        transformer.transform(xmlReaderSource, outputTarget);
        this.logger.debug("{} - XML Transformation complete", this.beanName);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to transform document", e);
    }
    final String mediaType = transformer.getOutputProperty(OutputKeys.MEDIA_TYPE);
    final List<XMLEvent> eventBuffer = eventWriterBuffer.getEventBuffer();
    final XMLEventReader outputEventReader = new XMLEventBufferReader(eventBuffer.listIterator());
    final Map<String, String> outputProperties = pipelineEventReader.getOutputProperties();
    final PipelineEventReaderImpl<XMLEventReader, XMLEvent> pipelineEventReaderImpl = new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(outputEventReader, outputProperties);
    pipelineEventReaderImpl.setOutputProperty(OutputKeys.MEDIA_TYPE, mediaType);
    return pipelineEventReaderImpl;
}
Also used : XMLEventBufferWriter(org.apereo.portal.xml.stream.XMLEventBufferWriter) Transformer(javax.xml.transform.Transformer) XMLStreamReader(javax.xml.stream.XMLStreamReader) FixedXMLEventStreamReader(org.springframework.util.xml.FixedXMLEventStreamReader) Properties(java.util.Properties) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) ContentHandler(org.xml.sax.ContentHandler) XMLEventReader(javax.xml.stream.XMLEventReader) LocatorImpl(org.xml.sax.helpers.LocatorImpl) TransformerException(javax.xml.transform.TransformerException) StAXSource(javax.xml.transform.stax.StAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) SAXResult(javax.xml.transform.sax.SAXResult) XMLEventBufferReader(org.apereo.portal.xml.stream.XMLEventBufferReader) PipelineEventReaderImpl(org.apereo.portal.rendering.PipelineEventReaderImpl) XMLEvent(javax.xml.stream.events.XMLEvent) Map(java.util.Map)

Example 15 with SAXResult

use of javax.xml.transform.sax.SAXResult in project tika by apache.

the class MimeTypesReader method read.

public void read(Document document) throws MimeTypeException {
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(new DOMSource(document), new SAXResult(this));
    } catch (TransformerException e) {
        throw new MimeTypeException("Failed to parse type registry", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) SAXResult(javax.xml.transform.sax.SAXResult) TransformerException(javax.xml.transform.TransformerException)

Aggregations

SAXResult (javax.xml.transform.sax.SAXResult)34 Transformer (javax.xml.transform.Transformer)15 TransformerException (javax.xml.transform.TransformerException)14 IOException (java.io.IOException)13 TransformerFactory (javax.xml.transform.TransformerFactory)10 StreamResult (javax.xml.transform.stream.StreamResult)10 StreamSource (javax.xml.transform.stream.StreamSource)10 Source (javax.xml.transform.Source)9 DOMResult (javax.xml.transform.dom.DOMResult)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 Result (javax.xml.transform.Result)8 ContentHandler (org.xml.sax.ContentHandler)8 Fop (org.apache.fop.apps.Fop)7 Document (org.w3c.dom.Document)7 DocumentBuilder (javax.xml.parsers.DocumentBuilder)6 Node (org.w3c.dom.Node)6 SAXException (org.xml.sax.SAXException)6 FileOutputStream (java.io.FileOutputStream)5 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 LexicalHandler (org.xml.sax.ext.LexicalHandler)5