source: trunk/src/org/expeditee/io/ExaWriter.java@ 919

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 7.0 KB
Line 
1/**
2 * ExaWriter.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.io;
20
21import java.io.BufferedOutputStream;
22import java.io.File;
23import java.io.FileOutputStream;
24import java.io.IOException;
25import java.io.OutputStreamWriter;
26import java.io.Writer;
27import java.util.LinkedList;
28import java.util.List;
29
30import org.expeditee.gui.AttributeUtils;
31import org.expeditee.gui.AttributeUtils.Attribute;
32import org.expeditee.gui.Frame;
33import org.expeditee.items.Constraint;
34import org.expeditee.items.Item;
35import org.expeditee.items.Line;
36import org.expeditee.items.widgets.WidgetEdge;
37
38/**
39 * Experimental format which is designed to be more readable
40 * NON FUNCTIONAL DUE TO CHANGES TO AttributeUtils
41 *
42 * @author jts21
43 *
44 */
45public class ExaWriter implements FrameWriter {
46
47 private StringBuilder sb = new StringBuilder();
48 private Writer _writer;
49 private String _output;
50 private String _frameName;
51 private List<Item> _lineEnds = new LinkedList<Item>();
52 private boolean _running = false;
53
54 public void setOutputLocation(String filename) {
55 _output = filename;
56 }
57
58 public String getFileContents() {
59 return sb.toString();
60 }
61
62 public String writeFrame(Frame frame) throws IOException {
63 if(_output == null) {
64 _frameName = frame.getPath() + frame.getFramesetName().toLowerCase() + File.separator
65 + frame.getNumber() + ExaReader.EXTENTION;
66 }
67 return writeFrame(frame, null);
68 }
69
70 public String writeFrame(Frame frame, Writer writer) throws IOException {
71
72 _running = true;
73 boolean setWriter = true;
74
75 if(writer != null) {
76 _writer = writer;
77 setWriter = false;
78 } else if(_output != null) {
79 try {
80 _writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(_output)), "UTF-8");
81 } catch (Exception e) {
82 System.err.println("Couldn't open " + _output + " for writing");
83 _writer = null;
84 }
85 } else if(_frameName != null) {
86 try {
87 _writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(_frameName)), "UTF-8");
88 } catch (Exception e) {
89 System.err.println("Couldn't open " + _frameName + " for writing");
90 _writer = null;
91 }
92 }
93
94 AttributeUtils.ensureReady();
95
96 writeHeader(frame);
97 propertyPrefix = "\t";
98 writeItems(frame);
99 writeLines();
100 writeConstraints();
101
102 _writer.flush();
103 if(setWriter) {
104 _writer.close();
105 }
106 _running = false;
107
108 return "Frame " + frame.getName() + " exported" + (writer != null ? writer.toString() : (_output != null ? _output : ""));
109 }
110
111 private void writeHeader(Frame frame) throws IOException {
112 // TODO: NON FUNCTIONAL DUE TO CHANGES TO AttributeUtils
113// for (String prop : AttributeUtils._FrameAttrib.keys) {
114// Attribute a = AttributeUtils._FrameAttrib.get(prop);
115// if(a == null || a.saveGetter == null) {
116// continue;
117// }
118// if(a.saveGetter.getDeclaringClass().isAssignableFrom(frame.getClass())) {
119// try {
120// Object o = a.saveGetter.invoke(frame);
121// o = Conversion.ConvertToExpeditee(a.saveGetter, o);
122// if (o != null) {
123// writeProperty(a.displayName, o);
124// }
125// } catch (Exception e) {
126// e.printStackTrace();
127// }
128// }
129// }
130 }
131
132 private void writeItems(Frame frame) throws IOException {
133 writeLine();
134 for (Item item : frame.getItemsToSave()) {
135 if (item.isLineEnd()) {
136 _lineEnds.add(item);
137 }
138 if(item instanceof Line) {
139 continue;
140 }
141 writeLine("Item " + item.getClass().getName() + " " + item.getID());
142 for (String prop : AttributeUtils._Attrib.keys) {
143 // TODO: NON FUNCTIONAL DUE TO CHANGES TO AttributeUtils
144// Attribute a = AttributeUtils._Attrib.get(prop);
145// if(a == null || a.saveGetter == null) {
146// continue;
147// }
148// Class<?> declarer = a.saveGetter.getDeclaringClass();
149// if (declarer.isAssignableFrom(item.getClass())) {
150// try {
151// Object o = a.saveGetter.invoke(item);
152// o = Conversion.ConvertToExpeditee(a.saveGetter, o);
153// if (o != null) {
154// if (o instanceof List) {
155// for (Object line : (List<?>) o) {
156// writeProperty(a.displayName, line);
157// }
158// } else {
159// writeProperty(a.displayName, o);
160// }
161// }
162// } catch (Exception e) {
163// e.printStackTrace();
164// }
165// }
166 }
167 }
168 }
169
170 private void writeLines() throws IOException {
171 writeLine();
172 List<Line> seen = new LinkedList<Line>();
173
174 // loop through all points stored
175 for(Item item : _lineEnds) {
176 List<Line> lines = item.getLines();
177
178 // if this point is part of one or more lines
179 if (lines != null && lines.size() > 0) {
180 for (Line line : lines) {
181
182 // Brook: widget edges are not saved
183 if (line instanceof WidgetEdge) {
184 seen.add(line);
185 continue;
186 }
187
188 // only output new lines that have not yet been output
189 if (!seen.contains(line)) {
190 writeLine("Line " + line.getID());
191 writeProperty("Start", line.getStartItem().getID());
192 writeProperty("End", line.getEndItem().getID());
193
194 // add this line to the list of lines that have been seen
195 seen.add(line);
196 }
197 }
198 }
199 }
200 }
201
202 private void writeConstraints() throws IOException {
203
204 writeLine();
205 while(!_lineEnds.isEmpty()) {
206 Item item = _lineEnds.get(0);
207
208 // if there are any constraints to write
209 List<Constraint> constraints = item.getConstraints();
210 if (constraints != null) {
211
212 // do not write constraints that have already been written
213 for (Constraint c : constraints) {
214 if (_lineEnds.contains(c.getStart()) && _lineEnds.contains(c.getEnd())) {
215 writeLine("Constraint " + c.getType() + " " + c.getID());
216 writeProperty("Start", c.getStart().getID());
217 writeProperty("End", c.getEnd().getID());
218 }
219 }
220 }
221
222 _lineEnds.remove(0);
223 }
224 }
225
226 private String propertyPrefix = "";
227
228 private void writeProperty(String prop, Object value) throws IOException {
229 writeLine(propertyPrefix + prop + " " + value.toString());
230 }
231
232 private void writeLine() throws IOException {
233 write("\n");
234 }
235
236 private void writeLine(String line) throws IOException {
237 write(line + "\n");
238 }
239
240 private void write(String data) throws IOException {
241 // System.out.print(data);
242 if(_writer != null) {
243 _writer.write(data);
244 }
245 sb.append(data);
246 }
247
248 public boolean isRunning() {
249 return _running;
250 }
251
252 public void stop() {
253 // surely we'll stop at some point in the future
254 }
255}
Note: See TracBrowser for help on using the repository browser.