Example usage for javax.xml.stream XMLEventFactory createEndElement

List of usage examples for javax.xml.stream XMLEventFactory createEndElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventFactory createEndElement.

Prototype

public abstract EndElement createEndElement(String prefix, String namespaceUri, String localName);

Source Link

Document

Create a new EndElement

Usage

From source file:org.apache.olingo.fit.utils.XMLUtilities.java

@Override
protected InputStream replaceLink(final InputStream toBeChanged, final String linkName,
        final InputStream replacement) throws Exception {
    final XMLEventReader reader = getEventReader(toBeChanged);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLEventWriter writer = getEventWriter(bos);

    final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent newLine = eventFactory.createSpace("\n");

    try {/*from   w w w  . ja  v a  2s  . c o  m*/
        final XMLElement linkElement = extractElement(reader, writer,
                Collections.<String>singletonList(Constants.get(ConstantKey.LINK)),
                Collections.<Map.Entry<String, String>>singletonList(
                        new SimpleEntry<String, String>("title", linkName)),
                false, 0, -1, -1).getValue();
        writer.add(linkElement.getStart());

        // ------------------------------------------
        // write inline ...
        // ------------------------------------------
        writer.add(newLine);
        writer.add(eventFactory.createStartElement("m", null, "inline"));

        addAtomElement(replacement, writer);

        writer.add(eventFactory.createEndElement("m", null, "inline"));
        writer.add(newLine);
        // ------------------------------------------

        writer.add(linkElement.getEnd());

        writer.add(reader);
        writer.flush();
        writer.close();
    } finally {
        reader.close();
        IOUtils.closeQuietly(toBeChanged);
    }

    return new ByteArrayInputStream(bos.toByteArray());
}

From source file:org.dkpro.core.io.xces.XcesBasicXmlWriter.java

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
    OutputStream docOS = null;/*from w w w  .  j  a  va2  s  .  c o  m*/
    try {
        docOS = getOutputStream(aJCas, filenameSuffix);
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter xmlEventWriter = new IndentingXMLEventWriter(
                xmlOutputFactory.createXMLEventWriter(docOS));
        JAXBContext context = JAXBContext.newInstance(XcesBodyBasic.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        XMLEventFactory xmlef = XMLEventFactory.newInstance();
        xmlEventWriter.add(xmlef.createStartDocument());
        // Begin cesDoc
        xmlEventWriter.add(xmlef.createStartElement("", "", "cesDoc"));
        // Begin and End cesHeader
        xmlEventWriter.add(xmlef.createStartElement("", "", "cesHeader"));
        xmlEventWriter.add(xmlef.createEndElement("", "", "cesHeader"));

        // Begin text and body
        xmlEventWriter.add(xmlef.createStartElement("", "", "text"));
        // xmlEventWriter.add(xmlef.createStartElement("", "", "body"));

        // Begin body of all the paragraphs            
        Collection<Paragraph> parasInCas = JCasUtil.select(aJCas, Paragraph.class);
        XcesBodyBasic xb = convertToXcesBasicPara(parasInCas);
        marshaller.marshal(new JAXBElement<XcesBodyBasic>(new QName("body"), XcesBodyBasic.class, xb),
                xmlEventWriter);
        // End body of all the paragraphs
        // xmlEventWriter.add(xmlef.createEndElement("", "", "body"));
        xmlEventWriter.add(xmlef.createEndElement("", "", "text"));
        xmlEventWriter.add(xmlef.createEndElement("", "", "cesDoc"));
        xmlEventWriter.add(xmlef.createEndDocument());
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    } finally {
        closeQuietly(docOS);
    }

}

From source file:org.dkpro.core.io.xces.XcesXmlWriter.java

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
    OutputStream docOS = null;//from  ww  w.j a  va 2 s . c om
    try {
        docOS = getOutputStream(aJCas, filenameSuffix);
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter xmlEventWriter = new IndentingXMLEventWriter(
                xmlOutputFactory.createXMLEventWriter(docOS));
        JAXBContext context = JAXBContext.newInstance(XcesBody.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        XMLEventFactory xmlef = XMLEventFactory.newInstance();
        xmlEventWriter.add(xmlef.createStartDocument());
        // Begin cesDoc
        xmlEventWriter.add(xmlef.createStartElement("", "", "cesDoc"));
        // Begin and End cesHeader
        xmlEventWriter.add(xmlef.createStartElement("", "", "cesHeader"));
        xmlEventWriter.add(xmlef.createEndElement("", "", "cesHeader"));

        // Begin text and body
        xmlEventWriter.add(xmlef.createStartElement("", "", "text"));
        // xmlEventWriter.add(xmlef.createStartElement("", "", "body"));

        // Begin body of all the paragraphs            
        Collection<Paragraph> parasInCas = JCasUtil.select(aJCas, Paragraph.class);
        XcesBody xb = convertToXcesPara(parasInCas);
        marshaller.marshal(new JAXBElement<XcesBody>(new QName("body"), XcesBody.class, xb), xmlEventWriter);
        // End body of all the paragraphs
        // xmlEventWriter.add(xmlef.createEndElement("", "", "body"));
        xmlEventWriter.add(xmlef.createEndElement("", "", "text"));
        xmlEventWriter.add(xmlef.createEndElement("", "", "cesDoc"));
        xmlEventWriter.add(xmlef.createEndDocument());
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    } finally {
        closeQuietly(docOS);
    }

}

From source file:org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilderTests.java

@Test
public void testConfiguration() throws Exception {
    Map<String, String> rootElementAttributes = new HashMap<>();
    rootElementAttributes.put("baz", "quix");

    StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>().name("fooWriter")
            .marshaller(marshaller).encoding("UTF-16").footerCallback(writer -> {
                XMLEventFactory factory = XMLEventFactory.newInstance();
                try {
                    writer.add(factory.createEndElement("ns", "http://www.springframework.org/test", "group"));
                } catch (XMLStreamException e) {
                    throw new RuntimeException(e);
                }/*from w w w .  j a  v a  2s . c o m*/
            }).headerCallback(writer -> {
                XMLEventFactory factory = XMLEventFactory.newInstance();
                try {
                    writer.add(
                            factory.createStartElement("ns", "http://www.springframework.org/test", "group"));
                } catch (XMLStreamException e) {
                    throw new RuntimeException(e);
                }
            }).resource(this.resource).rootTagName("foobarred").rootElementAttributes(rootElementAttributes)
            .saveState(false).version("1.1").build();

    staxEventItemWriter.afterPropertiesSet();

    ExecutionContext executionContext = new ExecutionContext();
    staxEventItemWriter.open(executionContext);

    staxEventItemWriter.write(this.items);

    staxEventItemWriter.update(executionContext);
    staxEventItemWriter.close();

    assertEquals(FULL_OUTPUT, getOutputFileContent("UTF-16"));
    assertEquals(0, executionContext.size());
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

/**
 * Item is written to the output file only after flush.
 *//*  ww w  . j a  v a  2s .c  om*/
@Test
public void testWriteWithHeader() throws Exception {

    writer.setHeaderCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "header"));
                writer.add(factory.createEndElement("", "", "header"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.open(executionContext);
    writer.write(items);
    String content = getOutputFileContent();
    assertTrue("Wrong content: " + content, content.contains(("<header/>")));
    assertTrue("Wrong content: " + content, content.contains(TEST_STRING));
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

/**
 * Open method writes the root tag, close method adds corresponding end tag.
 *///from   w w w  .j a  v  a 2s  .co  m
@Test
public void testOpenAndClose() throws Exception {
    writer.setHeaderCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "header"));
                writer.add(factory.createEndElement("", "", "header"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.setFooterCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "footer"));
                writer.add(factory.createEndElement("", "", "footer"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.setRootTagName("testroot");
    writer.setRootElementAttributes(Collections.<String, String>singletonMap("attribute", "value"));
    writer.open(executionContext);
    writer.close();
    String content = getOutputFileContent();

    assertTrue(content.contains("<testroot attribute=\"value\">"));
    assertTrue(content.contains("<header/>"));
    assertTrue(content.contains("<footer/>"));
    assertTrue(content.endsWith("</testroot>"));
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

/**
 * Resource is deleted when items have not been written and shouldDeleteIfEmpty flag is set.
 *///from   w  ww. ja  v  a  2  s.  com
@Test
public void testDeleteIfEmptyNoRecordsWrittenHeaderAndFooter() throws Exception {
    writer.setShouldDeleteIfEmpty(true);
    writer.setHeaderCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "header"));
                writer.add(factory.createEndElement("", "", "header"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.setFooterCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "footer"));
                writer.add(factory.createEndElement("", "", "footer"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.open(executionContext);
    writer.close();
    assertFalse("file should be deleted" + resource, resource.getFile().exists());
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

/**
 * Resource is not deleted when items have been written and shouldDeleteIfEmpty flag is set (restart after delete).
 *///ww w  .  ja v  a 2  s .co m
@Test
public void testDeleteIfEmptyNoRecordsWrittenHeaderAndFooterRestartAfterDelete() throws Exception {
    writer.setShouldDeleteIfEmpty(true);
    writer.setHeaderCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "header"));
                writer.add(factory.createEndElement("", "", "header"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.setFooterCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "footer"));
                writer.add(factory.createEndElement("", "", "footer"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.open(executionContext);
    writer.update(executionContext);
    writer.close();
    assertFalse("file should be deleted" + resource, resource.getFile().exists());
    writer.open(executionContext);
    writer.write(items);
    writer.update(executionContext);
    writer.close();
    String content = getOutputFileContent();
    assertTrue("Wrong content: " + content, content.contains(TEST_STRING));
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

private void initWriterForSimpleCallbackTests() throws Exception {
    writer = createItemWriter();/*from   w  w w.j a  v  a 2 s .c  om*/
    writer.setHeaderCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("ns", "http://www.springframework.org/test", "group"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }
        }

    });
    writer.setFooterCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createEndElement("ns", "http://www.springframework.org/test", "group"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.setRootTagName("{http://www.springframework.org/test}ns:testroot");
    writer.afterPropertiesSet();
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriterTests.java

private void initWriterForComplexCallbackTests() throws Exception {
    writer = createItemWriter();//from   w  ww  .  java 2s  . c  o m
    writer.setHeaderCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "preHeader"));
                writer.add(factory.createCharacters("PRE-HEADER"));
                writer.add(factory.createEndElement("", "", "preHeader"));
                writer.add(factory.createStartElement("ns", "http://www.springframework.org/test", "group"));
                writer.add(factory.createStartElement("", "", "subGroup"));
                writer.add(factory.createStartElement("", "", "postHeader"));
                writer.add(factory.createCharacters("POST-HEADER"));
                writer.add(factory.createEndElement("", "", "postHeader"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }
        }

    });
    writer.setFooterCallback(new StaxWriterCallback() {

        @Override
        public void write(XMLEventWriter writer) throws IOException {
            XMLEventFactory factory = XMLEventFactory.newInstance();
            try {
                writer.add(factory.createStartElement("", "", "preFooter"));
                writer.add(factory.createCharacters("PRE-FOOTER"));
                writer.add(factory.createEndElement("", "", "preFooter"));
                writer.add(factory.createEndElement("", "", "subGroup"));
                writer.add(factory.createEndElement("ns", "http://www.springframework.org/test", "group"));
                writer.add(factory.createStartElement("", "", "postFooter"));
                writer.add(factory.createCharacters("POST-FOOTER"));
                writer.add(factory.createEndElement("", "", "postFooter"));
            } catch (XMLStreamException e) {
                throw new RuntimeException(e);
            }

        }

    });
    writer.setRootTagName("{http://www.springframework.org/test}ns:testroot");
    writer.afterPropertiesSet();
}