1 23 package com.sun.enterprise.tools.jsfext.util; 24 25 import org.xml.sax.ext.EntityResolver2 ; 26 import org.xml.sax.InputSource ; 27 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.net.URL ; 31 32 import java.lang.reflect.Method ; 33 import java.lang.reflect.InvocationTargetException ; 34 35 36 44 public class ClasspathEntityResolver implements EntityResolver2 { 45 46 51 public InputSource resolveEntity(String publicId, String systemId) { 52 return resolveEntity("", publicId, "", systemId); 53 } 54 55 58 public InputSource getExternalSubset(String name, String baseURI) { 59 return null; 60 } 61 62 65 public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) { 66 if (systemId != null) { 67 if (baseURI != null) { 68 if (systemId.startsWith(baseURI)) { 69 systemId = systemId.substring(baseURI.length()); 70 } 71 } 72 if (systemId.startsWith("file:/")) { 73 systemId = systemId.substring(6); } 75 76 while (systemId.startsWith("/")) { 78 systemId = systemId.substring(1); 80 } 81 82 InputStream stream = null; 86 URL url = null; 87 if (FACES_CONTEXT != null) { 88 try { 89 Method meth = FACES_CONTEXT.getMethod( 92 "getCurrentInstance", (Class []) null); 93 Object ctx = meth.invoke((Object ) null, (Object []) null); 94 95 if (ctx != null) { 96 meth = ctx.getClass().getMethod( 98 "getExternalContext", (Class []) null); 99 ctx = meth.invoke(ctx, (Object []) null); 100 101 meth = ctx.getClass().getMethod( 103 "getContext", (Class []) null); 104 ctx = meth.invoke(ctx, (Object []) null); 105 106 meth = ctx.getClass().getMethod("getResource", STRING_ARG); 108 url = (URL ) meth.invoke( 110 ctx, new Object [] {"/" + systemId}); 111 if (url != null) { 112 stream = url.openStream(); 113 } 114 } 115 } catch (NoSuchMethodException ex) { 116 throw new RuntimeException (ex); 117 } catch (IllegalAccessException ex) { 118 throw new RuntimeException (ex); 119 } catch (InvocationTargetException ex) { 120 throw new RuntimeException (ex); 121 } catch (IOException ex) { 122 } 124 } 125 126 if (stream == null) { 132 try { 133 stream = new URL (baseURI + "/" + systemId).openStream(); 134 } catch (IOException ex) { 135 } 137 138 if (stream == null) { 139 ClassLoader loader = Util.getClassLoader(systemId); 142 143 stream = loader.getResourceAsStream(systemId); 145 if (stream == null) { 146 stream = loader.getResourceAsStream("/" + systemId); 148 if (stream == null) { 149 stream = loader.getResourceAsStream( 151 "META-INF/" + systemId); 152 } 153 } 154 } 155 } 156 157 if (stream != null) { 158 return new InputSource (stream); 160 } 161 if (LogUtil.configEnabled(LOGGER_NAME)) { 162 LogUtil.config((Object ) LOGGER_NAME, 163 "Unable to resolve entity." 164 + "\n\tsystemId: '" + systemId 165 + "'\n\tbaseURI: '" + baseURI 166 + "'\n\tpublicId: '" + publicId 167 + "'\n\tname: '" + name + "'"); 168 } 169 } 170 171 return null; 173 } 174 175 public static final String LOGGER_NAME = "javax.enterpise.system.tools.admin.guiframework"; 176 177 private static final Class [] STRING_ARG = new Class [] {String .class}; 178 private static Class FACES_CONTEXT; 179 180 static { 181 try { 182 FACES_CONTEXT = Class.forName("javax.faces.context.FacesContext"); 183 } catch (Exception ex) { 184 FACES_CONTEXT = null; 186 } 187 } 188 } 189 | Popular Tags |