1 9 10 package com.opensymphony.module.sitemesh.mapper; 11 12 import com.opensymphony.module.sitemesh.Config; 13 import com.opensymphony.module.sitemesh.Decorator; 14 import org.w3c.dom.*; 15 import org.xml.sax.SAXException ; 16 17 import javax.servlet.ServletException ; 18 import javax.xml.parsers.DocumentBuilder ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import javax.xml.parsers.ParserConfigurationException ; 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 54 public final class ConfigLoader { 55 private Map decorators = null; 56 private long configLastModified; 57 58 private File configFile = null; 59 private String configFileName = null; 60 private PathMapper pathMapper = null; 61 62 private Config config = null; 63 64 65 public ConfigLoader(File configFile) throws ServletException { 66 this.configFile = configFile; 67 this.configFileName = configFile.getName(); 68 loadConfig(); 69 } 70 71 72 public ConfigLoader(String configFileName, Config config) throws ServletException { 73 this.config = config; 74 this.configFileName = configFileName; 75 if (config.getServletContext().getRealPath(configFileName) != null) { 76 this.configFile = new File (config.getServletContext().getRealPath(configFileName)); 77 } 78 loadConfig(); 79 } 80 81 82 public Decorator getDecoratorByName(String name) throws ServletException { 83 refresh(); 84 return (Decorator)decorators.get(name); 85 } 86 87 88 public String getMappedName(String path) throws ServletException { 89 refresh(); 90 return pathMapper.get(path); 91 } 92 93 94 private synchronized void loadConfig() throws ServletException { 95 try { 96 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 98 DocumentBuilder builder = factory.newDocumentBuilder(); 99 100 Document document = null; 101 if (configFile != null && configFile.canRead()) { 102 configLastModified = configFile.lastModified(); 104 document = builder.parse(configFile); 105 } 106 else { 107 document = builder.parse(config.getServletContext().getResourceAsStream(configFileName)); 108 } 109 110 parseConfig(document); 112 } 113 catch (ParserConfigurationException e) { 114 throw new ServletException ("Could not get XML parser", e); 115 } 116 catch (IOException e) { 117 throw new ServletException ("Could not read the config file: " + configFileName, e); 118 } 119 catch (SAXException e) { 120 throw new ServletException ("Could not parse the config file: " + configFileName, e); 121 } 122 catch (IllegalArgumentException e) { 123 throw new ServletException ("Could not find the config file: " + configFileName, e); 124 } 125 } 126 127 128 private synchronized void parseConfig(Document document) { 129 Element root = document.getDocumentElement(); 130 131 String defaultDir = getAttribute(root, "defaultdir"); 133 if (defaultDir == null) defaultDir = getAttribute(root, "defaultDir"); 134 135 pathMapper = new PathMapper(); 137 decorators = new HashMap (); 138 139 NodeList decoratorNodes = root.getElementsByTagName("decorator"); 141 Element decoratorElement = null; 142 143 for (int i = 0; i < decoratorNodes.getLength(); i++) { 144 String name = null, page = null, uriPath = null, role = null; 145 146 decoratorElement = (Element) decoratorNodes.item(i); 148 149 if (getAttribute(decoratorElement, "name") != null) { 150 name = getAttribute(decoratorElement, "name"); 152 page = getAttribute(decoratorElement, "page"); 153 uriPath = getAttribute(decoratorElement, "webapp"); 154 role = getAttribute(decoratorElement, "role"); 155 156 if (defaultDir != null && page != null && page.length() > 0 && !page.startsWith("/")) { 158 if (page.charAt(0) == '/') page = defaultDir + page; 159 else page = defaultDir + '/' + page; 160 } 161 162 if (uriPath != null && uriPath.length() > 0) { 164 if (uriPath.charAt(0) != '/') uriPath = '/' + uriPath; 165 } 166 167 populatePathMapper(decoratorElement.getElementsByTagName("pattern"), role, name); 169 populatePathMapper(decoratorElement.getElementsByTagName("url-pattern"), role, name); 170 } 171 else { 172 name = getContainedText(decoratorNodes.item(i), "decorator-name"); 174 page = getContainedText(decoratorNodes.item(i), "resource"); 175 if (page == null) page = getContainedText(decoratorNodes.item(i), "jsp-file"); 178 } 179 180 Map params = new HashMap (); 181 182 NodeList paramNodes = decoratorElement.getElementsByTagName("init-param"); 183 for (int ii = 0; ii < paramNodes.getLength(); ii++) { 184 String paramName = getContainedText(paramNodes.item(ii), "param-name"); 185 String paramValue = getContainedText(paramNodes.item(ii), "param-value"); 186 params.put(paramName, paramValue); 187 } 188 storeDecorator(new DefaultDecorator(name, page, uriPath, role, params)); 189 } 190 191 NodeList mappingNodes = root.getElementsByTagName("decorator-mapping"); 193 for (int i = 0; i < mappingNodes.getLength(); i++) { 194 Element n = (Element)mappingNodes.item(i); 195 String name = getContainedText(mappingNodes.item(i), "decorator-name"); 196 197 populatePathMapper(n.getElementsByTagName("url-pattern"), null, name); 199 } 200 } 201 202 205 private void populatePathMapper(NodeList patternNodes, String role, String name) { 206 for (int j = 0; j < patternNodes.getLength(); j++) { 207 Element p = (Element)patternNodes.item(j); 208 Text patternText = (Text) p.getFirstChild(); 209 if (patternText != null) { 210 String pattern = patternText.getData().trim(); 211 if (pattern != null) { 212 if (role != null) { 213 pathMapper.put(name + role, pattern); 216 } 217 else { 218 pathMapper.put(name, pattern); 219 } 220 } 221 } 222 } 223 } 224 225 226 private static String getAttribute(Element element, String name) { 227 if (element != null && element.getAttribute(name) != null && element.getAttribute(name).trim() != "") { 228 return element.getAttribute(name).trim(); 229 } 230 else { 231 return null; 232 } 233 } 234 235 239 private static String getContainedText(Node parent, String childTagName) { 240 try { 241 Node tag = ((Element)parent).getElementsByTagName(childTagName).item(0); 242 String text = ((Text)tag.getFirstChild()).getData(); 243 return text; 244 } 245 catch (Exception e) { 246 return null; 247 } 248 } 249 250 251 private void storeDecorator(Decorator d) { 252 if (d.getRole() != null) { 253 decorators.put(d.getName() + d.getRole(), d); 254 } 255 else { 256 decorators.put(d.getName(), d); 257 } 258 } 259 260 261 private synchronized void refresh() throws ServletException { 262 if (configFile != null && configLastModified != configFile.lastModified()) loadConfig(); 263 } 264 } | Popular Tags |