001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tasklist.todo;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.io.BufferedReader;
047: import java.io.IOException;
048: import java.io.InputStreamReader;
049: import java.io.Reader;
050: import java.nio.charset.Charset;
051: import java.util.Collection;
052: import java.util.Collections;
053: import java.util.LinkedList;
054: import java.util.List;
055: import java.util.logging.Level;
056: import java.util.logging.Logger;
057: import java.util.regex.Matcher;
058: import java.util.regex.Pattern;
059: import java.util.regex.PatternSyntaxException;
060: import org.netbeans.api.queries.FileEncodingQuery;
061: import org.netbeans.modules.tasklist.todo.settings.Settings;
062: import org.netbeans.spi.tasklist.FileTaskScanner;
063: import org.netbeans.spi.tasklist.Task;
064: import org.openide.filesystems.FileObject;
065: import org.openide.filesystems.FileUtil;
066: import org.openide.util.NbBundle;
067:
068: /**
069: *
070: * @author S. Aubrecht
071: * @author Tor Norbye
072: * @author Trond Norbye
073: */
074: public class TodoTaskScanner extends FileTaskScanner implements
075: PropertyChangeListener {
076:
077: private static final String GROUP_NAME = "nb-tasklist-todo"; //NOI18N
078:
079: private Pattern regexp = null;
080: private Callback callback;
081:
082: /**
083: * Creates a new instance of TodoTaskProvider
084: *
085: */
086: TodoTaskScanner(String displayName, String description) {
087: super (displayName, description, "Advanced"); //NOI18N
088: }
089:
090: public static TodoTaskScanner create() {
091: return new TodoTaskScanner(NbBundle.getBundle(
092: TodoTaskScanner.class).getString("LBL_todotask"), //NOI18N
093: NbBundle.getBundle(TodoTaskScanner.class).getString(
094: "HINT_todotask")); //NOI18N
095: }
096:
097: public List<? extends Task> scan(FileObject resource) {
098: if (!isSupported(resource))
099: return null;
100:
101: if (Settings.getDefault().isScanCommentsOnly()) {
102: return scanComments(resource);
103: }
104: return scanAll(resource);
105: }
106:
107: private List<? extends Task> scanAll(FileObject resource) {
108: List<Task> tasks = null;
109:
110: try {
111: String text = getContent(resource);
112:
113: int index = 0;
114: int lineno = 1;
115: int len = text.length();
116:
117: Matcher matcher = getScanRegexp().matcher(text);
118: while (index < len && matcher.find(index)) {
119: int begin = matcher.start();
120: int end = matcher.end();
121:
122: // begin should be the beginning of this line (but avoid
123: // clash if I have two tokens on the same line...
124: char c = 'a'; // NOI18N
125: int nonwhite = begin;
126: while (begin >= index
127: && (c = text.charAt(begin)) != '\n') { // NOI18N
128: if (c != ' ' && c != '\t') { // NOI18N
129: nonwhite = begin;
130: }
131: --begin;
132: }
133:
134: begin = nonwhite;
135:
136: // end should be the last "nonwhite" character on this line...
137: nonwhite = end;
138: while (end < len) {
139: c = text.charAt(end);
140: if (c == '\n' || c == '\r') {// NOI18N
141: break;
142: } else if (c != ' ' && c != '\t') {// NOI18N
143: nonwhite = end;
144: }
145: ++end;
146: }
147:
148: // calculate current line number
149: int idx = index;
150: while (idx <= begin) {
151: if (text.charAt(idx) == '\n') {// NOI18N
152: ++lineno;
153: }
154: ++idx;
155: }
156:
157: index = end;
158:
159: String description = text.subSequence(begin,
160: nonwhite + 1).toString();
161:
162: Task task = Task.create(resource, GROUP_NAME,
163: description, lineno);
164: if (null == tasks) {
165: tasks = new LinkedList<Task>();
166: }
167: tasks.add(task);
168: }
169: } catch (IOException e) {
170: Logger.getLogger(getClass().getName()).log(Level.INFO,
171: null, e);
172: } catch (OutOfMemoryError oomE) {
173: System.gc();
174: Logger.getLogger(getClass().getName()).log(Level.INFO,
175: null, oomE);
176: }
177: return null == tasks ? getEmptyList() : tasks;
178: }
179:
180: private List<? extends Task> scanComments(FileObject resource) {
181: String ext = resource.getExt().toLowerCase();
182: String mime = FileUtil.getMIMEType(resource);
183:
184: String lineComment = Settings.getDefault().getLineComment(ext,
185: mime);
186: String blockCommentStart = Settings.getDefault()
187: .getBlockCommentStart(ext, mime);
188: String blockCommentEnd = Settings.getDefault()
189: .getBlockCommentEnd(ext, mime);
190:
191: SourceCodeCommentParser sccp = new SourceCodeCommentParser(
192: lineComment, blockCommentStart, blockCommentEnd);
193:
194: List<Task> tasks = null;
195:
196: try {
197: String text = getContent(resource);
198:
199: sccp.setText(text);
200:
201: SourceCodeCommentParser.CommentRegion reg = new SourceCodeCommentParser.CommentRegion();
202:
203: Matcher matcher = getScanRegexp().matcher(text);
204: int len = text.length();
205: int lineno = 1;
206: int index = 0;
207: int idx = 0;
208:
209: // find the first comment region
210: if (!sccp.nextRegion(reg)) {
211: // Done searching the document... bail out..
212: return getEmptyList();
213: }
214:
215: while (index < len && matcher.find(index)) {
216: int begin = matcher.start();
217: int end = matcher.end();
218: boolean toosoon = false;
219: boolean goahead;
220:
221: do {
222: goahead = true;
223:
224: // A match within the source comment?
225: if (begin < reg.start) {
226: toosoon = true;
227: // too soon.. get next match
228: } else if (begin > reg.stop) {
229: goahead = false;
230: if (!sccp.nextRegion(reg)) {
231: // Done searching the document... bail out..
232: return null == tasks ? getEmptyList()
233: : tasks;
234: }
235: }
236: } while (!goahead);
237:
238: if (toosoon) {
239: // find next match!
240: index = end;
241: continue;
242: }
243:
244: // begin should be the beginning of this line (but avoid
245: // clash if I have two tokens on the same line...
246: char c = 'a'; // NOI18N
247: int nonwhite = begin;
248: while (begin >= index
249: && (c = text.charAt(begin)) != '\n') { // NOI18N
250: if (c != ' ' && c != '\t') { // NOI18N
251: nonwhite = begin;
252: }
253: --begin;
254: }
255:
256: begin = nonwhite;
257:
258: // end should be the last "nonwhite" character on this line...
259: nonwhite = end;
260: while (end < len) {
261: c = text.charAt(end);
262: if (c == '\n' || c == '\r') {// NOI18N
263: break;
264: } else if (c != ' ' && c != '\t') {// NOI18N
265: nonwhite = end;
266: }
267: ++end;
268: }
269:
270: // calculate current line number
271: while (idx <= begin) {
272: if (text.charAt(idx) == '\n') {// NOI18N
273: ++lineno;
274: }
275: ++idx;
276: }
277:
278: index = end;
279:
280: String description = text.subSequence(begin,
281: nonwhite + 1).toString();
282:
283: Task task = Task.create(resource, GROUP_NAME,
284: description, lineno);
285: if (null == tasks) {
286: tasks = new LinkedList<Task>();
287: }
288: tasks.add(task);
289: }
290: } catch (IOException e) {
291: Logger.getLogger(getClass().getName()).log(Level.INFO,
292: null, e);
293: } catch (OutOfMemoryError oomE) {
294: System.gc();
295: Logger.getLogger(getClass().getName()).log(Level.INFO,
296: null, oomE);
297: }
298: return null == tasks ? getEmptyList() : tasks;
299: }
300:
301: private boolean isSupported(FileObject file) {
302: if (null == file || file.isFolder())
303: return false;
304: return Settings.getDefault()
305: .isExtensionSupported(file.getExt())
306: || Settings.getDefault().isMimeTypeSupported(
307: FileUtil.getMIMEType(file));
308: }
309:
310: Pattern getScanRegexp() {
311: // Create regexp from tags
312: if (regexp == null) {
313: StringBuffer sb = new StringBuffer(200);
314: Collection<String> patterns = Settings.getDefault()
315: .getPatterns();
316: boolean needSeparator = false;
317: for (String s : patterns) {
318: if (needSeparator) {
319: sb.append('|');
320: }
321: needSeparator = true;
322: int n = s.length();
323: // Insert token/boundary separator when we're dealing
324: // with text tokens, since you probably don't want
325: // a todo-match in a comment like
326: // "and now process GLYPTODON content".
327: // However, for non-token tags, such as "<<<<" don't
328: // insert word boundary markers since it won't work - there's
329: // no word on the right...
330: if (Character.isJavaIdentifierPart(s.charAt(0))) {
331: // isJavaIdentifierPart - roughly matches what regex
332: // considers a word ([a-zA-Z_0-9])
333:
334: // \W instead of \b: Workarond for issue 30250
335: sb.append("\\W"); // NOI18N
336: }
337: // "escape" the string here such that regexp meta
338: // characters are handled literally
339: for (int j = 0; j < n; j++) {
340: char c = s.charAt(j);
341: // regexp metachar?
342: if ((c == '(') || (c == ')') || (c == '{')
343: || (c == '}') || (c == '[') || (c == ']')
344: || (c == '?') || (c == '*') || (c == '+')
345: || (c == '!') || (c == '|') || (c == '\\')
346: || (c == '^') || (c == '$')) {
347: sb.append('\\');
348: }
349: sb.append(c);
350: }
351: if (Character.isJavaIdentifierPart(s.charAt(n - 1))) {
352: sb.append("\\b"); // NOI18N
353: }
354: }
355: try {
356: regexp = Pattern.compile(sb.toString());
357: } catch (PatternSyntaxException e) {
358: // Internal error: the regexp should have been validated when
359: // the user edited it
360: Logger.getLogger(getClass().getName()).log(Level.INFO,
361: null, e);
362: ;
363: return null;
364: }
365: }
366: return regexp;
367: }
368:
369: private String getContent(FileObject fileObject) throws IOException {
370: char[] buf = new char[1024 * 64];
371: StringBuffer sb = new StringBuffer();
372: Charset charset = FileEncodingQuery.getEncoding(fileObject);
373: Reader r = new BufferedReader(new InputStreamReader(fileObject
374: .getInputStream(), charset));
375: int len;
376: try {
377: while (true) {
378: len = r.read(buf);
379: if (len == -1)
380: break;
381: sb.append(buf, 0, len);
382: }
383: } finally {
384: r.close();
385: }
386: return sb.toString();
387: }
388:
389: private List<? extends Task> getEmptyList() {
390: List<? extends Task> res = Collections.emptyList();
391: return res;
392: }
393:
394: public void attach(Callback callback) {
395: if (null == callback && null != this .callback) {
396: regexp = null;
397: Settings.getDefault().removePropertyChangeListener(this );
398: } else if (null != callback && null == this .callback) {
399: Settings.getDefault().addPropertyChangeListener(this );
400: }
401: this .callback = callback;
402: }
403:
404: public void propertyChange(PropertyChangeEvent e) {
405: if (Settings.PROP_PATTERN_LIST.equals(e.getPropertyName())
406: || Settings.PROP_SCAN_COMMENTS_ONLY.equals(e
407: .getPropertyName())) {
408: regexp = null;
409: if (null != callback)
410: callback.refreshAll();
411: }
412: }
413:
414: @Override
415: public void notifyPrepare() {
416: getScanRegexp();
417: }
418:
419: @Override
420: public void notifyFinish() {
421: regexp = null;
422: }
423: }
|