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