.. | .. |
---|
13 | 13 | |
---|
14 | 14 | public class TimeflowFormat implements Import, Export |
---|
15 | 15 | { |
---|
16 | | - private static final String END_OF_SCHEMA="#TIMEFLOW\tend-metadata"; |
---|
17 | | - private static final String END_OF_METADATA="#TIMEFLOW\t====== End of Header. Data below is in tab-delimited format. ====="; |
---|
18 | | - public ActDB readFile(String fileName, PrintStream messages) throws Exception |
---|
19 | | - { |
---|
20 | | - return read(new File(fileName), messages); |
---|
21 | | - } |
---|
22 | | - |
---|
23 | | - public static ActDB read(File file, PrintStream out) throws Exception |
---|
24 | | - { |
---|
25 | | - String text=IO.read(file.getAbsolutePath()); |
---|
26 | | - DelimitedText quote=new DelimitedText('\t'); |
---|
27 | | - Iterator<String[]> lines=quote.read(text).iterator(); |
---|
28 | | - |
---|
29 | 16 | |
---|
30 | | - ActDB db=null; |
---|
31 | | - List<String> fieldNames=new ArrayList<String>(); |
---|
32 | | - List<Class> fieldTypes=new ArrayList<Class>(); |
---|
33 | | - List<Integer> fieldSizes=new ArrayList<Integer>(); |
---|
34 | | - String source="[unknown]", description=""; |
---|
35 | | - for (;;) |
---|
36 | | - { |
---|
37 | | - String[] t=lines.next(); |
---|
| 17 | + private static final String END_OF_SCHEMA = "#TIMEFLOW\tend-metadata"; |
---|
| 18 | + private static final String END_OF_METADATA = "#TIMEFLOW\t====== End of Header. Data below is in tab-delimited format. ====="; |
---|
38 | 19 | |
---|
39 | | - if (t[1].equals("field")) |
---|
40 | | - { |
---|
41 | | - fieldNames.add(t[2]); |
---|
42 | | - fieldTypes.add(FieldFormatCatalog.javaClass(t[3])); |
---|
43 | | - if (t.length>4) |
---|
44 | | - { |
---|
45 | | - fieldSizes.add(Integer.parseInt(t[4])); |
---|
46 | | - } |
---|
47 | | - else |
---|
48 | | - fieldSizes.add(-1); |
---|
49 | | - } |
---|
50 | | - else if (t[1].equals("source")) |
---|
51 | | - { |
---|
52 | | - source=t[2]; |
---|
53 | | - } |
---|
54 | | - else if (t[1].equals("description")) |
---|
55 | | - { |
---|
56 | | - description=t[2]; |
---|
57 | | - |
---|
58 | | - } |
---|
59 | | - else if (t[1].equals("end-metadata")) |
---|
60 | | - break; |
---|
61 | | - } |
---|
62 | | - db=new ArrayDB((String[])fieldNames.toArray(new String[0]), |
---|
63 | | - (Class[])fieldTypes.toArray(new Class[0]), source); |
---|
64 | | - db.setDescription(description); |
---|
65 | | - for (int i=0; i<fieldNames.size(); i++) |
---|
66 | | - if (fieldSizes.get(i)>0) |
---|
67 | | - db.getField(fieldNames.get(i)).setRecommendedSize(fieldSizes.get(i)); |
---|
68 | | - for (;;) |
---|
69 | | - { |
---|
70 | | - String[] t=lines.next(); |
---|
71 | | - if (t[1].startsWith("===")) |
---|
72 | | - break; |
---|
73 | | - if (t[1].equals("alias")) |
---|
74 | | - db.setAlias(db.getField(t[3]), t[2]); |
---|
75 | | - } |
---|
76 | | - |
---|
77 | | - // note: in some cases headers may be in a different order than in |
---|
78 | | - // metadata section, so we will read these. |
---|
79 | | - String[] headers=lines.next(); |
---|
80 | | - if (headers.length!=fieldNames.size()) |
---|
81 | | - throw new IllegalArgumentException("Different number of headers than fields!"); |
---|
82 | | - |
---|
83 | | - |
---|
84 | | - while (lines.hasNext()) |
---|
85 | | - { |
---|
86 | | - String[] t=lines.next(); |
---|
87 | | - Act a=db.createAct(); |
---|
88 | | - for (int i=0; i<t.length; i++) |
---|
89 | | - { |
---|
90 | | - Field f=db.getField(headers[i]); |
---|
91 | | - FieldFormat format=FieldFormatCatalog.getFormat(f.getType()); |
---|
92 | | - a.set(f, format.parse(t[i])); |
---|
93 | | - } |
---|
94 | | - } |
---|
95 | | - |
---|
96 | | - return db; |
---|
97 | | - } |
---|
98 | | - |
---|
99 | | - public static void write(ActList acts, BufferedWriter bw) throws IOException |
---|
100 | | - { |
---|
101 | | - ActDB db=acts.getDB(); |
---|
102 | | - |
---|
103 | | - PrintWriter out=new PrintWriter(bw); |
---|
104 | | - |
---|
105 | | - DelimitedText tab=new DelimitedText('\t'); |
---|
106 | | - |
---|
107 | | - // Write version |
---|
108 | | - out.println("#TIMEFLOW\tformat version\t1"); |
---|
109 | | - |
---|
110 | | - // Write source of data. |
---|
111 | | - out.println("#TIMEFLOW\tsource\t"+tab.write(db.getSource())); |
---|
112 | | - |
---|
113 | | - // Write description of data. |
---|
114 | | - out.println("#TIMEFLOW\tdescription\t"+tab.write(db.getDescription())); |
---|
115 | | - |
---|
116 | | - // Write schema. |
---|
117 | | - List<Field> fields=db.getFields(); |
---|
118 | | - for (Field f: fields) |
---|
119 | | - { |
---|
120 | | - String recSize=f.getRecommendedSize()<=0 ? "" : "\t"+f.getRecommendedSize(); |
---|
121 | | - out.println("#TIMEFLOW\tfield\t"+tab.write(f.getName())+ |
---|
122 | | - "\t"+FieldFormatCatalog.humanName(f.getType())+recSize); |
---|
123 | | - } |
---|
124 | | - |
---|
125 | | - out.println(END_OF_SCHEMA); |
---|
126 | | - |
---|
127 | | - // Write column mappings. |
---|
128 | | - List<String> aliases=DBUtils.getFieldAliases(db); |
---|
129 | | - for (String a:aliases) |
---|
130 | | - out.println("#TIMEFLOW\talias\t"+a+"\t"+tab.write(db.getField(a).getName())); |
---|
131 | | - |
---|
132 | | - // Write end of header indicator |
---|
133 | | - out.println(END_OF_METADATA); |
---|
134 | | - |
---|
135 | | - // Write data! |
---|
136 | | - new DelimitedFormat('\t').writeDelimited(db, acts, out); |
---|
137 | | - |
---|
138 | | - out.flush(); |
---|
139 | | - out.close(); |
---|
140 | | - } |
---|
141 | | - |
---|
142 | | - public static void main(String[] args) throws Exception |
---|
143 | | - { |
---|
144 | | - System.out.println("Reading"); |
---|
145 | | - ActDB db=read(new File("test/monet.txt"), System.out); |
---|
146 | | - System.out.println("# lines: "+db.size()); |
---|
147 | | - } |
---|
| 20 | + public ActDB readFile(String fileName, PrintStream messages) throws Exception |
---|
| 21 | + { |
---|
| 22 | + return read(new File(fileName), messages); |
---|
| 23 | + } |
---|
148 | 24 | |
---|
149 | | - @Override |
---|
150 | | - public String getName() { |
---|
151 | | - return "TimeFlow Format"; |
---|
152 | | - } |
---|
| 25 | + public static ActDB read(File file, PrintStream out) throws Exception |
---|
| 26 | + { |
---|
| 27 | + String text = IO.read(file.getAbsolutePath()); |
---|
| 28 | + DelimitedText quote = new DelimitedText('\t'); |
---|
| 29 | + Iterator<String[]> lines = quote.read(text).iterator(); |
---|
153 | 30 | |
---|
154 | | - @Override |
---|
155 | | - public ActDB importFile(File file) throws Exception { |
---|
156 | | - return read(file, System.out); |
---|
157 | | - } |
---|
158 | 31 | |
---|
159 | | - @Override |
---|
160 | | - public void export(TFModel model, BufferedWriter out) throws Exception { |
---|
161 | | - write(model.getDB().all(), out); |
---|
162 | | - } |
---|
| 32 | + ActDB db = null; |
---|
| 33 | + List<String> fieldNames = new ArrayList<String>(); |
---|
| 34 | + List<Class> fieldTypes = new ArrayList<Class>(); |
---|
| 35 | + List<Integer> fieldSizes = new ArrayList<Integer>(); |
---|
| 36 | + String source = "[unknown]", description = ""; |
---|
| 37 | + for (;;) |
---|
| 38 | + { |
---|
| 39 | + String[] t = lines.next(); |
---|
| 40 | + |
---|
| 41 | + if (t[1].equals("field")) |
---|
| 42 | + { |
---|
| 43 | + fieldNames.add(t[2]); |
---|
| 44 | + fieldTypes.add(FieldFormatCatalog.javaClass(t[3])); |
---|
| 45 | + if (t.length > 4) |
---|
| 46 | + { |
---|
| 47 | + fieldSizes.add(Integer.parseInt(t[4])); |
---|
| 48 | + } else |
---|
| 49 | + { |
---|
| 50 | + fieldSizes.add(-1); |
---|
| 51 | + } |
---|
| 52 | + } else if (t[1].equals("source")) |
---|
| 53 | + { |
---|
| 54 | + source = t[2]; |
---|
| 55 | + } else if (t[1].equals("description")) |
---|
| 56 | + { |
---|
| 57 | + description = t[2]; |
---|
| 58 | + |
---|
| 59 | + } else if (t[1].equals("end-metadata")) |
---|
| 60 | + { |
---|
| 61 | + break; |
---|
| 62 | + } |
---|
| 63 | + } |
---|
| 64 | + db = new ArrayDB((String[]) fieldNames.toArray(new String[0]), |
---|
| 65 | + (Class[]) fieldTypes.toArray(new Class[0]), source); |
---|
| 66 | + db.setDescription(description); |
---|
| 67 | + for (int i = 0; i < fieldNames.size(); i++) |
---|
| 68 | + { |
---|
| 69 | + if (fieldSizes.get(i) > 0) |
---|
| 70 | + { |
---|
| 71 | + db.getField(fieldNames.get(i)).setRecommendedSize(fieldSizes.get(i)); |
---|
| 72 | + } |
---|
| 73 | + } |
---|
| 74 | + for (;;) |
---|
| 75 | + { |
---|
| 76 | + String[] t = lines.next(); |
---|
| 77 | + if (t[1].startsWith("===")) |
---|
| 78 | + { |
---|
| 79 | + break; |
---|
| 80 | + } |
---|
| 81 | + if (t[1].equals("alias")) |
---|
| 82 | + { |
---|
| 83 | + db.setAlias(db.getField(t[3]), t[2]); |
---|
| 84 | + } |
---|
| 85 | + } |
---|
| 86 | + |
---|
| 87 | + // note: in some cases headers may be in a different order than in |
---|
| 88 | + // metadata section, so we will read these. |
---|
| 89 | + String[] headers = lines.next(); |
---|
| 90 | + if (headers.length != fieldNames.size()) |
---|
| 91 | + { |
---|
| 92 | + throw new IllegalArgumentException("Different number of headers than fields!"); |
---|
| 93 | + } |
---|
| 94 | + |
---|
| 95 | + |
---|
| 96 | + while (lines.hasNext()) |
---|
| 97 | + { |
---|
| 98 | + String[] t = lines.next(); |
---|
| 99 | + Act a = db.createAct(); |
---|
| 100 | + for (int i = 0; i < t.length; i++) |
---|
| 101 | + { |
---|
| 102 | + Field f = db.getField(headers[i]); |
---|
| 103 | + FieldFormat format = FieldFormatCatalog.getFormat(f.getType()); |
---|
| 104 | + a.set(f, format.parse(t[i])); |
---|
| 105 | + } |
---|
| 106 | + } |
---|
| 107 | + |
---|
| 108 | + return db; |
---|
| 109 | + } |
---|
| 110 | + |
---|
| 111 | + public static void write(ActList acts, BufferedWriter bw) throws IOException |
---|
| 112 | + { |
---|
| 113 | + ActDB db = acts.getDB(); |
---|
| 114 | + |
---|
| 115 | + PrintWriter out = new PrintWriter(bw); |
---|
| 116 | + |
---|
| 117 | + DelimitedText tab = new DelimitedText('\t'); |
---|
| 118 | + |
---|
| 119 | + // Write version |
---|
| 120 | + out.println("#TIMEFLOW\tformat version\t1"); |
---|
| 121 | + |
---|
| 122 | + // Write source of data. |
---|
| 123 | + out.println("#TIMEFLOW\tsource\t" + tab.write(db.getSource())); |
---|
| 124 | + |
---|
| 125 | + // Write description of data. |
---|
| 126 | + out.println("#TIMEFLOW\tdescription\t" + tab.write(db.getDescription())); |
---|
| 127 | + |
---|
| 128 | + // Write schema. |
---|
| 129 | + List<Field> fields = db.getFields(); |
---|
| 130 | + for (Field f : fields) |
---|
| 131 | + { |
---|
| 132 | + String recSize = f.getRecommendedSize() <= 0 ? "" : "\t" + f.getRecommendedSize(); |
---|
| 133 | + out.println("#TIMEFLOW\tfield\t" + tab.write(f.getName()) |
---|
| 134 | + + "\t" + FieldFormatCatalog.humanName(f.getType()) + recSize); |
---|
| 135 | + } |
---|
| 136 | + |
---|
| 137 | + out.println(END_OF_SCHEMA); |
---|
| 138 | + |
---|
| 139 | + // Write column mappings. |
---|
| 140 | + List<String> aliases = DBUtils.getFieldAliases(db); |
---|
| 141 | + for (String a : aliases) |
---|
| 142 | + { |
---|
| 143 | + out.println("#TIMEFLOW\talias\t" + a + "\t" + tab.write(db.getField(a).getName())); |
---|
| 144 | + } |
---|
| 145 | + |
---|
| 146 | + // Write end of header indicator |
---|
| 147 | + out.println(END_OF_METADATA); |
---|
| 148 | + |
---|
| 149 | + // Write data! |
---|
| 150 | + new DelimitedFormat('\t').writeDelimited(db, acts, out); |
---|
| 151 | + |
---|
| 152 | + out.flush(); |
---|
| 153 | + out.close(); |
---|
| 154 | + } |
---|
| 155 | + |
---|
| 156 | + public static void main(String[] args) throws Exception |
---|
| 157 | + { |
---|
| 158 | + System.out.println("Reading"); |
---|
| 159 | + ActDB db = read(new File("test/monet.txt"), System.out); |
---|
| 160 | + System.out.println("# lines: " + db.size()); |
---|
| 161 | + } |
---|
| 162 | + |
---|
| 163 | + @Override |
---|
| 164 | + public String getName() |
---|
| 165 | + { |
---|
| 166 | + return "TimeFlow Format"; |
---|
| 167 | + } |
---|
| 168 | + |
---|
| 169 | + @Override |
---|
| 170 | + public ActDB importFile(File file) throws Exception |
---|
| 171 | + { |
---|
| 172 | + return read(file, System.out); |
---|
| 173 | + } |
---|
| 174 | + |
---|
| 175 | + @Override |
---|
| 176 | + public void export(TimeflowModel model, BufferedWriter out) throws Exception |
---|
| 177 | + { |
---|
| 178 | + write(model.getDB().all(), out); |
---|
| 179 | + } |
---|
163 | 180 | } |
---|