TagHighlighter.java
01 package gate.util.web;
02 
03 import java.util.HashMap;
04 import java.util.Iterator;
05 
06 import java.util.regex.Matcher;
07 import java.util.regex.Pattern;
08 
09 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 (StringtagColors.get(tag);
26     }
27 
28     public String highlightText(String text) {
29         Iterator tags = tagColors.keySet().iterator();
30         while (tags.hasNext()) {
31             String tag = (Stringtags.next();
32             String color = (StringtagColors.get(tag);
33             Pattern pattern = Pattern.compile("(<" + tag + " .*?>)");
34             String toAppend = "<B style=\"color:black;background-color:" + color + "\">";
35             
36             Matcher matcher = pattern.matcher(text);
37             while (matcher.find()){
38               String group = matcher.group(1);
39               text = text.replaceAll(group,toAppend + group);
40             }
41             
42             String closing = "(</" + tag + ">)";
43             String closingReplacement = "</" + tag + "></B>";
44             text = text.replaceAll(closing,closingReplacement);
45         }
46 
47         return text;
48     }
49 }