1
13 package gate.creole;
14
15 import gate.Resource;
16 import gate.gui.MainFrame;
17 import gate.jape.Batch;
18 import gate.jape.JapeException;
19 import gate.util.Err;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.util.ArrayList;
26 import java.util.List;
27 import javax.swing.JFileChooser;
28 import javax.swing.JOptionPane;
29
30
34 public class Transducer extends AbstractLanguageAnalyser
35 implements
36 gate.gui.ActionsPublisher {
37 public static final String TRANSD_DOCUMENT_PARAMETER_NAME = "document";
38
39 public static final String TRANSD_INPUT_AS_PARAMETER_NAME = "inputASName";
40
41 public static final String TRANSD_OUTPUT_AS_PARAMETER_NAME = "outputASName";
42
43 public static final String TRANSD_ENCODING_PARAMETER_NAME = "encoding";
44
45 public static final String TRANSD_GRAMMAR_URL_PARAMETER_NAME = "grammarURL";
46
47 public static final String TRANSD_BINARY_GRAMMAR_URL_PARAMETER_NAME = "binaryGrammarURL";
48
49 protected List actionList;
50
51
56 public Transducer() {
57 actionList = new ArrayList();
58 actionList.add(null);
59 actionList.add(new SerializeTransducerAction());
60 }
61
62
67
74 public Resource init() throws ResourceInstantiationException {
75 try {
76 fireProgressChanged(0);
77 if(binaryGrammarURL != null) {
78 ObjectInputStream s = new ObjectInputStream(binaryGrammarURL
79 .openStream());
80 batch = (gate.jape.Batch)s.readObject();
81 } else if(grammarURL != null) {
82 if(encoding != null) {
83 batch = new Batch(grammarURL, encoding, new InternalStatusListener());
84 if(enableDebugging != null) {
85 batch.setEnableDebugging(enableDebugging.booleanValue());
86 } else {
87 batch.setEnableDebugging(false);
88 }
89 batch.setOntology(ontology);
90 } else {
91 throw new ResourceInstantiationException("encoding is not set!");
92 }
93 } else {
94 throw new ResourceInstantiationException(
95 "Neither grammarURL or binaryGrammarURL parameters are set!");
96 }
97 } catch(Exception e) {
98 throw new ResourceInstantiationException(e);
99 } finally {
100 fireProcessFinished();
101 }
102 batch.addProgressListener(new IntervalProgressListener(0, 100));
103 return this;
104 }
105
106
110 public void execute() throws ExecutionException {
111 interrupted = false;
112 if(document == null) throw new ExecutionException("No document provided!");
113 if(inputASName != null && inputASName.equals("")) inputASName = null;
114 if(outputASName != null && outputASName.equals("")) outputASName = null;
115 try {
116 batch.transduce(document, inputASName == null
117 ? document.getAnnotations()
118 : document.getAnnotations(inputASName), outputASName == null
119 ? document.getAnnotations()
120 : document.getAnnotations(outputASName));
121 } catch(JapeException je) {
122 throw new ExecutionException(je);
123 }
124 }
125
126
131 public List getActions() {
132 List result = new ArrayList();
133 result.addAll(actionList);
134 return result;
135 }
136
137
142 protected class SerializeTransducerAction extends javax.swing.AbstractAction {
143 public SerializeTransducerAction() {
144 super("Serialize Transducer");
145 putValue(SHORT_DESCRIPTION, "Serializes the Transducer as binary file");
146 }
147
148 public void actionPerformed(java.awt.event.ActionEvent evt) {
149 Runnable runnable = new Runnable() {
150 public void run() {
151 JFileChooser fileChooser = MainFrame.getFileChooser();
152 fileChooser.setFileFilter(fileChooser.getAcceptAllFileFilter());
153 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
154 fileChooser.setMultiSelectionEnabled(false);
155 if(fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
156 File file = fileChooser.getSelectedFile();
157 try {
158 MainFrame.lockGUI("Serializing JAPE Transducer...");
159 FileOutputStream out = new FileOutputStream(file);
160 ObjectOutputStream s = new ObjectOutputStream(out);
161 s.writeObject(batch);
162 s.flush();
163 s.close();
164 out.close();
165 } catch(IOException ioe) {
166 JOptionPane.showMessageDialog(null, "Error!\n" + ioe.toString(),
167 "GATE", JOptionPane.ERROR_MESSAGE);
168 ioe.printStackTrace(Err.getPrintWriter());
169 } finally {
170 MainFrame.unlockGUI();
171 }
172 }
173 }
174 };
175 Thread thread = new Thread(runnable, "Transduer Serialization");
176 thread.setPriority(Thread.MIN_PRIORITY);
177 thread.start();
178 }
179 }
180
181
185 public synchronized void interrupt() {
186 interrupted = true;
187 batch.interrupt();
188 }
189
190
196 public void setGrammarURL(java.net.URL newGrammarURL) {
197 grammarURL = newGrammarURL;
198 }
199
200
205 public java.net.URL getGrammarURL() {
206 return grammarURL;
207 }
208
209
219 public void setEncoding(String newEncoding) {
220 encoding = newEncoding;
221 }
222
223
226 public String getEncoding() {
227 return encoding;
228 }
229
230
236 public void setInputASName(String newInputASName) {
237 inputASName = newInputASName;
238 }
239
240
245 public String getInputASName() {
246 return inputASName;
247 }
248
249
255 public void setOutputASName(String newOutputASName) {
256 outputASName = newOutputASName;
257 }
258
259
264 public String getOutputASName() {
265 return outputASName;
266 }
267
268 public Boolean getEnableDebugging() {
269 return enableDebugging;
270 }
271
272 public void setEnableDebugging(Boolean enableDebugging) {
273 this.enableDebugging = enableDebugging;
274 }
275
276
279 protected java.net.URL grammarURL;
280
281
284 protected java.net.URL binaryGrammarURL;
285
286
289 protected Batch batch;
290
291
294 protected String encoding;
295
296
299 protected String inputASName;
300
301
304 protected String outputASName;
305
306
309 protected gate.creole.ontology.Ontology ontology;
310
311
316 public gate.creole.ontology.Ontology getOntology() {
317 return ontology;
318 }
319
320
326 public void setOntology(gate.creole.ontology.Ontology ontology) {
327 this.ontology = ontology;
328 if(batch!= null) batch.setOntology(ontology);
331 }
332
333
336 protected Boolean enableDebugging;
337
338
339 public java.net.URL getBinaryGrammarURL() {
340 return binaryGrammarURL;
341 }
342
343 public void setBinaryGrammarURL(java.net.URL binaryGrammarURL) {
344 this.binaryGrammarURL = binaryGrammarURL;
345 }
346 }