1 19 20 package org.netbeans.modules.xml.wsdl.model.impl; 21 22 import java.util.Collections ; 23 import java.util.HashSet ; 24 import java.util.Hashtable ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import javax.xml.namespace.QName ; 30 import org.netbeans.modules.xml.wsdl.model.spi.*; 31 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 32 import org.openide.util.Lookup; 33 import org.openide.util.lookup.Lookups; 34 35 44 public class ElementFactoryRegistry { 45 46 private static ElementFactoryRegistry registry = null; 47 private Map <QName , ElementFactory> factories = null; 48 49 private ElementFactoryRegistry() { 50 initialize(); 51 } 52 53 public static ElementFactoryRegistry getDefault(){ 54 if (registry == null) { 55 registry = new ElementFactoryRegistry(); 56 } 57 return registry; 58 } 59 60 private void initialize(){ 62 factories = new Hashtable <QName , ElementFactory>(); 63 Lookup.Result results = Lookup.getDefault().lookup(new Lookup.Template(ElementFactory.class)); 64 for (Object service : results.allInstances()){ 65 register((ElementFactory)service); 66 } 67 68 if (factories.size() < 1) { 72 Lookup lu2 = Lookups.metaInfServices(this.getClass().getClassLoader()); 73 Lookup.Result results2 = lu2.lookup(new Lookup.Template(ElementFactory.class)); 74 for (Object service : results2.allInstances()){ 75 register((ElementFactory)service); 76 } 77 } 78 } 79 80 public void register(ElementFactory factory) { 81 for (QName q : factory.getElementQNames()) { 82 factories.put(q, factory); 83 } 84 resetQNameCache(); 85 } 86 87 public void unregister(ElementFactory fac){ 88 for (QName q : fac.getElementQNames()) { 89 factories.remove(q); 90 } 91 resetQNameCache(); 92 } 93 94 public ElementFactory get(QName type){ 95 return factories.get(type); 96 } 97 98 private Set <Class > knownEmbeddedModelTypes = null; 99 private Set <QName > knownQNames = null; 100 private Set <String > knownNames = null; 101 102 public void resetQNameCache() { 103 knownEmbeddedModelTypes = null; 104 knownQNames = null; 105 knownNames = null; 106 } 107 108 public Set <QName > getKnownQNames() { 109 return Collections.unmodifiableSet(knownQNames()); 110 } 111 112 private Set <QName > knownQNames() { 113 if (knownQNames == null) { 114 knownQNames = new HashSet <QName >(); 115 for (ElementFactory f : factories.values()) { 116 for (QName q : f.getElementQNames()) { 117 if (! knownQNames.add(q)) { 118 String msg = "Duplicate factory for: "+q; 119 Logger.getLogger(this.getClass().getName()).log(Level.FINE, "getKnownQNames", msg); } 121 } 122 } 123 } 124 return knownQNames; 125 } 126 127 public Set <String > getKnownElementNames() { 128 return Collections.unmodifiableSet(knownElementNames()); 129 } 130 131 private Set <String > knownElementNames() { 132 if (knownNames == null) { 133 knownNames = new HashSet <String >(); 134 for (QName q : knownQNames()) { 135 knownNames.add(q.getLocalPart()); 136 } 137 } 138 return knownNames; 139 } 140 141 public void addEmbeddedModelQNames(AbstractDocumentModel embeddedModel) { 142 if (knownEmbeddedModelTypes == null) { 143 knownEmbeddedModelTypes = new HashSet (); 144 } 145 if (! knownEmbeddedModelTypes.contains(embeddedModel.getClass())) { 146 knownQNames().addAll(embeddedModel.getQNames()); 147 knownNames = null; 148 } 149 } 150 } 151 | Popular Tags |