Search in sources :

Example 1 with IndentingXMLEventWriter

use of org.apereo.portal.xml.stream.IndentingXMLEventWriter in project uPortal by Jasig.

the class XmlUtilitiesImpl method serializeXMLEvents.

@Override
public String serializeXMLEvents(List<XMLEvent> xmlEvents, boolean isHtml) {
    final XMLOutputFactory outputFactory;
    if (isHtml) {
        outputFactory = this.getHtmlOutputFactory();
    } else {
        outputFactory = this.getXmlOutputFactory();
    }
    final StringWriter writer = new StringWriter();
    final XMLEventWriter xmlEventWriter;
    try {
        xmlEventWriter = new IndentingXMLEventWriter(outputFactory.createXMLEventWriter(writer));
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XMLEventWriter", e);
    }
    try {
        for (final XMLEvent bufferedEvent : xmlEvents) {
            xmlEventWriter.add(bufferedEvent);
        }
        xmlEventWriter.flush();
        xmlEventWriter.close();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to write XMLEvents to XMLEventWriter", e);
    }
    return writer.toString();
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventWriter(javax.xml.stream.XMLEventWriter) IndentingXMLEventWriter(org.apereo.portal.xml.stream.IndentingXMLEventWriter) IndentingXMLEventWriter(org.apereo.portal.xml.stream.IndentingXMLEventWriter) XMLEvent(javax.xml.stream.events.XMLEvent)

Aggregations

StringWriter (java.io.StringWriter)1 XMLEventWriter (javax.xml.stream.XMLEventWriter)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 IndentingXMLEventWriter (org.apereo.portal.xml.stream.IndentingXMLEventWriter)1