KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > retouche > source > ParserTaskImpl


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.api.retouche.source;
20
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.api.gsf.Error;
25 import org.netbeans.api.gsf.ParseEvent;
26 import org.netbeans.api.gsf.ParseListener;
27 import org.netbeans.api.gsf.Parser;
28 import org.netbeans.api.gsf.ParserFile;
29 import org.netbeans.api.gsf.ParserResult;
30 import org.netbeans.api.gsf.SourceFileReader;
31 import org.netbeans.api.retouche.source.CompilationUnitTree;
32 import org.netbeans.modules.gsf.Language;
33 import org.netbeans.modules.retouche.source.parsing.SourceFileObject;
34 import org.openide.filesystems.FileObject;
35
36
37 public class ParserTaskImpl {
38     private Parser parser;
39     private ParseListener listener;
40
41     public ParserTaskImpl(Language language) {
42     }
43
44     public void setParser(Parser parser) {
45         this.parser = parser;
46     }
47
48     public void setParseListener(ParseListener listener) {
49         this.listener = listener;
50     }
51
52     public void finish() {
53     }
54
55     public Iterable JavaDoc<ParserResult> parse(ParserFile... files) throws IOException JavaDoc {
56         List JavaDoc<ParserResult> results = new ArrayList JavaDoc<ParserResult>(files.length);
57
58         for (ParserFile file : files) {
59             if (file == null) {
60                 continue;
61             }
62
63             //ParserResult result = parser.parseBuffer(currentInfo.getFileObject(), buffer, errorHandler);
64
final ParserResult[] resultHolder = new ParserResult[1];
65             ParseListener listener =
66                 new ParseListener() {
67                     public void started(ParseEvent e) {
68                         ParserTaskImpl.this.listener.started(e);
69                     }
70
71                     public void error(Error JavaDoc e) {
72                         ParserTaskImpl.this.listener.error(e);
73                     }
74
75                     public void exception(Exception JavaDoc e) {
76                         ParserTaskImpl.this.listener.exception(e);
77                     }
78
79                     public void finished(ParseEvent e) {
80                         // TODO - check state
81
if (e.getKind() == ParseEvent.Kind.PARSE) {
82                             resultHolder[0] = e.getResult();
83                         }
84                         ParserTaskImpl.this.listener.finished(e);
85                     }
86                 };
87
88             List JavaDoc<ParserFile> sourceFiles = new ArrayList JavaDoc<ParserFile>(1);
89             sourceFiles.add(file);
90
91             SourceFileReader reader =
92                 new SourceFileReader() {
93                     public CharSequence JavaDoc read(ParserFile file) throws IOException JavaDoc {
94                         //assert fileObject == file;
95
// XXX Ugh!
96
return SourceFileObject.create(file.getFileObject()).getCharContent(false).toString();
97                     }
98                     public int getCaretOffset(ParserFile fileObject) {
99                         return -1;
100                     }
101                 };
102
103             parser.parseFiles(sourceFiles, listener, reader);
104
105             ParserResult result = resultHolder[0];
106             assert result != null;
107             results.add(result);
108         }
109
110         return results;
111     }
112 }
113
Popular Tags