KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > server > util > WebAppXML


1 package org.enhydra.server.util;
2
3 import java.io.FileNotFoundException JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8
9 import org.apache.xerces.parsers.DOMParser;
10 import org.apache.xml.serialize.LineSeparator;
11 import org.apache.xml.serialize.Method;
12 import org.apache.xml.serialize.OutputFormat;
13 import org.apache.xml.serialize.XMLSerializer;
14 import org.enhydra.server.EnhydraServer;
15 import org.w3c.dom.Document JavaDoc;
16 import org.w3c.dom.Element JavaDoc;
17 import org.w3c.dom.Node JavaDoc;
18 import org.w3c.dom.NodeList JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20
21 /**
22  * @author Tweety
23  */

24 public class WebAppXML {
25
26     //xml document
27
public Document JavaDoc document;
28
29     //parser
30
DOMParser parser;
31
32     //xml document name
33
private String JavaDoc xmlFileName;
34
35     //Node that represents servlet tag with the name "enhydra"
36
private Node JavaDoc enhydraServlet;
37
38     //Node that represents root node (web-app tag)
39
private Element JavaDoc root;
40
41     //line separator for this operating system
42
private String JavaDoc lineSep;
43     
44     //
45
private final String JavaDoc[] tagsTillFilter = new String JavaDoc[] {"filter", "context-param", "distributable", "description", "display-name", "icon"};
46     private final String JavaDoc[] tagsTillFilterMapping = new String JavaDoc[] {"filter-mapping", "filter", "context-param", "distributable", "description", "display-name", "icon"};
47
48
49
50     private WebAppXML() {
51     }
52
53     public WebAppXML(String JavaDoc fileName) {
54         this.xmlFileName = fileName;
55         reloadDocument();
56     }
57
58     /**
59      * Call this method when need to reload web.xml file and parse them.
60      */

61     public void reloadDocument(){
62         try {
63             parser = new DOMParser();
64
65             
66             parser.setEntityResolver(new WebXMLEntityResolver());
67             parser.setFeature("http://xml.org/sax/features/validation", true);
68
69
70             parser.parse(this.xmlFileName);
71             document = parser.getDocument();
72
73             this.enhydraServlet = this.getEnhydraServletNode();
74             this.root = this.document.getDocumentElement();
75             this.lineSep = this.determineLineSeparator();
76
77         } catch (SAXException JavaDoc e) {
78             e.printStackTrace();
79             System.err.println("SAXException - bad xml format!");
80         } catch (IOException JavaDoc e) {
81             e.printStackTrace();
82         }
83     }
84
85
86  /*
87     * Determines which line separator to use in the xml document,
88     * on the basis of the operating system name.
89   */

90     String JavaDoc determineLineSeparator() {
91
92         String JavaDoc os = System.getProperty("os.name");
93
94         if(os.toLowerCase().indexOf("windows") != -1)
95             return LineSeparator.Windows;
96         if( (os.toLowerCase().indexOf("unix") != -1) || (os.toLowerCase().indexOf("linux") != -1) )
97             return LineSeparator.Unix;
98         //for all other systems set Web line separator ("\n")
99
return LineSeparator.Web;
100     }
101
102
103
104  /*
105     * Saves the formatted xml document.
106   */

107     public void saveDocument() {
108
109         try {
110
111             FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(this.xmlFileName);
112             OutputFormat of = new OutputFormat();
113
114             //of.setIndenting(true);
115
of.setIndent(4);
116
117             of.setMethod(Method.XML);
118             of.setPreserveSpace(true);
119
120             XMLSerializer out = new XMLSerializer(os, of);
121             out.serialize(this.document);
122
123         } catch (FileNotFoundException JavaDoc e) {
124             e.printStackTrace();
125         } catch (IOException JavaDoc e) {
126             e.printStackTrace();
127         }
128     }
129
130
131
132
133
134
135 /* FILTERS */
136
137  /*
138     * Adds new filter tag with the given attributes.
139     * Possible tags:
140     <!ELEMENT filter (icon?, filter-name, display-name?, description?, filter-class, init-param*)>
141
142     <!ELEMENT icon (small-icon?, large-icon?)>
143     <!ELEMENT small-icon (#PCDATA)>
144     <!ELEMENT large-icon (#PCDATA)>
145
146     <!ELEMENT filter-name (#PCDATA)>
147     <!ELEMENT display-name (#PCDATA)>
148     <!ELEMENT description (#PCDATA)>
149     <!ELEMENT filter-class (#PCDATA)>
150     <!ELEMENT init-param (param-name, param-value, description?)>
151     <!ELEMENT param-name (#PCDATA)>
152     <!ELEMENT param-value (#PCDATA)>
153
154     <!ELEMENT filter-mapping (filter-name, (url-pattern | servlet-name))>
155     <!ELEMENT url-pattern (#PCDATA)>
156     <!ELEMENT servlet-name (#PCDATA)>
157   */

158     public void addFilter(
159             /*filter*/
160                           String JavaDoc filterName, String JavaDoc filterClass, /*required*/
161                           String JavaDoc smallIcon, String JavaDoc largeIcon, String JavaDoc displayName, String JavaDoc description,
162                           String JavaDoc[] paramNames, String JavaDoc[] paramValues, String JavaDoc[] descriptions,
163             /*filter-mapping*/
164                           String JavaDoc mappingUrl, String JavaDoc mappingServlet){
165
166         Node JavaDoc newLine = this.document.createTextNode(this.lineSep);
167         Node JavaDoc newLinePlusTab = this.document.createTextNode(this.lineSep + "\t");
168         Node JavaDoc newLinePlusTwoTabs = this.document.createTextNode(this.lineSep + "\t\t");
169         Node JavaDoc newLinePlusThreeTabs = this.document.createTextNode(this.lineSep + "\t\t\t");
170         Node JavaDoc textNode = this.document.createTextNode("");
171
172         //create filter tag node
173
Node JavaDoc filter = this.document.createElement("filter");
174
175         //create filter-name tag node with the given name
176
Node JavaDoc filterNameNode = this.document.createElement("filter-name");
177         filterNameNode.appendChild(this.document.createTextNode(filterName));
178         filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
179         filter.appendChild(filterNameNode);
180
181         //create filter-class tag node with the given name
182
Node JavaDoc filterClassNode = this.document.createElement("filter-class");
183         filterClassNode.appendChild(this.document.createTextNode(filterClass));
184         filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
185         filter.appendChild(filterClassNode);
186
187         //create init-param tag node
188
Node JavaDoc initParamNode = null;
189         if(paramNames != null && paramValues != null){
190            for(int i = 0; i < paramNames.length; i++){
191                initParamNode = this.document.createElement("init-param");
192                Node JavaDoc paramNameNode = this.document.createElement("param-name");
193                Node JavaDoc paramValueNode = this.document.createElement("param-value");
194                paramNameNode.appendChild(this.document.createTextNode(paramNames[i]));
195                paramValueNode.appendChild(this.document.createTextNode(paramValues[i]));
196                initParamNode.appendChild(newLinePlusThreeTabs.cloneNode(true));
197                initParamNode.appendChild(paramNameNode);
198                initParamNode.appendChild(newLinePlusThreeTabs.cloneNode(true));
199                initParamNode.appendChild(paramValueNode);
200                if(descriptions != null && descriptions[i] != null){
201                    Node JavaDoc descriptNode = this.document.createElement("description");
202                    descriptNode.appendChild(this.document.createTextNode(descriptions[i]));
203                    initParamNode.appendChild(newLinePlusThreeTabs.cloneNode(true));
204                    initParamNode.appendChild(descriptNode);
205                }
206                initParamNode.appendChild(newLinePlusTwoTabs.cloneNode(true));
207                filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
208                filter.appendChild(initParamNode);
209            }
210        }
211
212
213         //create icon tag node
214
Node JavaDoc iconNode = null;
215         if(smallIcon != null || largeIcon != null){
216             iconNode = this.document.createElement("icon");
217             if(smallIcon != null) {
218                 Node JavaDoc smallIconNode = this.document.createElement("small-icon");
219                 smallIconNode.appendChild(this.document.createTextNode(smallIcon));
220                 iconNode.appendChild(newLinePlusThreeTabs.cloneNode(true));
221                 iconNode.appendChild(smallIconNode);
222             }
223             if(largeIcon != null){
224                 Node JavaDoc largeIconNode = this.document.createElement("large-icon");
225                 largeIconNode.appendChild(this.document.createTextNode(largeIcon));
226                 iconNode.appendChild(newLinePlusThreeTabs.cloneNode(true));
227                 iconNode.appendChild(largeIconNode);
228             }
229             iconNode.appendChild(newLinePlusTwoTabs.cloneNode(true));
230             filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
231             filter.appendChild(iconNode);
232         }
233
234
235         //create display-name tag node
236
Node JavaDoc displayNameNode = null;
237         if(displayName != null){
238             displayNameNode = this.document.createElement("display-name");
239             displayNameNode.appendChild(this.document.createTextNode(displayName));
240             filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
241             filter.appendChild(displayNameNode);
242         }
243
244
245         //create description tag node
246
Node JavaDoc descriptionNode = null;
247         if(description != null){
248             descriptionNode = this.document.createElement("description");
249             descriptionNode.appendChild(this.document.createTextNode(description));
250             filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
251             filter.appendChild(descriptionNode);
252         }
253
254
255         filter.appendChild(newLinePlusTab.cloneNode(true));
256
257
258         Node JavaDoc mapping = null;
259         if( !(mappingUrl == null && mappingServlet == null) ) {
260             //create filter-mapping tag node
261
mapping = this.document.createElement("filter-mapping");
262
263             //create filter-name tag node with the given name
264
Node JavaDoc mappingNameNode = this.document.createElement("filter-name");
265             mappingNameNode.appendChild(this.document.createTextNode(filterName));
266
267             //create url-pattern tag node with the given name
268
Node JavaDoc mappingUrlPatternNode = null;
269             Node JavaDoc mappingServletNameNode = null;
270             if(mappingUrl != null) {
271                 mappingUrlPatternNode = this.document.createElement("url-pattern");
272                 mappingUrlPatternNode.appendChild(this.document.createTextNode(mappingUrl));
273             }else if(mappingServlet != null){
274                 //create servlet-name tag node with the given name
275
mappingServletNameNode = this.document.createElement("servlet-name");
276                 mappingServletNameNode.appendChild(this.document.createTextNode(mappingServlet));
277             }
278
279             mapping.appendChild(newLinePlusTwoTabs.cloneNode(true));
280             mapping.appendChild(mappingNameNode);
281             mapping.appendChild(newLinePlusTwoTabs.cloneNode(true));
282
283             if(mappingServletNameNode != null)
284                 mapping.appendChild(mappingServletNameNode);
285             if(mappingUrlPatternNode != null)
286                 mapping.appendChild(mappingUrlPatternNode);
287
288             mapping.appendChild(newLinePlusTab.cloneNode(true));
289         }
290
291
292         
293         
294         
295         //insert new filter-mapping node after referent node:
296
Node JavaDoc ref = this.findReferentNodeForAddition(this.tagsTillFilterMapping);
297         ref = root.insertBefore(newLinePlusTab.cloneNode(true), ref);
298         if(mapping != null){
299             ref = root.insertBefore(mapping, ref);
300             ref = root.insertBefore(newLinePlusTab.cloneNode(true), ref);
301             ref = root.insertBefore(newLinePlusTab.cloneNode(true), ref);
302         }
303         
304         ref = this.findReferentNodeForAddition(this.tagsTillFilter);
305         ref = root.insertBefore(filter, ref);
306         ref = root.insertBefore(newLinePlusTab.cloneNode(true), ref);
307         root.insertBefore(newLinePlusTab.cloneNode(true), ref);
308         
309     }
310     
311     
312     //icon?, display-name?, description?, distributable?, context-param*, filter*
313
/*
314      * Finds node before which the new filter or filter-mapping will be added
315      */

316     private Node JavaDoc findReferentNodeForAddition(String JavaDoc[] tags) {
317         for(int i = 0; i < tags.length; i++) {
318             NodeList JavaDoc list = root.getElementsByTagName(tags[i]);
319             if(list.getLength() != 0) {
320                 if(list.item(list.getLength() - 1).getParentNode().equals(root))
321                     return list.item(list.getLength() - 1).getNextSibling();
322             }
323         }
324         return root.getFirstChild();
325     }
326
327
328
329  /*
330     * Removes filter tag with the given name.
331   */

332     public void removeFilter(String JavaDoc filterName) {
333         Node JavaDoc filter = this.findFilter(filterName);
334         if(filter != null) {
335             Node JavaDoc text = filter.getNextSibling();
336             if(text != null && text.getNodeType() == Node.TEXT_NODE)
337                 this.root.removeChild(text);
338             //text = filter.getPreviousSibling();
339
//if(text != null && text.getNodeType() == Node.TEXT_NODE)
340
// this.root.removeChild(text);
341
this.root.removeChild(filter);
342             
343             Node JavaDoc filterMapping = this.findFilterMapping(filterName);
344             if(filterMapping != null) {
345 // Node text = filter.getNextSibling();
346
// if(text != null && text.getNodeType() == Node.TEXT_NODE)
347
// this.root.removeChild(text);
348
// text = filter.getPreviousSibling();
349
// if(text != null && text.getNodeType() == Node.TEXT_NODE)
350
// this.root.removeChild(text);
351
// this.root.removeChild(filter);
352

353                 text = filterMapping.getNextSibling();
354                 if(text != null && text.getNodeType() == Node.TEXT_NODE)
355                     this.root.removeChild(text);
356                 this.root.removeChild(filterMapping);
357             }
358             else
359                 System.err.println("Cannot remove filter-mapping named \"" + filterName + "\"");
360         }
361         else
362             System.err.println("Cannot remove filter named \"" + filterName + "\"");
363     }
364
365     /**
366      *
367      * @return available filter names
368      */

369     public String JavaDoc[] getFilterNames(){
370         ArrayList JavaDoc array = new ArrayList JavaDoc();
371         NodeList JavaDoc possibleFilters = this.root.getChildNodes();
372         for(int i = 0; i < possibleFilters.getLength(); i++) {
373             if(possibleFilters.item(i).getNodeName().equals("filter")){
374                 NodeList JavaDoc names = ((Element JavaDoc)possibleFilters.item(i)).getElementsByTagName("filter-name");
375                 array.add(this.getNodeText(names.item(0)));
376             }
377         }
378
379         String JavaDoc[] names = new String JavaDoc[array.size()];
380         for(int i = 0; i < array.size(); i++)
381             names[i] = (String JavaDoc)array.get(i);
382         return names;
383     }
384
385     /**
386      *
387      * @return filter parameters
388      */

389     public HashMap JavaDoc getFilterParameters(String JavaDoc filterName){
390         HashMap JavaDoc hash = new HashMap JavaDoc();
391         Node JavaDoc filterNode = this.findFilter(filterName);
392         if(filterNode != null){
393             //get filter-class value
394
NodeList JavaDoc filterClasses = ((Element JavaDoc)filterNode).getElementsByTagName("filter-class");
395             hash.put("filter-class", this.getNodeText(filterClasses.item(0)));
396
397             //get init-param value
398
NodeList JavaDoc initParams = ((Element JavaDoc)filterNode).getElementsByTagName("init-param");
399             if(initParams != null && initParams.getLength() != 0){
400                 ArrayList JavaDoc initArray = new ArrayList JavaDoc();
401                 for(int i = 0; i < initParams.getLength(); i++){
402                     NodeList JavaDoc names = ((Element JavaDoc)initParams.item(i)).getElementsByTagName("param-name");
403                     NodeList JavaDoc values = ((Element JavaDoc)initParams.item(i)).getElementsByTagName("param-value");
404                     NodeList JavaDoc descripts = ((Element JavaDoc)initParams.item(i)).getElementsByTagName("description");
405                     ArrayList JavaDoc parameter = new ArrayList JavaDoc(3);
406                     parameter.add(this.getNodeText(names.item(0)));
407                     parameter.add(this.getNodeText(values.item(0)));
408                     if(descripts != null && descripts.getLength() != 0)
409                         parameter.add(this.getNodeText(descripts.item(0)));
410                     else
411                         parameter.add( null);
412                     initArray.add(parameter);
413                 }
414                 hash.put("init-param", initArray);
415             }
416             else
417                 hash.put("init-param", null);
418
419             //get icons values
420
NodeList JavaDoc icons = ((Element JavaDoc)filterNode).getElementsByTagName("icon");
421             if(icons != null && icons.getLength() != 0){
422                 NodeList JavaDoc ic = ((Element JavaDoc)icons.item(0)).getElementsByTagName("small-icon");
423                 if(ic != null && ic.getLength() != 0)
424                     hash.put("small-icon", this.getNodeText(ic.item(0)));
425                 ic = ((Element JavaDoc)icons.item(0)).getElementsByTagName("large-icon");
426                 if(ic != null && ic.getLength() != 0)
427                     hash.put("large-icon", this.getNodeText(ic.item(0)));
428             }
429
430             //get display-name values
431
NodeList JavaDoc displayNames = ((Element JavaDoc)filterNode).getElementsByTagName("display-name");
432             if(displayNames != null && displayNames.getLength() != 0)
433                 hash.put("display-name", this.getNodeText(displayNames.item(0)));
434
435             //get description values
436
NodeList JavaDoc descriptions = ((Element JavaDoc)filterNode).getElementsByTagName("description");
437             if(descriptions != null && descriptions.getLength() != 0){
438                 for(int i = 0; i < descriptions.getLength(); i++){
439                     //check only descriptions in filter tag node (not in init-param tag node)
440
if(descriptions.item(i).getParentNode().equals(filterNode))
441                         hash.put("description", this.getNodeText(descriptions.item(i)));
442                 }
443             }
444
445             //get filter-mapping value
446
Node JavaDoc filterMappingNode = this.findFilterMapping(filterName);
447             if(filterMappingNode != null){
448                 NodeList JavaDoc urlPatterns = ((Element JavaDoc)filterMappingNode).getElementsByTagName("url-pattern");
449                 if(urlPatterns != null && urlPatterns.getLength() != 0)
450                     hash.put("url-pattern", this.getNodeText(urlPatterns.item(0)));
451                 else{
452                     NodeList JavaDoc servlets = ((Element JavaDoc)filterMappingNode).getElementsByTagName("servlet-name");
453                     if(servlets != null && servlets.getLength() != 0)
454                         hash.put("servlet-name", this.getNodeText(servlets.item(0)));
455                 }
456             }
457         }
458         return hash;
459     }
460
461  /**
462   * Find filter tag with the given name.
463   */

464     private Node JavaDoc findFilter(String JavaDoc filterName) {
465         NodeList JavaDoc possibleFilters = this.root.getChildNodes();
466         for(int i = 0; i < possibleFilters.getLength(); i++) {
467             if(possibleFilters.item(i).getNodeName().equalsIgnoreCase("filter")) {
468                 NodeList JavaDoc filterChilds = possibleFilters.item(i).getChildNodes();
469                 for(int k = 0; k < filterChilds.getLength(); k++) {
470                     if(filterChilds.item(k).getNodeName().equalsIgnoreCase("filter-name")) {
471 // NodeList filterNameChilds = filterChilds.item(k).getChildNodes();
472
// for(int m = 0; m < filterNameChilds.getLength(); m++) {
473
// try {
474
// if(filterNameChilds.item(m).getNodeValue().equalsIgnoreCase(filterName))
475
// return possibleFilters.item(i);
476
// } catch(Exception e) {
477
// }
478
// }
479
if(this.getNodeText(filterChilds.item(k)).equalsIgnoreCase(filterName))
480                            return possibleFilters.item(i);
481                     }
482                 }
483             }
484         }
485         System.err.println("Cannot find filter named \"" + filterName + "\"");
486         return null;
487     }
488
489  /*
490     * Find filter tag with the given name.
491   */

492     private Node JavaDoc findFilterMapping(String JavaDoc filterName) {
493         NodeList JavaDoc possibleFilters = this.root.getChildNodes();
494         for(int i = 0; i < possibleFilters.getLength(); i++) {
495             if(possibleFilters.item(i).getNodeName().equalsIgnoreCase("filter-mapping")) {
496                 NodeList JavaDoc filterChilds = possibleFilters.item(i).getChildNodes();
497                 for(int k = 0; k < filterChilds.getLength(); k++) {
498                     if(filterChilds.item(k).getNodeName().equalsIgnoreCase("filter-name")) {
499                         NodeList JavaDoc filterNameChilds = filterChilds.item(k).getChildNodes();
500                         for(int m = 0; m < filterNameChilds.getLength(); m++) {
501                             try {
502                                 if(filterNameChilds.item(m).getNodeValue().equalsIgnoreCase(filterName))
503                                     return possibleFilters.item(i);
504                             } catch(Exception JavaDoc e) {
505                                 
506                             }
507                         }
508                     }
509                 }
510             }
511         }
512         System.err.println("Cannot find filter mapping named \"" + filterName + "\"");
513         return null;
514     }
515
516 /* SERVLETS */
517
518  /*
519     * Returns true if "enhydra" servlet is Enhydra application,
520     * false otherwise.
521   */

522     public boolean isEnhydraApplication() {
523         String JavaDoc org = "org.enhydra.Servlet";
524         String JavaDoc lutris = "com.lutris.appserver.server.httpPresentation.servlet.HttpPresentationServlet";
525
526         try {
527             NodeList JavaDoc servletChilds = this.enhydraServlet.getChildNodes();
528             for(int i = 0; i < servletChilds.getLength(); i++) {
529                 if(servletChilds.item(i).getNodeName().equalsIgnoreCase("servlet-class")) {
530 // NodeList text = servletChilds.item(i).getChildNodes();
531
// for(int k = 0; k < text.getLength(); k++) {
532
// try {
533
// if(text.item(k).getNodeValue().trim().equals(org) || text.item(k).getNodeValue().trim().equals(lutris))
534
// return true;
535
// } catch(Exception e) {
536
// }
537
// }
538
String JavaDoc className = this.getNodeText(servletChilds.item(i));
539                     if(className.equals(org) || className.equals(lutris))
540                         return true;
541                 }
542             }
543         } catch(Exception JavaDoc e) {
544         }
545
546         return false;
547     }
548
549
550  /*
551     * Returns text attached to the <param-value> tag node, paired with the
552     * <param-name> tag node that contains text "ConfFile"
553     * (all nodes are in servlet tag node named enhydra).
554   */

555     public String JavaDoc getConfFilePath() {
556         //inside enhydra servlet, find tag named <init-param>,
557
//and there find <param-name> tag that contains text "ConfFile"
558
//his <param-value> tag contains text to return from function
559
NodeList JavaDoc servletChilds = this.enhydraServlet.getChildNodes();
560         for(int i = 0; i < servletChilds.getLength(); i++) {
561             if(servletChilds.item(i).getNodeName().equalsIgnoreCase("init-param")) {
562                 Node JavaDoc paramValueNode = this.getConfFileValueNode(servletChilds.item(i));
563                 if(paramValueNode != null) {
564                     String JavaDoc value = this.getNodeText(paramValueNode);
565                     if(!value.equalsIgnoreCase(""))
566                         return value;
567                     else break;
568                 }
569             }
570         }
571
572         System.err.println("Cannot find ConfFilePath!");
573         return null;
574     }
575
576
577  /*
578     * Sets text attached to the <param-value> tag node, paired with the
579     * <param-name> tag node that contains text "ConfFile", to the given string
580     * (all nodes are in servlet tag node named enhydra).
581   */

582     public void setConfFilePath(String JavaDoc filePath) {
583         NodeList JavaDoc servletChilds = this.enhydraServlet.getChildNodes();
584         for(int i = 0; i < servletChilds.getLength(); i++) {
585             if(servletChilds.item(i).getNodeName().equalsIgnoreCase("init-param")) {
586                 Node JavaDoc paramValueNode = this.getConfFileValueNode(servletChilds.item(i));
587                 if(paramValueNode != null) {
588                     this.setNodeText(paramValueNode, filePath);
589                     return;
590                 }
591             }
592         }
593
594         System.err.println("Cannot set ConfFilePath!");
595     }
596
597
598  /*
599     * Returns node that represents <param-value> node that is paired
600     * with the <param-name> tag node with the value "ConfFile".
601   */

602     private Node JavaDoc getConfFileValueNode(Node JavaDoc initParamNode) {
603         NodeList JavaDoc params = initParamNode.getChildNodes();
604         for(int i = 0; i < params.getLength(); i++) {
605             if(params.item(i).getNodeName().equalsIgnoreCase("param-name")) {
606                 if(!this.getNodeText(params.item(i)).equalsIgnoreCase(EnhydraServer.CONF_FILE))
607                     break;
608             }
609             else {
610                 if(params.item(i).getNodeName().equalsIgnoreCase("param-value"))
611                     return params.item(i);
612             }
613         }
614
615         return null;
616     }
617
618    /*
619     * Returns text attached to the <param-value> tag node, paired with the
620     * <param-name> tag node that contains text "ConfFileClass"
621     * (all nodes are in servlet tag node named enhydra).
622     */

623     public String JavaDoc getConfFileClass() { // TJ 08.11.2003.
624
//inside enhydra servlet, find tag named <init-param>,
625
//and there find <param-name> tag that contains text "ConfFileClass"
626
//his <param-value> tag contains text to return from function
627
NodeList JavaDoc servletChilds = this.enhydraServlet.getChildNodes();
628         for(int i = 0; i < servletChilds.getLength(); i++) {
629             if(servletChilds.item(i).getNodeName().equalsIgnoreCase("init-param")) {
630                 Node JavaDoc paramValueNode = this.getConfFileClassValueNode(servletChilds.item(i));
631                 if(paramValueNode != null) {
632                     String JavaDoc value = this.getNodeText(paramValueNode);
633                     if(!value.equalsIgnoreCase(""))
634                         return value;
635                     else break;
636                 }
637             }
638         }
639         return null;
640     }
641
642
643    /*
644     * Sets text attached to the <param-value> tag node, paired with the
645     * <param-name> tag node that contains text "ConfFileClass", to the given
646     * string (all nodes are in servlet tag node named enhydra).
647     */

648     public void setConfFileClass(String JavaDoc filePath) { // TJ 08.11.2003.
649
NodeList JavaDoc servletChilds = this.enhydraServlet.getChildNodes();
650         for(int i = 0; i < servletChilds.getLength(); i++) {
651             if(servletChilds.item(i).getNodeName().equalsIgnoreCase("init-param")) {
652                 Node JavaDoc paramValueNode = this.getConfFileClassValueNode(servletChilds.item(i));
653                 if(paramValueNode != null) {
654                     this.setNodeText(paramValueNode, filePath);
655                     return;
656                 }
657             }
658         }
659     }
660
661
662    /*
663     * Returns node that represents <param-value> node that is paired
664     * with the <param-name> tag node with the value "ConfFileClass".
665     */

666     private Node JavaDoc getConfFileClassValueNode(Node JavaDoc initParamNode) { // TJ 08.11.2003.
667
NodeList JavaDoc params = initParamNode.getChildNodes();
668         for(int i = 0; i < params.getLength(); i++) {
669             if(params.item(i).getNodeName().equalsIgnoreCase("param-name")) {
670                 if(!this.getNodeText(params.item(i)).equalsIgnoreCase(EnhydraServer.CONF_FILE_CLASS))
671                     break;
672             }
673             else {
674                 if(params.item(i).getNodeName().equalsIgnoreCase("param-value"))
675                     return params.item(i);
676             }
677         }
678
679         return null;
680     }
681
682  /*
683     * Gets the string attached to the child node.
684   */

685     private String JavaDoc getNodeText(Node JavaDoc node) {
686         String JavaDoc text = "";
687         NodeList JavaDoc childs = node.getChildNodes();
688         for(int i = 0; i < childs.getLength(); i++) {
689             if(childs.item(i).getNodeType() == Node.TEXT_NODE)
690                 text += childs.item(i).getNodeValue();
691         }
692         text = text.trim();
693         return text;
694     }
695
696  /*
697     * Sets the string attached to the child node.
698   */

699     private void setNodeText(Node JavaDoc node, String JavaDoc newText) {
700         String JavaDoc text = "";
701         NodeList JavaDoc childs = node.getChildNodes();
702
703         for(int i = 0; i < childs.getLength(); i++) {
704             text = childs.item(i).getNodeValue();
705             text = text.replaceAll("\n", "").replaceAll("\r", "").replaceAll("\t", "").replaceAll(" ", "");
706             if(!text.equals("")) {
707                 childs.item(i).setNodeValue(newText);
708                 break;
709             }
710         }
711     }
712
713
714
715
716
717  /*
718     * Returns servlet tag node with the "servlet-name" tag value = "enhydra",
719     * null otherwise.
720   */

721     private Node JavaDoc getEnhydraServletNode() {
722 // NodeList servlets = this.document.getElementsByTagName("servlet");
723
// NodeList servletNames = this.document.getElementsByTagName("servlet-name");
724
// System.out.println("servletNames count = " + servletNames.getLength());
725
// for(int i = 0; i < servletNames.getLength(); i++) {
726
// System.out.println("servletNames[" + i + "]" + servletNames.item(i) + " value = " + servletNames.item(i).getNodeValue());
727
// NodeList childs = servletNames.item(i).getChildNodes();
728
// for(int k = 0; k < childs.getLength(); k++) {
729
// System.out.println("childs[" + k + "]" + childs.item(i));
730
// }
731
// System.out.println("parent = " + servletNames.item(i).getParentNode());
732
// }
733
//
734
NodeList JavaDoc servlets = this.document.getDocumentElement().getChildNodes();
735         for(int i = 0; i < servlets.getLength(); i++) {
736             try {
737                 //if it is servlet node
738
if(servlets.item(i).getNodeName().equalsIgnoreCase("servlet")) {
739                     //get all childs
740
NodeList JavaDoc childs = servlets.item(i).getChildNodes();
741                     for(int k = 0; k < childs.getLength(); k++) {
742                         //if child is servlet-name tag
743
if(childs.item(k).getNodeName().equalsIgnoreCase("servlet-name")) {
744                             //get his childs (that is text node with the servlet name)
745
// NodeList c = childs.item(k).getChildNodes();
746
// for(int m = 0; m < c.getLength(); m++) {
747
// if(c.item(m).getNodeValue().trim().equalsIgnoreCase("enhydra"))
748
// return servlets.item(i);
749
// }
750
if(this.getNodeText(childs.item(k)).equalsIgnoreCase("enhydra"))
751                                 return servlets.item(i);
752                         }
753                     }
754                 }
755             } catch(Exception JavaDoc e) {
756             }
757         }
758
759         System.err.println("Servlet tag with the name \"enhydra\" cannot be found !");
760         return null;
761     }
762
763
764
765
766     public static void main(String JavaDoc[] args) {
767         try {
768             WebAppXML test = new WebAppXML("C:/Temp/web.xml");
769
770             System.out.println("isEnhydraApplication = " + test.isEnhydraApplication());
771
772             test.addFilter("MY_FILTER_1", "MY_FILTER_CLASS", "iconsS", "iconL", null, "###",
773                            new String JavaDoc[]{"p1", "p2", "p3"}, new String JavaDoc[]{"v1", "v2", "v3"},
774                            null, null, "MY_SERVLET");
775                            
776             test.saveDocument();
777
778             test.addFilter("MY_FILTER_2", "MY_FILTER_CLASS2", null, null, "DISPLAY_NAME2", null,
779                            new String JavaDoc[]{"p1", "p2"}, new String JavaDoc[]{"v1", "v2"},
780                            new String JavaDoc[]{"d1", "d2"}, "MY_URL", null);
781                            
782             test.saveDocument();
783
784             //test.removeFilter("MY_FILTER");
785
System.out.println("--------- Before reloadDocument()-----------");
786             String JavaDoc[] names = test.getFilterNames();
787             for(int i = 0; i < names.length; i++){
788                 System.out.println("str[" + i + "] = " + names[i]);
789             }
790             System.out.println("--------- After reloadDocument()-----------");
791             test.reloadDocument();
792             names = test.getFilterNames();
793             for(int i = 0; i < names.length; i++){
794                 System.out.println("str[" + i + "] = " + names[i]);
795             }
796
797             HashMap JavaDoc hash = test.getFilterParameters("MY_FILTER_1");
798             System.out.println("hash: " + hash);
799
800             hash = test.getFilterParameters("MY_FILTER_2");
801             System.out.println("hash: " + hash);
802             
803 // test.removeFilter("MY_FILTER_1");
804

805 // test.removeFilter("MY_FILTER_2");
806

807             test.saveDocument();
808             
809
810             System.out.println("getConfFilePath() = " + test.getConfFilePath());
811
812             test.setConfFilePath("MyConfFilePath");
813
814             test.saveDocument();
815
816             System.out.println("Happy end");
817         } catch(Exception JavaDoc e) {
818             System.out.println("NOOOOOOOOOO");
819             e.printStackTrace();
820         }
821     }
822 }
823
824
Popular Tags