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.xml.api.model;
043:
044: import java.beans.FeatureDescriptor;
045: import java.io.PrintWriter;
046: import java.io.StringWriter;
047: import java.lang.ref.Reference;
048: import java.lang.ref.WeakReference;
049: import java.util.ArrayList;
050: import java.util.Enumeration;
051: import java.util.Iterator;
052: import org.openide.ErrorManager;
053: import org.openide.util.Lookup;
054: import org.openide.util.lookup.Lookups;
055:
056: /**
057: * GrammarQuery service provider definition. Manager methods
058: * are interleated and client must invoke them in particular
059: * sequence from single thread to get desired results:
060: * <pre>
061: * enabledFor = manager.enabled(context);
062: * if (enabled != null) {
063: * grammar = manager.getGrammar(context);
064: * //... guard enableness and enjoy the grammar
065: * }
066: * </pre>
067: *
068: * @author Petr Kuzel
069: */
070: public abstract class GrammarQueryManager {
071:
072: // default instance
073: private static Reference instance;
074:
075: /**
076: * Can this manager provide a grammar for given context?
077: * @param ctx GrammarEnvironment describing grammar context.
078: * @return <code>null</code> if a grammar cannot be provided for
079: * the context else return context items (subenum of
080: * <code>ctx.getDocumentChildren</code>) that defines
081: * grammar enableness context.
082: */
083: public abstract Enumeration enabled(GrammarEnvironment ctx);
084:
085: /**
086: * Factory method providing a root grammar for given document.
087: * @param ctx The same context that was passed to {@link #enabled}.
088: * @return GrammarQuery being able to work in the context
089: * or <code>null</code> if {@link #enabled} returns
090: * for the same context false.
091: */
092: public abstract GrammarQuery getGrammar(GrammarEnvironment ctx);
093:
094: /**
095: * @return detailed description.
096: */
097: public abstract FeatureDescriptor getDescriptor();
098:
099: /**
100: * A factory method looking for subclasses registered in Lookup
101: * under <code>Plugins/XML/GrammarQueryManagers</code>.
102: * <p>
103: * There are defined some ordering marks to which every registration
104: * must express its position:
105: * <code>semantics-grammar-to-generic-grammar-separator</code>.
106: * All generic grammars such as universal DTD and XML Schema grammar
107: * must be behing this mark. Semantics grammars such as
108: * XSLT only handling grammar must be placed before it.
109: * <code>generic-grammar-to-universal-grammar-separator</code>
110: * allows to distingwish between generic grammars and universal
111: * ones (e.g. a grammar that scans well-formed document and
112: * using heuritics methods it tries to guess actual grammar on fly).
113: *
114: * @return Best effort instance.
115: */
116: public static synchronized GrammarQueryManager getDefault() {
117: Object cached = instance != null ? instance.get() : null;
118: if (cached == null) {
119: cached = new DefaultQueryManager();
120: instance = new WeakReference(cached);
121: }
122: return (GrammarQueryManager) cached;
123: }
124:
125: /**
126: * Delegating implementation.
127: */
128: private static class DefaultQueryManager extends
129: GrammarQueryManager {
130:
131: private static final String FOLDER = "Plugins/XML/GrammarQueryManagers";// NOI18N
132:
133: private Lookup.Result registrations;
134:
135: private static ThreadLocal<GrammarQueryManager> transaction = new ThreadLocal<GrammarQueryManager>();
136:
137: public FeatureDescriptor getDescriptor() {
138: FeatureDescriptor desc = new FeatureDescriptor();
139: desc.setHidden(true);
140: desc.setName(getClass().getName());
141: return desc;
142: }
143:
144: public GrammarQuery getGrammar(GrammarEnvironment ctx) {
145: try {
146: GrammarQueryManager g = transaction.get();
147: if (g != null) {
148: GrammarQuery query = g.getGrammar(ctx);
149: if (query == null) {
150: ErrorManager err = ErrorManager.getDefault();
151: err.log(err.WARNING, "Broken contract: "
152: + g.getClass());
153: }
154: return query;
155: } else {
156: ErrorManager err = ErrorManager.getDefault();
157: Exception ex = new IllegalStateException(
158: "Broken contract");
159: StringWriter stringWriter = new StringWriter();
160: PrintWriter writer = new PrintWriter(stringWriter);
161: ex.printStackTrace(writer);
162: writer.flush();
163: err.log(err.WARNING, stringWriter.getBuffer()
164: .toString());
165: return null;
166: }
167: } finally {
168: transaction.set(null);
169: }
170: }
171:
172: public Enumeration enabled(GrammarEnvironment ctx) {
173: Iterator it = getRegistrations();
174: transaction.set(null);
175: ArrayList list = new ArrayList(5);
176: {
177: Enumeration en = ctx.getDocumentChildren();
178: while (en.hasMoreElements()) {
179: list.add(en.nextElement());
180: }
181: }
182: Object[] array = list.toArray();
183: while (it.hasNext()) {
184: GrammarQueryManager next = (GrammarQueryManager) it
185: .next();
186: GrammarEnvironment env = new GrammarEnvironment(
187: org.openide.util.Enumerations.array(array), ctx
188: .getInputSource(), ctx.getFileObject());
189: Enumeration en = next.enabled(env);
190: if (en != null) {
191: transaction.set(next);
192: return en;
193: }
194: }
195: return null;
196: }
197:
198: private synchronized Iterator getRegistrations() {
199: if (registrations != null) {
200: return registrations.allInstances().iterator();
201: }
202:
203: registrations = Lookups.forPath(FOLDER).lookupResult(
204: GrammarQueryManager.class);
205: return registrations.allInstances().iterator();
206: }
207: }
208: }
|