Search in sources :

Example 1 with FixedXMLEventStreamReader

use of org.springframework.util.xml.FixedXMLEventStreamReader 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 2 with FixedXMLEventStreamReader

use of org.springframework.util.xml.FixedXMLEventStreamReader in project uPortal by Jasig.

the class XsltPortalUrlProviderTest method setup.

@Before
public void setup() throws Exception {
    final ResourceLoaderURIResolver resolver = new ResourceLoaderURIResolver(new ClassRelativeResourceLoader(getClass()));
    this.tFactory = TransformerFactory.newInstance();
    this.tFactory.setURIResolver(resolver);
    this.tFactory.setErrorListener(new SimpleTransformErrorListener(LogFactory.getLog(getClass())));
    //Load the XML document so it reads the same way the rendering pipeline reads XML
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
    final XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(this.getClass().getResourceAsStream("test.xml"));
    final XMLStreamReader streamReader = new FixedXMLEventStreamReader(eventReader);
    xmlSource = new StAXSource(streamReader);
    xslTemplate = tFactory.newTemplates(new StreamSource(this.getClass().getResourceAsStream("test.xsl")));
    expected = IOUtils.toString(this.getClass().getResourceAsStream("result.xml"));
}
Also used : ResourceLoaderURIResolver(org.apereo.portal.xml.ResourceLoaderURIResolver) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) XMLStreamReader(javax.xml.stream.XMLStreamReader) SimpleTransformErrorListener(org.springframework.util.xml.SimpleTransformErrorListener) FixedXMLEventStreamReader(org.springframework.util.xml.FixedXMLEventStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Before(org.junit.Before)

Example 3 with FixedXMLEventStreamReader

use of org.springframework.util.xml.FixedXMLEventStreamReader in project uPortal by Jasig.

the class AbstractDom4jImporter method convertToElement.

protected Element convertToElement(Source source) {
    if (source instanceof StAXSource) {
        final StAXSource staxSource = (StAXSource) source;
        final DOMConverter domConverter = new DOMConverter();
        final Document document;
        try {
            XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader();
            if (xmlStreamReader == null) {
                final XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
                xmlStreamReader = new FixedXMLEventStreamReader(xmlEventReader);
            }
            document = domConverter.buildDocument(xmlStreamReader);
        } catch (XMLStreamException e) {
            throw new RuntimeException("Failed to parse StAX Reader into Dom4J Element", e);
        }
        final DOMReader domReader = new DOMReader();
        final org.dom4j.Document dom4JDocument = domReader.read(document);
        dom4JDocument.setName(source.getSystemId());
        return dom4JDocument.getRootElement();
    }
    throw new IllegalArgumentException("Source of type " + source.getClass() + " is not supported");
}
Also used : DOMReader(org.dom4j.io.DOMReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) FixedXMLEventStreamReader(org.springframework.util.xml.FixedXMLEventStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) DOMConverter(org.codehaus.staxmate.dom.DOMConverter)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 StAXSource (javax.xml.transform.stax.StAXSource)3 FixedXMLEventStreamReader (org.springframework.util.xml.FixedXMLEventStreamReader)3 XMLStreamException (javax.xml.stream.XMLStreamException)2 Map (java.util.Map)1 Properties (java.util.Properties)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)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 StreamSource (javax.xml.transform.stream.StreamSource)1 PipelineEventReaderImpl (org.apereo.portal.rendering.PipelineEventReaderImpl)1 ResourceLoaderURIResolver (org.apereo.portal.xml.ResourceLoaderURIResolver)1 XMLEventBufferReader (org.apereo.portal.xml.stream.XMLEventBufferReader)1 XMLEventBufferWriter (org.apereo.portal.xml.stream.XMLEventBufferWriter)1 DOMConverter (org.codehaus.staxmate.dom.DOMConverter)1 DOMReader (org.dom4j.io.DOMReader)1