1 19 package org.netbeans.modules.java.parser; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.Reader ; 24 import java.util.*; 25 26 import javax.swing.event.ChangeListener ; 27 import javax.swing.event.ChangeEvent ; 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 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 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 parserType; 58 59 62 private String sourceName; 63 64 public ParseSourceRequest() { 65 this((Object )JavaParser.SHALLOW_PARSER); 66 } 67 68 74 public ParseSourceRequest(String filename) { 75 this(); 76 this.sourceName = filename; 77 } 78 79 public ParseSourceRequest(Object 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 l) throws TooManyListenersException { 92 if (listener != null) 93 throw new TooManyListenersException(); 94 listener = l; 95 } 96 97 public synchronized void removeChangeListener(ChangeListener 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 114 public void sourceChanged() { 115 if (state == STATE_READING) { 116 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 } 139 140 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 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 (this)); 163 } 164 165 169 public char[] getSource() throws IOException { 170 valid = true; 173 enterState(STATE_READING); 174 175 Reader 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 187 public InputStream findCompiledClass(String className) { 188 return environment.findCompiledClass(className); 189 } 190 191 195 public boolean isValid() { 196 return this.valid; 197 } 198 199 public boolean needsProcessing() { 200 return this.valid; 202 } 203 204 public void notifyStart() { 205 } 206 207 public void notifyComplete() { 208 enterState(STATE_COMPLETED); 210 if (errors==null && JavaParser.DEEP_PARSER.equals(getParserType())) 211 errors=new ArrayList(); 212 } 215 216 public String getSourceName() { 217 if (sourceName != null) 218 return sourceName; 219 return environment.getSourceName(); 220 } 221 222 public Object getParserType() { 223 return parserType; 224 } 225 226 228 public Collection getMessages() { 229 return errors; 230 } 231 232 public ClassPath getSourcePath() { 233 assert environment != null; 234 235 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 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 FileObject fo = environment.getSourceFile(); 263 assert fo != null; 264 265 return ClassPath.getClassPath(fo, ClassPath.BOOT); 266 } 267 } 268 269 | Popular Tags |