use of javax.xml.stream.XMLEventWriter in project camel by apache.
the class StaxConverterTest method testEncodingXmlEventReader.
public void testEncodingXmlEventReader() throws Exception {
TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM.reset();
XMLEventReader reader = null;
XMLEventWriter writer = null;
ByteArrayOutputStream output = null;
try {
// enter text encoded with Latin1
reader = context.getTypeConverter().mandatoryConvertTo(XMLEventReader.class, TEST_XML_WITH_XML_HEADER_ISO_8859_1_AS_BYTE_ARRAY_STREAM);
output = new ByteArrayOutputStream();
// ensure UTF-8 encoding
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.toString());
writer = context.getTypeConverter().mandatoryConvertTo(XMLEventWriter.class, exchange, output);
while (reader.hasNext()) {
writer.add(reader.nextEvent());
}
} finally {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}
assertNotNull(output);
String result = new String(output.toByteArray(), UTF_8.name());
// normalize the auotation mark
if (result.indexOf('\'') > 0) {
result = result.replace('\'', '"');
}
boolean equals = TEST_XML_WITH_XML_HEADER.equals(result) || TEST_XML_WITH_XML_HEADER_ISO_8859_1.equals(result);
assertTrue("Should match header", equals);
}
use of javax.xml.stream.XMLEventWriter in project spring-framework by spring-projects.
the class StaxResultTests method eventWriterSource.
@Test
public void eventWriterSource() throws Exception {
StringWriter stringWriter = new StringWriter();
XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter);
Reader reader = new StringReader(XML);
Source source = new StreamSource(reader);
StaxResult result = new StaxResult(eventWriter);
assertEquals("Invalid eventWriter returned", eventWriter, result.getXMLEventWriter());
assertNull("StreamWriter returned", result.getXMLStreamWriter());
transformer.transform(source, result);
assertThat("Invalid result", stringWriter.toString(), isSimilarTo(XML));
}
use of javax.xml.stream.XMLEventWriter in project spring-framework by spring-projects.
the class StaxEventHandlerTests method createStaxHandler.
@Override
protected AbstractStaxHandler createStaxHandler(Result result) throws XMLStreamException {
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(result);
return new StaxEventHandler(eventWriter);
}
use of javax.xml.stream.XMLEventWriter in project spring-framework by spring-projects.
the class XStreamMarshallerTests method marshalStaxResultXMLEventWriter.
@Test
public void marshalStaxResultXMLEventWriter() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
StringWriter writer = new StringWriter();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
Result result = StaxUtils.createStaxResult(eventWriter);
marshaller.marshal(flight, result);
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
use of javax.xml.stream.XMLEventWriter in project bazel by bazelbuild.
the class AndroidManifestProcessor method writeManifestPackage.
/**
* Overwrite the package attribute of {@code <manifest>} in an AndroidManifest.xml file.
*
* @param manifest The input manifest.
* @param customPackage The package to write to the manifest.
* @param output The output manifest to generate.
* @return The output manifest if generated or the input manifest if no overwriting is required.
*/
/* TODO(apell): switch from custom xml parsing to Gradle merger with NO_PLACEHOLDER_REPLACEMENT
* set when android common is updated to version 2.5.0.
*/
public Path writeManifestPackage(Path manifest, String customPackage, Path output) {
if (Strings.isNullOrEmpty(customPackage)) {
return manifest;
}
try {
Files.createDirectories(output.getParent());
XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(Files.newInputStream(manifest), UTF_8.name());
XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(Files.newOutputStream(output), UTF_8.name());
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement() && event.asStartElement().getName().toString().equalsIgnoreCase("manifest")) {
StartElement element = event.asStartElement();
@SuppressWarnings("unchecked") Iterator<Attribute> attributes = element.getAttributes();
ImmutableList.Builder<Attribute> newAttributes = ImmutableList.builder();
while (attributes.hasNext()) {
Attribute attr = attributes.next();
if (attr.getName().toString().equalsIgnoreCase("package")) {
newAttributes.add(eventFactory.createAttribute("package", customPackage));
} else {
newAttributes.add(attr);
}
}
writer.add(eventFactory.createStartElement(element.getName(), newAttributes.build().iterator(), element.getNamespaces()));
} else {
writer.add(event);
}
}
writer.flush();
} catch (XMLStreamException | FactoryConfigurationError | IOException e) {
throw new RuntimeException(e);
}
return output;
}
Aggregations