1 | /**
|
---|
2 | * WriteTree.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.agents;
|
---|
20 |
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.lang.reflect.Constructor;
|
---|
23 |
|
---|
24 | import org.expeditee.actions.Actions;
|
---|
25 | import org.expeditee.gui.Frame;
|
---|
26 | import org.expeditee.gui.MessageBay;
|
---|
27 | import org.expeditee.io.FrameWriter;
|
---|
28 | import org.expeditee.io.TreeWriter;
|
---|
29 | import org.expeditee.items.Item;
|
---|
30 | import org.expeditee.stats.Logger;
|
---|
31 |
|
---|
32 | public class WriteTree extends DefaultAgent {
|
---|
33 |
|
---|
34 | private String _format = "txt";
|
---|
35 |
|
---|
36 | protected TreeWriter _treeWriter;
|
---|
37 |
|
---|
38 | protected FrameWriter _frameWriter;
|
---|
39 |
|
---|
40 | private boolean _clipboard = false;
|
---|
41 |
|
---|
42 | // write tree and write frame are almost identical, so WriteFrame.java
|
---|
43 | // sets this to false
|
---|
44 | private boolean _followLinks = true;
|
---|
45 |
|
---|
46 | private String _outFile = null;
|
---|
47 |
|
---|
48 | private static final String IO_PACKAGE = Actions.ROOT_PACKAGE + "io.";
|
---|
49 |
|
---|
50 | public WriteTree() {
|
---|
51 | }
|
---|
52 |
|
---|
53 | public WriteTree(String params) {
|
---|
54 | String format = params.trim();
|
---|
55 |
|
---|
56 | if (format.equalsIgnoreCase(CLIPBOARD)) {
|
---|
57 | _clipboard = true;
|
---|
58 | return;
|
---|
59 | }
|
---|
60 |
|
---|
61 | _format = format.trim();
|
---|
62 |
|
---|
63 |
|
---|
64 | int ind = params.indexOf(" ");
|
---|
65 | if (ind > 0) {
|
---|
66 | String lastParam = params.substring(ind + 1);
|
---|
67 | _format = params.substring(0, ind).toLowerCase();
|
---|
68 |
|
---|
69 | if (lastParam.equalsIgnoreCase(CLIPBOARD))
|
---|
70 | _clipboard = true;
|
---|
71 | else
|
---|
72 | _outFile = lastParam;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public WriteTree(String format, String outFile) {
|
---|
77 | _format = format;
|
---|
78 | _outFile = outFile;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void setFollowLinks(boolean val) {
|
---|
82 | _followLinks = val;
|
---|
83 | }
|
---|
84 |
|
---|
85 | @SuppressWarnings("unchecked")
|
---|
86 | @Override
|
---|
87 | public boolean initialise(Frame start, Item launcher) {
|
---|
88 | if (_outFile == null)
|
---|
89 | _outFile = start.getExportFileTagValue();
|
---|
90 |
|
---|
91 | try {
|
---|
92 | Class c = null;
|
---|
93 |
|
---|
94 | try {
|
---|
95 | c = Class
|
---|
96 | .forName(IO_PACKAGE + _format.toUpperCase() + "Writer");
|
---|
97 | } catch (NoClassDefFoundError ex) {
|
---|
98 | c = Class.forName(IO_PACKAGE + _format + "Writer");
|
---|
99 | }
|
---|
100 |
|
---|
101 | Constructor con = c.getConstructor();
|
---|
102 | Object o = con.newInstance();
|
---|
103 |
|
---|
104 | // if o is not a tree or frame writer, then it is not a valid
|
---|
105 | // format
|
---|
106 |
|
---|
107 | if (_followLinks) {
|
---|
108 | // check that o is a valid treewriter
|
---|
109 | if (!(o instanceof TreeWriter)) {
|
---|
110 | MessageBay.warningMessage(_format.toUpperCase()
|
---|
111 | + " format cannot be used to write trees.");
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 |
|
---|
115 | _treeWriter = (TreeWriter) o;
|
---|
116 |
|
---|
117 | if (_clipboard)
|
---|
118 | _treeWriter.setOutputLocation(CLIPBOARD);
|
---|
119 | else if (_outFile != null)
|
---|
120 | _treeWriter.setOutputLocation(_outFile);
|
---|
121 | } else {
|
---|
122 | if (!(o instanceof FrameWriter) && !(o instanceof TreeWriter)) {
|
---|
123 | MessageBay.warningMessage(_format.toUpperCase()
|
---|
124 | + " format cannot be used to write frames.");
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | _frameWriter = (FrameWriter) o;
|
---|
129 |
|
---|
130 | if (_clipboard)
|
---|
131 | _frameWriter.setOutputLocation(CLIPBOARD);
|
---|
132 | else if (_outFile != null)
|
---|
133 | _frameWriter.setOutputLocation(_outFile);
|
---|
134 | }
|
---|
135 |
|
---|
136 | } catch (ClassNotFoundException e) {
|
---|
137 | MessageBay
|
---|
138 | .warningMessage("The agent does not exist or has incorrect parametres.");
|
---|
139 | return false;
|
---|
140 | } catch (Exception e) {
|
---|
141 | e.printStackTrace();
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 |
|
---|
145 | return super.initialise(start, launcher);
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | protected Frame process(Frame frame) {
|
---|
150 | String msg = "Failed.";
|
---|
151 | try {
|
---|
152 |
|
---|
153 | if (_followLinks) {
|
---|
154 | msg = _treeWriter.writeTree(frame);
|
---|
155 | _frameCount = _treeWriter.getFrameCount();
|
---|
156 | } else {
|
---|
157 | msg = _frameWriter.writeFrame(frame);
|
---|
158 | _frameCount = 1;
|
---|
159 | }
|
---|
160 | } catch (IOException e) {
|
---|
161 | //System.out.println("Caught");
|
---|
162 | Logger.Log(e);
|
---|
163 | MessageBay.errorMessage("IOException 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 | }
|
---|