Search in sources :

Example 46 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project spring-framework by spring-projects.

the class StaxSourceTests method eventReaderSourceToStreamResult.

@Test
public void eventReaderSourceToStreamResult() throws Exception {
    XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
    StaxSource source = new StaxSource(eventReader);
    assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
    assertNull("StreamReader returned", source.getXMLStreamReader());
    StringWriter writer = new StringWriter();
    transformer.transform(source, new StreamResult(writer));
    assertThat("Invalid result", writer.toString(), isSimilarTo(XML));
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) Test(org.junit.Test)

Example 47 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project spring-framework by spring-projects.

the class StaxSourceTests method eventReaderSourceToDOMResult.

@Test
public void eventReaderSourceToDOMResult() throws Exception {
    XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
    StaxSource source = new StaxSource(eventReader);
    assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
    assertNull("StreamReader returned", source.getXMLStreamReader());
    Document expected = documentBuilder.parse(new InputSource(new StringReader(XML)));
    Document result = documentBuilder.newDocument();
    transformer.transform(source, new DOMResult(result));
    assertThat("Invalid result", result, isSimilarTo(expected));
}
Also used : InputSource(org.xml.sax.InputSource) DOMResult(javax.xml.transform.dom.DOMResult) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 48 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project spring-framework by spring-projects.

the class AbstractUnmarshallerTests method unmarshalJaxp14StaxSourceXmlEventReader.

@Test
public void unmarshalJaxp14StaxSourceXmlEventReader() throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
    StAXSource source = new StAXSource(eventReader);
    Object flights = unmarshaller.unmarshal(source);
    testFlights(flights);
}
Also used : StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 49 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project spring-framework by spring-projects.

the class AbstractUnmarshallerTests method unmarshalStaxSourceXmlEventReader.

@Test
public void unmarshalStaxSourceXmlEventReader() throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
    Source source = StaxUtils.createStaxSource(eventReader);
    Object flights = unmarshaller.unmarshal(source);
    testFlights(flights);
}
Also used : StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.Test)

Example 50 with XMLEventReader

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

the class CachingStAXPipelineComponentTest method testCacheHit.

@Test
public void testCacheHit() {
    final MockHttpServletRequest mockReq = new MockHttpServletRequest();
    final MockHttpServletResponse mockRes = new MockHttpServletResponse();
    final CacheKey cacheKey = CacheKey.build("testCacheKey");
    final CachedEventReader<XMLEvent> eventReader = new CachedEventReader<XMLEvent>(Collections.EMPTY_LIST, Collections.EMPTY_MAP);
    final Element cacheElement = new Element(cacheKey, eventReader);
    final Ehcache cache = createMock(Ehcache.class);
    final StAXPipelineComponent targetComponent = createMock(StAXPipelineComponent.class);
    final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
    expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
    expect(targetComponent.getCacheKey(mockReq, mockRes)).andReturn(cacheKey);
    expect(cache.get(cacheKey)).andReturn(cacheElement);
    replay(cache, targetComponent, elementsProvider);
    final CachingStAXPipelineComponent cachingComponent = new CachingStAXPipelineComponent();
    cachingComponent.setCache(cache);
    cachingComponent.setWrappedComponent(targetComponent);
    cachingComponent.setResourcesElementsProvider(elementsProvider);
    final PipelineEventReader<XMLEventReader, XMLEvent> actualEventReader = cachingComponent.getEventReader(mockReq, mockRes);
    Assert.assertNotNull(actualEventReader);
    Assert.assertNotNull(actualEventReader.getEventReader());
    Assert.assertFalse(actualEventReader.getEventReader().hasNext());
    verify(cache, targetComponent, elementsProvider);
}
Also used : StAXPipelineComponent(org.apereo.portal.rendering.StAXPipelineComponent) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Element(net.sf.ehcache.Element) ResourcesElementsProvider(org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider) XMLEvent(javax.xml.stream.events.XMLEvent) Ehcache(net.sf.ehcache.Ehcache) XMLEventReader(javax.xml.stream.XMLEventReader) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) CacheKey(org.apereo.portal.utils.cache.CacheKey) 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