1 package org.enhydra.server.util; 2 3 import java.io.FileNotFoundException ; 4 import java.io.FileOutputStream ; 5 import java.io.IOException ; 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 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 ; 16 import org.w3c.dom.Element ; 17 import org.w3c.dom.Node ; 18 import org.w3c.dom.NodeList ; 19 import org.xml.sax.SAXException ; 20 21 24 public class WebAppXML { 25 26 public Document document; 28 29 DOMParser parser; 31 32 private String xmlFileName; 34 35 private Node enhydraServlet; 37 38 private Element root; 40 41 private String lineSep; 43 44 private final String [] tagsTillFilter = new String [] {"filter", "context-param", "distributable", "description", "display-name", "icon"}; 46 private final String [] tagsTillFilterMapping = new String [] {"filter-mapping", "filter", "context-param", "distributable", "description", "display-name", "icon"}; 47 48 49 50 private WebAppXML() { 51 } 52 53 public WebAppXML(String fileName) { 54 this.xmlFileName = fileName; 55 reloadDocument(); 56 } 57 58 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 e) { 78 e.printStackTrace(); 79 System.err.println("SAXException - bad xml format!"); 80 } catch (IOException e) { 81 e.printStackTrace(); 82 } 83 } 84 85 86 90 String determineLineSeparator() { 91 92 String 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 return LineSeparator.Web; 100 } 101 102 103 104 107 public void saveDocument() { 108 109 try { 110 111 FileOutputStream os = new FileOutputStream (this.xmlFileName); 112 OutputFormat of = new OutputFormat(); 113 114 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 e) { 124 e.printStackTrace(); 125 } catch (IOException e) { 126 e.printStackTrace(); 127 } 128 } 129 130 131 132 133 134 135 136 137 158 public void addFilter( 159 160 String filterName, String filterClass, 161 String smallIcon, String largeIcon, String displayName, String description, 162 String [] paramNames, String [] paramValues, String [] descriptions, 163 164 String mappingUrl, String mappingServlet){ 165 166 Node newLine = this.document.createTextNode(this.lineSep); 167 Node newLinePlusTab = this.document.createTextNode(this.lineSep + "\t"); 168 Node newLinePlusTwoTabs = this.document.createTextNode(this.lineSep + "\t\t"); 169 Node newLinePlusThreeTabs = this.document.createTextNode(this.lineSep + "\t\t\t"); 170 Node textNode = this.document.createTextNode(""); 171 172 Node filter = this.document.createElement("filter"); 174 175 Node 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 Node 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 Node 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 paramNameNode = this.document.createElement("param-name"); 193 Node 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 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 Node iconNode = null; 215 if(smallIcon != null || largeIcon != null){ 216 iconNode = this.document.createElement("icon"); 217 if(smallIcon != null) { 218 Node 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 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 Node 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 Node 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 mapping = null; 259 if( !(mappingUrl == null && mappingServlet == null) ) { 260 mapping = this.document.createElement("filter-mapping"); 262 263 Node mappingNameNode = this.document.createElement("filter-name"); 265 mappingNameNode.appendChild(this.document.createTextNode(filterName)); 266 267 Node mappingUrlPatternNode = null; 269 Node 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 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 Node 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 316 private Node findReferentNodeForAddition(String [] tags) { 317 for(int i = 0; i < tags.length; i++) { 318 NodeList 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 332 public void removeFilter(String filterName) { 333 Node filter = this.findFilter(filterName); 334 if(filter != null) { 335 Node text = filter.getNextSibling(); 336 if(text != null && text.getNodeType() == Node.TEXT_NODE) 337 this.root.removeChild(text); 338 this.root.removeChild(filter); 342 343 Node filterMapping = this.findFilterMapping(filterName); 344 if(filterMapping != null) { 345 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 369 public String [] getFilterNames(){ 370 ArrayList array = new ArrayList (); 371 NodeList possibleFilters = this.root.getChildNodes(); 372 for(int i = 0; i < possibleFilters.getLength(); i++) { 373 if(possibleFilters.item(i).getNodeName().equals("filter")){ 374 NodeList names = ((Element )possibleFilters.item(i)).getElementsByTagName("filter-name"); 375 array.add(this.getNodeText(names.item(0))); 376 } 377 } 378 379 String [] names = new String [array.size()]; 380 for(int i = 0; i < array.size(); i++) 381 names[i] = (String )array.get(i); 382 return names; 383 } 384 385 389 public HashMap getFilterParameters(String filterName){ 390 HashMap hash = new HashMap (); 391 Node filterNode = this.findFilter(filterName); 392 if(filterNode != null){ 393 NodeList filterClasses = ((Element )filterNode).getElementsByTagName("filter-class"); 395 hash.put("filter-class", this.getNodeText(filterClasses.item(0))); 396 397 NodeList initParams = ((Element )filterNode).getElementsByTagName("init-param"); 399 if(initParams != null && initParams.getLength() != 0){ 400 ArrayList initArray = new ArrayList (); 401 for(int i = 0; i < initParams.getLength(); i++){ 402 NodeList names = ((Element )initParams.item(i)).getElementsByTagName("param-name"); 403 NodeList values = ((Element )initParams.item(i)).getElementsByTagName("param-value"); 404 NodeList descripts = ((Element )initParams.item(i)).getElementsByTagName("description"); 405 ArrayList parameter = new ArrayList (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 NodeList icons = ((Element )filterNode).getElementsByTagName("icon"); 421 if(icons != null && icons.getLength() != 0){ 422 NodeList ic = ((Element )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 )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 NodeList displayNames = ((Element )filterNode).getElementsByTagName("display-name"); 432 if(displayNames != null && displayNames.getLength() != 0) 433 hash.put("display-name", this.getNodeText(displayNames.item(0))); 434 435 NodeList descriptions = ((Element )filterNode).getElementsByTagName("description"); 437 if(descriptions != null && descriptions.getLength() != 0){ 438 for(int i = 0; i < descriptions.getLength(); i++){ 439 if(descriptions.item(i).getParentNode().equals(filterNode)) 441 hash.put("description", this.getNodeText(descriptions.item(i))); 442 } 443 } 444 445 Node filterMappingNode = this.findFilterMapping(filterName); 447 if(filterMappingNode != null){ 448 NodeList urlPatterns = ((Element )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 servlets = ((Element )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 464 private Node findFilter(String filterName) { 465 NodeList possibleFilters = this.root.getChildNodes(); 466 for(int i = 0; i < possibleFilters.getLength(); i++) { 467 if(possibleFilters.item(i).getNodeName().equalsIgnoreCase("filter")) { 468 NodeList filterChilds = possibleFilters.item(i).getChildNodes(); 469 for(int k = 0; k < filterChilds.getLength(); k++) { 470 if(filterChilds.item(k).getNodeName().equalsIgnoreCase("filter-name")) { 471 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 492 private Node findFilterMapping(String filterName) { 493 NodeList possibleFilters = this.root.getChildNodes(); 494 for(int i = 0; i < possibleFilters.getLength(); i++) { 495 if(possibleFilters.item(i).getNodeName().equalsIgnoreCase("filter-mapping")) { 496 NodeList 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 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 e) { 505 506 } 507 } 508 } 509 } 510 } 511 } 512 System.err.println("Cannot find filter mapping named \"" + filterName + "\""); 513 return null; 514 } 515 516 517 518 522 public boolean isEnhydraApplication() { 523 String org = "org.enhydra.Servlet"; 524 String lutris = "com.lutris.appserver.server.httpPresentation.servlet.HttpPresentationServlet"; 525 526 try { 527 NodeList servletChilds = this.enhydraServlet.getChildNodes(); 528 for(int i = 0; i < servletChilds.getLength(); i++) { 529 if(servletChilds.item(i).getNodeName().equalsIgnoreCase("servlet-class")) { 530 String className = this.getNodeText(servletChilds.item(i)); 539 if(className.equals(org) || className.equals(lutris)) 540 return true; 541 } 542 } 543 } catch(Exception e) { 544 } 545 546 return false; 547 } 548 549 550 555 public String getConfFilePath() { 556 NodeList servletChilds = this.enhydraServlet.getChildNodes(); 560 for(int i = 0; i < servletChilds.getLength(); i++) { 561 if(servletChilds.item(i).getNodeName().equalsIgnoreCase("init-param")) { 562 Node paramValueNode = this.getConfFileValueNode(servletChilds.item(i)); 563 if(paramValueNode != null) { 564 String 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 582 public void setConfFilePath(String filePath) { 583 NodeList servletChilds = this.enhydraServlet.getChildNodes(); 584 for(int i = 0; i < servletChilds.getLength(); i++) { 585 if(servletChilds.item(i).getNodeName().equalsIgnoreCase("init-param")) { 586 Node 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 602 private Node getConfFileValueNode(Node initParamNode) { 603 NodeList 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 623 public String getConfFileClass() { NodeList servletChilds = this.enhydraServlet.getChildNodes(); 628 for(int i = 0; i < servletChilds.getLength(); i++) { 629 if(servletChilds.item(i).getNodeName().equalsIgnoreCase("init-param")) { 630 Node paramValueNode = this.getConfFileClassValueNode(servletChilds.item(i)); 631 if(paramValueNode != null) { 632 String value = this.getNodeText(paramValueNode); 633 if(!value.equalsIgnoreCase("")) 634 return value; 635 else break; 636 } 637 } 638 } 639 return null; 640 } 641 642 643 648 public void setConfFileClass(String filePath) { NodeList servletChilds = this.enhydraServlet.getChildNodes(); 650 for(int i = 0; i < servletChilds.getLength(); i++) { 651 if(servletChilds.item(i).getNodeName().equalsIgnoreCase("init-param")) { 652 Node paramValueNode = this.getConfFileClassValueNode(servletChilds.item(i)); 653 if(paramValueNode != null) { 654 this.setNodeText(paramValueNode, filePath); 655 return; 656 } 657 } 658 } 659 } 660 661 662 666 private Node getConfFileClassValueNode(Node initParamNode) { NodeList 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 685 private String getNodeText(Node node) { 686 String text = ""; 687 NodeList 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 699 private void setNodeText(Node node, String newText) { 700 String text = ""; 701 NodeList 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 721 private Node getEnhydraServletNode() { 722 NodeList servlets = this.document.getDocumentElement().getChildNodes(); 735 for(int i = 0; i < servlets.getLength(); i++) { 736 try { 737 if(servlets.item(i).getNodeName().equalsIgnoreCase("servlet")) { 739 NodeList childs = servlets.item(i).getChildNodes(); 741 for(int k = 0; k < childs.getLength(); k++) { 742 if(childs.item(k).getNodeName().equalsIgnoreCase("servlet-name")) { 744 if(this.getNodeText(childs.item(k)).equalsIgnoreCase("enhydra")) 751 return servlets.item(i); 752 } 753 } 754 } 755 } catch(Exception 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 [] 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 []{"p1", "p2", "p3"}, new String []{"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 []{"p1", "p2"}, new String []{"v1", "v2"}, 780 new String []{"d1", "d2"}, "MY_URL", null); 781 782 test.saveDocument(); 783 784 System.out.println("--------- Before reloadDocument()-----------"); 786 String [] 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 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 805 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 e) { 818 System.out.println("NOOOOOOOOOO"); 819 e.printStackTrace(); 820 } 821 } 822 } 823 824 | Popular Tags |