1 9 10 package org.ozoneDB.xml.dom4j.o3impl; 11 12 import org.dom4j.Namespace; 13 import org.ozoneDB.OzoneInterface; 14 import org.ozoneDB.OzoneObject; 15 import org.ozoneDB.UnexpectedException; 16 import org.ozoneDB.xml.dom4j.OzoneNamespaceCache; 17 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 29 public class NamespaceCacheImpl extends OzoneObject implements OzoneNamespaceCache { 30 31 34 protected static Map cache; 35 36 39 protected static Map noPrefixCache; 40 41 public static Namespace XML_NAMESPACE; 42 43 public static Namespace NO_NAMESPACE; 44 45 46 private static transient Map instanceMap = new HashMap (); 47 48 private static String OBJECT_NAME = NamespaceCacheImpl.class.getName(); 49 50 public static synchronized OzoneNamespaceCache getInstance(OzoneInterface db) { 51 try { 52 OzoneNamespaceCache instance = (OzoneNamespaceCache) instanceMap.get(db); 53 if (instance == null) { 54 System.out.println("looking for OzoneDocumentFactory instance (" + OBJECT_NAME + ") for database " + db); 55 instance = (OzoneNamespaceCache) db.objectForName(OBJECT_NAME); 56 if (instance == null) { 57 System.out.println("creating new OzoneDocumentFactory"); 58 instance = NamespaceCacheImpl.create(db); 59 } 60 instanceMap.put(db, instance); 61 } 62 System.out.println("returning factory " + instance); 63 return instance; 64 } catch (Exception e) { 65 throw new UnexpectedException("failed to get NamespaceCacheImpl instance!", e); 66 } 67 } 68 69 private static OzoneNamespaceCache create(OzoneInterface db) { 70 return (OzoneNamespaceCache) db.createObject(NamespaceCacheImpl.class, OzoneInterface.Public, OBJECT_NAME); 71 } 72 73 public void onCreate() { 74 XML_NAMESPACE = get("xml", "http://www.w3.org/XML/1998/namespace"); 75 76 77 NO_NAMESPACE = get("", ""); 78 79 } 80 81 83 public synchronized Namespace get(String prefix, String uri) { 84 Map cache = getURICache(uri); 85 Namespace answer = (Namespace) cache.get(prefix); 86 if (answer == null) { 87 answer = createNamespace(prefix, uri); 88 cache.put(prefix, answer); 89 } 90 return answer; 91 } 92 93 94 96 public synchronized Namespace get(String uri) { 97 if (noPrefixCache == null) { 98 noPrefixCache = createURIMap(); 99 } 100 Namespace answer = (Namespace) noPrefixCache.get(uri); 101 if (answer == null) { 102 answer = createNamespace("", uri); 103 noPrefixCache.put(uri, answer); 104 } 105 return answer; 106 } 107 108 public Namespace getXmlNamespace() { 109 return XML_NAMESPACE; 110 } 111 112 public Namespace getNoNamespace() { 113 return NO_NAMESPACE; 114 } 115 116 117 120 protected synchronized Map getURICache(String uri) { 121 if (cache == null) { 122 cache = createURIMap(); 123 } 124 Map answer = (Map ) cache.get(uri); 125 if (answer == null) { 126 answer = createPrefixMap(); 127 cache.put(uri, answer); 128 } 129 return answer; 130 } 131 132 135 protected Namespace createNamespace(String prefix, String uri) { 136 throw new RuntimeException ("FIXME - should NamespaceCacheImpl become a real OzoneObject or whould db be passed?"); 137 } 139 140 143 protected Map createPrefixMap() { 144 return Collections.synchronizedMap(new HashMap ()); 145 } 146 147 150 protected Map createURIMap() { 151 return Collections.synchronizedMap(new HashMap ()); 152 } 153 } 154 155 156 200 | Popular Tags |