1 19 package org.netbeans.modules.xml.catalog.impl; 20 21 import java.awt.*; 22 import java.io.*; 23 import java.beans.*; 24 import java.util.*; 25 26 import org.xml.sax.EntityResolver ; 27 import org.xml.sax.InputSource ; 28 import org.xml.sax.SAXException ; 29 30 import org.openide.util.*; 31 32 import org.netbeans.modules.xml.catalog.spi.*; 33 import org.netbeans.modules.xml.catalog.lib.*; 34 35 44 public abstract class AbstractCatalog { 45 46 47 private Map publicMap = new HashMap(); 48 49 50 private Map systemMap = new HashMap(); 51 52 private String location; 53 54 private Vector listeners; 55 56 58 59 private Map delegate = new HashMap(); 60 61 62 private Vector delegateOrder = new Vector(); 63 64 65 protected Vector extenders = new Vector(); 66 67 71 74 public void setLocation(String location) { 75 this.location = location; 76 } 77 78 public String getLocation() { 79 return location; 80 } 81 82 83 87 public synchronized void addCatalogListener(CatalogListener l) { 88 if (listeners == null) listeners = new Vector(2); 89 listeners.add(l); 90 } 91 92 96 public synchronized void removeCatalogListener(CatalogListener l) { 97 if (listeners == null) return; 98 if (listeners != null) listeners.remove(l); 99 if (listeners.isEmpty()) listeners = null; 100 } 101 102 103 protected void notifyInvalidate() { 104 105 CatalogListener[] lis = null; 106 107 synchronized (this) { 108 if (listeners == null || listeners.isEmpty()) return; 109 lis = (CatalogListener[]) listeners.toArray(new CatalogListener[0]); 110 } 111 112 for (int i = 0; i<lis.length; i++) { 113 lis[i].notifyInvalidate(); 114 } 115 } 116 117 123 public void addPublicMapping(String publicId, String systemId) { 124 publicMap.put(publicId, systemId); 125 } 126 127 132 public void removePublicMapping(System publicId) { 133 publicMap.remove(publicId); 134 } 135 136 137 public Iterator getPublicMappingKeys() { 138 return publicMap.keySet().iterator(); 139 } 140 141 149 public String getPublicMapping(String publicId) { 150 return (String )publicMap.get(publicId); 151 } 152 153 159 public void addSystemMapping(String systemId1, String systemId2) { 160 systemMap.put(systemId1, systemId2); 161 } 162 163 168 public void removeSystemMapping(String systemId) { 169 systemMap.remove(systemId); 170 } 171 172 173 public Iterator getSystemMappingKeys() { 174 return systemMap.keySet().iterator(); 175 } 176 177 180 protected void clearAll() { 181 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("AbstractCatalog: clearing maps"); 183 publicMap.clear(); 184 systemMap.clear(); 185 delegate.clear(); 186 delegateOrder.clear(); 187 extenders.clear(); 188 } 189 190 198 public String getSystemMapping(String systemId) { 199 return (String )systemMap.get(systemId); 200 } 201 202 203 207 public Iterator getPublicIDs() { 208 return getPublicIDs(""); } 210 211 214 private Iterator getPublicIDs(String prefix) { 215 216 if (prefix == null) throw new IllegalArgumentException (); 217 218 IteratorIterator set = new IteratorIterator(); 219 set.add(getPublicMappingKeys()); 220 221 Iterator it = extenders.iterator(); 222 while (it.hasNext()) { 223 set.add(((AbstractCatalog) it.next()).getPublicIDs()); 224 } 225 226 Enumeration en = getDelegateCatalogKeys(); 227 while (en.hasMoreElements()) { 228 String _prefix = (String ) en.nextElement(); 229 AbstractCatalog delegee = (AbstractCatalog) delegate.get(_prefix); 230 set.add(delegee.getPublicIDs(_prefix)); 231 } 232 233 return new FilterIterator(set, new PrefixFilter(prefix)); 234 } 235 236 private class PrefixFilter implements FilterIterator.Filter { 237 238 private final String prefix; 239 240 PrefixFilter(String prefix) { 241 this.prefix = prefix; 242 } 243 244 public boolean accept(Object obj) { 245 return ((String )obj).startsWith(prefix); 246 } 247 } 248 249 250 254 public String getSystemID(String publicId) { 255 return getPublicMapping(publicId); 256 } 257 258 273 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , IOException 274 { 275 276 InputSource ret = resolvePublicId(publicId); 278 if (ret != null) return ret; 279 280 return resolveSystemId(systemId); 282 } 283 284 protected InputSource resolvePublicId(String publicId) { 285 286 if (publicId != null) { 288 String value = getPublicMapping(publicId); 289 if (value != null) { 290 InputSource input = new InputSource (value); 291 input.setPublicId(publicId); 292 return input; 293 } 294 } 295 296 return null; 297 } 298 299 protected InputSource resolveSystemId(String systemId) { 300 301 if (systemId != null) { 302 String value = getSystemMapping(systemId); 303 if (value == null) { 304 value = systemId; } 306 307 return new InputSource (value); 308 } 309 310 return null; 311 } 312 313 315 316 328 public void addDelegateCatalog(String prefix, AbstractCatalog catalog) { 329 330 synchronized (delegate) { 331 if (!delegate.containsKey(prefix)) { 333 int size = delegateOrder.size(); 334 boolean found = false; 335 for (int i = 0; i < size; i++) { 336 String element = (String )delegateOrder.elementAt(i); 337 if (prefix.startsWith(element) || prefix.compareTo(element) < 0) { 338 delegateOrder.insertElementAt(prefix, i); 339 found = true; 340 break; 341 } 342 } 343 if (!found) { 344 delegateOrder.addElement(prefix); 345 } 346 } 347 348 delegate.put(prefix, catalog); 350 } 351 352 } 353 354 359 public void removeDelegateCatalog(String prefix) { 360 361 synchronized (delegate) { 362 delegate.remove(prefix); 363 delegateOrder.removeElement(prefix); 364 } 365 366 } 368 369 public Enumeration getDelegateCatalogKeys() { 370 return delegateOrder.elements(); 371 } 372 373 374 public AbstractCatalog getDelegateCatalog(String prefix) { 375 return (AbstractCatalog)delegate.get(prefix); 376 } 377 378 379 380 382 383 384 private PropertyChangeSupport support = new PropertyChangeSupport(this); 385 386 390 public void addPropertyChangeListener(PropertyChangeListener l) { 391 support.addPropertyChangeListener(l); 392 } 393 394 public void removePropertyChangeListener(PropertyChangeListener l) { 395 support.removePropertyChangeListener(l); 396 } 397 398 protected void firePropertyChange(String prop, Object val1, Object val2) { 399 support.firePropertyChange(prop, val1, val2); 400 } 401 402 403 protected Image getDefaultIcon(int type) { 404 try { 405 BeanInfo info = Utilities.getBeanInfo(getClass()); 406 return info.getIcon(type); 407 } catch (IntrospectionException ex) { 408 return null; 409 } 410 } 411 412 416 protected Image getDefaultErrorIcon(int type) { 417 if (getDefaultIcon(type) == null) return null; 418 419 return null; 420 } 421 422 426 public String resolveURI(String name) { 427 return null; 428 } 429 433 public String resolvePublic(String publicId) { 434 return null; 435 } 436 437 } 438 | Popular Tags |