KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > parser > ParseSourceRequest


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 package org.netbeans.modules.java.parser;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.util.*;
25
26 import javax.swing.event.ChangeListener JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import org.netbeans.api.java.classpath.ClassPath;
29
30 import org.openide.filesystems.FileObject;
31 import org.openide.text.CloneableEditorSupport;
32
33 import org.netbeans.modules.java.ElementFactory;
34 import org.netbeans.modules.java.Util;
35
36 /**
37  *
38  * @author sdedic
39  */

40 public class ParseSourceRequest implements ParsableObjectRequest {
41     public static final int STATE_WAITING = 0;
42     public static final int STATE_READING = 1;
43     public static final int STATE_CANCELLED = 2;
44     public static final int STATE_ANALYSIS = 3;
45     public static final int STATE_UPDATING = 4;
46     public static final int STATE_COMPLETED = 10;
47
48     JavaParser.Env environment;
49     ChangeListener JavaDoc listener;
50     int state;
51     boolean valid;
52     int syntaxErrors;
53     ElementFactory builder;
54     CloneableEditorSupport editSupp;
55
56     private List errors = null;
57     private Object JavaDoc parserType;
58
59     /**
60      * For compatibility -- remove when issue #31581 is fixed!
61      */

62     private String JavaDoc sourceName;
63
64     public ParseSourceRequest() {
65         this((Object JavaDoc)JavaParser.SHALLOW_PARSER);
66     }
67
68     /**
69      * @deprecated The filename should be provided by parser's environment, rather than
70      * by parser request. Just use {@link ParseSourceRequest(java.lang.Object)}.
71      *
72      * @param filename
73      */

74     public ParseSourceRequest(String JavaDoc filename) {
75         this();
76         this.sourceName = filename;
77     }
78
79     public ParseSourceRequest(Object JavaDoc parserType) {
80         this.parserType = parserType;
81         state = STATE_WAITING;
82         valid = true;
83     }
84
85     ParseSourceRequest(JavaParser.Env env, CloneableEditorSupport editSupp) {
86         this();
87         this.editSupp = editSupp;
88         this.environment = env;
89     }
90
91     public synchronized void addChangeListener(ChangeListener JavaDoc l) throws TooManyListenersException {
92         if (listener != null)
93             throw new TooManyListenersException();
94         listener = l;
95     }
96
97     public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
98         if (listener == l)
99             listener = null;
100     }
101
102     public void setEnvironment(JavaParser.Env env) {
103         environment = env;
104     }
105
106     public void setEditorSupport(CloneableEditorSupport editor) {
107         editSupp = editor;
108     }
109
110     /**
111      * Notifies the request that the source text has been changed. This causes
112      * cancellation of the request in some cases.
113      */

114     public void sourceChanged() {
115         if (state == STATE_READING) {
116             // cancel the request only in reading state; if the text is already
117
// read, the request can still be cariied out.
118
valid = false;
119         }
120     }
121
122     public void modelChanged() {
123         if (state != STATE_WAITING && state != STATE_COMPLETED) {
124             valid = false;
125         }
126     }
127
128     public void setSyntaxErrors(int errors) {
129         this.syntaxErrors = errors;
130     }
131
132     public int getSyntaxErrors() {
133         return syntaxErrors;
134     }
135
136     public void setSemanticErrors(int errors) {
137         // ignore - for now.
138
}
139
140     /**
141      * DocumentModelBuilder actually carries out tasks associated with the Factory.
142      */

143     public ElementFactory getFactory() {
144         if (builder == null)
145             builder = createBuilder(editSupp);
146         return builder;
147     }
148
149     protected ElementFactory createBuilder(CloneableEditorSupport supp) {
150         return new DocumentModelBuilder(supp);
151     }
152
153     public void notifyReschedule() {
154         // clear old data.
155
builder = null;
156         enterState(STATE_WAITING);
157     }
158
159     protected void enterState(int state) {
160         this.state = state;
161         if (listener != null)
162             listener.stateChanged(new ChangeEvent JavaDoc(this));
163     }
164
165     /**
166      * The method is implemented so that it reads the whole contents from the environment's
167      * Reader and returns the resulting char array.
168      */

169     public char[] getSource() throws IOException JavaDoc {
170         // unsynchronized; the exact sequence really does not matter here too much;
171
// the real problem arises that AFTER the contents is read.
172
valid = true;
173         enterState(STATE_READING);
174
175         Reader JavaDoc r = environment.getSourceText();
176         char[] buf=Util.readContents(r);
177         ElementFactory builder = getFactory();
178         if (builder instanceof DocumentModelBuilder) {
179             ((DocumentModelBuilder)builder).setContent(buf, editSupp.isDocumentLoaded());
180         }
181         return buf;
182     }
183
184     /** This should locate appropriate .class containing the class 'className'.
185      * The current implementation will cowardly return false.
186      */

187     public InputStream JavaDoc findCompiledClass(String JavaDoc className) {
188         return environment.findCompiledClass(className);
189     }
190
191     /**
192      * Returns true, IF the request was not invalidated by the ParserSupport,
193      * or because of change in the document while parsing.
194      */

195     public boolean isValid() {
196         return this.valid;
197     }
198
199     public boolean needsProcessing() {
200         // always needs to process (?)
201
return this.valid;
202     }
203
204     public void notifyStart() {
205     }
206
207     public void notifyComplete() {
208         // safepoint: everything was processed.
209
enterState(STATE_COMPLETED);
210         if (errors==null && JavaParser.DEEP_PARSER.equals(getParserType()))
211             errors=new ArrayList();
212         // errors == null IS intentional for the shallow parser. Before you change it
213
// be aware of JavaEditor.processAnnotations(ParserMessage[] errors) depends on it
214
}
215
216     public String JavaDoc getSourceName() {
217         if (sourceName != null)
218             return sourceName;
219         return environment.getSourceName();
220     }
221
222     public Object JavaDoc getParserType() {
223         return parserType;
224     }
225
226     /** @return the set of errors encountered while processing this
227      * request */

228     public Collection getMessages() {
229         return errors;
230     }
231
232     public ClassPath getSourcePath() {
233         assert environment != null;
234         
235         // XXX
236
// if following method is really expected to be null then JavaParser.Env
237
// must be extended with something what will allow us to find project
238
FileObject fo = environment.getSourceFile();
239         assert fo != null;
240         
241         return ClassPath.getClassPath(fo, ClassPath.SOURCE);
242     }
243
244     public ClassPath getLibraryPath() {
245         assert environment != null;
246         
247         // XXX
248
// if following method is really expected to be null then JavaParser.Env
249
// must be extended with something what will allow us to find project
250
FileObject fo = environment.getSourceFile();
251         assert fo != null;
252         
253         return ClassPath.getClassPath(fo, ClassPath.COMPILE);
254     }
255
256     public ClassPath getBootClassPath() {
257         assert environment != null;
258         
259         // XXX
260
// if following method is really expected to be null then JavaParser.Env
261
// must be extended with something what will allow us to find project
262
FileObject fo = environment.getSourceFile();
263         assert fo != null;
264         
265         return ClassPath.getClassPath(fo, ClassPath.BOOT);
266     }
267 }
268
269
Popular Tags