1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.io.BufferedOutputStream;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileOutputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.OutputStream;
|
---|
8 | import java.io.OutputStreamWriter;
|
---|
9 | import java.io.Writer;
|
---|
10 | import java.lang.reflect.InvocationTargetException;
|
---|
11 | import java.lang.reflect.Method;
|
---|
12 | import java.util.Iterator;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | import org.expeditee.agents.DefaultAgent;
|
---|
17 | import org.expeditee.gui.Frame;
|
---|
18 | import org.expeditee.items.Constraint;
|
---|
19 | import org.expeditee.items.Dot;
|
---|
20 | import org.expeditee.items.Item;
|
---|
21 | import org.expeditee.items.Line;
|
---|
22 | import org.expeditee.stats.SessionStats;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Writes a Frame out to a KMS format file.
|
---|
26 | *
|
---|
27 | * @author jdm18
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public class KMSWriter extends DefaultFrameWriter {
|
---|
31 |
|
---|
32 | private ProxyWriter _writer = null;
|
---|
33 |
|
---|
34 | private StringBuilder _stringWriter = null;
|
---|
35 |
|
---|
36 | private static final char TERMINATOR = 'Z';
|
---|
37 |
|
---|
38 | private static final char DELIMITER = '+';
|
---|
39 |
|
---|
40 | public KMSWriter() {
|
---|
41 | super();
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void initialise(Frame start, Writer writer) throws IOException {
|
---|
45 | String name = start.getFramesetName().toLowerCase();
|
---|
46 |
|
---|
47 | if (_filename == null)
|
---|
48 | _filename = start.getPath() + name + File.separator + name + "."
|
---|
49 | + start.getNumber();
|
---|
50 |
|
---|
51 | _stringWriter = new StringBuilder();
|
---|
52 | if (_filename.toLowerCase().equals(DefaultAgent.CLIPBOARD)) {
|
---|
53 | _writer = new ProxyWriter(true);
|
---|
54 | _filename = DefaultAgent.CLIPBOARD;
|
---|
55 | } else{
|
---|
56 | // Open an Output Stream Writer to set encoding
|
---|
57 | OutputStream fout = new FileOutputStream(_filename);
|
---|
58 | OutputStream bout = new BufferedOutputStream(fout);
|
---|
59 | Writer out = new OutputStreamWriter(bout, "UTF-8");
|
---|
60 | _writer = new ProxyWriter(out);
|
---|
61 | }
|
---|
62 |
|
---|
63 | try {
|
---|
64 | _FrameTags.put("A", Frame.class.getMethod("getName", new Class[] {}));
|
---|
65 | _ItemTags.put("S", Item.class.getMethod("getID", new Class[] {}));
|
---|
66 | } catch (Exception e) {
|
---|
67 |
|
---|
68 | }
|
---|
69 | // _writer = new BufferedWriter(new FileWriter(filepath.toLowerCase() +
|
---|
70 | // filename.toLowerCase()));
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Writes the given Frame (and all items it contains) to a KMS file. Note:
|
---|
75 | * File path and location must be set before calling this or it will do
|
---|
76 | * nothing.
|
---|
77 | *
|
---|
78 | * @param frame
|
---|
79 | * The Frame to write out to the file.
|
---|
80 | * @throws IOException
|
---|
81 | * Any exceptions occured by the BufferedWriter.
|
---|
82 | */
|
---|
83 | public void outputFrame(Frame frame) throws IOException {
|
---|
84 | if (_writer == null)
|
---|
85 | return;
|
---|
86 |
|
---|
87 | writeHeader(frame);
|
---|
88 |
|
---|
89 | // write out each Item in the Frame.
|
---|
90 | for (Item i : frame.getItemsToSave()) {
|
---|
91 | writeItem(i);
|
---|
92 | }
|
---|
93 |
|
---|
94 | // write any lines or constraints
|
---|
95 | writeLineData();
|
---|
96 | writeConstraintData();
|
---|
97 |
|
---|
98 | return;
|
---|
99 | }
|
---|
100 |
|
---|
101 | private void writeHeader(Frame toWrite) throws IOException {
|
---|
102 | Iterator<String> it = _FrameTags.keySet().iterator();
|
---|
103 | Object[] param = {};
|
---|
104 |
|
---|
105 | // Write the version tag
|
---|
106 | writeLine("v", "22");
|
---|
107 |
|
---|
108 | while (it.hasNext()) {
|
---|
109 | String tag = it.next();
|
---|
110 | try {
|
---|
111 | Object o = _FrameTags.get(tag).invoke(toWrite, param);
|
---|
112 | o = Conversion.ConvertToExpeditee(_FrameTags.get(tag), o);
|
---|
113 | if (o != null) {
|
---|
114 | writeLine(tag, o.toString());
|
---|
115 | }
|
---|
116 | } catch (IllegalArgumentException e) {
|
---|
117 | // TODO Auto-generated catch block
|
---|
118 | e.printStackTrace();
|
---|
119 | } catch (IllegalAccessException e) {
|
---|
120 | // TODO Auto-generated catch block
|
---|
121 | e.printStackTrace();
|
---|
122 | } catch (InvocationTargetException e) {
|
---|
123 | // TODO Auto-generated catch block
|
---|
124 | e.printStackTrace();
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void writeLine(String tag, String line) throws IOException {
|
---|
130 | writeLine(DELIMITER + tag + DELIMITER + " " + line);
|
---|
131 | }
|
---|
132 |
|
---|
133 | private void writeTerminator() throws IOException {
|
---|
134 | writeLine("" + DELIMITER + TERMINATOR + DELIMITER);
|
---|
135 | }
|
---|
136 |
|
---|
137 | // writes the given line out to the file
|
---|
138 | private void writeLine(String line) throws IOException {
|
---|
139 | // do not write empty lines
|
---|
140 | if (line == null)
|
---|
141 | return;
|
---|
142 |
|
---|
143 | String toWrite = line + "\n";
|
---|
144 |
|
---|
145 | _writer.write(toWrite);
|
---|
146 | _stringWriter.append(toWrite);
|
---|
147 | }
|
---|
148 |
|
---|
149 | // writes the given Item out to the file
|
---|
150 | public void writeItem(Item item) throws IOException {
|
---|
151 | if (_writer == null)
|
---|
152 | return;
|
---|
153 |
|
---|
154 | writeLine("");
|
---|
155 | if (item.getLines().size() > 0)
|
---|
156 | writePoint(item);
|
---|
157 | else
|
---|
158 | writeClass(item);
|
---|
159 | }
|
---|
160 |
|
---|
161 | private List<Item> _points = new LinkedList<Item>();
|
---|
162 |
|
---|
163 | // Writes out a Dot to the file
|
---|
164 | protected void writePoint(Item point) throws IOException {
|
---|
165 | _points.add(point);
|
---|
166 | writeClass(point);
|
---|
167 | }
|
---|
168 |
|
---|
169 | // writes out all lines to the file
|
---|
170 | private void writeLineData() throws IOException {
|
---|
171 | List<Line> seen = new LinkedList<Line>();
|
---|
172 |
|
---|
173 | // loop through all points stored
|
---|
174 | for (int i = 0; i < _points.size(); i++) {
|
---|
175 | List<Line> lines = _points.get(i).getLines();
|
---|
176 |
|
---|
177 | // if this point is part of one or more lines
|
---|
178 | if (lines != null && lines.size() > 0) {
|
---|
179 | for (Line line : lines) {
|
---|
180 |
|
---|
181 | // only output new lines that have not yet been output
|
---|
182 | if (!seen.contains(line)) {
|
---|
183 | writeLine("L", line.getID() + " " + line.getLineType());
|
---|
184 | writeLine("s", line.getLineEnds());
|
---|
185 |
|
---|
186 | // add this line to the list of lines that have been
|
---|
187 | // seen
|
---|
188 | seen.add(line);
|
---|
189 | }
|
---|
190 |
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | // writes out any constraints to the file
|
---|
197 | private void writeConstraintData() throws IOException {
|
---|
198 | // outputs any constraints the points have
|
---|
199 |
|
---|
200 | // loop through all the points
|
---|
201 | while (_points.size() > 0) {
|
---|
202 | Item i = _points.get(0);
|
---|
203 |
|
---|
204 | if (i instanceof Dot) {
|
---|
205 | Item p = (Item) i;
|
---|
206 |
|
---|
207 | // if there are any constraints to write
|
---|
208 | if (p.getConstraints() != null) {
|
---|
209 | List<Constraint> constraints = p.getConstraints();
|
---|
210 |
|
---|
211 | // do not write constraints that have already been
|
---|
212 | // written
|
---|
213 | for (Constraint c : constraints) {
|
---|
214 | if (_points.contains(c.getStart())
|
---|
215 | && _points.contains(c.getEnd())) {
|
---|
216 | writeLine("C", c.getID() + " " + c.getType());
|
---|
217 | writeLine("s", c.getLineEnds());
|
---|
218 | }
|
---|
219 |
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | // remove the point from the list
|
---|
224 | _points.remove(0);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | @Override
|
---|
229 | protected String finaliseFrame() throws IOException {
|
---|
230 | writeTerminator();
|
---|
231 |
|
---|
232 | writeLine(SessionStats.getFrameEventList());
|
---|
233 |
|
---|
234 | _writer.flush();
|
---|
235 | _writer.close();
|
---|
236 | _writer = null;
|
---|
237 |
|
---|
238 | return "Frame successfully written to " + _filename;
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void writeClass(Object toWrite) throws IOException {
|
---|
242 | Iterator<String> it = _ItemTags.keySet().iterator();
|
---|
243 | Object[] param = {};
|
---|
244 |
|
---|
245 | while (it.hasNext()) {
|
---|
246 | String tag = it.next();
|
---|
247 | Method toRun = _ItemTags.get(tag);
|
---|
248 | Class<?> declarer = toRun.getDeclaringClass();
|
---|
249 | if (declarer.isAssignableFrom(toWrite.getClass())) {
|
---|
250 | try {
|
---|
251 | Object o = toRun.invoke(toWrite, param);
|
---|
252 | o = Conversion.ConvertToExpeditee(toRun, o);
|
---|
253 | if (o != null) {
|
---|
254 | if (o instanceof List) {
|
---|
255 | for (Object line : (List) o)
|
---|
256 | writeLine(tag, line.toString());
|
---|
257 | } else
|
---|
258 | writeLine(tag, o.toString());
|
---|
259 | }
|
---|
260 | } catch (IllegalArgumentException e) {
|
---|
261 | // TODO Auto-generated catch block
|
---|
262 | e.printStackTrace();
|
---|
263 | } catch (IllegalAccessException e) {
|
---|
264 | // TODO Auto-generated catch block
|
---|
265 | e.printStackTrace();
|
---|
266 | } catch (InvocationTargetException e) {
|
---|
267 | // TODO Auto-generated catch block
|
---|
268 | e.printStackTrace();
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Gets a string representation of the frame file contents.
|
---|
276 | */
|
---|
277 | public String getFileContents() {
|
---|
278 | return _stringWriter.toString();
|
---|
279 | }
|
---|
280 | }
|
---|