2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.xml.api.model;
44
import java.beans.FeatureDescriptor;
45
import java.io.PrintWriter;
46
import java.io.StringWriter;
47
import java.lang.ref.Reference;
48
import java.lang.ref.WeakReference;
49
import java.util.ArrayList;
50
import java.util.Enumeration;
51
import java.util.Iterator;
52
import org.openide.ErrorManager;
53
import org.openide.util.Lookup;
54
import org.openide.util.lookup.Lookups;
57
* GrammarQuery service provider definition. Manager methods
58
* are interleated and client must invoke them in particular
59
* sequence from single thread to get desired results:
61
* enabledFor = manager.enabled(context);
62
* if (enabled != null) {
63
* grammar = manager.getGrammar(context);
64
* //... guard enableness and enjoy the grammar
70
public abstract class GrammarQueryManager {
73
private static Reference instance;
76
* Can this manager provide a grammar for given context?
77
* @param ctx GrammarEnvironment describing grammar context.
78
* @return <code>null</code> if a grammar cannot be provided for
79
* the context else return context items (subenum of
80
* <code>ctx.getDocumentChildren</code>) that defines
81
* grammar enableness context.
83
public abstract Enumeration enabled(GrammarEnvironment ctx);
86
* Factory method providing a root grammar for given document.
87
* @param ctx The same context that was passed to {@link #enabled}.
88
* @return GrammarQuery being able to work in the context
89
* or <code>null</code> if {@link #enabled} returns
90
* for the same context false.
92
public abstract GrammarQuery getGrammar(GrammarEnvironment ctx);
95
* @return detailed description.
97
public abstract FeatureDescriptor getDescriptor();
100
* A factory method looking for subclasses registered in Lookup
101
* under <code>Plugins/XML/GrammarQueryManagers</code>.
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).
114
* @return Best effort instance.
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);
122
return (GrammarQueryManager) cached;
126
* Delegating implementation.
128
private static class DefaultQueryManager extends GrammarQueryManager {
130
private static final String FOLDER = "Plugins/XML/GrammarQueryManagers";// NOI18N
132
private Lookup.Result registrations;
134
private static ThreadLocal<GrammarQueryManager> transaction =
135
new ThreadLocal<GrammarQueryManager>();
137
public FeatureDescriptor getDescriptor() {
138
FeatureDescriptor desc = new FeatureDescriptor();
139
desc.setHidden(true);
140
desc.setName(getClass().getName());
144
public GrammarQuery getGrammar(GrammarEnvironment ctx) {
146
GrammarQueryManager g = transaction.get();
148
GrammarQuery query = g.getGrammar(ctx);
150
ErrorManager err = ErrorManager.getDefault();
151
err.log(err.WARNING, "Broken contract: " + g.getClass());
155
ErrorManager err = ErrorManager.getDefault();
156
Exception ex = new IllegalStateException("Broken contract");
157
StringWriter stringWriter = new StringWriter();
158
PrintWriter writer = new PrintWriter(stringWriter);
159
ex.printStackTrace(writer);
161
err.log(err.WARNING, stringWriter.getBuffer().toString());
165
transaction.set(null);
169
public Enumeration enabled(GrammarEnvironment ctx) {
170
Iterator it = getRegistrations();
171
transaction.set(null);
172
ArrayList list = new ArrayList(5);
174
Enumeration en = ctx.getDocumentChildren();
175
while (en.hasMoreElements()) {
176
list.add(en.nextElement());
179
Object[] array = list.toArray();
180
while (it.hasNext()) {
181
GrammarQueryManager next = (GrammarQueryManager) it.next();
182
GrammarEnvironment env = new GrammarEnvironment(
183
org.openide.util.Enumerations.array (array),
184
ctx.getInputSource(),
187
Enumeration en = next.enabled(env);
189
transaction.set(next);
196
private synchronized Iterator getRegistrations() {
197
if (registrations != null) {
198
return registrations.allInstances().iterator();
201
registrations = Lookups.forPath(FOLDER).lookupResult(GrammarQueryManager.class);
202
return registrations.allInstances().iterator();