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.modules.languages;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Map.Entry;
051: import java.util.Set;
052: import org.netbeans.api.languages.ASTEvaluator;
053: import org.netbeans.api.languages.ASTItem;
054: import org.netbeans.api.languages.ASTNode;
055: import org.netbeans.api.languages.ASTPath;
056: import org.netbeans.api.languages.ASTToken;
057: import org.netbeans.api.languages.ParserManager.State;
058:
059: /**
060: *
061: * @author hanz
062: */
063: public class FeatureList {
064:
065: private Map<String, List<Feature>> features;
066: private Map<String, FeatureList> lists;
067:
068: void add(Feature feature) {
069: Selector selector = feature.getSelector();
070: FeatureList list = this ;
071: if (selector != null) {
072: List<String> path = selector.getPath();
073: for (int i = path.size() - 1; i >= 0; i--) {
074: String name = path.get(i);
075: if (list.lists == null)
076: list.lists = new HashMap<String, FeatureList>();
077: FeatureList newList = list.lists.get(name);
078: if (newList == null) {
079: newList = new FeatureList();
080: list.lists.put(name, newList);
081: }
082: list = newList;
083: }
084: }
085: if (list.features == null)
086: list.features = new HashMap<String, List<Feature>>();
087: List<Feature> fs = list.features.get(feature.getFeatureName());
088: if (fs == null) {
089: fs = new ArrayList<Feature>();
090: list.features.put(feature.getFeatureName(), fs);
091: }
092: fs.add(feature);
093: }
094:
095: void importFeatures(FeatureList featureList) {
096:
097: }
098:
099: public List<Feature> getFeatures(String featureName) {
100: List<Feature> list = new ArrayList<Feature>();
101: collectFeatures(list, featureName);
102: return list;
103: }
104:
105: public Feature getFeature(String featureName) {
106: List<Feature> features = getFeatures(featureName);
107: if (features.isEmpty())
108: return null;
109: return features.get(0);
110: }
111:
112: private void collectFeatures(List result, String featureName) {
113: if (features != null) {
114: List<Feature> list = features.get(featureName);
115: if (list != null) {
116: result.addAll(list);
117: }
118: }
119: if (lists != null) {
120: for (Entry<String, FeatureList> entry : lists.entrySet()) {
121: FeatureList fList = entry.getValue();
122: if (fList != null) {
123: fList.collectFeatures(result, featureName);
124: }
125: } // for
126: } // if
127: }
128:
129: public List<Feature> getFeatures(String featureName, String id) {
130: if (lists == null)
131: return Collections.<Feature> emptyList();
132: FeatureList list = lists.get(id);
133: if (list == null)
134: return Collections.<Feature> emptyList();
135: if (list.features == null)
136: return Collections.<Feature> emptyList();
137: List<Feature> result = list.features.get(featureName);
138: if (result != null)
139: return result;
140: return Collections.<Feature> emptyList();
141: }
142:
143: public Feature getFeature(String featureName, String id) {
144: List<Feature> features = getFeatures(featureName, id);
145: if (features.isEmpty())
146: return null;
147: return features.get(0);
148: }
149:
150: public List<Feature> getFeatures(String featureName, ASTPath path) {
151: List<Feature> result = null;
152: FeatureList list = this ;
153: for (int i = path.size() - 1; i > 0; i--) {
154: ASTItem item = path.get(i);
155: String name = item instanceof ASTNode ? ((ASTNode) item)
156: .getNT() : ((ASTToken) item).getTypeName();
157: if (list.lists == null)
158: break;
159: list = list.lists.get(name);
160: if (list == null)
161: break;
162: if (list.features == null)
163: continue;
164: List<Feature> l = list.features.get(featureName);
165: if (l == null)
166: continue;
167: if (result == null)
168: result = new ArrayList<Feature>(l);
169: else
170: result.addAll(l);
171: }
172: if (result == null)
173: return Collections.<Feature> emptyList();
174: return result;
175: }
176:
177: public Feature getFeature(String featureName, ASTPath path) {
178: List<Feature> features = getFeatures(featureName, path);
179: if (features.isEmpty())
180: return null;
181: return features.get(0);
182: }
183:
184: void evaluate(State state, List<ASTItem> path,
185: Map<String, Set<ASTEvaluator>> evaluatorsMap //,Map<Object,Long> times
186: ) {
187: FeatureList list = this ;
188: for (int i = path.size() - 1; i > 0; i--) {
189: ASTItem item = path.get(i);
190: String name = item instanceof ASTNode ? ((ASTNode) item)
191: .getNT() : ((ASTToken) item).getTypeName();
192: if (list.lists == null)
193: return;
194: list = list.lists.get(name);
195: if (list == null)
196: return;
197: if (list.features != null) {
198: Iterator<String> it = evaluatorsMap.keySet().iterator();
199: while (it.hasNext()) {
200: String featureName = it.next();
201: if (featureName == null) {
202: Set<ASTEvaluator> evaluators = evaluatorsMap
203: .get(null);
204: Iterator<ASTEvaluator> it2 = evaluators
205: .iterator();
206: while (it2.hasNext()) {
207: ASTEvaluator evaluator = it2.next();
208: Collection<List<Feature>> featureListsC = list.features
209: .values();
210: Iterator<List<Feature>> it3 = featureListsC
211: .iterator();
212: while (it3.hasNext()) {
213: List<Feature> featureList = it3.next();
214: Iterator<Feature> it4 = featureList
215: .iterator();
216: while (it4.hasNext()) { //long time = System.currentTimeMillis ();
217: evaluator.evaluate(state, path, it4
218: .next());
219: //Long l = times.get (evaluator);time = System.currentTimeMillis () - time; if (l != null) time += l.longValue (); times.put (evaluator, time);
220: }
221: }
222: }
223: } else {
224: List<Feature> features = list.features
225: .get(featureName);
226: if (features == null)
227: continue;
228: Set<ASTEvaluator> evaluators = evaluatorsMap
229: .get(featureName);
230:
231: Iterator<Feature> it2 = features.iterator();
232: while (it2.hasNext()) {
233: Feature feature = it2.next();
234: Iterator<ASTEvaluator> it3 = evaluators
235: .iterator();
236: while (it3.hasNext()) {
237: ASTEvaluator evaluator = it3.next(); //long time = System.currentTimeMillis ();
238: evaluator
239: .evaluate(state, path, feature); //Long l = times.get (evaluator);time = System.currentTimeMillis () - time; if (l != null) time += l.longValue (); times.put (evaluator, time);
240: }
241: }
242: }
243: }
244: }
245: }
246: }
247:
248: public String toString() {
249: StringBuilder sb = new StringBuilder();
250: toString(sb, "");
251: return sb.toString();
252: }
253:
254: private void toString(StringBuilder sb, String selector) {
255: sb.append("Selector: ").append(selector).append("\n");
256: if (features != null) {
257: Iterator<String> it = features.keySet().iterator();
258: while (it.hasNext()) {
259: String featureName = it.next();
260: Iterator<Feature> it2 = features.get(featureName)
261: .iterator();
262: while (it2.hasNext()) {
263: Feature feature = it2.next();
264: sb.append(" ").append(featureName).append(": ")
265: .append(feature).append("\n");
266: }
267: }
268: }
269: if (lists != null) {
270: Iterator<String> it = lists.keySet().iterator();
271: while (it.hasNext()) {
272: String s = it.next();
273: lists.get(s).toString(sb, selector + "." + s);
274: }
275: }
276: }
277: }
|