Search in sources :

Example 51 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.

the class XSLTComponentTest method testXSLTComponent.

@Test
public void testXSLTComponent() throws Exception {
    final MockHttpServletRequest mockReq = new MockHttpServletRequest();
    final MockHttpServletResponse mockRes = new MockHttpServletResponse();
    final XMLEventReader xmlEventReader = this.getXmlEventReader("juser.xml");
    final PipelineEventReaderImpl<XMLEventReader, XMLEvent> cacheableEventReader = new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(xmlEventReader);
    final Transformer transformer = this.getTransformer("columns.xsl");
    final StAXPipelineComponent targetComponent = EasyMock.createMock(StAXPipelineComponent.class);
    final TransformerSource transformerSource = EasyMock.createMock(TransformerSource.class);
    EasyMock.expect(targetComponent.getEventReader(mockReq, mockRes)).andReturn(cacheableEventReader);
    EasyMock.expect(transformerSource.getTransformer(mockReq, mockRes)).andReturn(transformer);
    EasyMock.replay(targetComponent, transformerSource);
    final XSLTComponent xsltComponent = new XSLTComponent();
    xsltComponent.setWrappedComponent(targetComponent);
    xsltComponent.setTransformerSource(transformerSource);
    final PipelineEventReader<XMLEventReader, XMLEvent> eventReader = xsltComponent.getEventReader(mockReq, mockRes);
    Assert.assertNotNull(eventReader);
    final String output = this.serializeXMLEventReader(eventReader.getEventReader());
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setFeature("http://xml.org/sax/features/namespaces", false);
    dbf.setFeature("http://xml.org/sax/features/validation", false);
    dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    final DocumentBuilder db = dbf.newDocumentBuilder();
    XMLUnit.setIgnoreWhitespace(true);
    try {
        final Document expected = db.parse(this.getClass().getResourceAsStream("/org/apereo/portal/rendering/xslt/expected.xml"), "/org/apereo/portal/rendering/xslt/expected.xml");
        final Document actual = db.parse(new InputSource(new StringReader(output)));
        Diff d = new Diff(expected, actual);
        assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
    } catch (Exception e) {
        throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", output, e);
    } catch (Error e) {
        throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", output, e);
    }
    EasyMock.verify(targetComponent, transformerSource);
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) StAXPipelineComponent(org.apereo.portal.rendering.StAXPipelineComponent) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Diff(org.custommonkey.xmlunit.Diff) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Document(org.w3c.dom.Document) XmlTestException(org.apereo.portal.io.xml.XmlTestException) XMLStreamException(javax.xml.stream.XMLStreamException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) PipelineEventReaderImpl(org.apereo.portal.rendering.PipelineEventReaderImpl) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XMLEvent(javax.xml.stream.events.XMLEvent) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XmlTestException(org.apereo.portal.io.xml.XmlTestException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 52 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.

the class PortletRenderingInitiationStAXComponent method getEventReader.

/* (non-Javadoc)
     * @see org.apereo.portal.rendering.PipelineComponent#getEventReader(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
@Override
public PipelineEventReader<XMLEventReader, XMLEvent> getEventReader(HttpServletRequest request, HttpServletResponse response) {
    final PipelineEventReader<XMLEventReader, XMLEvent> pipelineEventReader = this.wrappedComponent.getEventReader(request, response);
    final XMLEventReader eventReader = pipelineEventReader.getEventReader();
    final PortletRenderingXMLEventReader filteredEventReader = new PortletRenderingXMLEventReader(request, response, eventReader);
    final Map<String, String> outputProperties = pipelineEventReader.getOutputProperties();
    return new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(filteredEventReader, outputProperties);
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FilteringXMLEventReader(org.apereo.portal.xml.stream.FilteringXMLEventReader)

Example 53 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.

the class UserLayoutStoreComponent method getEventReader.

@Override
public PipelineEventReader<XMLEventReader, XMLEvent> getEventReader(HttpServletRequest request, HttpServletResponse response) {
    final long timestamp = System.currentTimeMillis();
    logger.debug("STARTING user layout fetch for user '{}' #milestone", request.getRemoteUser());
    final IUserLayoutManager userLayoutManager = getUserLayoutManager(request);
    final XMLEventReader userLayoutReader = userLayoutManager.getUserLayoutReader();
    logger.debug("FINISHED user layout fetch for user '{}' in {}ms #milestone", request.getRemoteUser(), Long.toString(System.currentTimeMillis() - timestamp));
    return new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(userLayoutReader);
}
Also used : XMLEventReader(javax.xml.stream.XMLEventReader) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager)

Example 54 with XMLEventReader

use of javax.xml.stream.XMLEventReader 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)

Example 55 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.

the class BufferedXMLEventReaderTest method testBufferNoEvents.

@Test
public void testBufferNoEvents() throws Exception {
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
    final InputStream xmlStream = this.getClass().getResourceAsStream("document.xml");
    final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(xmlStream);
    final BufferedXMLEventReader reader = new BufferedXMLEventReader(xmlEventReader);
    int eventCount = 0;
    while (reader.hasNext()) {
        reader.nextEvent();
        eventCount++;
    }
    assertEquals(122, eventCount);
    reader.reset();
    while (reader.hasNext()) {
        reader.nextEvent();
        eventCount++;
    }
    assertEquals(122, eventCount);
}
Also used : InputStream(java.io.InputStream) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)71 XMLInputFactory (javax.xml.stream.XMLInputFactory)36 XMLEvent (javax.xml.stream.events.XMLEvent)34 XMLStreamException (javax.xml.stream.XMLStreamException)23 StringReader (java.io.StringReader)18 Test (org.junit.Test)17 InputStream (java.io.InputStream)15 StAXSource (javax.xml.transform.stax.StAXSource)13 StartElement (javax.xml.stream.events.StartElement)12 IOException (java.io.IOException)10 Unmarshaller (javax.xml.bind.Unmarshaller)9 Attribute (javax.xml.stream.events.Attribute)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 StringWriter (java.io.StringWriter)6 ArrayList (java.util.ArrayList)6 QName (javax.xml.namespace.QName)6 Document (org.w3c.dom.Document)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 JAXBContext (javax.xml.bind.JAXBContext)5