Search in sources :

Example 1 with XMLEventBufferWriter

use of org.apereo.portal.xml.stream.XMLEventBufferWriter 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)

Aggregations

Map (java.util.Map)1 Properties (java.util.Properties)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 Source (javax.xml.transform.Source)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 SAXResult (javax.xml.transform.sax.SAXResult)1 StAXSource (javax.xml.transform.stax.StAXSource)1 PipelineEventReaderImpl (org.apereo.portal.rendering.PipelineEventReaderImpl)1 XMLEventBufferReader (org.apereo.portal.xml.stream.XMLEventBufferReader)1 XMLEventBufferWriter (org.apereo.portal.xml.stream.XMLEventBufferWriter)1 FixedXMLEventStreamReader (org.springframework.util.xml.FixedXMLEventStreamReader)1 ContentHandler (org.xml.sax.ContentHandler)1 LocatorImpl (org.xml.sax.helpers.LocatorImpl)1