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 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.io.BufferedOutputStream;
|
---|
22 | import java.io.File;
|
---|
23 | import java.io.FileOutputStream;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.io.OutputStreamWriter;
|
---|
26 | import java.io.Writer;
|
---|
27 | import java.util.LinkedList;
|
---|
28 | import java.util.List;
|
---|
29 |
|
---|
30 | import org.expeditee.gui.AttributeUtils;
|
---|
31 | import org.expeditee.gui.Frame;
|
---|
32 | import org.expeditee.items.Constraint;
|
---|
33 | import org.expeditee.items.Item;
|
---|
34 | import org.expeditee.items.Line;
|
---|
35 | import org.expeditee.items.widgets.WidgetEdge;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Experimental format which is designed to be more readable
|
---|
39 | * NON FUNCTIONAL DUE TO CHANGES TO AttributeUtils
|
---|
40 | *
|
---|
41 | * @author jts21
|
---|
42 | *
|
---|
43 | */
|
---|
44 | public class ExaWriter implements FrameWriter {
|
---|
45 |
|
---|
46 | private StringBuilder sb = new StringBuilder();
|
---|
47 | private Writer _writer;
|
---|
48 | private String _output;
|
---|
49 | private String _frameName;
|
---|
50 | private List<Item> _lineEnds = new LinkedList<Item>();
|
---|
51 | private boolean _running = false;
|
---|
52 |
|
---|
53 | public void setOutputLocation(String filename) {
|
---|
54 | _output = filename;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public String getFileContents() {
|
---|
58 | return sb.toString();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public String writeFrame(Frame frame) throws IOException {
|
---|
62 | if(_output == null) {
|
---|
63 | _frameName = frame.getPath() + frame.getFramesetName().toLowerCase() + File.separator
|
---|
64 | + frame.getNumber() + ExaReader.EXTENTION;
|
---|
65 | }
|
---|
66 | return writeFrame(frame, null);
|
---|
67 | }
|
---|
68 |
|
---|
69 | public String writeFrame(Frame frame, Writer writer) throws IOException {
|
---|
70 |
|
---|
71 | _running = true;
|
---|
72 | boolean setWriter = true;
|
---|
73 |
|
---|
74 | if(writer != null) {
|
---|
75 | _writer = writer;
|
---|
76 | setWriter = false;
|
---|
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 | }
|
---|
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 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | AttributeUtils.ensureReady();
|
---|
94 |
|
---|
95 | writeHeader(frame);
|
---|
96 | propertyPrefix = "\t";
|
---|
97 | writeItems(frame);
|
---|
98 | writeLines();
|
---|
99 | writeConstraints();
|
---|
100 |
|
---|
101 | _writer.flush();
|
---|
102 | if(setWriter) {
|
---|
103 | _writer.close();
|
---|
104 | }
|
---|
105 | _running = false;
|
---|
106 |
|
---|
107 | return "Frame " + frame.getName() + " exported" + (writer != null ? writer.toString() : (_output != null ? _output : ""));
|
---|
108 | }
|
---|
109 |
|
---|
110 | private void writeHeader(Frame frame) throws IOException {
|
---|
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 | // }
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void writeItems(Frame frame) throws IOException {
|
---|
132 | writeLine();
|
---|
133 | for (Item item : frame.getItemsToSave()) {
|
---|
134 | if (item.isLineEnd()) {
|
---|
135 | _lineEnds.add(item);
|
---|
136 | }
|
---|
137 | if(item instanceof Line) {
|
---|
138 | continue;
|
---|
139 | }
|
---|
140 | writeLine("Item " + item.getClass().getName() + " " + item.getID());
|
---|
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 | // }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | private void writeLines() throws IOException {
|
---|
170 | writeLine();
|
---|
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)) {
|
---|
189 | writeLine("Line " + line.getID());
|
---|
190 | writeProperty("Start", line.getStartItem().getID());
|
---|
191 | writeProperty("End", line.getEndItem().getID());
|
---|
192 |
|
---|
193 | // add this line to the list of lines that have been seen
|
---|
194 | seen.add(line);
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void writeConstraints() throws IOException {
|
---|
202 |
|
---|
203 | writeLine();
|
---|
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())) {
|
---|
214 | writeLine("Constraint " + c.getType() + " " + c.getID());
|
---|
215 | writeProperty("Start", c.getStart().getID());
|
---|
216 | writeProperty("End", c.getEnd().getID());
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | _lineEnds.remove(0);
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | private String propertyPrefix = "";
|
---|
226 |
|
---|
227 | private void writeProperty(String prop, Object value) throws IOException {
|
---|
228 | writeLine(propertyPrefix + prop + " " + value.toString());
|
---|
229 | }
|
---|
230 |
|
---|
231 | private void writeLine() throws IOException {
|
---|
232 | write("\n");
|
---|
233 | }
|
---|
234 |
|
---|
235 | private void writeLine(String line) throws IOException {
|
---|
236 | write(line + "\n");
|
---|
237 | }
|
---|
238 |
|
---|
239 | private void write(String data) throws IOException {
|
---|
240 | // System.out.print(data);
|
---|
241 | if(_writer != null) {
|
---|
242 | _writer.write(data);
|
---|
243 | }
|
---|
244 | sb.append(data);
|
---|
245 | }
|
---|
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 | }
|
---|
254 | }
|
---|