added new CSV-interface
parent
518fa54ea3
commit
4425cc3670
|
@ -0,0 +1,44 @@
|
|||
package org.hwo.csv;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CSVContainer {
|
||||
|
||||
private ArrayList<CSVTable> tables;
|
||||
private CSVTable emptyTable;
|
||||
|
||||
public CSVContainer(){
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
tables = new ArrayList<>();
|
||||
}
|
||||
|
||||
public int getTableCount() {
|
||||
return this.tables.size();
|
||||
}
|
||||
|
||||
public CSVTable getTabel(int index) {
|
||||
return this.tables.get(index);
|
||||
}
|
||||
public void addTable(CSVTable table) {
|
||||
this.tables.add(table);
|
||||
}
|
||||
public void removeTable(CSVTable table) {
|
||||
this.tables.remove(table);
|
||||
}
|
||||
public void removeTable(int index) {
|
||||
this.tables.remove(index);
|
||||
}
|
||||
|
||||
public String getCSV() {
|
||||
String csv = new String();
|
||||
|
||||
for(CSVTable t : this.tables) {
|
||||
csv += t.getCSV();
|
||||
csv += CSVTableRow.NULL_ROW.getCSV();
|
||||
}
|
||||
return csv;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
package org.hwo.csv;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class CSVTable {
|
||||
|
||||
ArrayList<CSVTableRow> rows;
|
||||
|
||||
public CSVTable() {
|
||||
this.rows = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addRow(CSVTableRow row) {
|
||||
this.rows.add(row);
|
||||
}
|
||||
public CSVTableRow getRow(int index) {
|
||||
return this.rows.get(index);
|
||||
}
|
||||
|
||||
public int getRowCount() {
|
||||
return this.rows.size();
|
||||
}
|
||||
|
||||
public Object[] getColumn(int column) {
|
||||
ArrayList<Object> ret = new ArrayList<>();
|
||||
for(CSVTableRow r : this.rows) {
|
||||
ret.add(r.getValue(column));
|
||||
}
|
||||
return ret.toArray();
|
||||
}
|
||||
public int getColumnCount() {
|
||||
int count = 0;
|
||||
for(CSVTableRow r : this.rows) {
|
||||
int rc = r.getColumnCount();
|
||||
count = rc > count? rc : count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public String getCSV() {
|
||||
String csv = new String();
|
||||
for(CSVTableRow r : this.rows) {
|
||||
csv += r.getCSV();
|
||||
}
|
||||
return csv;
|
||||
}
|
||||
|
||||
public static CSVTable ParseCSV(String path) {
|
||||
CSVTable ret = new CSVTable();
|
||||
File f = new File(path);
|
||||
List<String> str = null;
|
||||
try {
|
||||
str = Files.readAllLines(f.toPath());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(str == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for(String s : str) {
|
||||
//parse line
|
||||
CSVTableRow row = new CSVTableRow();
|
||||
String[] tokens = s.split(",");
|
||||
for(String t : tokens) {
|
||||
if(t.contains("\"")) {
|
||||
row.addValue(t.substring(1,t.length() - 1));
|
||||
}
|
||||
else if(t.contains(".")) {
|
||||
row.addValue(Float.parseFloat(t));
|
||||
}
|
||||
else {
|
||||
row.addValue(Integer.parseInt(t));
|
||||
}
|
||||
}
|
||||
ret.addRow(row);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
package org.hwo.csv;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CSVTableRow {
|
||||
|
||||
public static CSVTableRow NULL_ROW = new CSVTableRow("");
|
||||
|
||||
private ArrayList<Object> values; //first value == rowTitle
|
||||
|
||||
public CSVTableRow() {
|
||||
init();
|
||||
}
|
||||
|
||||
public CSVTableRow(String title) {
|
||||
init();
|
||||
addValue(title);
|
||||
}
|
||||
|
||||
|
||||
private void init() {
|
||||
this.values = new ArrayList<>();
|
||||
}
|
||||
|
||||
//TODO add functionality for column detection (i.e. string column id, Object value)
|
||||
public void setValue(int column, Object value) {
|
||||
if(column < values.size()) {
|
||||
this.values.set(column, value);
|
||||
}
|
||||
}
|
||||
|
||||
public Object getValue(int column) {
|
||||
if(column >= this.values.size()) {
|
||||
return "";
|
||||
}
|
||||
return this.values.get(column);
|
||||
}
|
||||
public Object[] getValues() {
|
||||
return this.values.toArray();
|
||||
}
|
||||
|
||||
public int getColumnCount() {
|
||||
return this.values.size();
|
||||
}
|
||||
|
||||
public void addValue(Object value) {
|
||||
this.values.add(value);
|
||||
}
|
||||
|
||||
public void addData(Object[] data) {
|
||||
for(Object o : data) {
|
||||
this.values.add(o);
|
||||
}
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
Object n = this.values.get(0);
|
||||
if(n != null && n.getClass() == String.class) {
|
||||
return (String)n;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.values.clear();
|
||||
}
|
||||
|
||||
public String getCSV() {
|
||||
String ret = new String();
|
||||
for(int i = 0; i < values.size(); i++) {
|
||||
Object obj = values.get(i);
|
||||
if(obj != null) {
|
||||
if(obj.getClass() == String.class) {
|
||||
ret += formatString(obj.toString());
|
||||
}
|
||||
else {
|
||||
ret += obj.toString();
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret += formatString("null");
|
||||
}
|
||||
|
||||
if(i < this.values.size() -1)
|
||||
ret += ",";
|
||||
}
|
||||
ret += "\n";
|
||||
return ret;
|
||||
}
|
||||
|
||||
private String formatString(String str) {
|
||||
return "\"" + str + "\"";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue