1   /*
2    *  AnnotationDeletePR.java
3    *
4    *  Copyright (c) 1998-2004, 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.12 2004/07/21 17:10:03 akshay Exp $
14   */
15  
16  package gate.creole.annotdelete;
17  
18  import java.util.*;
19  
20  import gate.*;
21  import gate.creole.*;
22  import gate.util.GateRuntimeException;
23  
24  /**
25   * This class is the implementation of a processing resource which
26   * deletes all annotations and sets other than 'original markups'.
27   * If put at the start of an application, it'll ensure that the
28   * document is restored to its clean state before being processed.
29   */
30  public class AnnotationDeletePR extends AbstractLanguageAnalyser
31    implements ProcessingResource {
32  
33    public static final String
34      TRANSD_DOCUMENT_PARAMETER_NAME = "document";
35  
36    public static final String
37      TRANSD_ANNOT_TYPES_PARAMETER_NAME = "annotationTypes";
38  
39    public static final String
40      TRANSD_SETS_KEEP_PARAMETER_NAME = "setsToKeep";
41  
42    protected String markupSetName = GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME;
43    protected List annotationTypes;
44    protected List setsToKeep;
45  
46    /** Initialise this resource, and return it. */
47    public Resource init() throws ResourceInstantiationException
48    {
49      return super.init();
50    } // init()
51  
52    /**
53    * Reinitialises the processing resource. After calling this method the
54    * resource should be in the state it is after calling init.
55    * If the resource depends on external resources (such as rules files) then
56    * the resource will re-read those resources. If the data used to create
57    * the resource has changed since the resource has been created then the
58    * resource will change too after calling reInit().
59    */
60    public void reInit() throws ResourceInstantiationException
61    {
62      init();
63    } // reInit()
64  
65    /** Run the resource. */
66    public void execute() throws ExecutionException {
67  
68      if(document == null)
69        throw new GateRuntimeException("No document to process!");
70  
71      //first clear the default set, which cannot be removed
72      if (annotationTypes == null || annotationTypes.isEmpty())
73        document.getAnnotations().clear();
74      else
75        removeSubSet(document.getAnnotations());
76  
77      //get the names of all sets
78      Map namedSets = document.getNamedAnnotationSets();
79      //nothing left to do if there are no named sets
80      if (namedSets == null || namedSets.isEmpty())
81        return;
82  
83      //loop through the sets and delete them all unless they're original markups
84      List setNames = new ArrayList(namedSets.keySet());
85      Iterator iter = setNames.iterator();
86      String setName;
87  
88      while (iter.hasNext()) {
89        setName = (String) iter.next();
90        //check first whether this is the original markups or one of the sets
91        //that we want to keep
92        if (setName != null && !setName.equals(markupSetName) ) {
93          // skip named sets from setsToKeep
94          if(setsToKeep != null && setsToKeep.contains(setName)) continue;
95  
96          if (annotationTypes == null || annotationTypes.isEmpty())
97            document.removeAnnotationSet(setName);
98          else
99            removeSubSet(document.getAnnotations(setName));
100       }//if
101     }
102 
103   } // execute()
104 
105   private void removeSubSet(AnnotationSet theSet) {
106     AnnotationSet toRemove = theSet.get(new HashSet(annotationTypes));
107     if (toRemove == null || toRemove.isEmpty())
108       return;
109     theSet.removeAll(toRemove);
110 
111   }//removeSubSet
112 
113   public void setMarkupASName(String newMarkupASName) {
114     markupSetName = newMarkupASName;
115   }
116 
117   public String  getMarkupASName() {
118     return markupSetName;
119   }
120 
121   public List getAnnotationTypes() {
122     return this.annotationTypes;
123   }
124 
125   public void setAnnotationTypes(List newTypes) {
126     annotationTypes = newTypes;
127   }
128 
129   public List getSetsToKeep() {
130     return this.setsToKeep;
131   }
132 
133   public void setSetsToKeep(List newSetNames) {
134     setsToKeep = newSetNames;
135   }
136 
137 
138 } // class AnnotationSetTransfer
139