List of usage examples for javax.xml.stream XMLStreamWriter flush
public void flush() throws XMLStreamException;
From source file:org.wso2.ei.dataservice.integration.test.samples.EventingSampleTestCase.java
private void updateAxis2_ClientXML() throws Exception { String axis2_client_path = CarbonUtils.getCarbonHome() + File.separator + "conf" + File.separator + "axis2" + File.separator + "axis2_client.xml"; String mail_transport_config = getResourceLocation() + File.separator + "resources" + File.separator + "mailTransport.xml"; FileOutputStream fileOutputStream = null; XMLStreamWriter writer = null; try {//from ww w . java 2s . co m OMElement axis2_client_xml = AXIOMUtil.stringToOM(FileManager.readFile(axis2_client_path)); axis2_client_xml.addChild(AXIOMUtil.stringToOM(FileManager.readFile(mail_transport_config))); axis2_client_xml.build(); fileOutputStream = new FileOutputStream(axis2_client_path); writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream); axis2_client_xml.serialize(writer); } catch (Exception e) { throw new Exception("axis2_client.xml update fails"); } finally { if (fileOutputStream != null) { fileOutputStream.close(); } if (writer != null) { writer.flush(); } } }
From source file:org.xmlsh.commands.internal.json2xml.java
public int run(List<XValue> args) throws Exception { Options opts = new Options(SerializeOpts.getOptionDefs()); opts.parse(args);//from w w w . j a va2s . co m args = opts.getRemainingArgs(); OutputPort stdout = getStdout(); mSerializeOpts = getSerializeOpts(opts); SerializeOpts inputOpts = mSerializeOpts.clone(); // JSON is always UTF8 inputOpts.setInputTextEncoding("UTF-8"); InputPort in = args.isEmpty() ? this.getStdin() : this.getInput(args.get(0)); Reader inr = new InputStreamReader(in.asInputStream(inputOpts), inputOpts.getInputTextEncoding()); ; JSONTokener tokenizer = new JSONTokener(inr); /* * Assume JSON file is wrapped by an Object */ JSONObject obj = new JSONObject(tokenizer); XMLStreamWriter sw = stdout.asXMLStreamWriter(mSerializeOpts); sw.writeStartDocument(); write(obj, sw); sw.writeEndDocument(); sw.flush(); sw.close(); inr.close(); return 0; }
From source file:org.xronos.orcc.analysis.XronosDynamicWeights.java
public void getMeanWeights(String outputPath) { if (getModelsimWeights()) { XMLOutputFactory factory = XMLOutputFactory.newInstance(); try {/*from w w w .ja v a 2 s .co m*/ XMLStreamWriter writer = factory.createXMLStreamWriter(new FileWriter( outputPath + File.separator + network.getSimpleName() + "_dynamicWeights.xml")); writer.writeStartDocument(); writer.writeStartElement("actors"); for (Actor actor : statistics.keySet()) { writer.writeStartElement("actor"); writer.writeAttribute("name", actor.getSimpleName().toLowerCase()); Map<Action, SummaryStatistics> actionWeight = statistics.get(actor); writer.writeStartElement("actions"); for (Action action : actionWeight.keySet()) { writer.writeStartElement("action"); writer.writeAttribute("name", action.getName().toLowerCase()); double min = Double.isNaN(actionWeight.get(action).getMin()) ? 0 : actionWeight.get(action).getMin(); double mean = Double.isNaN(actionWeight.get(action).getMean()) ? 0 : actionWeight.get(action).getMean(); double max = Double.isNaN(actionWeight.get(action).getMax()) ? 0 : actionWeight.get(action).getMax(); double variance = Double.isNaN(actionWeight.get(action).getVariance()) ? 0 : actionWeight.get(action).getVariance(); writer.writeAttribute("min", Double.toString(min)); writer.writeAttribute("mean", Double.toString(mean)); writer.writeAttribute("max", Double.toString(max)); writer.writeAttribute("variance", Double.toString(variance)); writer.writeEndElement(); } writer.writeEndElement(); writer.writeEndElement(); } writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); writer.close(); } catch (XMLStreamException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.xwiki.xar.XarPackage.java
/** * Write the package descriptor to the passed stream as XML. * // ww w. j a v a2s . com * @param stream the stream to the resulting XML file * @param encoding the encoding to use to write the descriptor * @throws XarException when failing to parse the descriptor * @throws IOException when failing to read the file */ public void write(OutputStream stream, String encoding) throws XarException, IOException { XMLStreamWriter writer; try { writer = XMLOutputFactory.newInstance().createXMLStreamWriter(stream, encoding); } catch (Exception e) { throw new XarException("Failed to create an instance of XML stream writer", e); } writer = new IndentingXMLStreamWriter(writer); try { writer.writeStartDocument(encoding, "1.0"); write(writer); writer.writeEndDocument(); writer.flush(); } catch (Exception e) { throw new XarException("Failed to write XML", e); } finally { try { writer.close(); } catch (XMLStreamException e) { throw new XarException("Failed to close XML writer", e); } } }
From source file:spypunk.tailthis.configuration.xml.ConfigurationXMLWriter.java
public void save(final Configuration configuration) throws Exception { FileUtils.forceMkdir(configurationFile.getParentFile()); final XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter xtw = null; try {/*www . j a va2 s. c o m*/ xtw = new IndentingXMLStreamWriter(xof.createXMLStreamWriter(new FileWriter(configurationFile))); xtw.writeStartElement(ConfigurationXMLConstants.TAILTHIS_ELEMENT_NAME); xtw.writeStartElement(ConfigurationXMLConstants.REMEMBER_LAST_OPENED_FILES_ELEMENT_NAME); xtw.writeCharacters(configuration.getRememberLastOpenedFiles().toString()); xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.LAST_FOLDER_ELEMENT_NAME); xtw.writeCharacters(configuration.getLastFolder().getAbsolutePath()); xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.LAST_OPENED_FILES_ELEMENT_NAME); for (final File lastOpenedFile : configuration.getLastOpenedFiles()) { xtw.writeStartElement(ConfigurationXMLConstants.LAST_OPENED_FILE_ELEMENT_NAME); xtw.writeCharacters(lastOpenedFile.getAbsolutePath()); xtw.writeEndElement(); } xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.UPDATE_PERIOD_ELEMENT_NAME); xtw.writeCharacters(configuration.getUpdatePeriod().toString()); xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.ALWAYS_ON_TOP_ELEMENT_NAME); xtw.writeCharacters(configuration.getAlwaysOnTop().toString()); xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.LOCALE_ELEMENT_NAME); xtw.writeCharacters(configuration.getLocale().getKey()); xtw.writeEndElement(); xtw.writeStartElement(ConfigurationXMLConstants.HIGHLIGHTINGS_ELEMENT_NAME); for (final Highlighting highlighting : configuration.getHighlightings()) { xtw.writeStartElement(ConfigurationXMLConstants.HIGHLIGHTING_ELEMENT_NAME); xtw.writeAttribute(ConfigurationXMLConstants.BACKGROUND_COLOR_ATTRIBUTE_NAME, highlighting.getBackgroundColor().getKey()); xtw.writeAttribute(ConfigurationXMLConstants.FOREGROUND_COLOR_ATTRIBUTE_NAME, highlighting.getForegroundColor().getKey()); xtw.writeCharacters(highlighting.getPattern().pattern()); xtw.writeEndElement(); } xtw.writeEndElement(); xtw.writeEndElement(); xtw.flush(); } finally { if (xtw != null) { try { xtw.close(); } catch (final XMLStreamException e) { LOGGER.warn(e.getMessage(), e); } } } }