001: package org.enhydra.server.util;
002:
003: import java.io.FileNotFoundException;
004: import java.io.FileOutputStream;
005: import java.io.IOException;
006: import java.util.ArrayList;
007: import java.util.HashMap;
008:
009: import org.apache.xerces.parsers.DOMParser;
010: import org.apache.xml.serialize.LineSeparator;
011: import org.apache.xml.serialize.Method;
012: import org.apache.xml.serialize.OutputFormat;
013: import org.apache.xml.serialize.XMLSerializer;
014: import org.enhydra.server.EnhydraServer;
015: import org.w3c.dom.Document;
016: import org.w3c.dom.Element;
017: import org.w3c.dom.Node;
018: import org.w3c.dom.NodeList;
019: import org.xml.sax.SAXException;
020:
021: /**
022: * @author Tweety
023: */
024: public class WebAppXML {
025:
026: //xml document
027: public Document document;
028:
029: //parser
030: DOMParser parser;
031:
032: //xml document name
033: private String xmlFileName;
034:
035: //Node that represents servlet tag with the name "enhydra"
036: private Node enhydraServlet;
037:
038: //Node that represents root node (web-app tag)
039: private Element root;
040:
041: //line separator for this operating system
042: private String lineSep;
043:
044: //
045: private final String[] tagsTillFilter = new String[] { "filter",
046: "context-param", "distributable", "description",
047: "display-name", "icon" };
048: private final String[] tagsTillFilterMapping = new String[] {
049: "filter-mapping", "filter", "context-param",
050: "distributable", "description", "display-name", "icon" };
051:
052: private WebAppXML() {
053: }
054:
055: public WebAppXML(String fileName) {
056: this .xmlFileName = fileName;
057: reloadDocument();
058: }
059:
060: /**
061: * Call this method when need to reload web.xml file and parse them.
062: */
063: public void reloadDocument() {
064: try {
065: parser = new DOMParser();
066:
067: parser.setEntityResolver(new WebXMLEntityResolver());
068: parser.setFeature("http://xml.org/sax/features/validation",
069: true);
070:
071: parser.parse(this .xmlFileName);
072: document = parser.getDocument();
073:
074: this .enhydraServlet = this .getEnhydraServletNode();
075: this .root = this .document.getDocumentElement();
076: this .lineSep = this .determineLineSeparator();
077:
078: } catch (SAXException e) {
079: e.printStackTrace();
080: System.err.println("SAXException - bad xml format!");
081: } catch (IOException e) {
082: e.printStackTrace();
083: }
084: }
085:
086: /*
087: * Determines which line separator to use in the xml document,
088: * on the basis of the operating system name.
089: */
090: String determineLineSeparator() {
091:
092: String os = System.getProperty("os.name");
093:
094: if (os.toLowerCase().indexOf("windows") != -1)
095: return LineSeparator.Windows;
096: if ((os.toLowerCase().indexOf("unix") != -1)
097: || (os.toLowerCase().indexOf("linux") != -1))
098: return LineSeparator.Unix;
099: //for all other systems set Web line separator ("\n")
100: return LineSeparator.Web;
101: }
102:
103: /*
104: * Saves the formatted xml document.
105: */
106: public void saveDocument() {
107:
108: try {
109:
110: FileOutputStream os = new FileOutputStream(this .xmlFileName);
111: OutputFormat of = new OutputFormat();
112:
113: //of.setIndenting(true);
114: of.setIndent(4);
115:
116: of.setMethod(Method.XML);
117: of.setPreserveSpace(true);
118:
119: XMLSerializer out = new XMLSerializer(os, of);
120: out.serialize(this .document);
121:
122: } catch (FileNotFoundException e) {
123: e.printStackTrace();
124: } catch (IOException e) {
125: e.printStackTrace();
126: }
127: }
128:
129: /* FILTERS */
130:
131: /*
132: * Adds new filter tag with the given attributes.
133: * Possible tags:
134: <!ELEMENT filter (icon?, filter-name, display-name?, description?, filter-class, init-param*)>
135:
136: <!ELEMENT icon (small-icon?, large-icon?)>
137: <!ELEMENT small-icon (#PCDATA)>
138: <!ELEMENT large-icon (#PCDATA)>
139:
140: <!ELEMENT filter-name (#PCDATA)>
141: <!ELEMENT display-name (#PCDATA)>
142: <!ELEMENT description (#PCDATA)>
143: <!ELEMENT filter-class (#PCDATA)>
144: <!ELEMENT init-param (param-name, param-value, description?)>
145: <!ELEMENT param-name (#PCDATA)>
146: <!ELEMENT param-value (#PCDATA)>
147:
148: <!ELEMENT filter-mapping (filter-name, (url-pattern | servlet-name))>
149: <!ELEMENT url-pattern (#PCDATA)>
150: <!ELEMENT servlet-name (#PCDATA)>
151: */
152: public void addFilter(
153: /*filter*/
154: String filterName, String filterClass, /*required*/
155: String smallIcon, String largeIcon, String displayName,
156: String description, String[] paramNames,
157: String[] paramValues, String[] descriptions,
158: /*filter-mapping*/
159: String mappingUrl, String mappingServlet) {
160:
161: // Node newLine = this.document.createTextNode(this.lineSep);
162: Node newLinePlusTab = this .document.createTextNode(this .lineSep
163: + "\t");
164: Node newLinePlusTwoTabs = this .document
165: .createTextNode(this .lineSep + "\t\t");
166: Node newLinePlusThreeTabs = this .document
167: .createTextNode(this .lineSep + "\t\t\t");
168: // Node textNode = this.document.createTextNode("");
169:
170: //create filter tag node
171: Node filter = this .document.createElement("filter");
172:
173: //create filter-name tag node with the given name
174: Node filterNameNode = this .document
175: .createElement("filter-name");
176: filterNameNode.appendChild(this .document
177: .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 filterClassNode = this .document
183: .createElement("filter-class");
184: filterClassNode.appendChild(this .document
185: .createTextNode(filterClass));
186: filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
187: filter.appendChild(filterClassNode);
188:
189: //create init-param tag node
190: Node initParamNode = null;
191: if (paramNames != null && paramValues != null) {
192: for (int i = 0; i < paramNames.length; i++) {
193: initParamNode = this .document
194: .createElement("init-param");
195: Node paramNameNode = this .document
196: .createElement("param-name");
197: Node paramValueNode = this .document
198: .createElement("param-value");
199: paramNameNode.appendChild(this .document
200: .createTextNode(paramNames[i]));
201: paramValueNode.appendChild(this .document
202: .createTextNode(paramValues[i]));
203: initParamNode.appendChild(newLinePlusThreeTabs
204: .cloneNode(true));
205: initParamNode.appendChild(paramNameNode);
206: initParamNode.appendChild(newLinePlusThreeTabs
207: .cloneNode(true));
208: initParamNode.appendChild(paramValueNode);
209: if (descriptions != null && descriptions[i] != null) {
210: Node descriptNode = this .document
211: .createElement("description");
212: descriptNode.appendChild(this .document
213: .createTextNode(descriptions[i]));
214: initParamNode.appendChild(newLinePlusThreeTabs
215: .cloneNode(true));
216: initParamNode.appendChild(descriptNode);
217: }
218: initParamNode.appendChild(newLinePlusTwoTabs
219: .cloneNode(true));
220: filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
221: filter.appendChild(initParamNode);
222: }
223: }
224:
225: //create icon tag node
226: Node iconNode = null;
227: if (smallIcon != null || largeIcon != null) {
228: iconNode = this .document.createElement("icon");
229: if (smallIcon != null) {
230: Node smallIconNode = this .document
231: .createElement("small-icon");
232: smallIconNode.appendChild(this .document
233: .createTextNode(smallIcon));
234: iconNode.appendChild(newLinePlusThreeTabs
235: .cloneNode(true));
236: iconNode.appendChild(smallIconNode);
237: }
238: if (largeIcon != null) {
239: Node largeIconNode = this .document
240: .createElement("large-icon");
241: largeIconNode.appendChild(this .document
242: .createTextNode(largeIcon));
243: iconNode.appendChild(newLinePlusThreeTabs
244: .cloneNode(true));
245: iconNode.appendChild(largeIconNode);
246: }
247: iconNode.appendChild(newLinePlusTwoTabs.cloneNode(true));
248: filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
249: filter.appendChild(iconNode);
250: }
251:
252: //create display-name tag node
253: Node displayNameNode = null;
254: if (displayName != null) {
255: displayNameNode = this .document
256: .createElement("display-name");
257: displayNameNode.appendChild(this .document
258: .createTextNode(displayName));
259: filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
260: filter.appendChild(displayNameNode);
261: }
262:
263: //create description tag node
264: Node descriptionNode = null;
265: if (description != null) {
266: descriptionNode = this .document
267: .createElement("description");
268: descriptionNode.appendChild(this .document
269: .createTextNode(description));
270: filter.appendChild(newLinePlusTwoTabs.cloneNode(true));
271: filter.appendChild(descriptionNode);
272: }
273:
274: filter.appendChild(newLinePlusTab.cloneNode(true));
275:
276: Node mapping = null;
277: if (!(mappingUrl == null && mappingServlet == null)) {
278: //create filter-mapping tag node
279: mapping = this .document.createElement("filter-mapping");
280:
281: //create filter-name tag node with the given name
282: Node mappingNameNode = this .document
283: .createElement("filter-name");
284: mappingNameNode.appendChild(this .document
285: .createTextNode(filterName));
286:
287: //create url-pattern tag node with the given name
288: Node mappingUrlPatternNode = null;
289: Node mappingServletNameNode = null;
290: if (mappingUrl != null) {
291: mappingUrlPatternNode = this .document
292: .createElement("url-pattern");
293: mappingUrlPatternNode.appendChild(this .document
294: .createTextNode(mappingUrl));
295: } else if (mappingServlet != null) {
296: //create servlet-name tag node with the given name
297: mappingServletNameNode = this .document
298: .createElement("servlet-name");
299: mappingServletNameNode.appendChild(this .document
300: .createTextNode(mappingServlet));
301: }
302:
303: mapping.appendChild(newLinePlusTwoTabs.cloneNode(true));
304: mapping.appendChild(mappingNameNode);
305: mapping.appendChild(newLinePlusTwoTabs.cloneNode(true));
306:
307: if (mappingServletNameNode != null)
308: mapping.appendChild(mappingServletNameNode);
309: if (mappingUrlPatternNode != null)
310: mapping.appendChild(mappingUrlPatternNode);
311:
312: mapping.appendChild(newLinePlusTab.cloneNode(true));
313: }
314:
315: //insert new filter-mapping node after referent node:
316: Node ref = this
317: .findReferentNodeForAddition(this .tagsTillFilterMapping);
318: ref = root.insertBefore(newLinePlusTab.cloneNode(true), ref);
319: if (mapping != null) {
320: ref = root.insertBefore(mapping, ref);
321: ref = root
322: .insertBefore(newLinePlusTab.cloneNode(true), ref);
323: ref = root
324: .insertBefore(newLinePlusTab.cloneNode(true), ref);
325: }
326:
327: ref = this .findReferentNodeForAddition(this .tagsTillFilter);
328: ref = root.insertBefore(filter, ref);
329: ref = root.insertBefore(newLinePlusTab.cloneNode(true), ref);
330: root.insertBefore(newLinePlusTab.cloneNode(true), ref);
331:
332: }
333:
334: //icon?, display-name?, description?, distributable?, context-param*, filter*
335: /*
336: * Finds node before which the new filter or filter-mapping will be added
337: */
338: private Node findReferentNodeForAddition(String[] tags) {
339: for (int i = 0; i < tags.length; i++) {
340: NodeList list = root.getElementsByTagName(tags[i]);
341: if (list.getLength() != 0) {
342: if (list.item(list.getLength() - 1).getParentNode()
343: .equals(root))
344: return list.item(list.getLength() - 1)
345: .getNextSibling();
346: }
347: }
348: return root.getFirstChild();
349: }
350:
351: /*
352: * Removes filter tag with the given name.
353: */
354: public void removeFilter(String filterName) {
355: Node filter = this .findFilter(filterName);
356: if (filter != null) {
357: Node text = filter.getNextSibling();
358: if (text != null && text.getNodeType() == Node.TEXT_NODE)
359: this .root.removeChild(text);
360: //text = filter.getPreviousSibling();
361: //if(text != null && text.getNodeType() == Node.TEXT_NODE)
362: // this.root.removeChild(text);
363: this .root.removeChild(filter);
364:
365: Node filterMapping = this .findFilterMapping(filterName);
366: if (filterMapping != null) {
367: // Node text = filter.getNextSibling();
368: // if(text != null && text.getNodeType() == Node.TEXT_NODE)
369: // this.root.removeChild(text);
370: // text = filter.getPreviousSibling();
371: // if(text != null && text.getNodeType() == Node.TEXT_NODE)
372: // this.root.removeChild(text);
373: // this.root.removeChild(filter);
374:
375: text = filterMapping.getNextSibling();
376: if (text != null
377: && text.getNodeType() == Node.TEXT_NODE)
378: this .root.removeChild(text);
379: this .root.removeChild(filterMapping);
380: } else
381: System.err
382: .println("Cannot remove filter-mapping named \""
383: + filterName + "\"");
384: } else
385: System.err.println("Cannot remove filter named \""
386: + filterName + "\"");
387: }
388:
389: /**
390: *
391: * @return available filter names
392: */
393: public String[] getFilterNames() {
394: ArrayList array = new ArrayList();
395: NodeList possibleFilters = this .root.getChildNodes();
396: for (int i = 0; i < possibleFilters.getLength(); i++) {
397: if (possibleFilters.item(i).getNodeName().equals("filter")) {
398: NodeList names = ((Element) possibleFilters.item(i))
399: .getElementsByTagName("filter-name");
400: array.add(this .getNodeText(names.item(0)));
401: }
402: }
403:
404: String[] names = new String[array.size()];
405: for (int i = 0; i < array.size(); i++)
406: names[i] = (String) array.get(i);
407: return names;
408: }
409:
410: /**
411: *
412: * @return filter parameters
413: */
414: public HashMap getFilterParameters(String filterName) {
415: HashMap hash = new HashMap();
416: Node filterNode = this .findFilter(filterName);
417: if (filterNode != null) {
418: //get filter-class value
419: NodeList filterClasses = ((Element) filterNode)
420: .getElementsByTagName("filter-class");
421: hash.put("filter-class", this .getNodeText(filterClasses
422: .item(0)));
423:
424: //get init-param value
425: NodeList initParams = ((Element) filterNode)
426: .getElementsByTagName("init-param");
427: if (initParams != null && initParams.getLength() != 0) {
428: ArrayList initArray = new ArrayList();
429: for (int i = 0; i < initParams.getLength(); i++) {
430: NodeList names = ((Element) initParams.item(i))
431: .getElementsByTagName("param-name");
432: NodeList values = ((Element) initParams.item(i))
433: .getElementsByTagName("param-value");
434: NodeList descripts = ((Element) initParams.item(i))
435: .getElementsByTagName("description");
436: ArrayList parameter = new ArrayList(3);
437: parameter.add(this .getNodeText(names.item(0)));
438: parameter.add(this .getNodeText(values.item(0)));
439: if (descripts != null && descripts.getLength() != 0)
440: parameter.add(this .getNodeText(descripts
441: .item(0)));
442: else
443: parameter.add(null);
444: initArray.add(parameter);
445: }
446: hash.put("init-param", initArray);
447: } else
448: hash.put("init-param", null);
449:
450: //get icons values
451: NodeList icons = ((Element) filterNode)
452: .getElementsByTagName("icon");
453: if (icons != null && icons.getLength() != 0) {
454: NodeList ic = ((Element) icons.item(0))
455: .getElementsByTagName("small-icon");
456: if (ic != null && ic.getLength() != 0)
457: hash
458: .put("small-icon", this .getNodeText(ic
459: .item(0)));
460: ic = ((Element) icons.item(0))
461: .getElementsByTagName("large-icon");
462: if (ic != null && ic.getLength() != 0)
463: hash
464: .put("large-icon", this .getNodeText(ic
465: .item(0)));
466: }
467:
468: //get display-name values
469: NodeList displayNames = ((Element) filterNode)
470: .getElementsByTagName("display-name");
471: if (displayNames != null && displayNames.getLength() != 0)
472: hash.put("display-name", this .getNodeText(displayNames
473: .item(0)));
474:
475: //get description values
476: NodeList descriptions = ((Element) filterNode)
477: .getElementsByTagName("description");
478: if (descriptions != null && descriptions.getLength() != 0) {
479: for (int i = 0; i < descriptions.getLength(); i++) {
480: //check only descriptions in filter tag node (not in init-param tag node)
481: if (descriptions.item(i).getParentNode().equals(
482: filterNode))
483: hash.put("description", this
484: .getNodeText(descriptions.item(i)));
485: }
486: }
487:
488: //get filter-mapping value
489: Node filterMappingNode = this .findFilterMapping(filterName);
490: if (filterMappingNode != null) {
491: NodeList urlPatterns = ((Element) filterMappingNode)
492: .getElementsByTagName("url-pattern");
493: if (urlPatterns != null && urlPatterns.getLength() != 0)
494: hash.put("url-pattern", this
495: .getNodeText(urlPatterns.item(0)));
496: else {
497: NodeList servlets = ((Element) filterMappingNode)
498: .getElementsByTagName("servlet-name");
499: if (servlets != null && servlets.getLength() != 0)
500: hash.put("servlet-name", this
501: .getNodeText(servlets.item(0)));
502: }
503: }
504: }
505: return hash;
506: }
507:
508: /**
509: * Find filter tag with the given name.
510: */
511: private Node findFilter(String filterName) {
512: NodeList possibleFilters = this .root.getChildNodes();
513: for (int i = 0; i < possibleFilters.getLength(); i++) {
514: if (possibleFilters.item(i).getNodeName().equalsIgnoreCase(
515: "filter")) {
516: NodeList filterChilds = possibleFilters.item(i)
517: .getChildNodes();
518: for (int k = 0; k < filterChilds.getLength(); k++) {
519: if (filterChilds.item(k).getNodeName()
520: .equalsIgnoreCase("filter-name")) {
521: // NodeList filterNameChilds = filterChilds.item(k).getChildNodes();
522: // for(int m = 0; m < filterNameChilds.getLength(); m++) {
523: // try {
524: // if(filterNameChilds.item(m).getNodeValue().equalsIgnoreCase(filterName))
525: // return possibleFilters.item(i);
526: // } catch(Exception e) {
527: // }
528: // }
529: if (this .getNodeText(filterChilds.item(k))
530: .equalsIgnoreCase(filterName))
531: return possibleFilters.item(i);
532: }
533: }
534: }
535: }
536: System.err.println("Cannot find filter named \"" + filterName
537: + "\"");
538: return null;
539: }
540:
541: /*
542: * Find filter tag with the given name.
543: */
544: private Node findFilterMapping(String filterName) {
545: NodeList possibleFilters = this .root.getChildNodes();
546: for (int i = 0; i < possibleFilters.getLength(); i++) {
547: if (possibleFilters.item(i).getNodeName().equalsIgnoreCase(
548: "filter-mapping")) {
549: NodeList filterChilds = possibleFilters.item(i)
550: .getChildNodes();
551: for (int k = 0; k < filterChilds.getLength(); k++) {
552: if (filterChilds.item(k).getNodeName()
553: .equalsIgnoreCase("filter-name")) {
554: NodeList filterNameChilds = filterChilds
555: .item(k).getChildNodes();
556: for (int m = 0; m < filterNameChilds
557: .getLength(); m++) {
558: try {
559: if (filterNameChilds.item(m)
560: .getNodeValue()
561: .equalsIgnoreCase(filterName))
562: return possibleFilters.item(i);
563: } catch (Exception e) {
564:
565: }
566: }
567: }
568: }
569: }
570: }
571: System.err.println("Cannot find filter mapping named \""
572: + filterName + "\"");
573: return null;
574: }
575:
576: /* SERVLETS */
577:
578: /*
579: * Returns true if "enhydra" servlet is Enhydra application,
580: * false otherwise.
581: */
582: public boolean isEnhydraApplication() {
583: String org = "org.enhydra.Servlet";
584: String lutris = "com.lutris.appserver.server.httpPresentation.servlet.HttpPresentationServlet";
585:
586: try {
587: NodeList servletChilds = this .enhydraServlet
588: .getChildNodes();
589: for (int i = 0; i < servletChilds.getLength(); i++) {
590: if (servletChilds.item(i).getNodeName()
591: .equalsIgnoreCase("servlet-class")) {
592: // NodeList text = servletChilds.item(i).getChildNodes();
593: // for(int k = 0; k < text.getLength(); k++) {
594: // try {
595: // if(text.item(k).getNodeValue().trim().equals(org) || text.item(k).getNodeValue().trim().equals(lutris))
596: // return true;
597: // } catch(Exception e) {
598: // }
599: // }
600: String className = this .getNodeText(servletChilds
601: .item(i));
602: if (className.equals(org)
603: || className.equals(lutris))
604: return true;
605: }
606: }
607: } catch (Exception e) {
608: }
609:
610: return false;
611: }
612:
613: /*
614: * Returns text attached to the <param-value> tag node, paired with the
615: * <param-name> tag node that contains text "ConfFile"
616: * (all nodes are in servlet tag node named enhydra).
617: */
618: public String getConfFilePath() {
619: //inside enhydra servlet, find tag named <init-param>,
620: //and there find <param-name> tag that contains text "ConfFile"
621: //his <param-value> tag contains text to return from function
622: NodeList servletChilds = this .enhydraServlet.getChildNodes();
623: for (int i = 0; i < servletChilds.getLength(); i++) {
624: if (servletChilds.item(i).getNodeName().equalsIgnoreCase(
625: "init-param")) {
626: Node paramValueNode = this
627: .getConfFileValueNode(servletChilds.item(i));
628: if (paramValueNode != null) {
629: String value = this .getNodeText(paramValueNode);
630: if (!value.equalsIgnoreCase(""))
631: return value;
632: else
633: break;
634: }
635: }
636: }
637: return null;
638: }
639:
640: /*
641: * Sets text attached to the <param-value> tag node, paired with the
642: * <param-name> tag node that contains text "ConfFile", to the given string
643: * (all nodes are in servlet tag node named enhydra).
644: */
645: public void setConfFilePath(String filePath) {
646: NodeList servletChilds = this .enhydraServlet.getChildNodes();
647: for (int i = 0; i < servletChilds.getLength(); i++) {
648: if (servletChilds.item(i).getNodeName().equalsIgnoreCase(
649: "init-param")) {
650: Node paramValueNode = this
651: .getConfFileValueNode(servletChilds.item(i));
652: if (paramValueNode != null) {
653: this .setNodeText(paramValueNode, filePath);
654: return;
655: }
656: }
657: }
658:
659: System.err.println("Cannot set ConfFilePath!");
660: }
661:
662: /*
663: * Returns node that represents <param-value> node that is paired
664: * with the <param-name> tag node with the value "ConfFile".
665: */
666: private Node getConfFileValueNode(Node initParamNode) {
667: NodeList params = initParamNode.getChildNodes();
668: for (int i = 0; i < params.getLength(); i++) {
669: if (params.item(i).getNodeName().equalsIgnoreCase(
670: "param-name")) {
671: if (!this .getNodeText(params.item(i)).equalsIgnoreCase(
672: EnhydraServer.CONF_FILE))
673: break;
674: } else {
675: if (params.item(i).getNodeName().equalsIgnoreCase(
676: "param-value"))
677: return params.item(i);
678: }
679: }
680:
681: return null;
682: }
683:
684: /*
685: * Returns text attached to the <param-value> tag node, paired with the
686: * <param-name> tag node that contains text "ConfFileClass"
687: * (all nodes are in servlet tag node named enhydra).
688: */
689: public String getConfFileClass() { // TJ 08.11.2003.
690: //inside enhydra servlet, find tag named <init-param>,
691: //and there find <param-name> tag that contains text "ConfFileClass"
692: //his <param-value> tag contains text to return from function
693: NodeList servletChilds = this .enhydraServlet.getChildNodes();
694: for (int i = 0; i < servletChilds.getLength(); i++) {
695: if (servletChilds.item(i).getNodeName().equalsIgnoreCase(
696: "init-param")) {
697: Node paramValueNode = this
698: .getConfFileClassValueNode(servletChilds
699: .item(i));
700: if (paramValueNode != null) {
701: String value = this .getNodeText(paramValueNode);
702: if (!value.equalsIgnoreCase(""))
703: return value;
704: else
705: break;
706: }
707: }
708: }
709: return null;
710: }
711:
712: /*
713: * Sets text attached to the <param-value> tag node, paired with the
714: * <param-name> tag node that contains text "ConfFileClass", to the given
715: * string (all nodes are in servlet tag node named enhydra).
716: */
717: public void setConfFileClass(String filePath) { // TJ 08.11.2003.
718: NodeList servletChilds = this .enhydraServlet.getChildNodes();
719: for (int i = 0; i < servletChilds.getLength(); i++) {
720: if (servletChilds.item(i).getNodeName().equalsIgnoreCase(
721: "init-param")) {
722: Node paramValueNode = this
723: .getConfFileClassValueNode(servletChilds
724: .item(i));
725: if (paramValueNode != null) {
726: this .setNodeText(paramValueNode, filePath);
727: return;
728: }
729: }
730: }
731: }
732:
733: /*
734: * Returns node that represents <param-value> node that is paired
735: * with the <param-name> tag node with the value "ConfFileClass".
736: */
737: private Node getConfFileClassValueNode(Node initParamNode) { // TJ 08.11.2003.
738: NodeList params = initParamNode.getChildNodes();
739: for (int i = 0; i < params.getLength(); i++) {
740: if (params.item(i).getNodeName().equalsIgnoreCase(
741: "param-name")) {
742: if (!this .getNodeText(params.item(i)).equalsIgnoreCase(
743: EnhydraServer.CONF_FILE_CLASS))
744: break;
745: } else {
746: if (params.item(i).getNodeName().equalsIgnoreCase(
747: "param-value"))
748: return params.item(i);
749: }
750: }
751:
752: return null;
753: }
754:
755: /*
756: * Gets the string attached to the child node.
757: */
758: private String getNodeText(Node node) {
759: String text = "";
760: StringBuffer textBuffer = new StringBuffer(text);
761: NodeList childs = node.getChildNodes();
762: for (int i = 0; i < childs.getLength(); i++) {
763: if (childs.item(i).getNodeType() == Node.TEXT_NODE)
764: textBuffer.append(childs.item(i).getNodeValue());
765: }
766: text = textBuffer.toString();
767: text = text.trim();
768: return text;
769: }
770:
771: /*
772: * Sets the string attached to the child node.
773: */
774: private void setNodeText(Node node, String newText) {
775: String text = "";
776: NodeList childs = node.getChildNodes();
777:
778: for (int i = 0; i < childs.getLength(); i++) {
779: text = childs.item(i).getNodeValue();
780: text = text.replaceAll("\n", "").replaceAll("\r", "")
781: .replaceAll("\t", "").replaceAll(" ", "");
782: if (!text.equals("")) {
783: childs.item(i).setNodeValue(newText);
784: break;
785: }
786: }
787: }
788:
789: /*
790: * Returns servlet tag node with the "servlet-name" tag value = "enhydra",
791: * null otherwise.
792: */
793: private Node getEnhydraServletNode() {
794: // NodeList servlets = this.document.getElementsByTagName("servlet");
795: // NodeList servletNames = this.document.getElementsByTagName("servlet-name");
796: // System.out.println("servletNames count = " + servletNames.getLength());
797: // for(int i = 0; i < servletNames.getLength(); i++) {
798: // System.out.println("servletNames[" + i + "]" + servletNames.item(i) + " value = " + servletNames.item(i).getNodeValue());
799: // NodeList childs = servletNames.item(i).getChildNodes();
800: // for(int k = 0; k < childs.getLength(); k++) {
801: // System.out.println("childs[" + k + "]" + childs.item(i));
802: // }
803: // System.out.println("parent = " + servletNames.item(i).getParentNode());
804: // }
805: //
806: NodeList servlets = this .document.getDocumentElement()
807: .getChildNodes();
808: for (int i = 0; i < servlets.getLength(); i++) {
809: try {
810: //if it is servlet node
811: if (servlets.item(i).getNodeName().equalsIgnoreCase(
812: "servlet")) {
813: //get all childs
814: NodeList childs = servlets.item(i).getChildNodes();
815: for (int k = 0; k < childs.getLength(); k++) {
816: //if child is servlet-name tag
817: if (childs.item(k).getNodeName()
818: .equalsIgnoreCase("servlet-name")) {
819: //get his childs (that is text node with the servlet name)
820: // NodeList c = childs.item(k).getChildNodes();
821: // for(int m = 0; m < c.getLength(); m++) {
822: // if(c.item(m).getNodeValue().trim().equalsIgnoreCase("enhydra"))
823: // return servlets.item(i);
824: // }
825: if (this .getNodeText(childs.item(k))
826: .equalsIgnoreCase("enhydra"))
827: return servlets.item(i);
828: }
829: }
830: }
831: } catch (Exception e) {
832: }
833: }
834:
835: System.err
836: .println("Servlet tag with the name \"enhydra\" cannot be found !");
837: return null;
838: }
839:
840: public static void main(String[] args) {
841: try {
842: WebAppXML test = new WebAppXML("C:/Temp/web.xml");
843:
844: System.out.println("isEnhydraApplication = "
845: + test.isEnhydraApplication());
846:
847: test.addFilter("MY_FILTER_1", "MY_FILTER_CLASS", "iconsS",
848: "iconL", null, "###", new String[] { "p1", "p2",
849: "p3" }, new String[] { "v1", "v2", "v3" },
850: null, null, "MY_SERVLET");
851:
852: test.saveDocument();
853:
854: test.addFilter("MY_FILTER_2", "MY_FILTER_CLASS2", null,
855: null, "DISPLAY_NAME2", null, new String[] { "p1",
856: "p2" }, new String[] { "v1", "v2" },
857: new String[] { "d1", "d2" }, "MY_URL", null);
858:
859: test.saveDocument();
860:
861: //test.removeFilter("MY_FILTER");
862: System.out
863: .println("--------- Before reloadDocument()-----------");
864: String[] names = test.getFilterNames();
865: for (int i = 0; i < names.length; i++) {
866: System.out.println("str[" + i + "] = " + names[i]);
867: }
868: System.out
869: .println("--------- After reloadDocument()-----------");
870: test.reloadDocument();
871: names = test.getFilterNames();
872: for (int i = 0; i < names.length; i++) {
873: System.out.println("str[" + i + "] = " + names[i]);
874: }
875:
876: HashMap hash = test.getFilterParameters("MY_FILTER_1");
877: System.out.println("hash: " + hash);
878:
879: hash = test.getFilterParameters("MY_FILTER_2");
880: System.out.println("hash: " + hash);
881:
882: // test.removeFilter("MY_FILTER_1");
883:
884: // test.removeFilter("MY_FILTER_2");
885:
886: test.saveDocument();
887:
888: System.out.println("getConfFilePath() = "
889: + test.getConfFilePath());
890:
891: test.setConfFilePath("MyConfFilePath");
892:
893: test.saveDocument();
894:
895: System.out.println("Happy end");
896: } catch (Exception e) {
897: System.out.println("NOOOOOOOOOO");
898: e.printStackTrace();
899: }
900: }
901: }
|