List of usage examples for javax.xml.stream XMLStreamWriter writeEndDocument
public void writeEndDocument() throws XMLStreamException;
From source file:org.wso2.carbon.registry.synchronization.operation.CheckInCommand.java
private void restoreFromFileSystem(Registry registry, UserInputCallback callback) throws SynchronizationException { sentCount = 0;/*from ww w . j a v a2s . co m*/ // we are doing the check-in through a temp file. (so assumed enough spaces are there) File tempFile = null; boolean deleteTempFileFailed = false; XMLStreamWriter xmlWriter = null; Writer writer = null; try { try { tempFile = File.createTempFile(SynchronizationConstants.DUMP_META_FILE_NAME, SynchronizationConstants.META_FILE_EXTENSION); try { writer = new FileWriter(tempFile); // wrap the writer with an xml stream writer xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer); // prepare the dump xml xmlWriter.writeStartDocument(); createMetaElement(xmlWriter, workingLocation, checkInPath, callback); xmlWriter.writeEndDocument(); } finally { try { if (xmlWriter != null) { xmlWriter.close(); } } finally { if (writer != null) { writer.close(); } } } } catch (IOException e) { throw new SynchronizationException(MessageCode.ERROR_IN_CREATING_TEMP_FILE_FOR_DUMP, e); } catch (XMLStreamException e) { throw new SynchronizationException(MessageCode.ERROR_IN_CREATING_XML_STREAM_WRITER, e); } // do the restoring if the file system is actually updated if (updated) { try { Reader reader = null; try { reader = new FileReader(tempFile); registry.restore(checkInPath, reader); } finally { if (reader != null) { reader.close(); } } } catch (IOException e) { throw new SynchronizationException(MessageCode.ERROR_IN_READING_TEMP_FILE_OF_DUMP, e); } catch (RegistryException e) { throw new SynchronizationException(MessageCode.ERROR_IN_RESTORING, e, new String[] { "path: " + checkInPath, "registry url: " + registryUrl, "username: " + username }); } } } finally { if (tempFile != null) { // Our intention here is to delete the temporary file. We are not bothered whether // this operation fails. deleteTempFileFailed = !FileUtils.deleteQuietly(tempFile); } } if (deleteTempFileFailed) { throw new SynchronizationException(MessageCode.ERROR_IN_CLEANING_UP, new String[] { "file path: " + tempFile.getAbsolutePath() }); } if (cleanRegistry && registryUrl == null) { Utils.cleanEmbeddedRegistry(); } }
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 av a2s . c o 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 {/*ww w. j a v a 2 s. c om*/ 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. * /*from ww w . j a va2 s. c om*/ * @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:ro.kuberam.libs.java.ftclient.FTP.FTP.java
public StreamResult listResources(Object abstractConnection, String remoteResourcePath) throws Exception { long startTime = new Date().getTime(); boolean isDirectory = checkIsDirectory(remoteResourcePath); if (!isDirectory) { throw new Exception(ErrorMessages.err_FTC008); }//from ww w. java 2s . c o m FTPClient connection = (FTPClient) abstractConnection; if (!connection.isConnected()) { throw new Exception(ErrorMessages.err_FTC002); } List<Object> connectionObject = _checkResourcePath(connection, remoteResourcePath, "list-resources", isDirectory); System.out.println("resources: " + connectionObject.size()); FTPFile[] resources = (FTPFile[]) connectionObject.get(1); StringWriter writer = new StringWriter(); XMLStreamWriter xmlWriter = null; try { xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer); xmlWriter.setPrefix(modulePrefix, moduleNsUri); xmlWriter.writeStartDocument(); xmlWriter.writeStartElement(modulePrefix + ":resources-list"); xmlWriter.writeNamespace(modulePrefix, moduleNsUri); xmlWriter.writeAttribute("absolute-path", remoteResourcePath); for (FTPFile resource : resources) { _generateResourceElement(xmlWriter, resource, null, remoteResourcePath + resource.getName()); } xmlWriter.writeEndElement(); xmlWriter.writeEndDocument(); xmlWriter.close(); } catch (Exception ex) { throw new Exception(ex.getMessage()); } // FTPconnection.completePendingCommand(); StreamResult resultAsStreamResult = new StreamResult(writer); log.info("The FTP sub-module retrieved the list of resources in " + (new Date().getTime() - startTime) + " ms."); return resultAsStreamResult; }
From source file:ro.kuberam.libs.java.ftclient.FTP.FTP.java
public StreamResult getResourceMetadata(Object abstractConnection, String remoteResourcePath) throws Exception { long startTime = new Date().getTime(); FTPClient FTPconnection = (FTPClient) abstractConnection; if (!FTPconnection.isConnected()) { throw new Exception(ErrorMessages.err_FTC002); }/*from w ww . j av a2s . c o m*/ List<Object> FTPconnectionObject = _checkResourcePath(FTPconnection, remoteResourcePath, "get-resource-metadata", checkIsDirectory(remoteResourcePath)); FTPFile[] resources = (FTPFile[]) FTPconnectionObject.get(1); StringWriter writer = new StringWriter(); XMLStreamWriter xmlWriter = null; try { xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer); xmlWriter.setPrefix(modulePrefix, moduleNsUri); xmlWriter.writeStartDocument(); for (FTPFile resource : resources) { _generateResourceElement(xmlWriter, resource, null, remoteResourcePath); } xmlWriter.writeEndDocument(); xmlWriter.close(); } catch (Exception ex) { throw new Exception(ex.getMessage()); } // FTPconnection.completePendingCommand(); StreamResult resultAsStreamResult = new StreamResult(writer); log.info("The FTP sub-module retrieved the metadata for resource '" + remoteResourcePath + "' in " + (new Date().getTime() - startTime) + " ms."); return resultAsStreamResult; }
From source file:solidbase.core.DBVersion.java
/** * Dumps the current log in XML format to the given output stream, with the given character set. * * @param out The outputstream to which the xml will be written. * @param charSet The requested character set. *//* ww w. j ava2 s . c o m*/ protected void logToXML(OutputStream out, Charset charSet) { // This method does not care about staleness boolean spec11 = SPEC11.equals(this.effectiveSpec); try { Connection connection = this.database.getDefaultConnection(); Statement stat = connection.createStatement(); try { ResultSet result = stat.executeQuery("SELECT " + (spec11 ? "TYPE, " : "") + "SOURCE, TARGET, STATEMENT, STAMP, COMMAND, RESULT FROM " + this.logTableName + " ORDER BY STAMP"); XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter xml = xof.createXMLStreamWriter(new OutputStreamWriter(out, charSet)); xml.writeStartDocument("UTF-8", SPEC10); xml.writeStartElement("log"); while (result.next()) { int i = 1; xml.writeStartElement("record"); if (spec11) xml.writeAttribute("type", result.getString(i++)); xml.writeAttribute("source", StringUtils.defaultString(result.getString(i++))); xml.writeAttribute("target", result.getString(i++)); xml.writeAttribute("statement", String.valueOf(result.getInt(i++))); xml.writeAttribute("stamp", String.valueOf(result.getTimestamp(i++))); String sql = result.getString(i++); if (sql != null) { xml.writeStartElement("command"); xml.writeCharacters(sql); xml.writeEndElement(); } String res = result.getString(i++); if (res != null) { xml.writeStartElement("result"); xml.writeCharacters(res); xml.writeEndElement(); } xml.writeEndElement(); } xml.writeEndElement(); xml.writeEndDocument(); xml.close(); } finally { stat.close(); connection.commit(); } } catch (XMLStreamException e) { throw new SystemException(e); } catch (SQLException e) { throw new SystemException(e); } }
From source file:tds.itemscoringengine.ItemScoreRequest.java
@Override public void writeXML(XMLStreamWriter out) throws XMLStreamException { out.writeStartDocument();/* www. ja va2s .c om*/ out.writeStartElement("ItemScoreRequest"); String callBackUrl = getCallbackUrl(); if (!StringUtils.isEmpty(callBackUrl)) out.writeAttribute("callbackUrl", callBackUrl); out.writeStartElement("ResponseInfo"); out.writeAttribute("itemIdentifier", _responseInfo.getItemIdentifier()); out.writeAttribute("itemFormat", _responseInfo.getItemFormat()); out.writeStartElement("StudentResponse"); out.writeAttribute("encrypted", Boolean.toString(_responseInfo.isStudentResponseEncrypted())); out.writeCData(_responseInfo.getStudentResponse()); out.writeEndElement(); // </StudentResponse> out.writeStartElement("Rubric"); out.writeAttribute("type", _responseInfo.getContentType() == RubricContentType.ContentString ? "Data" : "Uri"); out.writeAttribute("cancache", Boolean.toString(_responseInfo.isCanCacheRubric())); out.writeAttribute("encrypted", Boolean.toString(_responseInfo.isRubricEncrypted())); if (_responseInfo.getRubric() != null) { out.writeCData(_responseInfo.getRubric().toString()); } out.writeEndElement(); // </Rubric> out.writeStartElement("ContextToken"); out.writeCData(_responseInfo.getContextToken().toString()); out.writeEndElement(); // </ContextToken> // <IncomingBindings> if (_responseInfo.getIncomingBindings() != null && _responseInfo.getIncomingBindings().size() > 0) { out.writeStartElement("IncomingBindings"); for (VarBinding varBinding : _responseInfo.getIncomingBindings()) { out.writeStartElement("Binding"); out.writeAttribute("name", varBinding.getName()); out.writeAttribute("type", varBinding.getType()); out.writeAttribute("value", varBinding.getValue()); out.writeEndElement(); } out.writeEndElement(); } // <OutgoingBindings> if (_responseInfo.getOutgoingBindings() != null && _responseInfo.getOutgoingBindings().size() > 0) { out.writeStartElement("OutgoingBindings"); for (VarBinding varBinding : _responseInfo.getOutgoingBindings()) { out.writeStartElement("Binding"); out.writeAttribute("name", varBinding.getName()); out.writeAttribute("type", varBinding.getType()); out.writeAttribute("value", varBinding.getValue()); out.writeEndElement(); } out.writeEndElement(); } out.writeEndElement(); // </ResponseInfo> out.writeEndElement(); // </ItemScoreRequest> out.writeEndDocument(); out.close(); }