1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileWriter;
|
---|
5 | import java.io.IOException;
|
---|
6 |
|
---|
7 | import org.expeditee.gui.Frame;
|
---|
8 | import org.expeditee.gui.FrameIO;
|
---|
9 | import org.expeditee.items.Item;
|
---|
10 |
|
---|
11 | public abstract class DefaultFrameWriter extends ItemWriter implements
|
---|
12 | FrameWriter {
|
---|
13 |
|
---|
14 | protected String _filename = null;
|
---|
15 |
|
---|
16 | protected String _output = null;
|
---|
17 |
|
---|
18 | protected ProxyWriter _writer = null;
|
---|
19 |
|
---|
20 | protected String _format = "";
|
---|
21 |
|
---|
22 | protected boolean _running = true;
|
---|
23 |
|
---|
24 | protected boolean _stop = false;
|
---|
25 |
|
---|
26 | public void setOutputLocation(String filename) {
|
---|
27 | _filename = filename;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public String writeFrame(Frame toWrite) throws IOException {
|
---|
31 | try {
|
---|
32 | initialise(toWrite);
|
---|
33 |
|
---|
34 | outputFrame(toWrite);
|
---|
35 |
|
---|
36 | _running = false;
|
---|
37 | return finaliseFrame();
|
---|
38 | } catch (IOException ioe) {
|
---|
39 | _running = false;
|
---|
40 | throw ioe;
|
---|
41 | }
|
---|
42 |
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected void writeStartFrame(Frame starting) throws IOException {
|
---|
46 | if (starting.getTitle() != null) {
|
---|
47 | if (starting.getTitle().isAnnotation())
|
---|
48 | this.writeAnnotationTitle(starting.getTitle());
|
---|
49 | else
|
---|
50 | this.writeTitle(starting.getTitle(), starting.getItems());
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected void writeEndFrame(Frame ending) throws IOException {
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected void initialise(Frame start) throws IOException {
|
---|
58 | if (_filename == null)
|
---|
59 | _filename = FrameIO.EXPORTS_DIR + start.getFrameName() + _format;
|
---|
60 |
|
---|
61 | if (_filename.toLowerCase().equals("clipboard")) {
|
---|
62 | _writer = new ProxyWriter(true);
|
---|
63 | _output = "Clipboard";
|
---|
64 | } else {
|
---|
65 | if (_filename.contains(File.separator)) {
|
---|
66 | String extTest = _filename.substring(_filename
|
---|
67 | .lastIndexOf(File.separator) + 1);
|
---|
68 | if (!extTest.contains("."))
|
---|
69 | _filename += _format;
|
---|
70 | } else if (!_filename.contains("."))
|
---|
71 | _filename += _format;
|
---|
72 |
|
---|
73 | if (!_filename.contains(File.separator))
|
---|
74 | _filename = FrameIO.EXPORTS_DIR + _filename;
|
---|
75 |
|
---|
76 | File test = new File(_filename);
|
---|
77 |
|
---|
78 | test = test.getParentFile();
|
---|
79 | if (test != null && !test.exists()) {
|
---|
80 | test.mkdirs();
|
---|
81 | }
|
---|
82 |
|
---|
83 | _writer = new ProxyWriter(new FileWriter(_filename));
|
---|
84 | _output = _filename;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | protected String finalise() throws IOException {
|
---|
89 | try {
|
---|
90 | _writer.flush();
|
---|
91 | _writer.close();
|
---|
92 | } catch (IOException ioe) {
|
---|
93 | } finally {
|
---|
94 | _writer.close();
|
---|
95 | }
|
---|
96 |
|
---|
97 | return " exported to " + _output;
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected String finaliseFrame() throws IOException {
|
---|
101 | return "Frame" + finalise();
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected void outputFrame(Frame toWrite) throws IOException {
|
---|
105 | writeStartFrame(toWrite);
|
---|
106 |
|
---|
107 | for (Item i : toWrite.getItems()) {
|
---|
108 | if (_stop)
|
---|
109 | return;
|
---|
110 | writeItem(i);
|
---|
111 | }
|
---|
112 |
|
---|
113 | writeEndFrame(toWrite);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public boolean isRunning() {
|
---|
117 | return _running;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void stop() {
|
---|
121 | _stop = true;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public String getFileContents() {
|
---|
125 | return "Not Supported";
|
---|
126 | }
|
---|
127 | }
|
---|