diff --git a/CsvReader.java b/CsvReader.java new file mode 100644 index 0000000..ce46fd5 --- /dev/null +++ b/CsvReader.java @@ -0,0 +1,57 @@ +package a6; + +import java.io.File; +import java.io.FileReader; +import java.io.BufferedReader; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class CsvReader { + + private HashMap columns = new HashMap(); + private ArrayList values = new ArrayList(); + private ArrayList strings = new ArrayList(); + private int curr_line = 0; + + public boolean load(String filename) { + boolean ret = SysUtils.readStrings(filename, strings); + if (ret) { + if (nextLine()) { + for (int i = 0; i < values.size(); ++i) { + columns.put(values.get(i), i); + } + } + } + return ret; + } + + public boolean nextLine() { + if (curr_line >= strings.size()) { + return false; + } + values.clear(); + for (String val : strings.get(curr_line).split(",")) { + values.add(val); + } + ++curr_line; + return true; + } + + public XValue getValue(String key) { + if (!keyExists(key)) { + return new XValue(); + } + String val = values.get(columns.get(key)); + return new XValue(val + .replaceAll("\\n", "\n") + .replaceAll(",", ",") + ); + } + + public boolean keyExists(String key) { + return columns.containsKey(key); + } + +} diff --git a/MutableXObject.java b/MutableXObject.java new file mode 100644 index 0000000..a43d4cb --- /dev/null +++ b/MutableXObject.java @@ -0,0 +1,9 @@ +package a6; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class MutableXObject extends XObject { + +} diff --git a/SysUtils.java b/SysUtils.java index 994ea01..28707c6 100644 --- a/SysUtils.java +++ b/SysUtils.java @@ -1,5 +1,13 @@ package a6; +import java.io.File; +import java.io.FileReader; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.FileNotFoundException; + +import java.util.ArrayList; + import java.lang.reflect.Array; public class SysUtils { @@ -29,4 +37,25 @@ public class SysUtils { } } + public static boolean readStrings(String filename, ArrayList strings) { + File csvfile = new File(filename); + csvfile.setReadable(true); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(csvfile)); + String line; + try { + while ((line = br.readLine()) != null) { + + } + br.close(); + } catch (IOException e) { + return false; + } + } catch (FileNotFoundException e) { + return false; + } + return true; + } + } diff --git a/XObject.java b/XObject.java index 36e8984..97caa52 100644 --- a/XObject.java +++ b/XObject.java @@ -1,5 +1,61 @@ package a6; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + public class XObject { + private XValue xValue; + private ArrayList arrayValue; + private HashMap objectValue; + + public XValue asXValue() { + return xValue; + } + + public XObject get(int index) { + return null; + } + + public XObject get(String key) { + return null; + } + + public boolean hasKey(int index) { + return false; + } + + public boolean hasKey(String key) { + return false; + } + + public boolean readFromJsonFile(String filename) { + return false; + } + + public boolean readFromJsonString(String data) { + return false; + } + + public boolean readFromXmlFile(String filename) { + return false; + } + + public boolean readFromXmlString(String data) { + return false; + } + + public void readFromUrlQueryString(String query_string) { + + } + + public String toJsonStr() { + return ""; + } + + public String toUrlEncodeStr() { + return ""; + } + }