Example usage for javax.xml.stream XMLStreamWriter writeStartDocument

List of usage examples for javax.xml.stream XMLStreamWriter writeStartDocument

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeStartDocument.

Prototype

public void writeStartDocument() throws XMLStreamException;

Source Link

Document

Write the XML Declaration.

Usage

From source file:org.wso2.carbon.registry.synchronization.operation.CheckInCommand.java

private void restoreFromFileSystem(Registry registry, UserInputCallback callback)
        throws SynchronizationException {
    sentCount = 0;/*from  www  .  j av  a  2 s .c o 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);/*ww  w. jav  a  2 s. com*/

    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   www  . j  a  v  a2s .c o 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: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 .  j a v a2s.  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);
    }/*www  . j  a va 2s.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:tds.itemscoringengine.ItemScoreRequest.java

@Override
public void writeXML(XMLStreamWriter out) throws XMLStreamException {

    out.writeStartDocument();
    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());
    }/*from   w w w. java2 s . co m*/
    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();
}