KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > demo > DemoTokenUpdater


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.lexer.demo;
21
22 import javax.swing.event.DocumentEvent JavaDoc;
23 import javax.swing.event.DocumentListener JavaDoc;
24 import javax.swing.text.Segment JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27 import org.netbeans.api.lexer.Token;
28 import org.netbeans.api.lexer.TokenId;
29 import org.netbeans.api.lexer.LexerInput;
30 import org.netbeans.api.lexer.SampleTextMatcher;
31 import org.netbeans.api.lexer.Language;
32 import org.netbeans.api.lexer.Lexer;
33 import org.netbeans.spi.lexer.util.Compatibility;
34 import org.netbeans.spi.lexer.inc.TextTokenUpdater;
35 import org.netbeans.spi.lexer.util.LexerUtilities;
36
37 /**
38  * Token updater working over a swing document.
39  *
40  * @author Miloslav Metelka
41  * @version 1.00
42  */

43
44 public class DemoTokenUpdater extends TextTokenUpdater {
45     
46     private Segment JavaDoc seg = new Segment JavaDoc(); // shared segment instance
47

48     private final Document JavaDoc doc;
49     
50     private final Language language;
51
52     private final boolean maintainLookbacks;
53     
54     private boolean debugTokenChanges;
55     
56     private Lexer sharedLexer; // shared lexer instance
57

58     public DemoTokenUpdater(Document JavaDoc doc, Language language) {
59         this(doc, language, true);
60     }
61
62     public DemoTokenUpdater(Document JavaDoc doc, Language language, boolean maintainLookbacks) {
63         this.doc = doc;
64         this.language = language;
65         this.maintainLookbacks = maintainLookbacks;
66
67         doc.addDocumentListener(
68             new DocumentListener JavaDoc() {
69                 public void insertUpdate(DocumentEvent JavaDoc evt) {
70                     if (debugTokenChanges) {
71                         try {
72                             System.out.println("\nDocument-insert \""
73                                 + LexerUtilities.toSource(
74                                     (evt.getDocument()).getText(evt.getOffset(),
75                                         evt.getLength()))
76                                 + "\""
77                             );
78                         } catch (BadLocationException JavaDoc e) {
79                             e.printStackTrace();
80                         }
81                     }
82                     
83                     update(evt.getOffset(), evt.getLength());
84                 }
85
86                 public void removeUpdate(DocumentEvent JavaDoc evt) {
87                     if (debugTokenChanges) {
88                         System.out.println("\nDocument-remove at offset="
89                             + evt.getOffset()
90                             + ", length="
91                             + evt.getLength()
92                         );
93                     }
94                     
95                     update(evt.getOffset(), -evt.getLength());
96                 }
97
98                 public void changedUpdate(DocumentEvent JavaDoc evt) {
99                 }
100             }
101         );
102     }
103     
104     public boolean getDebugTokenChanges() {
105         return debugTokenChanges;
106     }
107     
108     public void setDebugTokenChanges(boolean debugTokenChanges) {
109         this.debugTokenChanges = debugTokenChanges;
110     }
111     
112     protected final Document JavaDoc getDocument() {
113         return doc;
114     }
115
116     protected final Language getLanguage() {
117         return language;
118     }
119     
120     public char textCharAt(int index) {
121         synchronized (seg) {
122             try {
123                 doc.getText(index, 1, seg);
124                 return seg.array[seg.offset];
125
126             } catch (BadLocationException JavaDoc e) {
127                 throw new IllegalStateException JavaDoc(e.toString());
128             }
129         }
130     }
131
132     public int textLength() {
133         return doc.getLength();
134     }
135     
136     private String JavaDoc getDocumentText(int offset, int length) {
137         try {
138             return doc.getText(offset, length);
139         } catch (BadLocationException JavaDoc e) {
140             throw new IllegalStateException JavaDoc(e.getMessage());
141         }
142     }
143     
144     protected Token createToken(TokenId id, int index, int length) {
145         String JavaDoc sampleText = null;
146         if (Compatibility.charSequenceExists()) {
147             SampleTextMatcher matcher = id.getSampleTextMatcher();
148             if (matcher != null) {
149                 /* The recognizedText would not be a string
150                  * in the normal applications. It would be
151                  * a custom char sequence reused for every
152                  * recognized token. Here it's string
153                  * to simplify the code.
154                  */

155                 CharSequence JavaDoc recognizedText
156                     = (CharSequence JavaDoc)(Object JavaDoc)getDocumentText(index, length); // 1.3 compilability
157
sampleText = matcher.match(recognizedText);
158             }
159         }
160
161         Token token;
162         if (sampleText != null) {
163             token = new DemoSampleToken(id, sampleText);
164         } else {
165             token = new DemoToken(this, id, index, length);
166         }
167
168         return token;
169     }
170     
171     protected Lexer createLexer() {
172         if (sharedLexer == null) {
173             sharedLexer = language.createLexer();
174         }
175         
176         return sharedLexer;
177     }
178     
179     protected void add(Token token, int lookahead, Object JavaDoc state) {
180         add(token);
181
182         if (token instanceof DemoSampleToken) {
183             DemoSampleToken dft = (DemoSampleToken)token;
184             dft.setLookahead(lookahead);
185             dft.setState(state);
186         } else {
187             DemoToken dt = (DemoToken)token;
188             dt.setLookahead(lookahead);
189             dt.setState(state);
190         }
191         
192         if (debugTokenChanges) {
193             System.out.println("Added token: ["
194                 + (getNextIndex() - 1) + "]="
195                 + tokenToString(token, false)
196             );
197         }
198     }
199     
200     protected void remove() {
201         if (debugTokenChanges) {
202             System.out.println("Removed token at index="
203                 + (getNextIndex() - 1)
204             );
205         }
206
207         super.remove();
208     }
209     
210     public int getLookahead() {
211         Token token = getToken(getValidPreviousIndex());
212         return (token instanceof DemoSampleToken)
213             ? ((DemoSampleToken)token).getLookahead()
214             : ((DemoToken)token).getLookahead();
215     }
216
217     public Object JavaDoc getState() {
218         Token token = getToken(getValidPreviousIndex());
219         return (token instanceof DemoSampleToken)
220             ? ((DemoSampleToken)token).getState()
221             : ((DemoToken)token).getState();
222     }
223
224     public int getLookback() {
225         if (maintainLookbacks) {
226             Token token = getToken(getValidPreviousIndex());
227             return (token instanceof DemoSampleToken)
228                 ? ((DemoSampleToken)token).getLookback()
229                 : ((DemoToken)token).getLookback();
230                 
231         } else { // do not maintain the lookbacks
232
return -1;
233         }
234     }
235
236     protected void setLookback(int lookback) {
237         if (maintainLookbacks) {
238             Token token = getToken(getValidPreviousIndex());
239             if (token instanceof DemoSampleToken) {
240                 ((DemoSampleToken)token).setLookback(lookback);
241             } else {
242                 ((DemoToken)token).setLookback(lookback);
243             }
244         }
245     }
246     
247     public boolean hasNext() {
248         return super.hasNext();
249     }
250     
251     public Token next() {
252         return super.next();
253     }
254     
255     public int relocate(int index) {
256         return super.relocate(index);
257     }
258     
259     public String JavaDoc tokenToString(Token token, boolean extraInfo) {
260         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
261         
262         int length = org.netbeans.spi.lexer.util.Compatibility.getLength(token);
263         String JavaDoc text = org.netbeans.spi.lexer.util.Compatibility.toString(token);
264         sb.append("\""
265             + org.netbeans.spi.lexer.util.LexerUtilities.toSource(text)
266             + "\", " + token.getId()
267         );
268
269         if (token instanceof DemoToken) {
270             DemoToken dt = (DemoToken)token;
271
272             sb.append(", off=");
273             sb.append(getOffset(dt.getRawOffset()));
274
275             sb.append(", type=regular");
276             if (extraInfo) {
277                 sb.append(", la=" + dt.getLookahead()
278                     + ", lb=" + dt.getLookback()
279                     + ", st=" + dt.getState()
280                 );
281             }
282
283         } else {
284             DemoSampleToken dft = (DemoSampleToken)token;
285             sb.append(", type=sample");
286             if (extraInfo) {
287                 sb.append(", la=" + dft.getLookahead()
288                     + ", lb=" + dft.getLookback()
289                     + ", st=" + dft.getState()
290                 );
291             }
292         }
293         
294         return sb.toString();
295     }
296
297     public String JavaDoc allTokensToString() {
298         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
299         int cnt = getTokenCount();
300         sb.append("Dump of tokens (tokenCount=" + cnt + ") in token updater:\n");
301         int offset = 0;
302         try {
303             for (int i = 0; i < cnt; i++) {
304                 Token t = getToken(i);
305                 int length = org.netbeans.spi.lexer.util.Compatibility.getLength(t);
306                 String JavaDoc text = org.netbeans.spi.lexer.util.Compatibility.toString(t);
307                 sb.append("[" + i + "] \""
308                     + org.netbeans.spi.lexer.util.LexerUtilities.toSource(text)
309                     + "\", " + t.getId()
310                     + ", off=" + offset
311                 );
312                 
313                 if (t instanceof DemoToken) {
314                     DemoToken dt = (DemoToken)t;
315
316                     if (getOffset(dt.getRawOffset()) != offset) {
317                         throw new IllegalStateException JavaDoc("offsets differ");
318                     }
319                     
320                     sb.append(", type=regular"
321                         + ", la=" + dt.getLookahead()
322                         + ", lb=" + dt.getLookback()
323                         + ", st=" + dt.getState()
324                         + "\n"
325                     );
326                     
327                 } else {
328                     DemoSampleToken dft = (DemoSampleToken)t;
329                     sb.append(", type=sample"
330                         + ", la=" + dft.getLookahead()
331                         + ", lb=" + dft.getLookback()
332                         + ", st=" + dft.getState()
333                         + "\n"
334                     );
335                 }
336                 
337
338                 offset += length;
339             }
340         } catch (RuntimeException JavaDoc e) {
341             System.err.println(sb.toString());
342             throw e;
343         }
344         
345         return sb.toString();
346     }
347
348 }
349
Popular Tags