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