1 19 20 package org.netbeans.core.xml; 21 22 import java.io.*; 23 import java.util.*; 24 import java.beans.*; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 28 import org.xml.sax.*; 29 import org.xml.sax.helpers.*; 30 import org.openide.loaders.*; 31 import org.openide.cookies.InstanceCookie; 32 import org.openide.util.*; 33 import org.openide.xml.*; 34 35 41 public final class EntityCatalogImpl extends EntityCatalog { 42 43 44 private Map<String , String > id2uri; 46 private static final RequestProcessor catalogRP = new RequestProcessor("EntityCatalog/parser"); 48 49 private EntityCatalogImpl(Map<String ,String > map) { 50 id2uri = map; 51 } 52 53 56 public InputSource resolveEntity(String publicID, String systemID) { 57 if (publicID == null) return null; 58 59 String res = id2uri.get(publicID); 61 InputSource ret = null; 62 if (res != null) { 63 ret = new InputSource(res); 64 } 65 66 return ret; 68 } 69 70 74 public static class RegistrationProcessor extends DefaultHandler implements XMLDataObject.Processor, InstanceCookie, Runnable , PropertyChangeListener { 75 76 private XMLDataObject peer; 77 private Map<String , String > map; 78 private RequestProcessor.Task parsingTask = catalogRP.create(this); 79 private EntityCatalogImpl instance = null; 80 81 83 public void attachTo (XMLDataObject xmlDO) { 84 85 if (xmlDO == peer) return; 87 peer = xmlDO; 88 peer.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(this, peer)); parsingTask.schedule(0); 90 } 91 92 94 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 95 if ("public".equals(qName)) { String key = atts.getValue("publicId"); String val = atts.getValue("uri"); 99 if (key != null && val != null) { 100 map.put(key, val); 101 } else { 102 throw new SAXException ("invalid <public> element: missing publicId or uri"); } 104 } 105 } 106 107 public InputSource resolveEntity(String pid, String sid) { 108 if (EntityCatalog.PUBLIC_ID.equals(pid)) { 109 return new InputSource(EntityCatalogImpl.class.getClassLoader().getResource("org/openide/xml/EntityCatalog.dtd").toExternalForm()); } 112 return null; 113 } 114 115 117 public void run() { 118 map = new Hashtable<String , String >(); 120 try { 121 String loc = peer.getPrimaryFile().getURL().toExternalForm(); 122 InputSource src = new InputSource(loc); 123 124 XMLReader reader = XMLUtil.createXMLReader(false); 126 reader.setErrorHandler(this); 127 reader.setContentHandler(this); 128 reader.setEntityResolver(this); 129 reader.parse(src); 130 } catch (SAXException ex) { 131 Logger.getLogger(EntityCatalogImpl.class.getName()).log(Level.WARNING, null, ex); 133 } catch (IOException ex) { 134 Logger.getLogger(EntityCatalogImpl.class.getName()).log(Level.WARNING, null, ex); 136 } 137 } 138 139 141 public Class instanceClass() throws IOException, ClassNotFoundException { 142 return EntityCatalog.class; 143 } 144 145 146 public Object instanceCreate() throws IOException, ClassNotFoundException { 147 148 synchronized (this) { 149 if (instance == null) { 150 parsingTask.waitFinished(); 151 instance = new EntityCatalogImpl (map); 152 } 153 } 154 return instance; 155 } 156 157 public String instanceName() { 158 return "org.openide.xml.EntityCatalog"; } 160 161 164 public void propertyChange(PropertyChangeEvent e) { 165 166 synchronized(this) { 167 if (instance == null) return; 168 } 169 170 if (XMLDataObject.PROP_DOCUMENT.equals(e.getPropertyName())) { 171 run(); 176 instance.id2uri = map; } 178 } 179 180 } 181 } 182 | Popular Tags |