1 19 20 package org.netbeans.modules.tasklist.copyright; 21 22 import javax.swing.text.*; 23 import java.awt.*; 24 import java.text.SimpleDateFormat ; 25 import javax.swing.*; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 import java.util.regex.*; 30 31 import org.openide.ErrorManager; 32 import org.openide.loaders.DataObject; 33 import org.openide.loaders.DataObjectNotFoundException; 34 import org.openide.util.NbBundle; 35 36 import org.netbeans.modules.tasklist.client.*; 37 import org.netbeans.modules.tasklist.providers.DocumentSuggestionProvider; 38 import org.netbeans.modules.tasklist.providers.SuggestionContext; 39 40 import org.netbeans.modules.tasklist.core.TLUtils; 41 42 68 public class CopyrightChecker extends DocumentSuggestionProvider { 69 final private static String TYPE = "nb-tasklist-copyright"; 71 private final String year = new SimpleDateFormat ("yyyy").format(new Date ()); 73 74 75 private List showingTasks = null; 76 77 private Pattern re; 78 79 public CopyrightChecker() { 80 try { 81 re = Pattern.compile("copyright|Copyright|COPYRIGHT|\\(C\\)|\\(c\\)|\u00a9"); } catch (PatternSyntaxException e) { 84 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 87 } 88 } 89 90 public String getType() { 91 return TYPE; 92 } 93 94 public List scan(SuggestionContext env) { 95 SuggestionManager manager = SuggestionManager.getDefault(); 96 if (!manager.isEnabled(TYPE)) { 97 return null; 98 } 99 Suggestion s = checkCopyright(env); 100 if (s != null) { 101 List tasks = new ArrayList (1); 102 tasks.add(s); 103 return tasks; 104 } 105 return null; 106 } 107 108 146 private Suggestion checkCopyright(final SuggestionContext env) { 147 CharSequence chars = env.getCharSequence(); 149 int len = chars.length(); 150 if (len > 10 * 80) { 151 len = 10 * 80; } 153 154 int index = 0; 155 Matcher matcher = re.matcher(chars); 156 if (matcher.find(index)) { int begin = matcher.start(); 158 index = matcher.end(); 159 160 164 while (index < len) { 166 char c = chars.charAt(index); 167 if (c == '\n' || c == '\r') { 168 break; 169 } 170 index++; 171 } 172 173 while (begin >= 0) { 175 char c = chars.charAt(begin); 176 if (c == '\n' || c == '\r') { 177 break; 178 } 179 begin--; 180 } 181 begin++; CharSequence line; 183 if (index < len) { 184 line = chars.subSequence(begin, index); 185 } else { 186 line = chars.subSequence(begin, chars.length()); 187 } 188 189 int n = line.length() - 3; int c0 = -1; int cp = -1; int rangeEnd = -1; 194 int listEnd = -1; 195 int dateEnd = -1; 196 int firstDate = 0; 197 for (int i = 0; i < n; i++) { 198 char c1 = line.charAt(i); 199 char c2 = line.charAt(i + 1); 200 char c3 = line.charAt(i + 2); 201 char c4 = line.charAt(i + 3); 202 if ((c1 == '1' || (c1 == '2')) && (c2 == '9' || (c2 == '0')) && Character.isDigit(c3) && 205 Character.isDigit(c4)) { 206 207 if (firstDate == 0) { 208 firstDate = i; 209 } 210 211 216 if ((c4 == year.charAt(3)) && 218 (c3 == year.charAt(2)) && 219 (c2 == year.charAt(1)) && 220 (c1 == year.charAt(0))) { 221 return null; 223 } 224 225 if (c0 == '-') { 227 rangeEnd = i; 229 } else if ((c0 == ',') || ((cp == ',') && (c0 == ' '))) { 230 listEnd = i; 232 } else { 233 dateEnd = i; 234 } 235 } 236 cp = c0; 237 c0 = c1; 238 } 239 240 if ((rangeEnd == -1) && (listEnd == -1) && (dateEnd == -1)) { 241 return null; 244 } 245 246 if (dateEnd > listEnd) { 249 listEnd = -1; 250 } 251 if (listEnd > rangeEnd) { 252 rangeEnd = -1; 253 } 254 255 int lastDate = 0; 256 if (rangeEnd != -1) { 257 lastDate = rangeEnd + 4; 258 } else if (listEnd != -1) { 259 lastDate = listEnd + 4; 260 } else if (dateEnd != -1) { 261 lastDate = dateEnd + 4; 262 } 263 264 String range; 265 if (rangeEnd != -1) { 266 range = line.subSequence(firstDate, rangeEnd).toString() + year; 267 } else if (listEnd != -1) { 268 range = line.subSequence(firstDate, lastDate).toString() + 269 ", " + year; } else { range = line.subSequence(firstDate, lastDate).toString() + 272 "-" + year; } 274 275 final String oldRange = line.subSequence(firstDate, lastDate).toString(); 276 final String newRange = range; 277 String description = 278 NbBundle.getMessage(CopyrightChecker.class, 279 "CopyrightDesc", newRange, oldRange); 281 282 int linenum = 1; 283 for (int k = 0; k < begin; k++) { 284 if (chars.charAt(k) == '\n') { 288 linenum++; 289 } 290 } 291 final int lineno = linenum; 292 293 SuggestionManager manager = SuggestionManager.getDefault(); 294 SuggestionAgent copyrightTask = manager.createSuggestion( 295 env.getFileObject(), TYPE, description, null, this); 296 try { 297 DataObject dataObject = DataObject.find(env.getFileObject()); 298 copyrightTask.setLine(TLUtils.getLineByNumber(dataObject, lineno)); 299 } catch (DataObjectNotFoundException e) { 300 ErrorManager.getDefault().notify(e); 302 } 303 304 310 final int fRangeEnd = rangeEnd; 314 final int fBegin = begin; 315 final int fListEnd = listEnd; 316 final int fDateEnd = dateEnd; 317 final Document doc = env.getDocument(); 318 319 SuggestionPerformer jackson = new ChangeCopyrightDatesPerformer( 320 env, fRangeEnd, fBegin, fListEnd, fDateEnd, doc, lineno, year); 321 copyrightTask.setAction(jackson); 322 copyrightTask.setPriority(SuggestionPriority.LOW); 323 return copyrightTask.getSuggestion(); 324 } else { 325 328 String warning = NbBundle.getMessage(CopyrightChecker.class, 329 "NoCopyright"); 331 SuggestionPerformer action = new AddCopyrightPerformer(env); 332 SuggestionManager manager = SuggestionManager.getDefault(); 333 SuggestionAgent copyrightTask = manager.createSuggestion( 334 env.getFileObject(), TYPE, warning, action, this); 335 try { 336 DataObject dataObject = DataObject.find(env.getFileObject()); 337 copyrightTask.setLine(TLUtils.getLineByNumber(dataObject, 1)); 338 } catch (DataObjectNotFoundException e) { 339 ErrorManager.getDefault().notify(e); 341 } 342 343 return copyrightTask.getSuggestion(); 345 } 346 } 347 } 348 | Popular Tags |