import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
/**
*
* @author billy.johnson
*/
public class Archivos {
public File readFile(File a, TipoArchivo tipoLectura, TipoArchivo tipoSalida) {
JSONObject o;
if (null == tipoLectura) {
o = null;
} else {
switch (tipoLectura) {
case CSV:
o = readCSV(a);
break;
case XML:
o = readXML(a);
break;
case JSON:
o = readJSON(a);
break;
default:
o = null;
break;
}
}
File f;
if (null == tipoSalida) {
f = null;
} else {
switch (tipoSalida) {
case CSV:
if (o == null) {
f = null;
} else {
f = writeCSV(a.getName()
.substring(0, a.getName().lastIndexOf(".")) + ".csv",
(String[]) o.get("columns"),
(List<String[]>) o.get("rows"));
}
break;
case XML:
f = writeXML(a.getName()
.substring(0, a.getName().lastIndexOf(".")) + ".xml",
o);
break;
case JSON:
f = writeJSON(a.getName()
.substring(0, a.getName().lastIndexOf(".")) + ".xml",
o);
break;
default:
f = null;
break;
}
}
return f;
}
/**
* Lee un archivo CSV y devuelve un Map con 2 valores
* <ul>
* <li><b>columns</b> String [] con los nombres de las columnas</li>
* <li><b>rows</b> List<String []> con los registros del archivo</li>
*
* </ul>
*
* @param a
* @return
*/
public JSONObject readCSV(File a) {
Map<String, Object> m = new HashMap();
try {
try (BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(a), "UTF8"))) {
String line;
boolean primeraFila = true;
List<String[]> rows = new ArrayList();
while ((line = in.readLine()) != null) {
if (primeraFila) {
m.put("columns", line.split(";"));
primeraFila = false;
} else {
rows.add(line.split(";"));
}
}
m.put("rows", rows);
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
return new JSONObject(m);
}
/**
* Lee un archivo en formato JSON
*
* @param a
* @return
*/
public JSONObject readJSON(File a) {
String body = "";
try {
try (BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(a), "UTF8"))) {
String line;
while ((line = in.readLine()) != null) {
body += line;
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
JSONObject o = null;
if (!body.isEmpty()) {
try {
o = new JSONObject(body);
} catch (JSONException ex) {
ex.printStackTrace(System.out);
}
}
return o;
}
public JSONObject readXML(File a) {
String body = "";
try {
try (BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(a), "UTF8"))) {
String line;
while ((line = in.readLine()) != null) {
body += line;
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
JSONObject o = null;
if (!body.isEmpty()) {
try {
// o = new JSONObject(body);
o = XML.toJSONObject(body);
} catch (JSONException ex) {
ex.printStackTrace(System.out);
}
}
return o;
}
public File writeCSV(String newFile, String[] columns, List<String[]> rows) {
File f = null;
FileWriter fichero = null;
PrintWriter pw;
try {
fichero = new FileWriter(newFile);
pw = new PrintWriter(fichero);
for (int i = 0; i < columns.length; i++) {
pw.print(columns[i] + (i + 1 < columns.length ? ";" : "\n"));
}
rows.forEach(r -> {
for (int i = 0; i < r.length; i++) {
pw.print(r[i] + (i + 1 < r.length ? ";" : "\n"));
}
});
f = new File(newFile);
} catch (IOException e) {
e.printStackTrace(System.out);
} finally {
try {
if (null != fichero) {
fichero.close();
}
} catch (IOException e2) {
e2.printStackTrace(System.out);
}
}
return f;
}
public File writeJSON(String newFile, JSONObject o) {
File f = null;
FileWriter fichero = null;
PrintWriter pw;
try {
fichero = new FileWriter(newFile);
pw = new PrintWriter(fichero);
pw.println(o);
f = new File(newFile);
} catch (IOException e) {
e.printStackTrace(System.out);
} finally {
try {
if (null != fichero) {
fichero.close();
}
} catch (IOException e2) {
e2.printStackTrace(System.out);
}
}
return f;
}
public File writeXML(String newFile, JSONObject o) {
String root = "";
File f = null;
for (String k : o.keySet()) {
root = k;
}
FileWriter fichero = null;
PrintWriter pw;
try {
fichero = new FileWriter(newFile);
pw = new PrintWriter(fichero);
pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?><" + root + ">");
pw.println(XML.toString(o));
pw.println("</" + root + ">");
f = new File(newFile);
} catch (IOException e) {
e.printStackTrace(System.out);
} finally {
try {
if (null != fichero) {
fichero.close();
}
} catch (IOException e2) {
e2.printStackTrace(System.out);
}
}
return f;
}
}