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);
}
}
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));
}
}
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);
}
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);
}
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);
}
Aggregations