1   /*
2    *  AnnotationDeletePR.java
3    *
4    *  Copyright (c) 1998-2001, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Kalina Bontcheva, 19/10/2001
12   *
13   *  $Id: AnnotationDeletePR.java,v 1.5 2001/11/24 17:01:39 kalina Exp $
14   */
15  
16  package gate.creole.annotdelete;
17  
18  import java.util.*;
19  import gate.*;
20  import gate.creole.*;
21  import gate.util.*;
22  
23  /**
24   * This class is the implementation of a processing resource which
25   * deletes all annotations and sets other than 'original markups'.
26   * If put at the start of an application, it'll ensure that the
27   * document is restored to its clean state before being processed.
28   */
29  public class AnnotationDeletePR extends AbstractLanguageAnalyser
30    implements ProcessingResource {
31  
32    protected String markupSetName = GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME;
33    protected List annotationTypes;
34  
35    /** Initialise this resource, and return it. */
36    public Resource init() throws ResourceInstantiationException
37    {
38      return super.init();
39    } // init()
40  
41    /**
42    * Reinitialises the processing resource. After calling this method the
43    * resource should be in the state it is after calling init.
44    * If the resource depends on external resources (such as rules files) then
45    * the resource will re-read those resources. If the data used to create
46    * the resource has changed since the resource has been created then the
47    * resource will change too after calling reInit().
48    */
49    public void reInit() throws ResourceInstantiationException
50    {
51      init();
52    } // reInit()
53  
54    /** Run the resource. */
55    public void execute() throws ExecutionException {
56  
57      if(document == null)
58        throw new GateRuntimeException("No document to process!");
59  
60      //first clear the default set, which cannot be removed
61      if (annotationTypes == null || annotationTypes.isEmpty())
62        document.getAnnotations().clear();
63      else
64        removeSubSet(document.getAnnotations());
65  
66      //get the names of all sets
67      Map namedSets = document.getNamedAnnotationSets();
68      //nothing left to do if there are no named sets
69      if (namedSets == null || namedSets.isEmpty())
70        return;
71  
72      //loop through the sets and delete them all unless they're original markups
73      List setNames = new ArrayList(namedSets.keySet());
74      Iterator iter = setNames.iterator();
75      while (iter.hasNext()) {
76        String setName = (String) iter.next();
77        if (! setName.equals(markupSetName)) {
78          if(annotationTypes == null || annotationTypes.isEmpty())
79            document.removeAnnotationSet(setName);
80          else
81            removeSubSet(document.getAnnotations(setName));
82        }//if
83      }
84  
85    } // execute()
86  
87    private void removeSubSet(AnnotationSet theSet) {
88      AnnotationSet toRemove = theSet.get(new HashSet(annotationTypes));
89      if (toRemove == null || toRemove.isEmpty())
90        return;
91      theSet.removeAll(toRemove);
92  
93    }//removeSubSet
94  
95    public void setMarkupASName(String newMarkupASName) {
96      markupSetName = newMarkupASName;
97    }
98  
99    public String  getMarkupASName() {
100     return markupSetName;
101   }
102 
103   public List getAnnotationTypes() {
104     return this.annotationTypes;
105   }
106 
107   public void setAnnotationTypes(List newTypes) {
108     annotationTypes = newTypes;
109   }
110 
111 } // class AnnotationSetTransfer
112