001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.napi.gsfret.source;
042:
043: import java.io.IOException;
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.List;
047: import javax.swing.text.Document;
048: import org.netbeans.modules.gsf.api.EmbeddingModel;
049: import org.netbeans.modules.gsf.api.Error;
050: import org.netbeans.modules.gsf.api.ParseEvent;
051: import org.netbeans.modules.gsf.api.ParseListener;
052: import org.netbeans.modules.gsf.api.Parser;
053: import org.netbeans.modules.gsf.api.ParserFile;
054: import org.netbeans.modules.gsf.api.ParserResult;
055: import org.netbeans.modules.gsf.api.SourceFileReader;
056: import org.netbeans.modules.gsf.api.TranslatedSource;
057: import org.netbeans.modules.gsf.Language;
058: import org.netbeans.modules.gsf.LanguageRegistry;
059: import org.netbeans.modules.gsfret.source.parsing.SourceFileObject;
060: import org.openide.ErrorManager;
061: import org.openide.filesystems.FileObject;
062:
063: public class ParserTaskImpl {
064: private ParseListener listener;
065: private Language language;
066:
067: public ParserTaskImpl(Language language) {
068: this .language = language;
069: }
070:
071: public void setParseListener(ParseListener listener) {
072: this .listener = listener;
073: }
074:
075: public void finish() {
076: }
077:
078: public Iterable<ParserResult> parse(ParserFile... files)
079: throws IOException {
080: List<ParserResult> results = new ArrayList<ParserResult>(
081: files.length);
082:
083: for (ParserFile file : files) {
084: if (file == null) {
085: continue;
086: }
087:
088: //ParserResult result = parser.parseBuffer(currentInfo.getFileObject(), buffer, errorHandler);
089: final ParserResult[] resultHolder = new ParserResult[1];
090: ParseListener listener = // TODO make innerclass
091: new ParseListener() {
092: public void started(ParseEvent e) {
093: ParserTaskImpl.this .listener.started(e);
094: }
095:
096: public void error(Error e) {
097: ParserTaskImpl.this .listener.error(e);
098: }
099:
100: public void exception(Exception e) {
101: ParserTaskImpl.this .listener.exception(e);
102: }
103:
104: public void finished(ParseEvent e) {
105: // TODO - check state
106: if (e.getKind() == ParseEvent.Kind.PARSE) {
107: resultHolder[0] = e.getResult();
108: }
109: ParserTaskImpl.this .listener.finished(e);
110: }
111: };
112:
113: List<ParserFile> sourceFiles = new ArrayList<ParserFile>(1);
114: sourceFiles.add(file);
115:
116: String mimeType = file.getFileObject().getMIMEType();
117: LanguageRegistry registry = LanguageRegistry.getInstance();
118: List<Language> languages = registry
119: .getApplicableLanguages(mimeType);
120: //Language language = currentInfo.getLanguage();
121:
122: for (Language l : languages) {
123: // HACK - I really need to iterate over ALL the languages and index all of them
124: if (l != language) {
125: continue;
126: }
127:
128: EmbeddingModel model = registry.getEmbedding(l
129: .getMimeType(), mimeType);
130: assert language != null;
131: Parser parser = language.getParser(); // Todo - call createParserTask here?
132:
133: if (parser != null) {
134: if (model != null) {
135: FileObject bufferFo = file.getFileObject();
136: Document document = UiUtils.getDocument(
137: bufferFo, true);
138: if (document == null) {
139: continue;
140: }
141:
142: Collection<? extends TranslatedSource> translations = model
143: .translate(document);
144: for (TranslatedSource translatedSource : translations) {
145: String buffer = translatedSource
146: .getSource();
147: SourceFileReader reader = new StringSourceFileReader(
148: buffer, bufferFo);
149: Parser.Job job = new Parser.Job(
150: sourceFiles, listener, reader,
151: translatedSource);
152: parser.parseFiles(job);
153:
154: ParserResult result = resultHolder[0];
155: result
156: .setTranslatedSource(translatedSource);
157: assert result != null;
158: results.add(result);
159: }
160: } else {
161: SourceFileReader reader = new SourceFileReader() {
162: public CharSequence read(ParserFile file)
163: throws IOException {
164: //assert fileObject == file;
165: //assert file.getFileObject() != null : file.getNameExt();
166: // #100618: Get more info but don't blow up
167: if (file.getFileObject() == null) {
168: ErrorManager
169: .getDefault()
170: .log(
171: "Null fileobject for "
172: + file
173: .getNameExt());
174: return "";
175: }
176: return SourceFileObject.create(
177: file.getFileObject())
178: .getCharContent(false)
179: .toString();
180: }
181:
182: public int getCaretOffset(
183: ParserFile fileObject) {
184: return -1;
185: }
186: };
187:
188: Parser.Job job = new Parser.Job(sourceFiles,
189: listener, reader, null);
190: parser.parseFiles(job);
191: ParserResult result = resultHolder[0];
192: assert result != null;
193: results.add(result);
194: }
195: }
196: }
197: }
198:
199: return results;
200: }
201: }
|