001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: AbsWebLoader.java 2054 2007-11-20 14:43:55Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.xml.parsing;
025:
026: import java.net.URL;
027:
028: import org.ow2.easybeans.deployment.xml.struct.Listener;
029: import org.ow2.easybeans.deployment.xml.struct.Servlet;
030: import org.ow2.easybeans.deployment.xml.struct.WAR;
031: import org.ow2.easybeans.util.xml.XMLUtils;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.NodeList;
034:
035: /**
036: * Common stuff for parsing Web xml files (web.xml and .tld files).
037: * @author Florent BENOIT
038: */
039: public class AbsWebLoader extends AbsDeploymentDescLoader {
040:
041: /**
042: * WAR instance object that is the result of the parsing.
043: */
044: private WAR war = null;
045:
046: /**
047: * Constructor that creates an empty WAR object.
048: * @param url the url that is pointing to the web xml file.
049: */
050: protected AbsWebLoader(final URL url) {
051: this (url, new WAR());
052: }
053:
054: /**
055: * Constructor that use an existing WAR object.
056: * @param url the url that is pointing to the web xml file.
057: * @param war the given war object if any.
058: */
059: protected AbsWebLoader(final URL url, final WAR war) {
060: super (url);
061: this .war = war;
062: }
063:
064: /**
065: * Analyze the servlet.
066: * @param servletElement the dom element.
067: * @param servlet the structure representing object.
068: */
069: protected void analyzeServlet(final Element servletElement,
070: final Servlet servlet) {
071: // Get name
072: String name = XMLUtils.getStringValueElement(servletElement,
073: "servlet-name");
074: servlet.setName(name);
075:
076: // Get class
077: String className = XMLUtils.getStringValueElement(
078: servletElement, "servlet-class");
079: servlet.setClassName(className);
080: }
081:
082: /**
083: * Find and analyze the listeners in the given DOM element.
084: * @param rootElement the element to analyze
085: */
086: protected void analyzeListeners(final Element rootElement) {
087: // Listeners
088: NodeList listenerList = rootElement
089: .getElementsByTagName(Listener.NAME);
090: for (int i = 0; i < listenerList.getLength(); i++) {
091: Element listenerElement = (Element) listenerList.item(i);
092:
093: // Build instance of a Listener struct object
094: Listener listener = new Listener();
095: war.addListener(listener);
096:
097: // Analyze listener
098: analyzeListener(listenerElement, listener);
099: }
100: }
101:
102: /**
103: * Find and analyze the servlets in the given DOM element.
104: * @param rootElement the element to analyze
105: */
106: protected void analyzeServlets(final Element rootElement) {
107: // Servlets
108: NodeList servletList = rootElement
109: .getElementsByTagName(Servlet.NAME);
110: for (int i = 0; i < servletList.getLength(); i++) {
111: Element servletElement = (Element) servletList.item(i);
112:
113: // Build instance of Servlet XML struct object
114: Servlet servlet = new Servlet();
115: getWAR().addServlet(servlet);
116:
117: // Analyze servlet
118: analyzeServlet(servletElement, servlet);
119: }
120: }
121:
122: /**
123: * Analyze the given listener.
124: * @param listenerElement the dom element.
125: * @param listener the structure representing object.
126: */
127: protected void analyzeListener(final Element listenerElement,
128: final Listener listener) {
129: // Get class
130: String listenerClassName = XMLUtils.getStringValueElement(
131: listenerElement, "listener-class");
132: listener.setListenerClassName(listenerClassName);
133: }
134:
135: /**
136: * @return the object resulting of the XML parsing.
137: */
138: public WAR getWAR() {
139: return war;
140: }
141:
142: }
|