List of usage examples for javax.xml.stream XMLStreamWriter close
public void close() throws XMLStreamException;
From source file:org.plasma.sdo.xml.StreamMarshaller.java
public void marshal(OutputStream stream) throws XMLStreamException, MarshallerException { String encoding = findEncoding(); XMLStreamWriter writer = null; if (encoding != null) writer = factory.createXMLStreamWriter(stream, encoding); else/*www .jav a 2s . c o m*/ writer = factory.createXMLStreamWriter(stream); if (isPrettyPrint()) writer = new IndentingXMLStreamWriter(writer); try { write(writer); } finally { writer.close(); } }
From source file:org.plasma.sdo.xml.StreamMarshaller.java
public void marshal(Writer outputWriter) throws XMLStreamException, MarshallerException { XMLStreamWriter writer = factory.createXMLStreamWriter(outputWriter); writer = new IndentingXMLStreamWriter(writer); try {/* ww w .jav a2 s. co m*/ write(writer); } finally { writer.close(); } }
From source file:org.plasma.sdo.xml.StreamMarshaller.java
public void marshal(Result outputResult) throws XMLStreamException, MarshallerException { XMLStreamWriter writer = factory.createXMLStreamWriter(outputResult); writer = new IndentingXMLStreamWriter(writer); try {/* w ww. ja v a2 s . c o m*/ write(writer); } finally { writer.close(); } }
From source file:org.qi4j.valueserialization.stax.StaxValueSerializer.java
@Override protected void onSerializationEnd(Object object, XMLStreamWriter output) throws Exception { output.writeEndDocument();// w w w . j a v a 2s. com output.flush(); output.close(); }
From source file:org.rhq.enterprise.server.sync.ExportingInputStream.java
private void exporterMain() { XMLStreamWriter wrt = null; OutputStream out = null;//w w w.j a v a2s.c o m try { XMLOutputFactory ofactory = XMLOutputFactory.newInstance(); ofactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true); try { out = exportOutput; if (zipOutput) { out = new GZIPOutputStream(out); } wrt = new IndentingXMLStreamWriter(ofactory.createXMLStreamWriter(out, "UTF-8")); //wrt = ofactory.createXMLStreamWriter(out, "UTF-8"); } catch (XMLStreamException e) { LOG.error("Failed to create the XML stream writer to output the export file to.", e); return; } exportPrologue(wrt); for (Synchronizer<?, ?> exp : synchronizers) { exportSingle(wrt, exp); } exportEpilogue(wrt); wrt.flush(); } catch (Exception e) { LOG.error("Error while exporting.", e); unexpectedExporterException = e; } finally { if (wrt != null) { try { wrt.close(); } catch (XMLStreamException e) { LOG.warn("Failed to close the exporter XML stream.", e); } } safeClose(out); } }
From source file:org.rhq.enterprise.server.sync.test.DeployedAgentPluginsValidatorTest.java
public void testCanExportAndImportState() throws Exception { final PluginManagerLocal pluginManager = context.mock(PluginManagerLocal.class); final DeployedAgentPluginsValidator validator = new DeployedAgentPluginsValidator(pluginManager); context.checking(new Expectations() { {/*from w ww . j av a 2s .c o m*/ oneOf(pluginManager).getInstalledPlugins(); will(returnValue(new ArrayList<Plugin>(getDeployedPlugins()))); } }); validator.initialize(null, null); StringWriter output = new StringWriter(); try { XMLOutputFactory ofactory = XMLOutputFactory.newInstance(); XMLStreamWriter wrt = ofactory.createXMLStreamWriter(output); //wrap the exported plugins in "something" so that we produce //a valid xml wrt.writeStartDocument(); wrt.writeStartElement("root"); validator.exportState(new ExportWriter(wrt)); wrt.writeEndDocument(); wrt.close(); StringReader input = new StringReader(output.toString()); try { XMLInputFactory ifactory = XMLInputFactory.newInstance(); XMLStreamReader rdr = ifactory.createXMLStreamReader(input); //push the reader to the start of the plugin elements //this is what is expected by the validators rdr.nextTag(); validator.initializeExportedStateValidation(new ExportReader(rdr)); rdr.close(); assertEquals(validator.getPluginsToValidate(), getDeployedPlugins()); } finally { input.close(); } } catch (Exception e) { LOG.error("Test failed. Output generated so far:\n" + output, e); throw e; } finally { output.close(); } }
From source file:org.rhq.plugins.hadoop.HadoopServerConfigurationDelegate.java
private static void updateFile(File configFile, Map<String, PropertySimple> allProps) throws IOException, InterruptedException, XMLStreamException { InputStream in = null;/*ww w . j ava 2 s . c o m*/ XMLStreamReader rdr = null; OutputStream out = null; XMLStreamWriter outWrt = null; try { Set<String> processedPropertyNames = new HashSet<String>(); in = new BufferedInputStream(new FileInputStream(configFile)); rdr = XML_INPUT_FACTORY.createXMLStreamReader(in); File tmpFile = File.createTempFile("hadoop-plugin", null); out = new FileOutputStream(tmpFile); outWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(out); ByteArrayOutputStream stash = new ByteArrayOutputStream(); XMLStreamWriter stashWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(stash); boolean outputActive = true; outWrt.writeStartDocument(); while (rdr.hasNext()) { int event = rdr.next(); XMLStreamWriter wrt = outputActive ? outWrt : stashWrt; switch (event) { case XMLStreamConstants.ATTRIBUTE: break; case XMLStreamConstants.CDATA: wrt.writeCData(rdr.getText()); break; case XMLStreamConstants.CHARACTERS: wrt.writeCharacters(rdr.getText()); break; case XMLStreamConstants.COMMENT: wrt.writeComment(rdr.getText()); break; case XMLStreamConstants.DTD: wrt.writeDTD(rdr.getText()); break; case XMLStreamConstants.END_DOCUMENT: wrt.writeEndDocument(); break; case XMLStreamConstants.END_ELEMENT: if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) { String encoding = rdr.getEncoding(); if (encoding == null) { encoding = "UTF-8"; } String propertyTagSoFar = Charset.forName(encoding) .decode(ByteBuffer.wrap(stash.toByteArray())).toString(); DetectedPropertyNameAndUpdatedTag propAndTag = updateProperty(propertyTagSoFar, allProps); //yes, we're intentionally circumventing the xml stream writer, because we already have the XML data we want to write. outWrt.flush(); out.write(propAndTag.updatedTag.getBytes("UTF-8")); processedPropertyNames.add(propAndTag.propertyName); //reset stuff stash.reset(); wrt = outWrt; outputActive = true; } else if (CONFIGURATION_TAG_NAME.equals(rdr.getName().getLocalPart())) { //now add the new props for (String prop : processedPropertyNames) { allProps.remove(prop); } for (Map.Entry<String, PropertySimple> e : allProps.entrySet()) { outWrt.writeStartElement(PROPERTY_TAG_NAME); outWrt.writeStartElement(NAME_TAG_NAME); outWrt.writeCharacters(e.getKey()); outWrt.writeEndElement(); outWrt.writeStartElement(VALUE_TAG_NAME); outWrt.writeCharacters(e.getValue().getStringValue()); outWrt.writeEndElement(); outWrt.writeEndElement(); } } wrt.writeEndElement(); break; case XMLStreamConstants.ENTITY_DECLARATION: //XXX could not find what to do with this break; case XMLStreamConstants.ENTITY_REFERENCE: wrt.writeEntityRef(rdr.getText()); break; case XMLStreamConstants.NAMESPACE: for (int i = 0; i < rdr.getNamespaceCount(); ++i) { wrt.writeNamespace(rdr.getNamespacePrefix(i), rdr.getNamespaceURI(i)); } break; case XMLStreamConstants.NOTATION_DECLARATION: //XXX could not find what to do with this break; case XMLStreamConstants.PROCESSING_INSTRUCTION: wrt.writeProcessingInstruction(rdr.getPITarget(), rdr.getPIData()); break; case XMLStreamConstants.SPACE: wrt.writeCharacters(rdr.getText()); break; case XMLStreamConstants.START_DOCUMENT: //this seems to be never called for some strange reason //wrt.writeStartDocument(); break; case XMLStreamConstants.START_ELEMENT: wrt.writeStartElement(rdr.getName().getPrefix(), rdr.getName().getLocalPart(), rdr.getName().getNamespaceURI()); for (int i = 0; i < rdr.getAttributeCount(); ++i) { wrt.writeAttribute(rdr.getAttributePrefix(i), rdr.getAttributeNamespace(i), rdr.getAttributeLocalName(i), rdr.getAttributeValue(i)); } if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) { wrt.writeCharacters(""); outputActive = false; } break; } } outWrt.flush(); out.flush(); out.close(); in.close(); //now copy the temp file in the place of the original one FileUtil.copyFile(tmpFile, configFile); } finally { rdr.close(); outWrt.flush(); outWrt.close(); try { in.close(); } finally { out.flush(); out.close(); } } }
From source file:org.slc.sli.modeling.tools.xsdgen.Uml2XsdWriter.java
public static final void writeSchema(final List<PsmDocument<Type>> documents, final ModelIndex model, final Uml2XsdPlugin plugin, final OutputStream outstream) { final XMLOutputFactory xof = XMLOutputFactory.newInstance(); try {/*from w w w . j a v a 2 s . c o m*/ final XMLStreamWriter xsw = new IndentingXMLStreamWriter(xof.createXMLStreamWriter(outstream, "UTF-8")); xsw.writeStartDocument("UTF-8", "1.0"); try { writeSchema(documents, model, plugin, xsw); } finally { xsw.writeEndDocument(); } xsw.flush(); // We close the stream writer knowing (from the documentation) that it will not close // the underlying output stream. xsw.close(); } catch (final XMLStreamException e) { throw new XsdGenRuntimeException(e); } }
From source file:org.slc.sli.modeling.xmi.comp.XmiMappingWriter.java
public static final void writeMappingDocument(final XmiComparison document, final OutputStream outstream) { final XMLOutputFactory xof = XMLOutputFactory.newInstance(); try {/* w ww .j a v a2s . c o m*/ final XMLStreamWriter xsw = new IndentingXMLStreamWriter(xof.createXMLStreamWriter(outstream, "UTF-8")); xsw.writeStartDocument("UTF-8", "1.0"); try { writeMappingDocument(document, xsw); } finally { xsw.writeEndDocument(); } xsw.flush(); xsw.close(); } catch (final XMLStreamException e) { throw new XmiCompRuntimeException(e); } }
From source file:org.staxcel.internal.SpreadSheetImpl.java
protected void flushDynamicTemplateWriter(TokenEnumerableTemplateWriter templateWriter, XMLOutputFactory xmlOutFactory) throws IOException { String token = templateWriter.getToken(); while (token != null) { XMLStreamWriter xmlWriter = null; try {// w ww .j a v a2 s . c om if (token.equals("worksheets.count")) { templateWriter.getOutputWriter().write(String.valueOf(this.worksheetList.size())); } else if (token.equals("worksheets.vector")) { xmlWriter = xmlOutFactory.createXMLStreamWriter(appXmlTemplateWriter.getOutputWriter()); xmlWriter.writeStartElement("vt:vector"); xmlWriter.writeAttribute("size", String.valueOf(this.worksheetList.size())); xmlWriter.writeAttribute("baseType", "lpstr"); for (int index = 0; index < this.worksheetList.size(); ++index) { xmlWriter.writeStartElement("vt:lpstr"); xmlWriter.writeCharacters(this.worksheetList.get(index).getName()); xmlWriter.writeEndElement(); } xmlWriter.writeEndElement(); } else if (token.equals("worksheet.relationships")) { xmlWriter = xmlOutFactory.createXMLStreamWriter(templateWriter.getOutputWriter()); for (int index = 0; index < worksheetList.size(); ++index) { WorksheetImpl worksheet = worksheetList.get(index); xmlWriter.writeStartElement("Relationship"); xmlWriter.writeAttribute("Id", "rId" + worksheet.getResourceId()); xmlWriter.writeAttribute("Type", OpenXMLNamespace.NS_OPENXML_OFFICE_DOC_2006_REL_WORKSHEET); xmlWriter.writeAttribute("Target", "worksheets/sheet" + worksheet.getWorksheetNumber() + ".xml"); xmlWriter.writeEndElement(); } } else if (token.equals("workbook.sheets")) { xmlWriter = xmlOutFactory.createXMLStreamWriter(templateWriter.getOutputWriter()); for (int index = 0; index < worksheetList.size(); ++index) { WorksheetImpl worksheet = worksheetList.get(index); xmlWriter.writeStartElement("sheet"); xmlWriter.writeAttribute("name", "Sheet" + worksheet.getWorksheetNumber()); xmlWriter.writeAttribute("sheetId", String.valueOf(worksheet.getWorksheetNumber())); xmlWriter.writeAttribute("r:id", "rId" + worksheet.getResourceId()); xmlWriter.writeEndElement(); } } else { // unknown. Invoke a protected method which can be // overridden by derived classes // to add new template variables and interpret them // accordingly. expandToken(token, templateWriter); } } catch (XMLStreamException ex) { throw new StaxcelException("Error when replacing " + token + " from template file", ex); } finally { if (xmlWriter != null) { try { xmlWriter.flush(); xmlWriter.close(); } catch (XMLStreamException ex2) { logger.error("Error when closing xmlstream", ex2); } } } token = templateWriter.nextElement(); } templateWriter.getOutputWriter().flush(); templateWriter.getOutputWriter().close(); }