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-2007 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.lib.lexer;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.lang.ref.WeakReference;
047: import java.util.ArrayList;
048: import java.util.Collection;
049: import java.util.Collections;
050: import java.util.HashMap;
051: import java.util.List;
052: import java.util.WeakHashMap;
053: import org.netbeans.api.lexer.InputAttributes;
054: import org.netbeans.api.lexer.Language;
055: import org.netbeans.api.lexer.LanguagePath;
056: import org.netbeans.api.lexer.Token;
057: import org.netbeans.api.lexer.TokenId;
058: import org.netbeans.spi.lexer.LanguageEmbedding;
059: import org.netbeans.spi.lexer.LanguageHierarchy;
060: import org.netbeans.spi.lexer.LanguageProvider;
061: import org.netbeans.spi.lexer.Lexer;
062: import org.netbeans.spi.lexer.LexerRestartInfo;
063: import org.openide.util.Lookup;
064: import org.openide.util.LookupEvent;
065: import org.openide.util.LookupListener;
066:
067: /**
068: *
069: * @author vita
070: */
071: public final class LanguageManager extends LanguageProvider implements
072: LookupListener, PropertyChangeListener {
073:
074: // Using lazy initialization because of deadlock in #108043.
075: private static Language<TokenId> NO_LANG = null;
076:
077: private static Language<TokenId> NO_LANG() {
078: if (NO_LANG == null) {
079: NO_LANG = new LanguageHierarchy<TokenId>() {
080: @Override
081: protected Lexer<TokenId> createLexer(
082: LexerRestartInfo<TokenId> info) {
083: return null;
084: }
085:
086: @Override
087: protected Collection<TokenId> createTokenIds() {
088: return Collections.emptyList();
089: }
090:
091: @Override
092: protected String mimeType() {
093: return "obscure/no-language-marker"; //NOI18N
094: }
095: }.language();
096: }
097: return NO_LANG;
098: }
099:
100: private static LanguageEmbedding<TokenId> NO_LANG_EMBEDDING = null;
101:
102: private static LanguageEmbedding<TokenId> NO_LANG_EMBEDDING() {
103: if (NO_LANG_EMBEDDING == null) {
104: NO_LANG_EMBEDDING = LanguageEmbedding.create(NO_LANG(), 0,
105: 0);
106: }
107: return NO_LANG_EMBEDDING;
108: }
109:
110: private static LanguageManager instance = null;
111:
112: public static synchronized LanguageManager getInstance() {
113: if (instance == null) {
114: instance = new LanguageManager();
115: }
116: return instance;
117: }
118:
119: private Lookup.Result<LanguageProvider> lookupResult = null;
120:
121: private List<LanguageProvider> providers = Collections
122: .<LanguageProvider> emptyList();
123: private HashMap<String, WeakReference<Language<?>>> langCache = new HashMap<String, WeakReference<Language<?>>>();
124: private WeakHashMap<Token, LanguageEmbedding<?>> tokenLangCache = new WeakHashMap<Token, LanguageEmbedding<?>>();
125:
126: private final String LOCK = new String("LanguageManager.LOCK");
127:
128: /** Creates a new instance of LanguageManager */
129: private LanguageManager() {
130: lookupResult = Lookup.getDefault().lookup(
131: new Lookup.Template<LanguageProvider>(
132: LanguageProvider.class));
133: lookupResult.addLookupListener(this );
134: refreshProviders();
135: }
136:
137: // -------------------------------------------------------------------
138: // LanguageProvider implementation
139: // -------------------------------------------------------------------
140:
141: public Language<?> findLanguage(String mimeType) {
142: assert mimeType != null : "The mimeType parameter can't be null"; //NOI18N
143:
144: // XXX: This hack is here to normalize mime types used by
145: // Tools-Options -> Fonts & Colors for previewing changes done by users
146: if (mimeType.startsWith("test")) { //NOI18N
147: int idx = mimeType.indexOf('_'); //NOI18N
148: assert idx != -1 : "Invalid 'testXXX_' mimeType: "
149: + mimeType; //NOI18N
150: mimeType = mimeType.substring(idx + 1);
151: }
152:
153: synchronized (LOCK) {
154: WeakReference<Language<?>> ref = langCache.get(mimeType);
155: Language<?> lang = ref == null ? null : ref.get();
156:
157: if (lang == null) {
158: for (LanguageProvider p : providers) {
159: if (null != (lang = p.findLanguage(mimeType))) {
160: break;
161: }
162: }
163:
164: if (lang == null) {
165: lang = NO_LANG();
166: }
167:
168: langCache.put(mimeType, new WeakReference<Language<?>>(
169: lang));
170: }
171:
172: return lang == NO_LANG() ? null : lang;
173: }
174: }
175:
176: public LanguageEmbedding<?> findLanguageEmbedding(Token<?> token,
177: LanguagePath languagePath, InputAttributes inputAttributes) {
178: synchronized (LOCK) {
179: LanguageEmbedding<?> lang = tokenLangCache.get(token);
180:
181: if (lang == null) {
182: for (LanguageProvider p : providers) {
183: if (null != (lang = p.findLanguageEmbedding(token,
184: languagePath, inputAttributes))) {
185: break;
186: }
187: }
188:
189: if (lang == null) {
190: lang = NO_LANG_EMBEDDING();
191: }
192:
193: tokenLangCache.put(token, lang);
194: }
195:
196: return lang == NO_LANG_EMBEDDING() ? null : lang;
197: }
198: }
199:
200: // -------------------------------------------------------------------
201: // LookupListener implementation
202: // -------------------------------------------------------------------
203:
204: public void resultChanged(LookupEvent ev) {
205: refreshProviders();
206: }
207:
208: // -------------------------------------------------------------------
209: // PropertyChangeListener implementation
210: // -------------------------------------------------------------------
211:
212: public void propertyChange(PropertyChangeEvent evt) {
213: if (evt.getPropertyName() == null) {
214: synchronized (LOCK) {
215: langCache.clear();
216: tokenLangCache.clear();
217: }
218: } else if (LanguageProvider.PROP_LANGUAGE.equals(evt
219: .getPropertyName())) {
220: synchronized (LOCK) {
221: langCache.clear();
222: }
223: } else if (LanguageProvider.PROP_EMBEDDED_LANGUAGE.equals(evt
224: .getPropertyName())) {
225: synchronized (LOCK) {
226: tokenLangCache.clear();
227: }
228: }
229: // Forward firing of the property change to registered clients
230: firePropertyChange(evt.getPropertyName());
231: }
232:
233: // -------------------------------------------------------------------
234: // private implementation
235: // -------------------------------------------------------------------
236:
237: private void refreshProviders() {
238: Collection<? extends LanguageProvider> newProviders = lookupResult
239: .allInstances();
240:
241: synchronized (LOCK) {
242: for (LanguageProvider p : providers) {
243: p.removePropertyChangeListener(this );
244: }
245:
246: providers = new ArrayList<LanguageProvider>(newProviders);
247:
248: for (LanguageProvider p : providers) {
249: p.addPropertyChangeListener(this);
250: }
251:
252: langCache.clear();
253: tokenLangCache.clear();
254: }
255: }
256:
257: }
|