1
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
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
47 public Resource init() throws ResourceInstantiationException
48 {
49 return super.init();
50 }
52
60 public void reInit() throws ResourceInstantiationException
61 {
62 init();
63 }
65
66 public void execute() throws ExecutionException {
67
68 if(document == null)
69 throw new GateRuntimeException("No document to process!");
70
71 if (annotationTypes == null || annotationTypes.isEmpty())
73 document.getAnnotations().clear();
74 else
75 removeSubSet(document.getAnnotations());
76
77 Map namedSets = document.getNamedAnnotationSets();
79 if (namedSets == null || namedSets.isEmpty())
81 return;
82
83 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 if (setName != null && !setName.equals(markupSetName) ) {
93 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 } }
102
103 }
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 }
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 }