Search in sources :

Example 6 with XMLEventWriter

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

Example 7 with XMLEventWriter

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

the class XSLTComponentTest method serializeXMLEventReader.

protected String serializeXMLEventReader(XMLEventReader reader) {
    final StringWriter writer = new StringWriter();
    final XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", true);
    final XMLEventWriter xmlEventWriter;
    try {
        xmlEventWriter = outputFactory.createXMLEventWriter(writer);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XMLEventWriter", e);
    }
    try {
        xmlEventWriter.setDefaultNamespace("http://www.w3.org/1999/xhtml");
        xmlEventWriter.add(reader);
        xmlEventWriter.flush();
        xmlEventWriter.close();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to write events to Writer", 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)

Example 8 with XMLEventWriter

use of javax.xml.stream.XMLEventWriter in project bazel by bazelbuild.

the class XmlResourceValues method readContentsAsString.

/**
   * Reads the xml events as a string until finding a closing tag.
   *
   * @param eventReader The current xml stream.
   * @param startTag The name of the tag to close on.
   * @param namespacesCollector A builder for collecting namespaces.
   * @return A xml escaped string representation of the xml stream
   */
@Nullable
public static String readContentsAsString(XMLEventReader eventReader, QName startTag, Namespaces.Collector namespacesCollector) throws XMLStreamException {
    StringWriter contents = new StringWriter();
    XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(contents);
    while (!isEndTag(eventReader.peek(), startTag)) {
        XMLEvent xmlEvent = (XMLEvent) eventReader.next();
        if (xmlEvent.isStartElement()) {
            namespacesCollector.collectFrom(xmlEvent.asStartElement());
            writer.add(xmlEvent);
        } else {
            writer.add(xmlEvent);
        }
    }
    // Verify the end element.
    EndElement endElement = eventReader.nextEvent().asEndElement();
    Preconditions.checkArgument(endElement.getName().equals(startTag));
    return contents.toString();
}
Also used : StringWriter(java.io.StringWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) EndElement(javax.xml.stream.events.EndElement) XMLEvent(javax.xml.stream.events.XMLEvent) Nullable(javax.annotation.Nullable)

Example 9 with XMLEventWriter

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

the class AbstractMarshallerTests method marshalStaxResultEventWriter.

@Test
public void marshalStaxResultEventWriter() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    StringWriter writer = new StringWriter();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
    Result result = StaxUtils.createStaxResult(eventWriter);
    marshaller.marshal(flights, result);
    assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) StAXResult(javax.xml.transform.stax.StAXResult) DOMResult(javax.xml.transform.dom.DOMResult) Test(org.junit.Test)

Example 10 with XMLEventWriter

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

the class AbstractMarshallerTests method marshalJaxp14StaxResultEventWriter.

@Test
public void marshalJaxp14StaxResultEventWriter() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    StringWriter writer = new StringWriter();
    XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
    StAXResult result = new StAXResult(eventWriter);
    marshaller.marshal(flights, result);
    assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) Test(org.junit.Test)

Aggregations

XMLEventWriter (javax.xml.stream.XMLEventWriter)14 StringWriter (java.io.StringWriter)10 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)9 XMLEvent (javax.xml.stream.events.XMLEvent)5 Test (org.junit.Test)5 XMLStreamException (javax.xml.stream.XMLStreamException)4 XMLEventReader (javax.xml.stream.XMLEventReader)3 DOMResult (javax.xml.transform.dom.DOMResult)3 Result (javax.xml.transform.Result)2 StAXResult (javax.xml.transform.stax.StAXResult)2 StreamResult (javax.xml.transform.stream.StreamResult)2 IndentingXMLEventWriter (org.apereo.portal.xml.stream.IndentingXMLEventWriter)2 ImmutableList (com.google.common.collect.ImmutableList)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Nullable (javax.annotation.Nullable)1 FactoryConfigurationError (javax.xml.stream.FactoryConfigurationError)1 XMLEventFactory (javax.xml.stream.XMLEventFactory)1