/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.isi.karma.imp.json;


import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.isi.karma.imp.Import;
import edu.isi.karma.rep.Worksheet;
import edu.isi.karma.rep.Workspace;
import edu.isi.karma.rep.metadata.WorksheetProperties.Property;
import edu.isi.karma.rep.metadata.WorksheetProperties.SourceTypes;
import edu.isi.karma.util.FileUtil;
import edu.isi.karma.webserver.KarmaException;


/**
 *
 * @author mielvandersande
 */
public class XMLImport extends Import {

    Import jsonImport;
    File jsonFile;
    private static Logger logger = LoggerFactory.getLogger(XMLImport.class);

    public XMLImport(File xmlFile, String worksheetName, 
    		Workspace workspace, String encoding, int maxNumLines,
    		JSONArray columnsJson) {
        super(worksheetName, workspace, encoding);

        try {
            String fileContents = FileUtil.readFileContentsToString(xmlFile, encoding);

            // Converting the XML to JSON
            JSONObject json = XML.toJSONObject(fileContents);
            jsonFile = new File("tmp.json");
            PrintWriter pw = new PrintWriter(jsonFile);
            pw.println(json.toString(4));
            pw.close();
            jsonImport = new JsonImport(jsonFile, this.getFactory(), this.getWorksheet(), workspace, maxNumLines, columnsJson);
        } catch (JSONException ex) {
            logger.error("Error in populating the worksheet with XML", ex);
        } catch (IOException ex) {
            logger.error("Error in reading the XML file", ex);
        }
    }


    @Override
    public Worksheet generateWorksheet() throws JSONException, IOException, KarmaException, ClassNotFoundException {
        jsonImport.generateWorksheet();
        getWorksheet().getMetadataContainer().getWorksheetProperties().setPropertyValue(Property.sourceType, SourceTypes.XML.toString());
        if (jsonFile != null)
        	jsonFile.delete();
        return jsonImport.getWorksheet();
    }

}

@@ -642,16 +642,52 @@
 leUtil;%0A
+import edu.isi.karma.util.JSONUtil;%0A
 import e
@@ -1235,50 +1235,8 @@
 );%0A%0A
-            // Converting the XML to JSON%0A
     
@@ -1258,16 +1258,19 @@
 ect json
+Obj
  = XML.t
@@ -1312,124 +1312,49 @@
     
-jsonFile = new File(%22tmp.json%22);%0A            PrintWriter pw = new PrintWriter(jsonFile);%0A            pw.printl
+Object json = JSONUtil.createJso
 n(json
+Obj
 .toS
@@ -1363,37 +1363,12 @@
 ing(
-4
 ));%0A
-            pw.close();%0A
     
@@ -1407,22 +1407,17 @@
 ort(json
-File, 
+,
 this.get
@@ -1474,21 +1474,8 @@
 ines
-, columnsJson
 );%0A 

Fix issue with Xml Import.