KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > docscan > TaskTags


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.docscan;
21
22 import java.io.Externalizable JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.ObjectInput JavaDoc;
25 import java.io.ObjectOutput JavaDoc;
26 import java.util.regex.*;
27
28 import org.openide.ErrorManager;
29
30
31 /** Represents a set of tags in the user's source code that marks
32  * lines describing tasks
33  * @todo Move regexp code out of source scanner such that the
34  * matching is done in this file (by abstracting getParenStart & friends)
35  *
36  *
37  * @author Tor Norbye */

38 public final class TaskTags implements Externalizable JavaDoc {
39
40     static final long serialVersionUID = 1L;
41     private TaskTag[] tags = null;
42
43     public TaskTags() {
44     }
45     
46     /** Set the token associated with the tag - this is a case sensitive
47      * string which when present in the user's code marks a task.
48      */

49     public void setTags(TaskTag[] tags) {
50         this.tags = tags;
51     }
52
53     public TaskTag[] getTags() {
54         return tags;
55     }
56
57     public TaskTag getTag(CharSequence JavaDoc token, int start, int len) {
58         if (tags == null) {
59             return null;
60         }
61         for (int i = 0; i < tags.length; i++) {
62             if (same(tags[i].getToken(), token, start, len)) {
63                 return tags[i];
64             }
65         }
66         return null;
67     }
68
69     private static boolean same(String JavaDoc s1, CharSequence JavaDoc s2, int start, int len) {
70         if (s1.length() != len) return false;
71         for (int i = 0; i<len; i++) {
72             if (s1.charAt(i) != s2.charAt(start+i)) return false;
73         }
74         return true;
75     }
76
77     private Pattern regexp = null;
78
79     /**
80      * Gets the scan regular expression - used during scanning for
81      * todo items. We use a regular expression since (I believe, but
82      * haven't checked) that this might be faster than a simple string
83      * matching algorithm I could easily write. The regular expression
84      * package should be able to build a check routine which is really
85      * fast since it precomputes the bytecode(?) which as quickly as
86      * possible checks all the matches.
87      *
88      * [ccc] I tied and that's true.
89      */

90     public Pattern getScanRegexp() {
91         // Create regexp from tags
92
if (regexp == null) {
93             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(200);
94             TaskTag[] tgs = getTags();
95             for (int i = 0; i < tgs.length; i++) {
96                 if (i > 0) {
97                     sb.append('|');
98                 }
99                 String JavaDoc s = tgs[i].getToken();
100                 int n = s.length();
101                 // Insert token/boundary separator when we're dealing
102
// with text tokens, since you probably don't want
103
// a todo-match in a comment like
104
// "and now process GLYPTODON content".
105
// However, for non-token tags, such as "<<<<" don't
106
// insert word boundary markers since it won't work - there's
107
// no word on the right...
108
if (Character.isJavaIdentifierPart(s.charAt(0))) {
109                     // isJavaIdentifierPart - roughly matches what regex
110
// considers a word ([a-zA-Z_0-9])
111

112                     // \W instead of \b: Workarond for issue 30250
113
sb.append("\\W"); // NOI18N
114
}
115                 // "escape" the string here such that regexp meta
116
// characters are handled literally
117
for (int j = 0; j < n; j++) {
118                     char c = s.charAt(j);
119                     // regexp metachar?
120
if ((c == '(') || (c == ')') ||
121                         (c == '{') || (c == '}') ||
122                         (c == '[') || (c == ']') ||
123                         (c == '?') || (c == '*') || (c == '+') ||
124                         (c == '!') || (c == '|') || (c == '\\') ||
125                         (c == '^') || (c == '$')) {
126                         sb.append('\\');
127                     }
128                     sb.append(c);
129                 }
130                 if (Character.isJavaIdentifierPart(s.charAt(n-1))) {
131                     sb.append("\\b"); // NOI18N
132
}
133             }
134             try {
135                 regexp = Pattern.compile(sb.toString());
136             } catch (PatternSyntaxException e) {
137                 // Internal error: the regexp should have been validated when
138
// the user edited it
139
ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
140                 return null;
141             }
142         }
143         return regexp;
144     }
145
146     /** Read in a serialized version of the task tag
147      * @param objectInput object stream to read from
148      * @todo Use a more robust serialization format (not int uid based)
149      * @throws IOException
150      * @throws ClassNotFoundException */

151     public void readExternal(ObjectInput JavaDoc objectInput) throws IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
152         int ver = objectInput.read();
153         assert ver == 1 : "serialization version incorrect; should be 1";
154
155         // Read in the priority
156
int num = ((Integer JavaDoc)objectInput.readObject()).intValue();
157         tags = new TaskTag[num];
158         for (int i = 0; i < num; i++) {
159             tags[i] = (TaskTag)objectInput.readObject();
160         }
161     }
162
163     /** Write out relevant task tag settings data
164      * @param objectOutput Object stream to write to
165      * @throws IOException */

166     public void writeExternal(ObjectOutput JavaDoc objectOutput) throws IOException JavaDoc {
167         objectOutput.write(1); // SERIAL VERSION
168
objectOutput.writeObject(new Integer JavaDoc(tags.length));
169         for (int i = 0; i < tags.length; i++) {
170             objectOutput.writeObject(tags[i]);
171         }
172     }
173 }
174
175
176
177
Popular Tags