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