01 /*
02 * CorpusControllerDocumentProcessor.java
03 * Copyright (c) 1998-2009, The University of Sheffield.
04 *
05 * This code is from the GATE project (http://gate.ac.uk/) and is free
06 * software licenced under the GNU General Public License version 3. It is
07 * distributed without any warranty. For more details see COPYING.txt in the
08 * top level directory (or at http://gatewiki.sf.net/COPYING.txt).
09 *
10 * Ian Roberts, 3rd September 2009
11 */
12
13 package gate.util;
14
15 import gate.Corpus;
16 import gate.CorpusController;
17 import gate.Document;
18 import gate.Factory;
19 import gate.creole.ExecutionException;
20
21 /**
22 * {@link DocumentProcessor} that processes documents using a
23 * {@link CorpusController}.
24 */
25 public class CorpusControllerDocumentProcessor implements DocumentProcessor {
26
27 /**
28 * The controller used to process documents.
29 */
30 private CorpusController controller;
31
32 /**
33 * Corpus used to contain the document being processed.
34 */
35 private Corpus corpus;
36
37 public CorpusControllerDocumentProcessor() {
38 }
39
40 /**
41 * Set the controller used to process documents.
42 */
43 public void setController(CorpusController c) {
44 this.controller = c;
45 }
46
47 public synchronized void processDocument(Document doc) throws GateException {
48 if(corpus == null) {
49 corpus = Factory.newCorpus("DocumentProcessor corpus");
50 }
51 try {
52 corpus.add(doc);
53 controller.setCorpus(corpus);
54 controller.execute();
55 }
56 finally {
57 controller.setCorpus(null);
58 corpus.clear();
59 }
60 }
61
62 /**
63 * Clean up resources. Should be called when this processor is no longer
64 * required.
65 */
66 public synchronized void cleanup() {
67 Factory.deleteResource(controller);
68 Factory.deleteResource(corpus);
69 }
70 }
|