1 19 package org.jahia.data.webapps; 20 21 22 import java.util.HashMap ; 23 import java.util.Vector ; 24 25 import org.jahia.data.xml.JahiaXmlDocument; 26 import org.jahia.exceptions.JahiaException; 27 import org.jahia.utils.xml.XMLParser; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 31 32 39 public class Web_App_Xml extends JahiaXmlDocument { 40 41 42 private static final int SERVLET_TYPE = 1; 43 44 45 private static final int JSP_TYPE = 2; 46 47 48 private String m_DisplayName; 49 50 51 private String m_desc; 52 53 57 private Vector m_Servlets = new Vector (); 58 59 60 private HashMap m_ServletMappings = new HashMap (); 61 62 63 private Vector m_Roles = new Vector (); 64 65 66 private Vector m_WelcomeFiles = new Vector (); 67 68 73 public Web_App_Xml (String docPath) throws JahiaException 74 { 75 super(docPath); 76 } 77 78 84 public Web_App_Xml (String docPath, org.xml.sax.helpers.ParserAdapter parser) 85 throws JahiaException { 86 super(docPath,parser); 87 } 88 89 94 public void extractDocumentData() throws JahiaException { 95 96 if (m_XMLDocument == null) { 97 98 throw new JahiaException( "Web_App_Xml", 99 "Parsed web.xml document is null", 100 JahiaException.ERROR_SEVERITY, 101 JahiaException.CONFIG_ERROR); 102 } 103 104 105 if (!m_XMLDocument.hasChildNodes()) { 106 107 throw new JahiaException( "Web_App_Xml", 108 "Main document node has no children", 109 JahiaException.ERROR_SEVERITY, 110 JahiaException.CONFIG_ERROR); 111 } 112 113 Element webAppNode; 115 webAppNode = (Element ) m_XMLDocument.getDocumentElement(); 116 117 if (!webAppNode.getNodeName().equalsIgnoreCase("web-app")) { 118 119 throw new JahiaException( "Invalid XML format", 120 "web-app tag is not present as starting tag in file", 121 JahiaException.ERROR_SEVERITY, 122 JahiaException.CONFIG_ERROR); 123 } 124 125 Node displayNameNode = XMLParser.nextChildOfTag(webAppNode,"display-name"); 127 if ( displayNameNode != null ){ 128 m_DisplayName = displayNameNode.getFirstChild().getNodeValue().trim(); 129 } 130 131 m_Servlets = getServlets(webAppNode); 132 m_ServletMappings = getServletMappings(webAppNode); 133 m_Roles = getRoles(webAppNode); 134 m_WelcomeFiles = getWelcomeFiles(webAppNode); 135 } 136 137 142 public String getDisplayName(){ 143 return m_DisplayName; 144 } 145 146 150 protected void setDisplayName(String name){ 151 m_DisplayName = name; 152 } 153 154 159 public Vector getServlets(){ 160 return m_Servlets; 161 } 162 163 168 public HashMap getServletMappings(){ 169 return m_ServletMappings; 170 } 171 172 177 public Vector getWelcomeFiles(){ 178 return m_WelcomeFiles; 179 } 180 181 186 public Vector getRoles(){ 187 return m_Roles; 188 } 189 190 195 public String getdesc(){ 196 return m_desc; 197 } 198 199 203 protected void setdesc(String descr){ 204 m_desc = descr; 205 } 206 207 212 private Vector getRoles(Node parentNode) throws JahiaException { 213 214 Vector nodesList = XMLParser.getChildNodes(parentNode,"security-role"); 215 Vector roles = new Vector (); 216 217 int size = nodesList.size(); 218 if ( size>0 ){ 219 220 Node nodeItem = null; 221 String name = ""; 222 String descr = ""; 223 224 Node childNode = null; 225 226 Security_Role role = null; 227 228 for ( int i=0 ; i<size ; i++ ){ 229 230 name = ""; 231 nodeItem = (Node )nodesList.get(i); 232 233 childNode = XMLParser.nextChildOfTag(nodeItem,"role-name"); 234 if (childNode != null ){ 235 name = childNode.getFirstChild().getNodeValue().trim(); 236 } 237 238 childNode = XMLParser.nextChildOfTag(nodeItem,"desc"); 239 if (childNode != null ){ 240 descr = childNode.getFirstChild().getNodeValue().trim(); 241 } 242 243 if ( descr == null ){ 244 descr = ""; 245 } 246 247 if ( name != null && (name.length()>0) ) { 248 role = new Security_Role(name, descr); 249 roles.add(role); 251 } 252 } 253 } 254 return roles; 255 } 256 257 261 private Vector getServlets(Node parentNode) throws JahiaException { 262 263 Vector servlets = new Vector (); 264 265 Vector nodesList = XMLParser.getChildNodes(parentNode,"servlet"); 267 int size = nodesList.size(); 268 if ( size>0 ){ 269 270 Node nodeItem = null; 271 String displayName = ""; 272 String servletName = ""; 273 String descr = ""; 274 String servletsrc = ""; 275 int servletType = 1; 276 277 Node childNode = null; 278 279 for ( int i=0 ; i<size ; i++ ){ 280 nodeItem = (Node )nodesList.get(i); 281 282 childNode = XMLParser.nextChildOfTag(nodeItem,"servlet-name"); 283 if (childNode != null ){ 284 servletName = childNode.getFirstChild().getNodeValue().trim(); 285 } 286 287 childNode = XMLParser.nextChildOfTag(nodeItem,"display-name"); 288 if (childNode != null ){ 289 displayName = childNode.getFirstChild().getNodeValue().trim(); 290 } else { 291 displayName = servletName; 292 } 293 294 childNode = XMLParser.nextChildOfTag(nodeItem,"desc"); 295 if (childNode != null ){ 296 descr = childNode.getFirstChild().getNodeValue().trim(); 297 } 298 299 if ( XMLParser.nextChildOfTag(nodeItem,"servlet-class") != null ) { 300 servletsrc = XMLParser.nextChildOfTag(nodeItem,"servlet-class").getFirstChild().getNodeValue().trim(); 301 servletType = SERVLET_TYPE; 302 } else { 303 servletsrc = XMLParser.nextChildOfTag(nodeItem,"jsp-file").getFirstChild().getNodeValue().trim(); 304 servletType = JSP_TYPE; 305 } 306 307 if ( descr == null ){ 308 descr = ""; 309 } 310 311 if ( (displayName != null) && (displayName.length()>0) && 312 (servletName != null) && (servletName.length()>0) && 313 (servletsrc != null) && (servletsrc.length()>0) ){ 314 315 Servlet_Element servlet = new Servlet_Element( 316 servletName, 317 displayName, 318 descr, 319 servletsrc, 320 servletType, 321 i + 1 322 ); 323 324 331 servlets.add(servlet); 332 } 333 } 334 } 335 return servlets; 336 } 337 338 342 private Vector getWelcomeFiles(Node parentNode) throws JahiaException { 343 344 Vector results = new Vector (); 345 346 Node welcomeFileListNode = XMLParser.nextChildOfTag(parentNode,"welcome-file-list"); 348 if ( welcomeFileListNode == null ){ 349 return results; 350 } 351 352 Vector nodesList = XMLParser.getChildNodes(welcomeFileListNode,"welcome-file"); 353 354 int size = nodesList.size(); 355 if ( size>0 ){ 356 357 Node nodeItem = null; 358 String filename = ""; 359 360 for ( int i=0 ; i<size ; i++ ){ 361 362 nodeItem = (Node )nodesList.get(i); 363 filename = nodeItem.getFirstChild().getNodeValue().trim(); 364 365 if ( filename != null && (filename.length()>0) ) { 366 results.add(filename); 367 } 368 } 369 } 370 return results; 371 } 372 373 377 private HashMap getServletMappings(Node parentNode) throws JahiaException { 378 379 HashMap hash = new HashMap (); 380 381 Vector nodesList = XMLParser.getChildNodes(parentNode,"servlet-mapping"); 382 383 int size = nodesList.size(); 384 if ( size>0 ){ 385 386 Node nodeItem = null; 387 String servletName = ""; 388 String urlPattern = ""; 389 390 Node childNode = null; 391 392 for ( int i=0 ; i<size ; i++ ){ 393 394 servletName = ""; 395 urlPattern = ""; 396 397 nodeItem = (Node )nodesList.get(i); 398 399 childNode = XMLParser.nextChildOfTag(nodeItem,"servlet-name"); 400 if (childNode != null ){ 401 servletName = childNode.getFirstChild().getNodeValue().trim(); 402 } 403 404 childNode = XMLParser.nextChildOfTag(nodeItem,"url-pattern"); 405 if (childNode != null ){ 406 urlPattern = childNode.getFirstChild().getNodeValue().trim(); 407 } 408 409 if ( servletName != null && (servletName.length()>0) 410 && urlPattern != null && (urlPattern.length()>0) ) { 411 hash.put(urlPattern,servletName); 412 } 413 } 414 } 415 416 return hash; 417 } 418 419 420 } | Popular Tags |