1 19 20 package org.netbeans.modules.xml.api.model; 21 22 import java.beans.FeatureDescriptor ; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.lang.ref.Reference ; 26 import java.lang.ref.WeakReference ; 27 import java.util.ArrayList ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import org.netbeans.modules.xml.api.model.GrammarQuery; 31 import org.openide.ErrorManager; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileSystem; 34 import org.openide.filesystems.Repository; 35 import org.openide.loaders.DataObject; 36 import org.openide.loaders.DataObjectNotFoundException; 37 import org.openide.loaders.FolderLookup; 38 import org.openide.util.Lookup; 39 import org.xml.sax.InputSource ; 40 41 55 public abstract class GrammarQueryManager { 56 57 private static Reference instance; 59 60 68 public abstract Enumeration enabled(GrammarEnvironment ctx); 69 70 77 public abstract GrammarQuery getGrammar(GrammarEnvironment ctx); 78 79 82 public abstract FeatureDescriptor getDescriptor(); 83 84 101 public static synchronized GrammarQueryManager getDefault() { 102 Object cached = instance != null ? instance.get() : null; 103 if (cached == null) { 104 cached = new DefaultQueryManager(); 105 instance = new WeakReference (cached); 106 } 107 return (GrammarQueryManager) cached; 108 } 109 110 113 private static class DefaultQueryManager extends GrammarQueryManager { 114 115 private static final String FOLDER = "Plugins/XML/GrammarQueryManagers"; 117 private Lookup.Result registrations; 118 119 private static ThreadLocal transaction = new ThreadLocal (); 120 121 public FeatureDescriptor getDescriptor() { 122 FeatureDescriptor desc = new FeatureDescriptor (); 123 desc.setHidden(true); 124 desc.setName(getClass().getName()); 125 return desc; 126 } 127 128 public GrammarQuery getGrammar(GrammarEnvironment ctx) { 129 try { 130 GrammarQueryManager g = (GrammarQueryManager) transaction.get(); 131 if (g != null) { 132 GrammarQuery query = g.getGrammar(ctx); 133 if (query == null) { 134 ErrorManager err = ErrorManager.getDefault(); 135 err.log(err.WARNING, "Broken contract: " + g.getClass()); 136 } 137 return query; 138 } else { 139 ErrorManager err = ErrorManager.getDefault(); 140 Exception ex = new IllegalStateException ("Broken contract"); 141 StringWriter stringWriter = new StringWriter (); 142 PrintWriter writer = new PrintWriter (stringWriter); 143 ex.printStackTrace(writer); 144 writer.flush(); 145 err.log(err.WARNING, stringWriter.getBuffer().toString()); 146 return null; 147 } 148 } finally { 149 transaction.set(null); 150 } 151 } 152 153 public Enumeration enabled(GrammarEnvironment ctx) { 154 Iterator it = getRegistrations(); 155 transaction.set(null); 156 ArrayList list = new ArrayList (5); 157 { 158 Enumeration en = ctx.getDocumentChildren(); 159 while (en.hasMoreElements()) { 160 list.add(en.nextElement()); 161 } 162 } 163 Object [] array = list.toArray(); 164 while (it.hasNext()) { 165 GrammarQueryManager next = (GrammarQueryManager) it.next(); 166 GrammarEnvironment env = new GrammarEnvironment( 167 org.openide.util.Enumerations.array (array), 168 ctx.getInputSource(), 169 ctx.getFileObject() 170 ); 171 Enumeration en = next.enabled(env); 172 if (en != null) { 173 transaction.set(next); 174 return en; 175 } 176 } 177 return null; 178 } 179 180 private synchronized Iterator getRegistrations() { 181 if (registrations != null) { 182 return registrations.allInstances().iterator(); 183 } 184 185 187 try { 188 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 189 FileObject fo = fs.findResource(FOLDER); 190 DataObject df = DataObject.find(fo); 191 if (df instanceof DataObject.Container) { 192 FolderLookup lookup = 193 new FolderLookup((DataObject.Container) df); 194 Lookup.Template template = 195 new Lookup.Template(GrammarQueryManager.class); 196 registrations = lookup.getLookup().lookup(template); 197 return registrations.allInstances().iterator(); 198 } else { 199 return new ArrayList (0).iterator(); 200 } 201 } catch (DataObjectNotFoundException ex) { 202 return new ArrayList (0).iterator(); 203 } 204 } 205 } 206 } 207 | Popular Tags |