KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > webapps > Web_App_Xml


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
//
15
// NK 29.01.2001
16
//
17
//
18

19 package org.jahia.data.webapps;
20
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Vector JavaDoc;
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 JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30
31
32 /**
33  * Holds Informations about the Web Component deployment descriptors file
34  * web.xml
35  *
36  * @author Khue ng
37  * @version 1.0
38  */

39 public class Web_App_Xml extends JahiaXmlDocument {
40
41     /** The Servlet Type WebApp **/
42     private static final int SERVLET_TYPE = 1;
43     
44     /** The JSP Type WebApp **/
45     private static final int JSP_TYPE = 2;
46
47     /** The Display Name **/
48     private String JavaDoc m_DisplayName;
49     
50     /** The desc **/
51     private String JavaDoc m_desc;
52     
53     /**
54      * The list of Servlet
55      * @associates Servlet_Element
56      */

57     private Vector JavaDoc m_Servlets = new Vector JavaDoc();
58
59     /** The hashMap of servlet mapping, keyed with the pattern used to map a servlet **/
60     private HashMap JavaDoc m_ServletMappings = new HashMap JavaDoc();
61     
62     /** The list of security-role **/
63     private Vector JavaDoc m_Roles = new Vector JavaDoc();
64     
65     /** The list of Welcome files **/
66     private Vector JavaDoc m_WelcomeFiles = new Vector JavaDoc();
67     
68     /**
69      * Constructor
70      *
71      * @param (String) path, the full path to the application.xml file
72      */

73     public Web_App_Xml (String JavaDoc docPath) throws JahiaException
74     {
75       super(docPath);
76     }
77
78     /**
79      * Constructor using a gived parser
80      *
81      * @param (String) path, the full path to a xml file
82      * @param (Parser) parser, the parser to use
83      */

84     public Web_App_Xml (String JavaDoc docPath, org.xml.sax.helpers.ParserAdapter JavaDoc parser)
85     throws JahiaException {
86         super(docPath,parser);
87     }
88
89     //--------------------------------------------------------------------------
90
/**
91      * Extracts data from the web.xml file.
92      *
93      */

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         // get web-app node
114
Element JavaDoc webAppNode;
115         webAppNode = (Element JavaDoc) 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         // get the webapp display name
126
Node JavaDoc 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     /**
138      * Return the Display Name
139      *
140      * @return (String) the display name of the Application
141      */

142     public String JavaDoc getDisplayName(){
143         return m_DisplayName;
144     }
145
146     /**
147      * Set the DisplayName
148      * @param (String) the display name of the webApp
149      */

150     protected void setDisplayName(String JavaDoc name){
151         m_DisplayName = name;
152     }
153
154     /**
155      * Return the servlets list
156      *
157      * @return (Vector) the list of servlets
158      */

159     public Vector JavaDoc getServlets(){
160         return m_Servlets;
161     }
162
163     /**
164      * Return the map of servlet mapping
165      *
166      * @return (HashMap) the map of servlets mapping
167      */

168     public HashMap JavaDoc getServletMappings(){
169         return m_ServletMappings;
170     }
171
172     /**
173      * Return the welcome files list
174      *
175      * @return (Vector) the list of welcome files
176      */

177     public Vector JavaDoc getWelcomeFiles(){
178         return m_WelcomeFiles;
179     }
180
181     /**
182      * return the vector of roles
183      *
184      * @return (Vector) the roles
185      */

186     public Vector JavaDoc getRoles(){
187         return m_Roles;
188     }
189
190     /**
191      * Return the Web App desc
192      *
193      * @return (String) the desc
194      */

195     public String JavaDoc getdesc(){
196         return m_desc;
197     }
198
199     /**
200      * Set the desc
201      * @param (String) the desc
202      */

203     protected void setdesc(String JavaDoc descr){
204         m_desc = descr;
205     }
206
207     //--------------------------------------------------------------------------
208
/**
209      * extract the list of roles
210      *
211      */

212     private Vector JavaDoc getRoles(Node JavaDoc parentNode) throws JahiaException {
213         
214         Vector JavaDoc nodesList = XMLParser.getChildNodes(parentNode,"security-role");
215         Vector JavaDoc roles = new Vector JavaDoc();
216         
217         int size = nodesList.size();
218         if ( size>0 ){
219
220             Node JavaDoc nodeItem = null;
221             String JavaDoc name = "";
222             String JavaDoc descr = "";
223
224             Node JavaDoc childNode = null;
225          
226             Security_Role role = null;
227             
228             for ( int i=0 ; i<size ; i++ ){
229                 
230                 name = "";
231                 nodeItem = (Node JavaDoc)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                     //System.out.println(" found role : name = " + role.getName() );
250
roles.add(role);
251                 }
252             }
253         }
254         return roles;
255     }
256
257     //--------------------------------------------------------------------------
258
/**
259      * Extract the list of servlets
260      */

261     private Vector JavaDoc getServlets(Node JavaDoc parentNode) throws JahiaException {
262         
263         Vector JavaDoc servlets = new Vector JavaDoc();
264         
265         // build the servlets list
266
Vector JavaDoc nodesList = XMLParser.getChildNodes(parentNode,"servlet");
267         int size = nodesList.size();
268         if ( size>0 ){
269
270             Node JavaDoc nodeItem = null;
271             String JavaDoc displayName = "";
272             String JavaDoc servletName = "";
273             String JavaDoc descr = "";
274             String JavaDoc servletsrc = "";
275             int servletType = 1;
276
277             Node JavaDoc childNode = null;
278
279             for ( int i=0 ; i<size ; i++ ){
280                 nodeItem = (Node JavaDoc)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                     //JahiaConsole.println(">>"," servlet name :" + servlet.getName());
325
//JahiaConsole.println(">>"," display name :" + servlet.getDisplayName());
326
//JahiaConsole.println(">>"," descr :" + servlet.getdesc());
327
//JahiaConsole.println(">>"," servlet source :" + servlet.getSource());
328
//JahiaConsole.println(">>"," servletType :" + servlet.getType());
329
//JahiaConsole.println(">>"," servletNumber :" + servlet.getNumber());
330

331                     servlets.add(servlet);
332                 }
333             }
334         }
335         return servlets;
336     }
337
338     //--------------------------------------------------------------------------
339
/**
340      * Extract the list of welcome files
341      */

342     private Vector JavaDoc getWelcomeFiles(Node JavaDoc parentNode) throws JahiaException {
343
344         Vector JavaDoc results = new Vector JavaDoc();
345         
346         // get the welcome-file-list element
347
Node JavaDoc welcomeFileListNode = XMLParser.nextChildOfTag(parentNode,"welcome-file-list");
348         if ( welcomeFileListNode == null ){
349             return results;
350         }
351
352         Vector JavaDoc nodesList = XMLParser.getChildNodes(welcomeFileListNode,"welcome-file");
353         
354         int size = nodesList.size();
355         if ( size>0 ){
356
357             Node JavaDoc nodeItem = null;
358             String JavaDoc filename = "";
359             
360             for ( int i=0 ; i<size ; i++ ){
361                 
362                 nodeItem = (Node JavaDoc)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     //--------------------------------------------------------------------------
374
/**
375      * Extract a map of servlet mapping.
376      */

377     private HashMap JavaDoc getServletMappings(Node JavaDoc parentNode) throws JahiaException {
378
379         HashMap JavaDoc hash = new HashMap JavaDoc();
380
381         Vector JavaDoc nodesList = XMLParser.getChildNodes(parentNode,"servlet-mapping");
382         
383         int size = nodesList.size();
384         if ( size>0 ){
385
386             Node JavaDoc nodeItem = null;
387             String JavaDoc servletName = "";
388             String JavaDoc urlPattern = "";
389
390             Node JavaDoc childNode = null;
391          
392             for ( int i=0 ; i<size ; i++ ){
393                 
394                 servletName = "";
395                 urlPattern = "";
396                 
397                 nodeItem = (Node JavaDoc)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 } // end Web_App_Xml
421
Popular Tags