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

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 4.7 KB
Line 
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
19package org.expeditee.agents;
20
21import java.io.IOException;
22import java.lang.reflect.Constructor;
23
24import org.expeditee.actions.Actions;
25import org.expeditee.gui.Frame;
26import org.expeditee.gui.MessageBay;
27import org.expeditee.io.FrameWriter;
28import org.expeditee.io.TreeWriter;
29import org.expeditee.items.Item;
30import org.expeditee.stats.Logger;
31
32public 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 int ind = params.indexOf(" ");
64 if (ind > 0) {
65 String lastParam = params.substring(ind + 1);
66 _format = params.substring(0, ind).toLowerCase();
67
68 if (lastParam.equalsIgnoreCase(CLIPBOARD))
69 _clipboard = true;
70 else
71 _outFile = lastParam;
72 }
73 }
74
75 public WriteTree(String format, String outFile) {
76 _format = format;
77 _outFile = outFile;
78 }
79
80 public void setFollowLinks(boolean val) {
81 _followLinks = val;
82 }
83
84 @SuppressWarnings("unchecked")
85 @Override
86 public boolean initialise(Frame start, Item launcher) {
87 if (_outFile == null)
88 _outFile = start.getExportFileTagValue();
89
90 try {
91 Class c = null;
92
93 try {
94 c = Class
95 .forName(IO_PACKAGE + _format.toUpperCase() + "Writer");
96 } catch (NoClassDefFoundError ex) {
97 c = Class.forName(IO_PACKAGE + _format + "Writer");
98 }
99
100 Constructor con = c.getConstructor();
101 Object o = con.newInstance();
102
103 // if o is not a tree or frame writer, then it is not a valid
104 // format
105
106 if (_followLinks) {
107 // check that o is a valid treewriter
108 if (!(o instanceof TreeWriter)) {
109 MessageBay.warningMessage(_format.toUpperCase()
110 + " format cannot be used to write trees.");
111 return false;
112 }
113
114 _treeWriter = (TreeWriter) o;
115
116 if (_clipboard)
117 _treeWriter.setOutputLocation(CLIPBOARD);
118 else if (_outFile != null)
119 _treeWriter.setOutputLocation(_outFile);
120 } else {
121 if (!(o instanceof FrameWriter) && !(o instanceof TreeWriter)) {
122 MessageBay.warningMessage(_format.toUpperCase()
123 + " format cannot be used to write frames.");
124 return false;
125 }
126
127 _frameWriter = (FrameWriter) o;
128
129 if (_clipboard)
130 _frameWriter.setOutputLocation(CLIPBOARD);
131 else if (_outFile != null)
132 _frameWriter.setOutputLocation(_outFile);
133 }
134
135 } catch (ClassNotFoundException e) {
136 MessageBay
137 .warningMessage("The agent does not exist or has incorrect parametres.");
138 return false;
139 } catch (Exception e) {
140 e.printStackTrace();
141 return false;
142 }
143
144 return super.initialise(start, launcher);
145 }
146
147 @Override
148 protected Frame process(Frame frame) {
149 String msg = "Failed.";
150 try {
151
152 if (_followLinks) {
153 msg = _treeWriter.writeTree(frame);
154 _frameCount = _treeWriter.getFrameCount();
155 } else {
156 msg = _frameWriter.writeFrame(frame);
157 _frameCount = 1;
158 }
159 } catch (IOException e) {
160 //System.out.println("Caught");
161 Logger.Log(e);
162 MessageBay.errorMessage("IOException in WriteTree: "
163 + e.getMessage());
164 e.printStackTrace();
165 super.stop();
166 return null;
167 }
168
169 if (_stop)
170 message("WriteTree halted by user.");
171 else
172 message(msg);
173
174 return null;
175 }
176
177 @Override
178 public void stop() {
179 super.stop();
180
181 if (_treeWriter != null)
182 _treeWriter.stop();
183
184 if (_frameWriter != null)
185 _frameWriter.stop();
186 }
187
188 @Override
189 public boolean isRunning() {
190 if (_treeWriter != null)
191 return _treeWriter.isRunning();
192
193 if (_frameWriter != null)
194 return _frameWriter.isRunning();
195
196 return false;
197 }
198
199}
Note: See TracBrowser for help on using the repository browser.