source: trunk/src/org/expeditee/agents/WriteTree.java@ 86

Last change on this file since 86 was 80, checked in by ra33, 16 years ago

Added some more unit tests
Did a bunch of refactoring
AND added a few new features... @b @v were the most significant

File size: 3.8 KB
Line 
1package org.expeditee.agents;
2
3import java.io.IOException;
4import java.lang.reflect.Constructor;
5
6import org.expeditee.actions.Actions;
7import org.expeditee.gui.Frame;
8import org.expeditee.gui.FrameGraphics;
9import org.expeditee.io.FrameWriter;
10import org.expeditee.io.Logger;
11import org.expeditee.io.TreeWriter;
12
13public class WriteTree extends DefaultAgent {
14
15 private String _format = "txt";
16
17 protected TreeWriter _treeWriter;
18
19 protected FrameWriter _frameWriter;
20
21 private boolean _clipboard = false;
22
23 // write tree and write frame are almost identical, so WriteFrame.java
24 // sets this to false
25 private boolean _followLinks = true;
26
27 private String _outFile = null;
28
29 private static final String IO_PACKAGE = Actions.ROOT_PACKAGE + "io.";
30
31 public WriteTree() {
32 }
33
34 public WriteTree(String params) {
35 String format = params.trim();
36
37 if (format.equalsIgnoreCase(CLIPBOARD)) {
38 _clipboard = true;
39 return;
40 }
41
42 _format = format.trim();
43
44 int ind = params.indexOf(" ");
45 if (ind > 0) {
46 String lastParam = params.substring(ind + 1);
47 _format = params.substring(0, ind).toLowerCase();
48
49 if (lastParam.equalsIgnoreCase(CLIPBOARD))
50 _clipboard = true;
51 else
52 _outFile = lastParam;
53 }
54 }
55
56 public WriteTree(String format, String outFile) {
57 _format = format;
58 _outFile = outFile;
59 }
60
61 public void setFollowLinks(boolean val) {
62 _followLinks = val;
63 }
64
65 @SuppressWarnings("unchecked")
66 @Override
67 public boolean initialise(Frame start) {
68 if (_outFile == null)
69 _outFile = start.getExportFileName();
70
71 try {
72 Class c = null;
73
74 try {
75 c = Class
76 .forName(IO_PACKAGE + _format.toUpperCase() + "Writer");
77 } catch (NoClassDefFoundError ex) {
78 c = Class.forName(IO_PACKAGE + _format + "Writer");
79 }
80
81 Constructor con = c.getConstructor();
82 Object o = con.newInstance();
83
84 // if o is not a tree or frame writer, then it is not a valid
85 // format
86
87 if (_followLinks) {
88 // check that o is a valid treewriter
89 if (!(o instanceof TreeWriter)) {
90 FrameGraphics.WarningMessage(_format.toUpperCase()
91 + " format cannot be used to write trees.");
92 return false;
93 }
94
95 _treeWriter = (TreeWriter) o;
96
97 if (_clipboard)
98 _treeWriter.setOutputLocation(CLIPBOARD);
99 else if (_outFile != null)
100 _treeWriter.setOutputLocation(_outFile);
101 } else {
102 if (!(o instanceof FrameWriter) && !(o instanceof TreeWriter)) {
103 FrameGraphics.WarningMessage(_format.toUpperCase()
104 + " format cannot be used to write frames.");
105 return false;
106 }
107
108 _frameWriter = (FrameWriter) o;
109
110 if (_clipboard)
111 _frameWriter.setOutputLocation(CLIPBOARD);
112 else if (_outFile != null)
113 _frameWriter.setOutputLocation(_outFile);
114 }
115
116 } catch (ClassNotFoundException e) {
117 FrameGraphics
118 .WarningMessage("The agent does not exist or has incorrect parametres.");
119 return false;
120 } catch (Exception e) {
121 e.printStackTrace();
122 return false;
123 }
124
125 return super.initialise(start);
126 }
127
128 @Override
129 protected Frame process(Frame frame) {
130 String msg = "Failed.";
131 try {
132
133 if (_followLinks) {
134 msg = _treeWriter.writeTree(frame);
135 _frameCount = _treeWriter.getFrameCount();
136 } else {
137 msg = _frameWriter.writeFrame(frame);
138 _frameCount = 1;
139 }
140 } catch (IOException e) {
141 //System.out.println("Caught");
142 Logger.Log(e);
143 FrameGraphics.ErrorMessage("IOException in WriteTree: "
144 + e.getMessage());
145 e.printStackTrace();
146 super.stop();
147 return null;
148 }
149
150 if (_stop)
151 message("WriteTree halted by user.");
152 else
153 message(msg);
154
155 return null;
156 }
157
158 @Override
159 public void stop() {
160 super.stop();
161
162 if (_treeWriter != null)
163 _treeWriter.stop();
164
165 if (_frameWriter != null)
166 _frameWriter.stop();
167 }
168
169 @Override
170 public boolean isRunning() {
171 if (_treeWriter != null)
172 return _treeWriter.isRunning();
173
174 if (_frameWriter != null)
175 return _frameWriter.isRunning();
176
177 return false;
178 }
179
180}
Note: See TracBrowser for help on using the repository browser.