1 16 package com.blandware.atleap.webapp.util.core; 17 18 import org.apache.commons.digester.Digester; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.xml.sax.SAXException ; 22 23 import javax.servlet.ServletContext ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.PrintWriter ; 27 import java.io.StringWriter ; 28 import java.net.URL ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 39 public class DescriptorParser { 40 41 protected transient final Log log = LogFactory.getLog(getClass()); 42 43 46 private static final String WEB_APP_2_3_DTD_PUBLIC_ID = "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"; 47 48 51 private static final String WEB_APP_2_3_DTD_LOCATION = "/org/apache/struts/resources/web-app_2_3.dtd"; 52 53 56 protected String configPath = "/WEB-INF/web.xml"; 57 58 61 protected ServletContext servletContext; 62 63 66 protected Map securityRoles = null; 67 68 73 public DescriptorParser(ServletContext servletContext) { 74 this.servletContext = servletContext; 75 } 76 77 82 public Map getSecurityRoles() { 83 84 Digester digester = new Digester(); 85 digester.push(this); 86 digester.setNamespaceAware(true); 87 digester.setValidating(false); 88 digester.setUseContextClassLoader(true); 89 90 URL dtdUrl = getClass().getResource(WEB_APP_2_3_DTD_LOCATION); 92 digester.register(WEB_APP_2_3_DTD_PUBLIC_ID, dtdUrl.toString()); 93 94 digester.addCallMethod("web-app/security-role", "addSecurityRole", 2, new Class []{String .class, String .class}); 95 digester.addCallParam("web-app/security-role/role-name", 0); 96 digester.addCallParam("web-app/security-role/description", 1); 97 98 InputStream input = servletContext.getResourceAsStream(configPath); 99 100 if ( input == null ) { 101 if ( log.isErrorEnabled() ) { 102 log.error("Failed to retrieve deployment descriptor by path: " + configPath); 103 } 104 return null; 105 } 106 107 try { 108 digester.parse(input); 109 } catch ( IOException e ) { 110 if ( log.isErrorEnabled() ) { 111 StringWriter sw = new StringWriter (); 112 e.printStackTrace(new PrintWriter (sw)); 113 log.error(sw.toString()); 114 } 115 116 } catch ( SAXException e ) { 117 if ( log.isErrorEnabled() ) { 118 StringWriter sw = new StringWriter (); 119 e.printStackTrace(new PrintWriter (sw)); 120 log.error(sw.toString()); 121 } 122 } finally { 123 try { 124 input.close(); 125 } catch ( IOException e ) { 126 if ( log.isErrorEnabled() ) { 127 StringWriter sw = new StringWriter (); 128 e.printStackTrace(new PrintWriter (sw)); 129 log.error(sw.toString()); 130 } 131 } 132 } 133 134 return securityRoles; 135 } 136 137 143 public void addSecurityRole(String roleName, String description) { 144 if ( securityRoles == null ) { 145 securityRoles = new HashMap (); 146 } 147 securityRoles.put(roleName, description); 148 } 149 150 155 public void setConfigPath(String configPath) { 156 this.configPath = configPath; 157 } 158 159 } 160 | Popular Tags |