1 19 package org.netbeans.modules.xml.catalog; 20 21 import java.util.*; 22 import java.io.*; 23 import java.net.URL ; 24 25 import org.xml.sax.*; 26 import org.netbeans.modules.xml.catalog.spi.*; 27 import org.netbeans.modules.xml.catalog.lib.*; 28 import org.netbeans.modules.xml.catalog.settings.CatalogSettings; 29 30 import org.netbeans.api.xml.services.*; 31 import org.openide.util.Lookup; 32 import javax.xml.transform.URIResolver ; 33 34 46 public class CatalogEntityResolver extends UserCatalog implements EntityResolver, URIResolver { 47 48 49 public CatalogEntityResolver() { 50 } 51 52 public EntityResolver getEntityResolver() { 53 return this; 54 } 55 56 60 public URIResolver getURIResolver() { 61 return this; 62 } 63 64 public InputSource resolveEntity(String publicId,String systemId) 66 throws SAXException, IOException { 67 InputSource result = null; 68 Iterator it = null; 69 70 72 CatalogSettings mounted = CatalogSettings.getDefault(); 73 it = mounted.getCatalogs( new Class [] {EntityResolver.class}); 74 75 while (it.hasNext()) { 76 EntityResolver next = (EntityResolver) it.next(); 77 result = next.resolveEntity(publicId, systemId); 78 if (result != null) break; 79 } 80 81 83 if (result == null && publicId != null) { 84 85 it = mounted.getCatalogs(new Class [] {CatalogReader.class}); 86 87 while (it.hasNext()) { 88 CatalogReader next = (CatalogReader) it.next(); 89 String sid = next.getSystemID(publicId); 90 if (sid != null) { 91 result = new InputSource(sid); 92 break; 93 } 94 } 95 } 96 97 99 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("CatalogEntityResolver:PublicID: " + publicId + ", " + systemId + " => " + (result == null ? "null" : result.getSystemId())); 101 if (result == null && "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN".equals(publicId)) { URL url = org.apache.xml.resolver.Catalog.class.getResource("etc/catalog.dtd"); result = new InputSource(url.toExternalForm()); 105 } 106 107 if (result != null) { 109 String patchedSystemId = result.getSystemId(); 110 if (patchedSystemId != null) { 111 patchedSystemId = patchedSystemId.replaceAll("\\+", "%20"); patchedSystemId = patchedSystemId.replaceAll("\\ ", "%20"); result.setSystemId(patchedSystemId); 114 } 115 } 116 return result; 117 118 } 119 120 123 public Iterator getPublicIDs() { 124 125 IteratorIterator ret = new IteratorIterator(); 126 127 CatalogSettings mounted = CatalogSettings.getDefault(); 128 Iterator it = mounted.getCatalogs( new Class [] {CatalogReader.class}); 129 130 while (it.hasNext()) { 131 CatalogReader next = (CatalogReader) it.next(); 132 Iterator ids = next.getPublicIDs(); 133 if (ids != null) { 134 ret.add(ids); 135 } 136 } 137 138 return ret; 139 } 140 141 public javax.xml.transform.Source resolve(String publicId, String systemId) 142 throws javax.xml.transform.TransformerException { 143 144 146 javax.xml.transform.Source result = null; 147 148 150 CatalogSettings mounted = CatalogSettings.getDefault(); 151 152 if (publicId != null) { 153 154 Iterator it = mounted.getCatalogs(new Class [] {CatalogReader.class}); 155 156 while (it.hasNext()) { 157 CatalogReader next = (CatalogReader) it.next(); 158 try { 159 String sid=null; 160 if (publicId.startsWith("urn:publicid:")) { String urn = publicId.substring(13); 163 sid=next.resolvePublic(URNtoPublic(urn)); 164 } else sid = next.resolveURI(publicId); 165 if (sid != null) { 166 javax.xml.transform.Source source = new javax.xml.transform.sax.SAXSource (); 167 source.setSystemId(sid); 168 result=source; 169 break; 170 } 171 } catch (java.lang.Error error) {} 172 } 173 } 174 175 177 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("CatalogEntityResolver:PublicID: " + publicId + ", " + systemId + " => " + (result == null ? "null" : result.getSystemId())); return result; 179 } 180 181 184 private String URNtoPublic(String urn) { 185 return urn.replace('+',' ').replaceAll(":","//").replaceAll(";","::").replaceAll("%2B","+").replaceAll("%3A",":").replaceAll("%2F","/").replaceAll("%3B",";").replaceAll("%27","'").replaceAll("%3F","?").replaceAll("%23","#").replaceAll("%25","%"); 186 } 187 } 188 | Popular Tags |