1 19 20 23 package org.netbeans.modules.tasklist.docscan; 24 25 import org.netbeans.modules.tasklist.providers.SuggestionContext; 26 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.IOException ; 31 import java.io.Reader ; 32 import java.io.StringReader ; 33 import java.util.regex.Pattern ; 34 import java.util.regex.Matcher ; 35 36 37 43 final class SourceCodeCommentParser { 44 45 48 public SourceCodeCommentParser() { 49 parser = new SourceParser(); 50 } 51 52 57 public SourceCodeCommentParser(String lineComment) { 58 parser = new CommentParser(lineComment); 59 } 60 61 67 public SourceCodeCommentParser(String blockStart, String blockEnd) { 68 parser = new CommentParser(null, blockStart, blockEnd); 69 } 70 71 78 public SourceCodeCommentParser(String lineComment, 79 String blockStart, 80 String blockEnd) { 81 parser = new CommentParser(lineComment, blockStart, blockEnd); 82 } 83 84 88 public void setDocument(SuggestionContext env) { 89 parser.setDocument(env); 90 } 91 92 97 public boolean nextRegion(CommentRegion reg) throws IOException { 98 return parser.nextRegion(reg); 99 } 100 101 108 private static class SourceParser { 109 110 113 public SourceParser() { 114 text = null; 115 curr = 0; 116 matcher = null; 117 } 118 119 126 public boolean nextRegion(CommentRegion reg) throws IOException { 127 if (text == null) { 128 return false; 129 } 130 131 reg.start = curr; 132 reg.stop = text.length(); 133 134 if (reg.start == reg.stop) { 135 return false; 136 } 137 138 curr = reg.stop; 139 return true; 140 } 141 142 146 public void setDocument(SuggestionContext env) { 147 text = env.getCharSequence().toString(); 148 149 if (pattern != null) { 150 matcher = pattern.matcher(text); 151 } 152 } 153 154 159 protected void appendEncodedChars(StringBuffer buf, String str) { 160 int len = str.length(); 161 162 for (int ii = 0; ii < len; ++ii) { 163 String s = Integer.toHexString((int)str.charAt(ii)); 164 165 buf.append("\\u"); 166 for(int i = 0, n = 4 - s.length(); i < n; i++) { 167 buf.append('0'); 168 } 169 buf.append(s); 170 } 171 } 172 173 177 protected String text; 178 179 180 protected int curr; 181 182 183 protected Matcher matcher; 184 185 protected Pattern pattern; 186 187 } 188 189 194 private static class CommentParser extends SourceParser { 195 200 public CommentParser(String lineComment) { 201 this(lineComment, null, null); 202 } 203 204 210 public CommentParser(String lineComment, 211 String blockStart, 212 String blockEnd) { 213 super(); 214 this.lineComment = lineComment; 215 this.blockStart = blockStart; 216 this.blockEnd = blockEnd; 217 218 StringBuffer sb = new StringBuffer (); 219 220 boolean needor = false; 221 222 if (lineComment != null) { 223 appendEncodedChars(sb, lineComment); 224 needor = true; 225 } 226 227 if (blockStart != null) { 228 if (needor) { 229 sb.append('|'); 230 } 231 appendEncodedChars(sb, blockStart); 232 } 233 234 pattern = Pattern.compile(sb.toString()); 235 matcher = null; 236 } 237 238 245 public boolean nextRegion(CommentRegion reg) throws IOException { 246 boolean ret = false; 247 248 if (matcher != null && matcher.find(curr)) { 249 String token = text.substring(matcher.start(), matcher.end()); 250 251 reg.start = matcher.start(); 252 253 if (lineComment != null && lineComment.equals(token)) { 254 int idx = text.indexOf("\n", reg.start); 255 if (idx != -1) { 256 reg.stop = idx; 257 } else { 258 reg.stop = text.length(); 259 } 260 } else if (blockStart != null) { 261 int idx = text.indexOf(blockEnd, reg.start); 262 if (idx != -1) { 263 reg.stop = idx + blockEnd.length(); 264 } else { 265 reg.stop = text.length(); 266 } 267 } else { 268 return false; } 270 271 curr = reg.stop + 1; 272 ret = true; 273 } 274 return ret; 275 } 276 277 278 protected String lineComment; 279 280 protected String blockStart; 281 282 protected String blockEnd; 283 284 } 285 286 287 public static class CommentRegion { 288 289 public int start; 290 291 public int stop; 292 293 294 public CommentRegion() { 295 start = stop = 0; 296 } 297 } 298 299 300 private SourceParser parser; 301 } 302 | Popular Tags |