Search in sources :

Example 21 with XMLEventReader

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

the class AbstractDom4jImporterExporterTest method testDom4jRoundTripWithComment.

@Test
public void testDom4jRoundTripWithComment() throws Exception {
    final TestDom4jImporter importer = new TestDom4jImporter();
    final TestDom4jExporter exporter = new TestDom4jExporter();
    exporter.setXmlUtilities(new XmlUtilitiesImpl());
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    final InputStream resource = this.getClass().getResourceAsStream("/org/apereo/portal/io/xml/crn/pilot-lo.fragment-layout.xml");
    final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(resource);
    final Tuple<String, Element> result = importer.unmarshal(new StAXSource(xmlEventReader));
    assertNotNull(result);
    final StringWriter writer = new StringWriter();
    exporter.marshal(result, new StreamResult(writer));
    final String marshalResult = writer.toString();
    assertNotNull(marshalResult);
    XMLUnit.setIgnoreWhitespace(true);
    try {
        Diff d = new Diff(new InputStreamReader(this.getClass().getResourceAsStream("/org/apereo/portal/io/xml/crn/pilot-lo.fragment-layout.xml")), new StringReader(marshalResult));
        assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
    } catch (Exception e) {
        throw new XmlTestException("Failed to assert similar between marshall output and expected XML", marshalResult, e);
    } catch (Error e) {
        throw new XmlTestException("Failed to assert similar between marshall output and expected XML", marshalResult, e);
    }
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) InputStreamReader(java.io.InputStreamReader) Diff(org.custommonkey.xmlunit.Diff) InputStream(java.io.InputStream) Element(org.dom4j.Element) StAXSource(javax.xml.transform.stax.StAXSource) XmlTestException(org.apereo.portal.io.xml.XmlTestException) XmlUtilitiesImpl(org.apereo.portal.xml.XmlUtilitiesImpl) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XmlTestException(org.apereo.portal.io.xml.XmlTestException) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 22 with XMLEventReader

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

the class LayoutPortalDataTypeTest method testPostProcessSinglePortalDataKey_detectFragmentLayout.

@Test
public void testPostProcessSinglePortalDataKey_detectFragmentLayout() throws Exception {
    LayoutPortalDataType lpdt = new LayoutPortalDataType();
    XMLEventReader fragmentLayoutReader = FAC.createXMLEventReader(new StringReader(FRAGMENT_LAYOUT_NODE));
    String[] fragmentSystemIds = new String[] { "foo.fragment-layout", "foo.fragment-layout.xml" };
    for (String sysId : fragmentSystemIds) {
        Set<PortalDataKey> keys = lpdt.postProcessPortalDataKey(sysId, REGULAR_LAYOUT_DATA_KEY, fragmentLayoutReader);
        assertEquals("postProcessPortalDataKey() returned the wrong number of results.  Expected 1, was " + keys.size(), keys.size(), 1);
        assertTrue("postProcessPortalDataKey() failed to detect a fragment layout for systemId=" + sysId, keys.contains(FRAGMENT_LAYOUT_DATA_KEY));
    }
    XMLEventReader regularLayoutReader = FAC.createXMLEventReader(new StringReader(REGULAR_LAYOUT_NODE));
    String[] regularSystemIds = new String[] { "foo.layout", "foo.layout.xml" };
    for (String sysId : regularSystemIds) {
        Set<PortalDataKey> keys = lpdt.postProcessPortalDataKey(sysId, REGULAR_LAYOUT_DATA_KEY, regularLayoutReader);
        assertEquals("postProcessPortalDataKey() returned the wrong number of results.  Expected 1, was " + keys.size(), keys.size(), 1);
        assertTrue("postProcessPortalDataKey() failed to detect a fragment layout for systemId=" + sysId, keys.contains(REGULAR_LAYOUT_DATA_KEY));
    }
}
Also used : StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) PortalDataKey(org.apereo.portal.io.xml.PortalDataKey) Test(org.junit.Test)

Example 23 with XMLEventReader

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

the class BufferedXMLEventReaderTest method testBufferSomeEvents.

@Test
public void testBufferSomeEvents() 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, 10);
    int eventCount = 0;
    while (reader.hasNext()) {
        reader.nextEvent();
        eventCount++;
    }
    assertEquals(122, eventCount);
    reader.reset();
    while (reader.hasNext()) {
        reader.nextEvent();
        eventCount++;
    }
    assertEquals(132, eventCount);
}
Also used : InputStream(java.io.InputStream) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 24 with XMLEventReader

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

the class BufferedXMLEventReaderTest method testBufferAllEvents.

@Test
public void testBufferAllEvents() 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, -1);
    final XMLEvent firstEvent = reader.peek();
    int eventCount = 0;
    while (reader.hasNext()) {
        reader.nextEvent();
        eventCount++;
    }
    assertEquals(122, eventCount);
    reader.reset();
    final XMLEvent firstEventAgain = reader.peek();
    assertEquals(firstEvent, firstEventAgain);
    while (reader.hasNext()) {
        reader.nextEvent();
        eventCount++;
    }
    assertEquals(244, eventCount);
}
Also used : InputStream(java.io.InputStream) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 25 with XMLEventReader

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

the class StaticDocumentComponent 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 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder documentBuilder;
    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    final InputStream documentStream;
    try {
        documentStream = this.document.getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    final Document document;
    try {
        document = documentBuilder.parse(documentStream);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(documentStream);
    }
    final XMLInputFactory inputFactory = XMLInputFactory.newFactory();
    final DOMSource source = new DOMSource(document);
    final XMLEventReader streamReader;
    try {
        streamReader = inputFactory.createXMLEventReader(source);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
    return new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(streamReader);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XMLStreamException(javax.xml.stream.XMLStreamException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XMLEventReader(javax.xml.stream.XMLEventReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)62 XMLInputFactory (javax.xml.stream.XMLInputFactory)30 XMLEvent (javax.xml.stream.events.XMLEvent)30 XMLStreamException (javax.xml.stream.XMLStreamException)21 StringReader (java.io.StringReader)18 InputStream (java.io.InputStream)15 Test (org.junit.Test)14 StAXSource (javax.xml.transform.stax.StAXSource)13 StartElement (javax.xml.stream.events.StartElement)10 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)6 Unmarshaller (javax.xml.bind.Unmarshaller)6 Attribute (javax.xml.stream.events.Attribute)6 Document (org.w3c.dom.Document)6 ArrayList (java.util.ArrayList)5 JAXBContext (javax.xml.bind.JAXBContext)5 Diff (org.custommonkey.xmlunit.Diff)5 InputSource (org.xml.sax.InputSource)5 JAXBException (javax.xml.bind.JAXBException)4 DOMSource (javax.xml.transform.dom.DOMSource)4