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

Last change on this file since 1510 was 1102, checked in by davidb, 6 years ago

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

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