Java Code Examples for javax.xml.stream.XMLEventWriter#close()
The following examples show how to use
javax.xml.stream.XMLEventWriter#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JAXBEncoderDecoderTest.java From cxf with Apache License 2.0 | 6 votes |
@Test public void testMarshallWithFormQualifiedElement() throws Exception { ObjectWithQualifiedElementElement testObject = new ObjectWithQualifiedElementElement(); testObject.setString1("twine"); testObject.setString2("cord"); QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName()); MessagePartInfo part = new MessagePartInfo(elName, null); part.setElement(true); part.setElementQName(elName); StringWriter stringWriter = new StringWriter(); XMLOutputFactory opFactory = XMLOutputFactory.newInstance(); opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); XMLEventWriter writer = opFactory.createXMLEventWriter(stringWriter); JAXBEncoderDecoder.marshall(context.createMarshaller(), testObject, part, writer); writer.flush(); writer.close(); String xmlResult = stringWriter.toString(); // the following is a bit of a crock, but, to tell the truth, this test case most exists // so that it could be examined inside the debugger to see how JAXB works. assertTrue(xmlResult.contains(":string2>cord</ns")); }
Example 2
Source File: XMLStreamReaderHandle.java From java-client-api with Apache License 2.0 | 6 votes |
@Override public void write(OutputStream out) throws IOException { try { XMLInputFactory inputFactory = getFactory(); if (inputFactory == null) { throw new MarkLogicInternalException("Failed to make StAX input factory"); } // TODO: rework to copy straight from stream reader to stream writer XMLEventReader reader = inputFactory.createXMLEventReader(content); XMLOutputFactory outputFactory = XMLOutputFactory.newFactory(); XMLEventWriter writer = outputFactory.createXMLEventWriter(out, "UTF-8"); writer.add(reader); writer.flush(); writer.close(); content.close(); } catch (XMLStreamException e) { logger.error("Failed to parse StAX events from input stream",e); throw new MarkLogicInternalException(e); } }
Example 3
Source File: EmployeeRepository.java From JavaMainRepo with Apache License 2.0 | 6 votes |
public void save(ArrayList<Employee> employees) throws FileNotFoundException, XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME)); XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); StartElement configStartElement = eventFactory.createStartElement("", "", "content"); eventWriter.add(configStartElement); eventWriter.add(end); for (XML_Parsable employee : employees) { StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.EMPLOYEE); eventWriter.add(sElement); eventWriter.add(end); employee.encodeToXml(eventWriter); EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.EMPLOYEE); eventWriter.add(eElement); eventWriter.add(end); } eventWriter.add(eventFactory.createEndElement("", "", "content")); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close(); }
Example 4
Source File: XMLEventReaderHandle.java From java-client-api with Apache License 2.0 | 6 votes |
@Override public void write(OutputStream out) throws IOException { try { XMLOutputFactory factory = XMLOutputFactory.newFactory(); XMLEventWriter writer = factory.createXMLEventWriter(out, "UTF-8"); writer.add(content); writer.flush(); writer.close(); content.close(); } catch (XMLStreamException e) { logger.error("Failed to parse StAX events from input stream",e); throw new MarkLogicInternalException(e); } }
Example 5
Source File: XMLUtilities.java From olingo-odata4 with Apache License 2.0 | 6 votes |
@Override public InputStream deleteProperty(final InputStream src, final List<String> path) throws Exception { final XMLEventReader reader = getEventReader(src); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); final XMLEventReader changesReader = new XMLEventReaderWrapper(IOUtils.toInputStream( String.format("<%s m:null=\"true\" />", path.get(path.size() - 1)), Constants.ENCODING)); writer.add(changesReader); changesReader.close(); writer.add(reader); reader.close(); IOUtils.closeQuietly(src); writer.flush(); writer.close(); return new ByteArrayInputStream(bos.toByteArray()); }
Example 6
Source File: XmlOutputStreamFilter.java From KeePassJava2 with Apache License 2.0 | 5 votes |
public XmlOutputStreamFilter(final OutputStream outputStream, final XmlEventTransformer eventTransformer) throws IOException { super(); pipedInputStream = new PipedInputStream(this); Callable<Boolean> output = new Callable<Boolean>() { public Boolean call() { try { XMLEventReader eventReader = new com.fasterxml.aalto.stax.InputFactoryImpl() .createXMLEventReader(pipedInputStream); XMLEventWriter eventWriter = new com.fasterxml.aalto.stax.OutputFactoryImpl() .createXMLEventWriter(outputStream); XMLEvent event = null; while (eventReader.hasNext()) { event = eventReader.nextEvent(); event = eventTransformer.transform(event); eventWriter.add(event); eventWriter.flush(); } eventReader.close(); eventWriter.flush(); eventWriter.close(); outputStream.flush(); outputStream.close(); } catch (XMLStreamException | IOException e) { throw new IllegalStateException(e); } return true; } }; future = Executors.newSingleThreadExecutor().submit(output); }
Example 7
Source File: EntityRepository.java From JavaMainRepo with Apache License 2.0 | 5 votes |
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename)); XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); StartElement configStartElement = eventFactory.createStartElement("", "", "content"); eventWriter.add(configStartElement); eventWriter.add(end); for (XML_Parsable entity : entities) { StartElement sElement = eventFactory.createStartElement("", "", this.entityTag); eventWriter.add(sElement); eventWriter.add(end); entity.encodeToXml(eventWriter); EndElement eElement = eventFactory.createEndElement("", "", this.entityTag); eventWriter.add(eElement); eventWriter.add(end); } eventWriter.add(eventFactory.createEndElement("", "", "content")); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close(); }
Example 8
Source File: AnimalRepository.java From JavaMainRepo with Apache License 2.0 | 5 votes |
public void save(ArrayList<Animal> animals) throws FileNotFoundException, XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newFactory(); // Create XMLEventWriter XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME)); // Create an EventFactory XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); // Create and write Start Tag StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); // Create content open tag StartElement configStartElement = eventFactory.createStartElement("", "", "content"); eventWriter.add(configStartElement); eventWriter.add(end); for (XML_Parsable animal : animals) { StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.ANIMAL); eventWriter.add(sElement); eventWriter.add(end); animal.encodeToXml(eventWriter); EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.ANIMAL); eventWriter.add(eElement); eventWriter.add(end); } eventWriter.add(eventFactory.createEndElement("", "", "content")); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close(); }
Example 9
Source File: EntityRepository.java From JavaMainRepo with Apache License 2.0 | 5 votes |
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); // Create XMLEventWriter XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename)); // Create a EventFactory XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); // Create and write Start Tag StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); // Create content open tag StartElement configStartElement = eventFactory.createStartElement("", "", "content"); eventWriter.add(configStartElement); eventWriter.add(end); for (XML_Parsable entity : entities) { StartElement sElement = eventFactory.createStartElement("", "", this.entityTag); eventWriter.add(sElement); eventWriter.add(end); entity.encodeToXml(eventWriter); EndElement eElement = eventFactory.createEndElement("", "", this.entityTag); eventWriter.add(eElement); eventWriter.add(end); } eventWriter.add(eventFactory.createEndElement("", "", "content")); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close(); }
Example 10
Source File: EntityRepository.java From JavaMainRepo with Apache License 2.0 | 5 votes |
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); // Create XMLEventWriter XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename)); // Create a EventFactory XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); // Create and write Start Tag StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); // Create content open tag StartElement configStartElement = eventFactory.createStartElement("", "", "content"); eventWriter.add(configStartElement); eventWriter.add(end); for (XML_Parsable animal : entities) { StartElement sElement = eventFactory.createStartElement("", "", this.entityTag); eventWriter.add(sElement); eventWriter.add(end); animal.encodeToXml(eventWriter); EndElement eElement = eventFactory.createEndElement("", "", this.entityTag); eventWriter.add(eElement); eventWriter.add(end); } eventWriter.add(eventFactory.createEndElement("", "", "content")); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close(); }
Example 11
Source File: StaxXMLWriter.java From journaldev with MIT License | 5 votes |
public void writeXML(String fileName, String rootElement, Map<String, String> elementsMap){ XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); try { // XMLEventWriter xmlEventWriter = xmlOutputFactory // .createXMLEventWriter(new FileOutputStream(fileName), "UTF-8"); //For Debugging - below code to print XML to Console XMLEventWriter xmlEventWriter = xmlOutputFactory.createXMLEventWriter(System.out); XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); StartDocument startDocument = eventFactory.createStartDocument(); xmlEventWriter.add(startDocument); xmlEventWriter.add(end); StartElement configStartElement = eventFactory.createStartElement("", "", rootElement); xmlEventWriter.add(configStartElement); xmlEventWriter.add(end); // Write the element nodes Set<String> elementNodes = elementsMap.keySet(); for(String key : elementNodes){ createNode(xmlEventWriter, key, elementsMap.get(key)); } xmlEventWriter.add(eventFactory.createEndElement("", "", rootElement)); xmlEventWriter.add(end); xmlEventWriter.add(eventFactory.createEndDocument()); xmlEventWriter.close(); } catch ( XMLStreamException e) { e.printStackTrace(); } }
Example 12
Source File: EntityRepository.java From JavaMainRepo with Apache License 2.0 | 5 votes |
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); // Create XMLEventWriter XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename)); // Create a EventFactory XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); // Create and write Start Tag StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); // Create content open tag StartElement configStartElement = eventFactory.createStartElement("", "", "content"); eventWriter.add(configStartElement); eventWriter.add(end); for (XML_Parsable animal : entities) { StartElement sElement = eventFactory.createStartElement("", "", this.entityTag); eventWriter.add(sElement); eventWriter.add(end); animal.encodeToXml(eventWriter); EndElement eElement = eventFactory.createEndElement("", "", this.entityTag); eventWriter.add(eElement); eventWriter.add(end); } eventWriter.add(eventFactory.createEndElement("", "", "content")); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close(); }
Example 13
Source File: XMLUtilities.java From olingo-odata4 with Apache License 2.0 | 5 votes |
public InputStream addAtomInlinecount(final InputStream feed, final int count) throws Exception { final XMLEventReader reader = getEventReader(feed); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final XMLEventWriter writer = getEventWriter(bos); try { final XMLElement feedElement = extractElement(reader, writer, Collections.<String> singletonList("feed"), 0, 1, 1).getValue(); writer.add(feedElement.getStart()); addAtomElement(IOUtils.toInputStream(String.format("<m:count>%d</m:count>", count), Constants.ENCODING), writer); writer.add(feedElement.getContentReader()); writer.add(feedElement.getEnd()); while (reader.hasNext()) { writer.add(reader.nextEvent()); } } finally { writer.flush(); writer.close(); reader.close(); IOUtils.closeQuietly(feed); } return new ByteArrayInputStream(bos.toByteArray()); }
Example 14
Source File: AnimalRepository.java From JavaMainRepo with Apache License 2.0 | 5 votes |
public void save(ArrayList<Animal> animals) throws FileNotFoundException, XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); // Create XMLEventWriter XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME)); // Create a EventFactory XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); // Create and write Start Tag StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); // Create content open tag StartElement configStartElement = eventFactory.createStartElement("", "", "content"); eventWriter.add(configStartElement); eventWriter.add(end); for (XML_Parsable animal : animals) { StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.ANIMAL); eventWriter.add(sElement); eventWriter.add(end); animal.encodeToXml(eventWriter); EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.ANIMAL); eventWriter.add(eElement); eventWriter.add(end); } eventWriter.add(eventFactory.createEndElement("", "", "content")); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close(); }
Example 15
Source File: EmployeeRepository.java From JavaMainRepo with Apache License 2.0 | 5 votes |
public void save(ArrayList<Employee> employees) throws FileNotFoundException, XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME)); XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); StartElement configStartElement = eventFactory.createStartElement("", "", "content"); eventWriter.add(configStartElement); eventWriter.add(end); for (XML_Parsable employee : employees) { StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.EMPLOYEE); eventWriter.add(sElement); eventWriter.add(end); employee.encodeToXml(eventWriter); EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.EMPLOYEE); eventWriter.add(eElement); eventWriter.add(end); } eventWriter.add(eventFactory.createEndElement("", "", "content")); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close(); }
Example 16
Source File: EntityRepository.java From JavaMainRepo with Apache License 2.0 | 5 votes |
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename)); XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent end = eventFactory.createDTD("\n"); StartDocument startDocument = eventFactory.createStartDocument(); eventWriter.add(startDocument); StartElement configStartElement = eventFactory.createStartElement("", "", "content"); eventWriter.add(configStartElement); eventWriter.add(end); for (XML_Parsable entity : entities) { StartElement sElement = eventFactory.createStartElement("", "", this.entityTag); eventWriter.add(sElement); eventWriter.add(end); entity.encodeToXml(eventWriter); EndElement eElement = eventFactory.createEndElement("", "", this.entityTag); eventWriter.add(eElement); eventWriter.add(end); } eventWriter.add(eventFactory.createEndElement("", "", "content")); eventWriter.add(eventFactory.createEndDocument()); eventWriter.close(); }
Example 17
Source File: ExclusionFiles.java From cloud-opensource-java with Apache License 2.0 | 5 votes |
/** Writes {@code linkageErrors} as exclusion rules into {@code outputFile}. */ static void write(Path outputFile, Multimap<SymbolProblem, ClassFile> linkageErrors) throws IOException, XMLStreamException, TransformerException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XMLEventWriter writer = null; try { writer = XMLOutputFactory.newInstance().createXMLEventWriter(buffer); writer.add(eventFactory.createStartDocument()); writer.add(eventFactory.createStartElement(LINKAGE_CHECKER_FILTER_TAG, null, null)); for (SymbolProblem symbolProblem : linkageErrors.keySet()) { for (ClassFile classFile : linkageErrors.get(symbolProblem)) { writeXmlEvents(writer, symbolProblem, classFile); } } writer.add(eventFactory.createEndElement(LINKAGE_CHECKER_FILTER_TAG, null)); writer.add(eventFactory.createEndDocument()); } finally { if (writer != null) { writer.close(); } } try (OutputStream outputStream = Files.newOutputStream(outputFile)) { insertIndent(new ByteArrayInputStream(buffer.toByteArray()), outputStream); } }
Example 18
Source File: XMLUtilities.java From olingo-odata4 with Apache License 2.0 | 4 votes |
@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 { 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()); }
Example 19
Source File: XMLUtilities.java From olingo-odata4 with Apache License 2.0 | 4 votes |
@Override public InputStream addEditLink( final InputStream content, final String title, final String href) throws Exception { final ByteArrayOutputStream copy = new ByteArrayOutputStream(); IOUtils.copy(content, copy); IOUtils.closeQuietly(content); XMLEventReader reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEventWriter writer = getEventWriter(bos); final String editLinkElement = String.format("<link rel=\"edit\" title=\"%s\" href=\"%s\" />", title, href); try { // check edit link existence extractElement(reader, writer, Collections.<String> singletonList(Constants.get(ConstantKey.LINK)), Collections.<Map.Entry<String, String>> singletonList( new AbstractMap.SimpleEntry<String, String>("rel", "edit")), false, 0, -1, -1); addAtomElement(IOUtils.toInputStream(editLinkElement, Constants.ENCODING), writer); writer.add(reader); } catch (Exception e) { reader.close(); reader = getEventReader(new ByteArrayInputStream(copy.toByteArray())); bos = new ByteArrayOutputStream(); writer = getEventWriter(bos); final XMLElement entryElement = extractElement(reader, writer, Collections.<String> singletonList("entry"), 0, 1, 1).getValue(); writer.add(entryElement.getStart()); addAtomElement(IOUtils.toInputStream(editLinkElement, Constants.ENCODING), writer); writer.add(entryElement.getContentReader()); writer.add(entryElement.getEnd()); writer.add(reader); writer.flush(); writer.close(); } finally { reader.close(); } return new ByteArrayInputStream(bos.toByteArray()); }
Example 20
Source File: ArtifactsXmlAbsoluteUrlRemover.java From nexus-repository-p2 with Eclipse Public License 1.0 | 4 votes |
private TempBlob transformXmlMetadata(final TempBlob artifact, final Repository repository, final String file, final String extension, final XmlStreamTransformer transformer) throws IOException { Path tempFile = createTempFile("", ".xml"); // This is required in the case that the input stream is a jar to allow us to extract a single file Path artifactsTempFile = createTempFile("", ".xml"); try { try (InputStream xmlIn = xmlInputStream(artifact, file + "." + "xml", extension, artifactsTempFile); OutputStream xmlOut = xmlOutputStream(file + "." + "xml", extension, tempFile)) { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(SUPPORT_DTD, false); inputFactory.setProperty(IS_SUPPORTING_EXTERNAL_ENTITIES, false); XMLOutputFactory outputFactory = XMLOutputFactory.newFactory(); XMLEventReader reader = null; XMLEventWriter writer = null; //try-with-resources will be better here, but XMLEventReader and XMLEventWriter are not AutoCloseable try { reader = inputFactory.createXMLEventReader(xmlIn); writer = outputFactory.createXMLEventWriter(xmlOut); transformer.transform(reader, writer); writer.flush(); } finally { if (reader != null) { reader.close(); } if (writer != null) { writer.close(); } } } catch (XMLStreamException ex) { log.error("Failed to rewrite metadata for file with extension {} and blob {} with reason: {} ", ex, artifact.getBlob().getId(), ex); return artifact; } return convertFileToTempBlob(tempFile, repository); } finally { delete(tempFile); delete(artifactsTempFile); } }