1 23 package com.sun.enterprise.tools.jsfext.layout.xml; 24 25 import com.sun.enterprise.tools.jsfext.layout.LayoutDefinitionManager; 26 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutDefinition; 27 import com.sun.enterprise.tools.jsfext.util.Util; 28 import com.sun.enterprise.tools.jsfext.util.ClasspathEntityResolver; 29 30 import java.io.File ; 31 import java.io.InputStream ; 32 import java.io.IOException ; 33 import java.io.OutputStreamWriter ; 34 import java.io.PrintWriter ; 35 import java.io.UnsupportedEncodingException ; 36 import java.lang.reflect.InvocationTargetException ; 37 import java.lang.reflect.Method ; 38 import java.net.URL ; 39 import java.util.HashMap ; 40 import java.util.Map ; 41 42 import javax.faces.context.FacesContext; 43 44 import org.xml.sax.EntityResolver ; 45 import org.xml.sax.ErrorHandler ; 46 import org.xml.sax.InputSource ; 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.SAXParseException ; 49 50 51 75 public class XMLLayoutDefinitionManager extends LayoutDefinitionManager { 76 77 80 protected XMLLayoutDefinitionManager() { 81 super(); 82 83 try { 85 setErrorHandler(new XMLErrorHandler(new PrintWriter ( 86 new OutputStreamWriter (System.err, "UTF-8"), true))); 87 } catch (UnsupportedEncodingException ex) { 88 throw new RuntimeException (ex); 89 } 90 91 setEntityResolver(new ClasspathEntityResolver()); 93 } 94 95 102 public static LayoutDefinitionManager getInstance() { 103 if (instance == null) { 104 instance = new XMLLayoutDefinitionManager(); 105 } 106 return instance; 107 } 108 109 117 public LayoutDefinition getLayoutDefinition(String key) throws IOException { 118 while (key.startsWith("/")) { 120 key = key.substring(1); 121 } 122 123 LayoutDefinition ld = (LayoutDefinition) layouts.get(key); 125 if (DEBUG) { 126 ld = null; 128 } 129 if (ld == null) { 130 String baseURI = getBaseURI(); 131 132 URL ldURL = null; 135 Object ctx = FacesContext.getCurrentInstance(). 136 getExternalContext().getContext(); 137 String url; 138 139 Method method = null; 141 try { 142 method = ctx.getClass().getMethod( 143 "getRealPath", GET_REAL_PATH_ARGS); 144 } catch (NoSuchMethodException ex) { 145 throw new RuntimeException (ex); 146 } 147 try { 148 if (baseURI == null) { 149 baseURI = "file:///" 150 + method.invoke(ctx, new Object [] {"/"}); 151 } 152 url = (String ) method.invoke(ctx, new Object [] {key}); 153 } catch (IllegalAccessException ex) { 156 throw new RuntimeException (ex); 157 } catch (InvocationTargetException ex) { 158 throw new RuntimeException (ex); 159 } 160 161 if ((url != null) && new File (url).canRead()) { 163 try { 164 ldURL = new URL ("file:///" + url); 165 } catch (Exception ex) { 166 throw new RuntimeException ( 167 "Unable to create URL: 'file:///" + url 168 + "' while attempting to locate '" + key + "'", ex); 169 } 170 } 171 172 if (ldURL == null) { 173 ClassLoader loader = Util.getClassLoader(key); 175 ldURL = loader.getResource(key); 176 if (ldURL == null) { 177 ldURL = loader.getResource("/"+key); 178 if (ldURL == null) { 179 ldURL = loader.getResource("META-INF/"+key); 180 } 181 } 182 } 183 184 if (ldURL == null) { 186 throw new java.io.FileNotFoundException ( 187 "Unable to locate '" + key + "'"); 188 } 189 190 try { 192 ld = new XMLLayoutDefinitionReader( 193 ldURL, getEntityResolver(), getErrorHandler(), baseURI). 194 read(); 195 } catch (IOException ex) { 196 throw new RuntimeException ("Unable to process '" + ldURL 197 + "'. EntityResolver: '" + getEntityResolver() 198 + "'. ErrorHandler: '" + getErrorHandler() 199 + "'. baseURI: '" + baseURI + "'.", ex); 200 } 201 202 synchronized (layouts) { 204 layouts.put(key, ld); 205 } 206 } 207 return ld; 208 } 209 210 215 public EntityResolver getEntityResolver() { 216 return (EntityResolver) getAttribute(ENTITY_RESOLVER); 217 } 218 219 224 public void setEntityResolver(EntityResolver entityResolver) { 225 setAttribute(ENTITY_RESOLVER, entityResolver); 226 } 227 228 233 public ErrorHandler getErrorHandler() { 234 return (ErrorHandler ) getAttribute(ERROR_HANDLER); 235 } 236 237 242 public void setErrorHandler(ErrorHandler errorHandler) { 243 setAttribute(ERROR_HANDLER, errorHandler); 244 } 245 246 252 public String getBaseURI() { 253 return (String ) getAttribute(BASE_URI); 254 } 255 256 261 public void setBaseURI(String baseURI) { 262 setAttribute(BASE_URI, baseURI); 263 } 264 265 268 private static class XMLErrorHandler implements ErrorHandler { 269 270 private PrintWriter out; 271 272 XMLErrorHandler(PrintWriter outWriter) { 273 this.out = outWriter; 274 } 275 276 279 private String getParseExceptionInfo(SAXParseException spe) { 280 String systemId = spe.getSystemId(); 281 if (systemId == null) { 282 systemId = "null"; 283 } 284 String info = "URI=" + systemId + " Line=" + spe.getLineNumber() 285 + ": " + spe.getMessage(); 286 return info; 287 } 288 289 292 public void warning(SAXParseException spe) throws SAXException { 293 out.println("Warning: " + getParseExceptionInfo(spe)); 294 } 295 296 public void error(SAXParseException spe) throws SAXException { 297 String message = "Error: " + getParseExceptionInfo(spe); 298 throw new SAXException (message, spe); 299 } 300 301 public void fatalError(SAXParseException spe) throws SAXException { 302 String message = "Fatal Error: " + getParseExceptionInfo(spe); 303 throw new SAXException (message, spe); 304 } 305 } 306 307 311 private static Map layouts = new HashMap (); 312 313 317 private static LayoutDefinitionManager instance = null; 318 319 322 private static final Class [] GET_REAL_PATH_ARGS = 323 new Class [] {String .class}; 324 325 329 public static final String ENTITY_RESOLVER = "entityResolver"; 330 331 334 public static final String ERROR_HANDLER = "errorHandler"; 335 336 339 public static final String BASE_URI = "baseURI"; 340 341 private static boolean DEBUG = 342 Boolean.getBoolean("com.sun.enterprise.tools.jsfext.DEBUG"); 343 } 344 | Popular Tags |