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