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.Collections;
047: import java.util.HashMap;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Set;
051: import org.netbeans.modules.gsf.api.Error;
052: import org.netbeans.modules.gsf.api.Index;
053: import org.netbeans.modules.gsf.api.ParserResult;
054: import org.netbeans.modules.gsf.Language;
055: import org.netbeans.modules.gsf.LanguageRegistry;
056: import org.netbeans.modules.gsfret.source.parsing.SourceFileObject;
057: import org.openide.ErrorManager;
058: import org.openide.filesystems.FileObject;
059: import org.netbeans.api.lexer.TokenHierarchy;
060: import org.openide.filesystems.FileUtil;
061:
062: /**
063: * This file is originally from Retouche, the Java Support
064: * infrastructure in NetBeans. I have modified the file as little
065: * as possible to make merging Retouche fixes back as simple as
066: * possible.
067: *
068: * Assorted information about the Source.
069: *
070: *
071: * @author Petr Hrebejk, Tomas Zezula
072: */
073: public class CompilationInfo extends
074: org.netbeans.modules.gsf.api.CompilationInfo {
075: private Phase phase = Phase.MODIFIED;
076: private ParserTaskImpl javacTask;
077: final SourceFileObject jfo;
078: final Source javaSource;
079: boolean needsRestart;
080: private Language language;
081: private Map<String, ParserResult> embeddedResults = new HashMap<String, ParserResult>();
082:
083: CompilationInfo() throws IOException {
084: super (null);
085: this .javaSource = null;
086: this .jfo = null;
087: this .javacTask = null;
088: }
089:
090: CompilationInfo(final Source javaSource, final FileObject fo,
091: final ParserTaskImpl javacTask) throws IOException {
092: super (fo);
093: assert javaSource != null;
094: this .javaSource = javaSource;
095:
096: //this.jfo = fo != null ? javaSource.jfoProvider.createJavaFileObject(fo) : null;
097: if (fo.isValid() && !fo.isVirtual()) {
098: this .jfo = new SourceFileObject(fo, true);
099: } else {
100: this .jfo = null;
101: }
102:
103: this .javacTask = javacTask;
104: }
105:
106: // API of the class --------------------------------------------------------
107:
108: /**
109: * Returns the current phase of the {@link Source}.
110: *
111: *
112: * @return {@link Source.Phase} the state which was reached by the {@link Source}.
113: */
114: public Phase getPhase() {
115: return this .phase;
116: }
117:
118: /**
119: * Returns the content of the file represented by the {@link Source}.
120: *
121: *
122: * @return String the java source
123: */
124: public String getText() {
125: if (this .jfo == null) {
126: throw new IllegalStateException();
127: }
128:
129: try {
130: return this .jfo.getCharContent(false).toString();
131: } catch (IOException ioe) {
132: //Should never happen
133: ErrorManager.getDefault().notify(ioe);
134:
135: return null;
136: }
137: }
138:
139: public TokenHierarchy<?> getTokenHierarchy() {
140: if (this .jfo == null) {
141: throw new IllegalStateException();
142: }
143: try {
144: return ((SourceFileObject) this .jfo).getTokenHierarchy();
145: } catch (IOException ioe) {
146: //Should never happen
147: ErrorManager.getDefault().notify(ioe);
148: return null;
149: }
150: }
151:
152: public Source getSource() {
153: return javaSource;
154: }
155:
156: public ClasspathInfo getClasspathInfo() {
157: return javaSource.getClasspathInfo();
158: }
159:
160: @Override
161: public Index getIndex(String mimeType) {
162: return getClasspathInfo().getClassIndex(mimeType);
163: }
164:
165: void setPhase(final Phase phase) {
166: assert phase != null;
167: this .phase = phase;
168: }
169:
170: synchronized ParserTaskImpl getParserTask() {
171: if (javacTask == null) {
172: javacTask = javaSource.createParserTask( /*new DiagnosticListenerImpl(errors),*/
173: this );
174: }
175:
176: return javacTask;
177: }
178:
179: // TODO - get rid of this method - it doesn't work for embedding langauges where you really
180: // need to iterate over the results to pick the best language!
181: public Language getLanguage() {
182: if (language == null) {
183: FileObject fo = getFileObject();
184: String mimeType = fo.getMIMEType();
185: language = LanguageRegistry.getInstance()
186: .getLanguageByMimeType(mimeType);
187: }
188: return language;
189: }
190:
191: public void setLanguage(Language language) {
192: this .language = language;
193: }
194:
195: // TODO - switch over to making an iterator for parse results instead!
196: public Set<String> getEmbeddedMimeTypes() {
197: return embeddedResults.keySet();
198: }
199:
200: public void addEmbeddingResult(String mimeType, ParserResult result) {
201: embeddedResults.put(mimeType, result);
202: result.setInfo(this );
203: }
204:
205: @Override
206: public Collection<? extends ParserResult> getEmbeddedResults(
207: String mimeType) {
208: ParserResult result = getEmbeddedResult(mimeType, 0);
209: if (result != null) {
210: return Collections.singletonList(result);
211: } else {
212: return Collections.emptyList();
213: }
214: }
215:
216: @Override
217: public ParserResult getEmbeddedResult(String embeddedMimeType,
218: int offset) {
219: ParserResult root = embeddedResults.get(embeddedMimeType);
220:
221: return root;
222: }
223:
224: @Override
225: public List<Error> getErrors() {
226: List<Error> errors = new ArrayList<Error>();
227: for (ParserResult result : embeddedResults.values()) {
228: errors.addAll(result.getDiagnostics());
229: }
230:
231: return errors;
232: }
233:
234: @Override
235: public String toString() {
236: return "CompilationInfo for "
237: + FileUtil.getFileDisplayName(getFileObject())
238: + "; phase=" + getPhase();
239: }
240:
241: }
|