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.languages;
043:
044: import java.beans.PropertyChangeListener;
045: import java.io.BufferedReader;
046: import java.io.IOException;
047: import java.io.InputStream;
048: import java.io.InputStreamReader;
049: import java.util.ArrayList;
050: import java.util.Collections;
051: import java.util.Map;
052: import java.util.List;
053:
054: import org.netbeans.api.languages.ASTNode;
055: import org.netbeans.api.languages.ASTPath;
056: import org.netbeans.api.languages.ParseException;
057: import org.netbeans.api.languages.SyntaxContext;
058: import org.netbeans.api.languages.TokenInput;
059: import org.netbeans.modules.languages.parser.LLSyntaxAnalyser;
060: import org.netbeans.modules.languages.parser.Parser;
061: import org.netbeans.modules.languages.parser.StringInput;
062: import org.netbeans.modules.languages.parser.SyntaxError;
063: import org.netbeans.modules.languages.parser.TokenInputUtils;
064:
065: /**
066: *
067: * @author Jan Jancura
068: */
069: public abstract class Language extends
070: org.netbeans.api.languages.Language {
071:
072: public static Language create(String mimeType) {
073: return new EmptyLanguage(mimeType);
074: }
075:
076: // public methods ..........................................................
077:
078: public abstract String getMimeType();
079:
080: public abstract Parser getParser();
081:
082: public abstract LLSyntaxAnalyser getAnalyser();
083:
084: public abstract FeatureList getFeatureList();
085:
086: public abstract void addPropertyChangeListener(
087: PropertyChangeListener l);
088:
089: public abstract void removePropertyChangeListener(
090: PropertyChangeListener l);
091:
092: public abstract int getTokenID(String tokenType);
093:
094: public abstract int getTokenTypeCount();
095:
096: public abstract String getTokenType(int tokenTypeID);
097:
098: public abstract int getNTID(String nt);
099:
100: public abstract int getNTCount();
101:
102: public abstract String getNT(int ntid);
103:
104: public abstract List<Language> getImportedLanguages();
105:
106: public abstract Feature getPreprocessorImport();
107:
108: public abstract Map<String, Feature> getTokenImports();
109:
110: // private helper methods ..................................................
111:
112: public ASTNode parse(InputStream is) throws IOException,
113: ParseException {
114: BufferedReader br = new BufferedReader(
115: new InputStreamReader(is));
116: StringBuilder sb = new StringBuilder();
117: String ln = br.readLine();
118: while (ln != null) {
119: sb.append(ln).append('\n');
120: ln = br.readLine();
121: }
122: TokenInput ti = TokenInputUtils.create(this , getParser(),
123: new StringInput(sb.toString()));
124: ASTNode root = getAnalyser().read(ti, true,
125: new ArrayList<SyntaxError>(), new boolean[] { false });
126: Feature astProperties = getFeatureList().getFeature("AST");
127: if (astProperties != null && root != null) {
128: ASTNode root1 = (ASTNode) astProperties.getValue("process",
129: SyntaxContext.create(null, ASTPath.create(root)));
130: if (root1 != null)
131: root = root1;
132: }
133: return root;
134: }
135:
136: private static class EmptyLanguage extends Language {
137:
138: private String mimeType;
139:
140: EmptyLanguage(String mimeType) {
141: this .mimeType = mimeType;
142: }
143:
144: public String getMimeType() {
145: return mimeType;
146: }
147:
148: public Parser getParser() {
149: return null;
150: }
151:
152: private LLSyntaxAnalyser analyser = LLSyntaxAnalyser
153: .createEmpty(this );
154:
155: public LLSyntaxAnalyser getAnalyser() {
156: return analyser;
157: }
158:
159: private FeatureList featureList = new FeatureList();
160:
161: public FeatureList getFeatureList() {
162: return featureList;
163: }
164:
165: public void addPropertyChangeListener(PropertyChangeListener l) {
166: }
167:
168: public void removePropertyChangeListener(
169: PropertyChangeListener l) {
170: }
171:
172: // ids ...
173:
174: public int getTokenID(String tokenType) {
175: throw new UnsupportedOperationException(
176: "Not supported yet.");
177: }
178:
179: public int getTokenTypeCount() {
180: return 0;
181: }
182:
183: public String getTokenType(int tokenTypeID) {
184: throw new UnsupportedOperationException(
185: "Not supported yet.");
186: }
187:
188: public int getNTID(String nt) {
189: throw new UnsupportedOperationException(
190: "Not supported yet.");
191: }
192:
193: public int getNTCount() {
194: return 0;
195: }
196:
197: public String getNT(int ntid) {
198: throw new UnsupportedOperationException(
199: "Not supported yet.");
200: }
201:
202: // imports ...
203:
204: public List<Language> getImportedLanguages() {
205: return Collections.<Language> emptyList();
206: }
207:
208: public Feature getPreprocessorImport() {
209: return null;
210: }
211:
212: public Map<String, Feature> getTokenImports() {
213: return Collections.<String, Feature> emptyMap();
214: }
215: }
216: }
|