KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > Scanner


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 package org.netbeans.modules.java.source.builder;
20
21 import com.sun.tools.javadoc.DocCommentScanner;
22 import org.netbeans.api.java.source.query.CommentHandler;
23 import com.sun.tools.javac.util.Context;
24 import org.netbeans.modules.java.source.save.SourceBuffer;
25
26 /**
27  * Subclass of javac's Scanner that allows us to intercept tokens, comments and whitespace and queue
28  * them up as BufferRuns for later use when building the insync.java mutable Node tree.
29  */

30 public class Scanner extends DocCommentScanner {
31
32     static final boolean TRACE_TOKEN = false;
33     static final boolean TRACE_COMMENT = false;
34     static final boolean TRACE_WHITESPACE = false;
35
36     /**
37      * Minimal Scanner.Factory subclass to allow us to get to the Scanner ctor that we want
38      * without needed to be compiled with a 1.5 compiler.
39      */

40     public static class Factory extends DocCommentScanner.Factory {
41
42         public static com.sun.tools.javac.parser.Scanner.Factory instance(Context context) {
43             com.sun.tools.javac.parser.Scanner.Factory instance = context.get(scannerFactoryKey);
44             if (instance == null)
45                 instance = new Factory(context);
46             return instance;
47         }
48
49         Factory(Context context) {
50             super(context);
51         }
52
53         public com.sun.tools.javac.parser.Scanner newScanner(CharSequence JavaDoc source,
54                                                              BufferRunQueue runs) {
55             return new Scanner(this, source, runs);
56         }
57     }
58
59     CharSequence JavaDoc source;
60     BufferRunQueue runs;
61
62     /**
63      * Construct a Scanner given a source buffer and a run queue.
64      *
65      * @param fac
66      * @param source
67      * @param runs
68      */

69     protected Scanner(Factory fac, CharSequence JavaDoc source, BufferRunQueue runs) {
70         super(fac, source.toString().toCharArray(), source.length());
71         this.source = source;
72         this.runs = runs;
73         
74         if (TRACE_TOKEN || TRACE_COMMENT)
75             System.err.println("\nScanner:\n--------");
76     }
77
78     /*
79      * @see com.sun.tools.javac.parser.Scanner#nextToken()
80      */

81     @Override JavaDoc
82     public void nextToken() {
83         super.nextToken();
84         BufferRun br = new TokenRun(pos(), endPos(), token());
85         runs.add(br);
86         if (TRACE_TOKEN) {
87             System.err.println("token:" + token() + " pos:" + pos() +
88                     " endpos:" + endPos() + " \"" + br.getString(source) + "\"");
89         }
90     }
91
92     /*
93      * @see com.sun.tools.javac.parser.Scanner#processComment(com.sun.tools.javac.parser.Scanner.CommentStyle)
94      */

95     @Override JavaDoc
96     protected void processComment(CommentStyle style) {
97         super.processComment(style);
98         BufferRun br = new CommentRun(pos(), endPos(), style);
99         if (TRACE_COMMENT) {
100             String JavaDoc dc = docComment();
101             System.err.println("comment:" + style + " pos:" + pos() +
102                                " endpos:" + endPos() +
103                                " \"" + br.getString(source) + "\"");
104             if (dc != null)
105                 System.err.println("docComment:" + " \"" + dc + "\"");
106         }
107         runs.add(br);
108     }
109
110     /*
111      * @see com.sun.tools.javac.parser.Scanner#processWhiteSpace()
112      */

113     @Override JavaDoc
114     protected void processWhiteSpace() {
115         super.processWhiteSpace();
116         if (TRACE_WHITESPACE) {
117             System.err.println("whitespace pos:" + pos() +
118                                " endpos:" + endPos());
119         }
120         runs.add/*Coallescing*/(new WhitespaceRun(pos(), endPos()));
121     }
122
123     /*
124      * @see com.sun.tools.javac.parser.Scanner#processLineTerminator()
125      */

126     @Override JavaDoc
127     protected void processLineTerminator() {
128         super.processLineTerminator();
129         if (TRACE_WHITESPACE) {
130             System.err.println("lineterminator pos:" + pos() +
131                                " endpos:" + endPos());
132         }
133         runs.add(new LineEndingRun(pos(), endPos()));
134     }
135 }
136
Popular Tags