1 package hudson; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.List ; 6 import java.util.regex.Matcher ; 7 import java.util.regex.Pattern ; 8 9 20 public class MarkupText { 21 private final String text; 22 23 26 private final List <Tag> tags = new ArrayList <Tag>(); 27 28 31 private static final class Tag implements Comparable <Tag> { 32 private final int pos; 33 private final String markup; 34 35 public Tag(int pos, String markup) { 36 this.pos = pos; 37 this.markup = markup; 38 } 39 40 public int compareTo(Tag that) { 41 return this.pos-that.pos; 42 } 43 } 44 45 48 public final class SubText { 49 private final int start,end; 50 private final int[] groups; 51 52 public SubText(Matcher m) { 53 start = m.start(); 54 end = m.end(); 55 56 int cnt = m.groupCount(); 57 groups = new int[cnt*2]; 58 for( int i=0; i<cnt; i++ ) { 59 groups[i*2 ] = m.start(i+1); 60 groups[i*2+1] = m.end(i+1); 61 } 62 } 63 64 72 public void surroundWith(String startTag, String endTag) { 73 addMarkup(start,end,replace(startTag),replace(endTag)); 74 } 75 76 83 public int start(int groupIndex) { 84 if(groupIndex==0) return start; 85 return groups[groupIndex*2-2]; 86 } 87 88 91 public int start() { 92 return start; 93 } 94 95 98 public int end(int groupIndex) { 99 if(groupIndex==0) return end; 100 return groups[groupIndex*2-1]; 101 } 102 103 106 public int end() { 107 return end; 108 } 109 110 113 public String group(int groupIndex) { 114 if(start(groupIndex)==-1) 115 return null; 116 return text.substring(start(groupIndex),end(groupIndex)); 117 } 118 119 122 private String replace(String s) { 123 StringBuffer buf = new StringBuffer (); 124 125 for( int i=0; i<s.length(); i++) { 126 char ch = s.charAt(i); 127 if (ch == '\\') { i++; 129 buf.append(s.charAt(i)); 130 } else if (ch == '$') { i++; 132 133 int groupId = s.charAt(i) - '0'; 135 136 String group = group(groupId); 138 if (group != null) 139 buf.append(group); 140 } else { 141 buf.append(ch); 143 } 144 } 145 146 return buf.toString(); 147 } 148 } 149 150 public MarkupText(String text) { 151 this.text = text; 152 } 153 154 158 public String getText() { 159 return text; 160 } 161 162 169 public void addMarkup( int startPos, int endPos, String startTag, String endTag ) { 170 rangeCheck(startPos); 171 rangeCheck(endPos); 172 if(startPos>endPos) throw new IndexOutOfBoundsException (); 173 174 tags.add(0,new Tag(startPos, startTag)); 178 tags.add(new Tag(endPos,endTag)); 179 } 180 181 private void rangeCheck(int pos) { 182 if(pos<0 || pos>text.length()) 183 throw new IndexOutOfBoundsException (); 184 } 185 186 189 public String toString() { 190 if(tags.isEmpty()) 191 return text; 193 Collections.sort(tags); 195 StringBuilder buf = new StringBuilder (); 196 buf.append(text); 197 int offset = 0; for (Tag tag : tags) { 199 buf.insert(tag.pos+offset,tag.markup); 200 offset += tag.markup.length(); 201 } 202 203 return buf.toString(); 204 } 205 206 219 public List <SubText> findTokens(Pattern pattern) { 220 Matcher m = pattern.matcher(text); 221 List <SubText> r = new ArrayList <SubText>(); 222 223 while(m.find()) { 224 int idx = m.start(); 225 if(idx>0) { 226 char ch = text.charAt(idx-1); 227 if(Character.isLetter(ch) || Character.isDigit(ch)) 228 continue; } 230 idx = m.end(); 231 if(idx<text.length()) { 232 char ch = text.charAt(idx); 233 if(Character.isLetter(ch) || Character.isDigit(ch)) 234 continue; } 236 r.add(new SubText(m)); 237 } 238 239 return r; 240 } 241 } 242 | Popular Tags |