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