Example usage for javax.xml.stream XMLEventReader close

List of usage examples for javax.xml.stream XMLEventReader close

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventReader close.

Prototype

public void close() throws XMLStreamException;

Source Link

Document

Frees any resources associated with this Reader.

Usage

From source file:org.talend.repository.json.util.JSONUtil.java

public static String changeJsonToXml(String jsonPath) {
    Project project = ProjectManager.getInstance().getCurrentProject();
    IProject fsProject = null;/*from  ww  w.j a va2  s.c o m*/
    try {
        fsProject = ResourceUtils.getProject(project);
    } catch (PersistenceException e2) {
        ExceptionHandler.process(e2);
    }
    if (fsProject == null) {
        return jsonPath;
    }
    String temPath = fsProject.getLocationURI().getPath() + File.separator + "temp" + File.separator
            + "jsonwizard" + File.separator;

    ConvertJSONString convertJSON = new ConvertJSONString();
    de.odysseus.staxon.json.JsonXMLConfig jsonConfig = new de.odysseus.staxon.json.JsonXMLConfigBuilder()
            .multiplePI(false).build();
    de.odysseus.staxon.json.JsonXMLInputFactory jsonXMLInputFactory = new de.odysseus.staxon.json.JsonXMLInputFactory(
            jsonConfig);
    javax.xml.stream.XMLOutputFactory xmlOutputFactory = javax.xml.stream.XMLOutputFactory.newInstance();

    java.io.ByteArrayOutputStream outStream = new java.io.ByteArrayOutputStream();
    InputStream inStream = null;
    File file = new File(jsonPath);

    // String filename = file.getName().replaceAll("\\.", "_");
    // filename = "tempTest";
    boolean isFromUrl = false;
    boolean illegalURL = false;
    InputStream input = null;

    if (file.exists()) {
        if (file.isDirectory()) {
            return "";
        }
        try {
            input = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            ExceptionHandler.process(e);
        }
    } else {
        isFromUrl = true;
        try {
            input = new URL(jsonPath).openStream();
        } catch (MalformedURLException e) {
            illegalURL = true;
        } catch (IOException e) {
            illegalURL = true;
        }
        if (illegalURL) {
            return "";
        }
    }

    try {
        String jsonStr = IOUtils.toString(input);

        convertJSON.setJsonString(jsonStr);

        convertJSON.generate();
        jsonStr = convertJSON.getJsonString4XML();
        inStream = new ByteArrayInputStream(jsonStr.getBytes());
        javax.xml.stream.XMLEventReader xmlEventReader = jsonXMLInputFactory.createXMLEventReader(inStream);
        javax.xml.stream.XMLEventWriter xmLEventWriter = xmlOutputFactory.createXMLEventWriter(outStream);
        xmLEventWriter.add(xmlEventReader);
        String xmlStr = outStream.toString();

        File xmlFolder = new File(temPath);
        if (!xmlFolder.exists()) {
            xmlFolder.mkdirs();
        }
        temPath = temPath + TMP_JSON_FILE;
        FileWriter writer = new FileWriter(temPath);
        writer.write(xmlStr);
        writer.flush();
        writer.close();

        xmLEventWriter.close();
        xmlEventReader.close();
        if (isFromUrl) {
            tempJSONXsdPath = temPath;
        }
    } catch (java.lang.Exception e) {
        ExceptionHandler.process(e);
    } finally {
        try {
            outStream.close();
            if (inStream != null) {
                inStream.close();
            }
        } catch (IOException e) {
            ExceptionHandler.process(e);
        }

    }
    return temPath;
}

From source file:org.xmlsh.commands.internal.xml2json.java

public int run(List<XValue> args) throws Exception {
    Options opts = new Options("p=print", SerializeOpts.getOptionDefs());

    opts.parse(args);//from  w w w . j  a v  a2s  .  c om

    bIndent = opts.hasOpt("p");

    args = opts.getRemainingArgs();

    OutputPort stdout = getStdout();

    InputPort inp = args.isEmpty() ? getStdin() : getInput(args.get(0));

    SerializeOpts serializeOpts = getSerializeOpts(opts);
    XMLEventReader reader = inp.asXMLEventReader(serializeOpts);

    // Override the text encoding to UTF-8 - JSON is *always* USTF8
    mSerializeOpts = serializeOpts.clone();
    serializeOpts.setOutputTextEncoding(kENCODING_UTF_8);
    PrintWriter writer = stdout.asPrintWriter(serializeOpts);

    parse(reader, writer, false);
    writer.flush();
    writer.close();

    // Consume input or we can get a Piped Close
    while (reader.hasNext())
        reader.nextEvent();

    reader.close();
    inp.release();

    return 0;

}