1   package gate.util.web;
2   
3   import org.apache.regexp.*;
4   import java.util.*;
5   
6   public class TagHighlighter {
7   
8       private HashMap tagColors;
9   
10      public TagHighlighter () {
11          tagColors = new HashMap();
12          tagColors.put("Person", "#FFA0FF");
13          tagColors.put("Location", "#A0FFFF");
14          tagColors.put("Organization", "#FFFFA0");
15      }
16  
17      public void colorTag(String tag, String color) {
18          tagColors.put(tag, color);
19      }
20  
21      public String getColor(String tag) {
22          return (String) tagColors.get(tag);
23      }
24  
25      public String highlightText(String text) {
26          Iterator tags = tagColors.keySet().iterator();
27          while (tags.hasNext()) {
28              String tag = (String) tags.next();
29              String color = (String) tagColors.get(tag);
30  
31              try {
32                  RE r = new RE("(<" + tag + " .*?>)");
33                  if (r.match(text)) {
34                      text = r.subst(text,
35                                     "<B style=\"color:black;background-color:" +
36                                     color +
37                                     "\">" + r.getParen(1));
38                  }  
39                  
40                  r = new RE("(</" + tag + ">)");
41                  if (r.match(text)) {
42                      text = r.subst(text, r.getParen(1) + "</B>");
43                  }
44              } catch (RESyntaxException rese) {
45                  // log something, I guess
46              }
47          }
48  
49          return text;
50      }
51  }
52