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.api.languages;
042:
043: import java.lang.ref.WeakReference;
044: import java.util.Iterator;
045: import java.util.Map;
046: import java.util.WeakHashMap;
047: import javax.swing.text.Document;
048: import org.netbeans.modules.languages.ParserManagerImpl;
049:
050: /**
051: * Represents parser implementation.
052: *
053: * @author Jan Jancura
054: */
055: public abstract class ParserManager {
056:
057: /**
058: * State of parser.
059: */
060: public static enum State {
061: /** Parser is running. */
062: PARSING,
063: /** Parsed without errors. */
064: OK,
065: /** Parser with errors. */
066: ERROR,
067: /** Parser has not been started yet. */
068: NOT_PARSED
069: }
070:
071: private static Map<Document, WeakReference<ParserManager>> managers = new WeakHashMap<Document, WeakReference<ParserManager>>();
072:
073: /**
074: * Returns parser for given {@link javax.swing.text.Document}.
075: *
076: * @return parser for given {@link javax.swing.text.Document}
077: */
078: public static synchronized ParserManager get(Document doc) {
079: WeakReference<ParserManager> wr = managers.get(doc);
080: ParserManager pm = wr != null ? wr.get() : null;
081: if (pm == null) {
082: pm = new ParserManagerImpl(doc);
083: managers.put(doc, new WeakReference<ParserManager>(pm));
084: //Utils.startTest ("ParserManager.managers", managers);
085: // printManagers ();
086: }
087: return pm;
088: }
089:
090: /**
091: * Returns state of parser.
092: *
093: * @retrun a state of parser
094: */
095: public abstract State getState();
096:
097: /**
098: * Returns AST tree root node.
099: *
100: * @throws in the case of errors in document
101: * @retrun AST tree root node
102: */
103: public abstract ASTNode getAST() throws ParseException;
104:
105: /**
106: * Registers ParserManagerListener.
107: *
108: * @param l ParserManagerListener to be registerred
109: */
110: public abstract void addListener(ParserManagerListener l);
111:
112: /**
113: * Unregisters ParserManagerListener.
114: *
115: * @param l ParserManagerListener to be unregisterred
116: */
117: public abstract void removeListener(ParserManagerListener l);
118:
119: /**
120: * Registers ASTEvaluator.
121: *
122: * @param l ASTEvaluator to be unregisterred
123: */
124: public abstract void addASTEvaluator(ASTEvaluator e);
125:
126: /**
127: * Unregisters ASTEvaluator.
128: *
129: * @param l ASTEvaluator to be unregisterred
130: */
131: public abstract void removeASTEvaluator(ASTEvaluator e);
132:
133: private static void printManagers() {
134: System.out.println("\nParserManagers:");
135: Iterator<Document> it = managers.keySet().iterator();
136: while (it.hasNext()) {
137: Document document = it.next();
138: String title = (String) document.getProperty("title");
139: if (title == null)
140: title = document.toString();
141: WeakReference wr = managers.get(document);
142: if (wr.get() != null)
143: System.out.println(" " + title);
144: }
145: }
146: }
|