1 19 20 package org.netbeans.modules.openide.loaders; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import org.openide.xml.EntityCatalog; 27 import org.xml.sax.InputSource ; 28 import org.xml.sax.SAXException ; 29 30 37 public final class RuntimeCatalog extends EntityCatalog { 38 39 40 public RuntimeCatalog() {} 41 42 private Map <String ,String > id2uri; 44 45 private Map <String ,String > id2resource; 47 private Map <String ,ClassLoader > id2loader; 48 49 50 public InputSource resolveEntity(String name, String uri) throws IOException , SAXException { 51 52 InputSource retval; 53 String mappedURI = name2uri(name); 54 InputStream stream = mapResource(name); 55 56 if (mappedURI != null) { 58 retval = new InputSource (mappedURI); 59 retval.setPublicId(name); 60 return retval; 61 62 } else if (stream != null) { 63 uri = "java:resource:" + id2resource.get(name); retval = new InputSource (stream); 66 retval.setPublicId(name); 67 return retval; 68 69 } else { 70 return null; 71 } 72 } 73 74 public void registerCatalogEntry(String publicId, String uri) { 75 if (id2uri == null) { 76 id2uri = new HashMap <String ,String >(); 77 } 78 id2uri.put(publicId, uri); 79 } 80 81 82 public void registerCatalogEntry(String publicId, String resourceName, ClassLoader loader) { 83 if (id2resource == null) { 84 id2resource = new HashMap <String ,String >(); 85 } 86 id2resource.put(publicId, resourceName); 87 88 if (loader != null) { 89 if (id2loader == null) { 90 id2loader = new HashMap <String ,ClassLoader >(); 91 } 92 id2loader.put(publicId, loader); 93 } 94 } 95 96 private String name2uri(String publicId) { 98 99 if (publicId == null || id2uri == null) { 100 return null; 101 } 102 return id2uri.get(publicId); 103 } 104 105 106 private InputStream mapResource(String publicId) { 108 if (publicId == null || id2resource == null) { 109 return null; 110 } 111 112 String resourceName = id2resource.get(publicId); 113 ClassLoader loader = null; 114 115 if (resourceName == null) { 116 return null; 117 } 118 119 if (id2loader != null) { 120 loader = id2loader.get(publicId); 121 } 122 123 if (loader == null) { 124 return ClassLoader.getSystemResourceAsStream(resourceName); 125 } 126 return loader.getResourceAsStream(resourceName); 127 } 128 129 } 130 | Popular Tags |