Search in sources :

Example 1 with XMLEventWriter

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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) XMLEventWriter(javax.xml.stream.XMLEventWriter) XMLEventReader(javax.xml.stream.XMLEventReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with XMLEventWriter

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));
}
Also used : StringWriter(java.io.StringWriter) XMLEventWriter(javax.xml.stream.XMLEventWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 3 with XMLEventWriter

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);
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLEventWriter(javax.xml.stream.XMLEventWriter)

Example 4 with XMLEventWriter

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));
}
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) SAXResult(javax.xml.transform.sax.SAXResult) DOMResult(javax.xml.transform.dom.DOMResult) Test(org.junit.Test)

Example 5 with XMLEventWriter

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;
}
Also used : Attribute(javax.xml.stream.events.Attribute) XMLEventFactory(javax.xml.stream.XMLEventFactory) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventWriter(javax.xml.stream.XMLEventWriter) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError)

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