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:
042: package org.netbeans.napi.gsfret.source;
043:
044: import java.util.Map;
045: import java.util.Set;
046: import org.netbeans.modules.gsf.api.OffsetRange;
047: import org.netbeans.modules.gsf.api.Parser;
048: import org.netbeans.modules.gsf.api.ParserResult;
049: import org.netbeans.modules.gsf.api.ColoringAttributes;
050: import java.io.IOException;
051: import java.util.Collection;
052: import java.util.List;
053: import javax.swing.text.Document;
054: import org.netbeans.modules.gsf.api.Error;
055: import org.netbeans.modules.gsf.api.Index;
056: import org.netbeans.modules.gsf.api.PositionManager;
057: import org.netbeans.api.lexer.TokenHierarchy;
058: import org.netbeans.modules.gsf.Language;
059: import org.openide.filesystems.FileObject;
060: import static org.netbeans.napi.gsfret.source.Phase.*;
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: * Class for explicit invocation of compilation phases on a source file.
069: * The implementation delegates to the {@link CompilationInfo} to get the data,
070: * the access to {@link CompilationInfo} is not synchronized, so the class isn't
071: * reentrant.
072: *
073: * XXX: make toPhase automatic in getTrees(), Trees.getElement, etc....
074: * @author Petr Hrebejk, Tomas Zezula
075: */
076: public class CompilationController extends CompilationInfo {
077:
078: //Not private for unit tests
079: /*private*/final CompilationInfo delegate;
080:
081: CompilationController(final CompilationInfo delegate)
082: throws IOException {
083: super ();
084: assert delegate != null;
085: this .delegate = delegate;
086: }
087:
088: // API of the class --------------------------------------------------------
089:
090: /**
091: * Moves the state to required phase. If given state was already reached
092: * the state is not changed. The method will throw exception if a state is
093: * illegal required. Acceptable parameters for thid method are <BR>
094: * <LI>{@link org.netbeans.napi.gsfret.source.Source.Phase.PARSED}
095: * <LI>{@link org.netbeans.napi.gsfret.source.Source.Phase.ELEMENTS_RESOLVED}
096: * <LI>{@link org.netbeans.napi.gsfret.source.Source.Phase.RESOLVED}
097: * <LI>{@link org.netbeans.napi.gsfret.source.Source.Phase.UP_TO_DATE}
098: *
099: * @param phase The required phase
100: * @return the reached state
101: * @throws IllegalArgumentException in case that given state can not be
102: * reached using this method
103: * @throws IOException when the file cannot be red
104: */
105: public Phase toPhase(Phase phase) throws IOException {
106: if (phase == MODIFIED) {
107: throw new IllegalArgumentException("Wrong phase" + phase);
108: }
109: if (delegate.jfo == null) {
110: Phase currentPhase = delegate.getPhase();
111: if (currentPhase.compareTo(phase) < 0) {
112: delegate.setPhase(phase);
113: }
114: return delegate.getPhase();
115: } else {
116: Phase currentPhase = Source.moveToPhase(phase,
117: this .delegate, false);
118: return currentPhase.compareTo(phase) < 0 ? currentPhase
119: : phase;
120: }
121: }
122:
123: /**
124: * Returns the current phase of the {@link Source}.
125: *
126: *
127: * @return {@link Source.Phase} the state which was reached by the {@link Source}.
128: */
129: @Override
130: public Phase getPhase() {
131: return this .delegate.getPhase();
132: }
133:
134: /**
135: * Returns the content of the file represented by the {@link Source}.
136: *
137: *
138: * @return String the java source
139: */
140: @Override
141: public String getText() {
142: return this .delegate.getText();
143: }
144:
145: @Override
146: public TokenHierarchy<?> getTokenHierarchy() {
147: return this .delegate.getTokenHierarchy();
148: }
149:
150: // @Override
151: // public Trees getTrees() {
152: // return this.delegate.getTrees();
153: // }
154: //
155: // @Override
156: // public Types getTypes() {
157: // return this.delegate.getTypes();
158: // }
159: //
160: // @Override
161: // public Elements getElements() {
162: // return this.delegate.getElements();
163: // }
164:
165: @Override
166: public Source getSource() {
167: return this .delegate.getSource();
168: }
169:
170: @Override
171: public ClasspathInfo getClasspathInfo() {
172: return this .delegate.getClasspathInfo();
173: }
174:
175: @Override
176: public FileObject getFileObject() {
177: return this .delegate.getFileObject();
178: }
179:
180: @Override
181: public Document getDocument() throws IOException {
182: return this .delegate.getDocument();
183: }
184:
185: // Package private methods -------------------------------------------------
186:
187: @Override
188: void setPhase(final Phase phase) {
189: throw new UnsupportedOperationException(
190: "CompilationController supports only read interface"); //NOI18N
191: }
192:
193: @Override
194: public void setLanguage(final Language language) {
195: throw new UnsupportedOperationException(
196: "CompilationController supports only read interface"); //NOI18N
197: }
198:
199: @Override
200: public Language getLanguage() {
201: return this .delegate.getLanguage();
202: }
203:
204: @Override
205: ParserTaskImpl getParserTask() {
206: return this .delegate.getParserTask();
207: }
208:
209: @Override
210: public Index getIndex(String mimeType) {
211: return this .delegate.getIndex(mimeType);
212: }
213:
214: @Override
215: public Collection<? extends ParserResult> getEmbeddedResults(
216: String mimeType) {
217: return this .delegate.getEmbeddedResults(mimeType);
218: }
219:
220: @Override
221: public ParserResult getEmbeddedResult(String mimeType, int offset) {
222: return this .delegate.getEmbeddedResult(mimeType, offset);
223: }
224:
225: @Override
226: public List<Error> getErrors() {
227: return this .delegate.getErrors();
228: }
229:
230: @Override
231: public void addEmbeddingResult(String mimeType, ParserResult result) {
232: this .delegate.addEmbeddingResult(mimeType, result);
233: }
234:
235: @Override
236: public Set<String> getEmbeddedMimeTypes() {
237: return this.delegate.getEmbeddedMimeTypes();
238: }
239:
240: }
|