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.modules.php.editor.completion;
043:
044: import java.io.IOException;
045: import java.util.Collections;
046: import java.util.ConcurrentModificationException;
047: import java.util.Set;
048: import javax.swing.text.Document;
049: import org.netbeans.modules.gsf.api.HtmlFormatter;
050: import org.netbeans.api.lexer.Token;
051: import org.netbeans.api.lexer.TokenSequence;
052: import org.netbeans.modules.php.editor.TokenUtils;
053: import org.netbeans.modules.php.model.PhpModel;
054:
055: /**
056: *
057: * @author Victor G. Vasilyev
058: */
059: public abstract class ASTBasedProvider {
060: protected CodeCompletionContext myContext;
061:
062: @SuppressWarnings("unchecked")
063: protected static final Set<ExpectedToken> ANY_TOKEN = Collections.EMPTY_SET;
064:
065: /**
066: * A service that is used as the key to recognize a given project as the
067: * PHP Project.
068: */
069: // private static final Class PHP_PROJECT_KEY_SERVICE = null;
070: /**
071: * Wrapps info about expected token.
072: */
073: protected static class ExpectedToken {
074: private String type;
075: private String text;
076:
077: /**
078: * Creates wrapper.
079: * @param type expected type of the token (non-null value)
080: * @param text expected text of the token, <code>null</code> means any
081: * text.
082: */
083: public ExpectedToken(String type, String text) {
084: assert type != null;
085: this .type = type;
086: this .text = text;
087: }
088:
089: public String getType() {
090: return type;
091: }
092:
093: public String getText() {
094: return text;
095: }
096:
097: public boolean isAnyText() {
098: return text == null;
099: }
100: }
101:
102: /**
103: *
104: * @param c the set of expected tokens in the current position.
105: * @param l1 the set of expected tokens in the previous position.
106: * @return <code>true</code> if expected tokens are matched.
107: */
108: protected boolean isMatchedCL1(Set<ExpectedToken> c,
109: Set<ExpectedToken> l1) {
110: try {
111: TokenSequence ts = getTokenSequencePHP();
112: Token t = TokenUtils.getEnteredToken(ts, getCaretOffset());
113: if (c != ANY_TOKEN) {
114: assertOneOf(t, c);
115: }
116: if (l1 != ANY_TOKEN) {
117: boolean hasPreviousToken = ts.movePrevious();
118: if (hasPreviousToken) {
119: t = ts.token();
120: assertOneOf(t, l1);
121: }
122: }
123: return true;
124: } catch (IOException ioe) {
125: return false; // getDocument() fails
126: } catch (ConcurrentModificationException cme) {
127: // the token sequence is no longer valid because of an underlying
128: // mutable input source modification.
129: return false;
130: } catch (Exception e) {
131: return false;
132: }
133: }
134:
135: /**
136: * Returns the <code>TokenSequence</code> of the PHP block pointed by
137: * the caret offset of the <code>CodeCompletionContext</code>.
138: * @return the <code>TokenSequence</code> of the PHP block.
139: * @throws java.io.IOException if {@link #getDocument()} fails
140: * @throws java.lang.Exception if pointed code is not a PHP block or
141: * <code>TokenSequence</code> can't be returned for this code.
142: */
143: protected TokenSequence getTokenSequencePHP() throws IOException,
144: Exception {
145: assertPHPContext();
146: Document doc = getDocument();
147: int offset = getCaretOffset();
148: TokenSequence ts = TokenUtils.getEmbeddedTokenSequence(doc,
149: offset);
150: if (ts == null) {
151: throw new Exception();
152: }
153: return ts;
154:
155: }
156:
157: /**
158: * Assert that a given <code>CodeCompletionContext</code> in the scope of a
159: * PHP project.
160: * @throws java.lang.Exception if a given <code>CodeCompletionContext</code>
161: * is not in the scope of a PHP project.
162: */
163: protected void assertPHPProject() throws Exception {
164: // FileObject fo = myContext.getCompilationInfo().getFileObject();
165: // Project project = FileOwnerQuery.getOwner(fo);
166: // if (project == null ||
167: // project.getLookup().lookup(PHP_PROJECT_KEY_SERVICE) == null) {
168: throw new Exception();
169: // }
170: }
171:
172: /**
173: * Assert that a given <code>CodeCompletionContext</code> in the file that
174: * has a MIME type registered in the IDE for PHP processing.
175: * @throws java.lang.Exception if a given <code>CodeCompletionContext</code>
176: * is not in the in the file that has a MIME type registered in the IDE for
177: * PHP processing.
178: */
179: protected void assertMIMETypePHP() throws Exception {
180: String type = myContext.getCompilationInfo().getFileObject()
181: .getMIMEType();
182: if (!type.equals("text/x-php5")) {
183: throw new Exception();
184: }
185: }
186:
187: protected void assertPHPContext() throws IOException, Exception {
188: Document doc = getDocument();
189: int offset = getCaretOffset();
190: if (!TokenUtils.checkPhp(doc, offset)) {
191: throw new Exception();
192: }
193: }
194:
195: protected void assertWhitespace(Token t) throws Exception {
196: if (!TokenUtils.PHPTokenName.WHITESPACE.value().equals(
197: TokenUtils.getTokenType(t))) {
198: throw new Exception();
199: }
200: }
201:
202: protected void assertOneOf(Token t, Set<ExpectedToken> tokenSet)
203: throws Exception {
204: String tType = TokenUtils.getTokenType(t);
205: String tText = t.text().toString();
206: for (ExpectedToken et : tokenSet) {
207: if (et.getType().equals(tType)) {
208: String etText = et.getText();
209: if (et.isAnyText()) {
210: return;
211: }
212: if (etText.equals(tText)) {
213: return;
214: }
215: }
216: }
217: throw new Exception();
218: }
219:
220: protected Document getDocument() throws IOException {
221: return myContext.getCompilationInfo().getDocument();
222: }
223:
224: protected int getCaretOffset() {
225: return myContext.getCaretOffset();
226: }
227:
228: protected HtmlFormatter getFormatter() {
229: return myContext.getFormatter();
230: }
231:
232: protected PhpModel getModel() {
233: return myContext.getSourceElement().getModel();
234: }
235:
236: protected void checkContext(CodeCompletionContext context)
237: throws IllegalStateException {
238: if (context != myContext) {
239: throw new IllegalStateException(
240: "The isApplicable method MUST BE called before.");
241: }
242: }
243:
244: }
|