| 1 18 package org.enhydra.convert.xml; 19 20 import java.io.File ; 22 import java.io.FileReader ; 23 import java.io.FileWriter ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.Reader ; 30 import java.io.Writer ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import org.xml.sax.EntityResolver ; 35 import org.xml.sax.ErrorHandler ; 36 import org.xml.sax.InputSource ; 37 import org.xml.sax.Locator ; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.SAXParseException ; 40 import org.xml.sax.XMLReader ; 41 import org.xml.sax.ext.LexicalHandler ; 42 import org.xml.sax.helpers.DefaultHandler ; 43 import org.xml.sax.helpers.XMLReaderFactory ; 44 45 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.LinkedList ; 49 50 public class WebAppImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , WebApp { 51 52 private Icon icon; 53 private DisplayName displayName; 54 private Description description; 55 private Distributable distributable; 56 private List contextParamList; 57 private List filterList; 58 private List filterMappingList; 59 private List listenerList; 60 private List servletList; 61 private List servletMappingList; 62 private SessionConfig sessionConfig; 63 private List mimeMappingList; 64 private WelcomeFileList welcomeFileList; 65 private List errorPageList; 66 private List taglibList; 67 private List resourceEnvRefList; 68 private List resourceRefList; 69 private List securityConstraintList; 70 private LoginConfig loginConfig; 71 private List securityRoleList; 72 private List envEntryList; 73 private List ejbRefList; 74 private List ejbLocalRefList; 75 private String id; 76 private boolean zeus_IdSet; 77 78 79 private String docTypeString; 80 81 82 private String outputEncoding; 83 84 85 private Unmarshallable zeus_currentUNode; 86 87 88 private Unmarshallable zeus_parentUNode; 89 90 91 private boolean zeus_thisNodeHandled = false; 92 93 94 private boolean hasDTD; 95 96 97 private boolean validate; 98 99 100 private Map namespaceMappings; 101 102 103 private static EntityResolver entityResolver; 104 105 106 private static ErrorHandler errorHandler; 107 108 private static WebAppImpl prototype = null; 109 110 public static void setPrototype(WebAppImpl prototype) { 111 WebAppImpl.prototype = prototype; 112 } 113 public static WebAppImpl newInstance() { 114 try { 115 return (prototype!=null)?(WebAppImpl)prototype.clone(): new WebAppImpl(); 116 } catch (CloneNotSupportedException e) { 117 return null; } 119 } 120 public WebAppImpl() { 121 contextParamList = new LinkedList (); 122 filterList = new LinkedList (); 123 filterMappingList = new LinkedList (); 124 listenerList = new LinkedList (); 125 servletList = new LinkedList (); 126 servletMappingList = new LinkedList (); 127 mimeMappingList = new LinkedList (); 128 errorPageList = new LinkedList (); 129 taglibList = new LinkedList (); 130 resourceEnvRefList = new LinkedList (); 131 resourceRefList = new LinkedList (); 132 securityConstraintList = new LinkedList (); 133 securityRoleList = new LinkedList (); 134 envEntryList = new LinkedList (); 135 ejbRefList = new LinkedList (); 136 ejbLocalRefList = new LinkedList (); 137 zeus_IdSet = false; 138 docTypeString = ""; 139 hasDTD = false; 140 validate = false; 141 namespaceMappings = new HashMap (); 142 } 143 144 public Icon getIcon() { 145 return icon; 146 } 147 148 public void setIcon(Icon icon) { 149 this.icon = icon; 150 } 151 152 public DisplayName getDisplayName() { 153 return displayName; 154 } 155 156 public void setDisplayName(DisplayName displayName) { 157 this.displayName = displayName; 158 } 159 160 public Description getDescription() { 161 return description; 162 } 163 164 public void setDescription(Description description) { 165 this.description = description; 166 } 167 168 public Distributable getDistributable() { 169 return distributable; 170 } 171 172 public void setDistributable(Distributable distributable) { 173 this.distributable = distributable; 174 } 175 176 public List getContextParamList() { 177 return contextParamList; 178 } 179 180 public void setContextParamList(List contextParamList) { 181 this.contextParamList = contextParamList; 182 } 183 184 public void addContextParam(ContextParam contextParam) { 185 contextParamList.add(contextParam); 186 } 187 188 public void removeContextParam(ContextParam contextParam) { 189 contextParamList.remove(contextParam); 190 } 191 192 public List getFilterList() { 193 return filterList; 194 } 195 196 public void setFilterList(List filterList) { 197 this.filterList = filterList; 198 } 199 200 public void addFilter(Filter filter) { 201 filterList.add(filter); 202 } 203 204 public void removeFilter(Filter filter) { 205 filterList.remove(filter); 206 } 207 208 public List getFilterMappingList() { 209 return filterMappingList; 210 } 211 212 public void setFilterMappingList(List filterMappingList) { 213 this.filterMappingList = filterMappingList; 214 } 215 216 public void addFilterMapping(FilterMapping filterMapping) { 217 filterMappingList.add(filterMapping); 218 } 219 220 public void removeFilterMapping(FilterMapping filterMapping) { 221 filterMappingList.remove(filterMapping); 222 } 223 224 public List getListenerList() { 225 return listenerList; 226 } 227 228 public void setListenerList(List listenerList) { 229 this.listenerList = listenerList; 230 } 231 232 public void addListener(Listener listener) { 233 listenerList.add(listener); 234 } 235 236 public void removeListener(Listener listener) { 237 listenerList.remove(listener); 238 } 239 240 public List getServletList() { 241 return servletList; 242 } 243 244 public void setServletList(List servletList) { 245 this.servletList = servletList; 246 } 247 248 public void addServlet(Servlet servlet) { 249 servletList.add(servlet); 250 } 251 252 public void removeServlet(Servlet servlet) { 253 servletList.remove(servlet); 254 } 255 256 public List getServletMappingList() { 257 return servletMappingList; 258 } 259 260 public void setServletMappingList(List servletMappingList) { 261 this.servletMappingList = servletMappingList; 262 } 263 264 public void addServletMapping(ServletMapping servletMapping) { 265 servletMappingList.add(servletMapping); 266 } 267 268 public void removeServletMapping(ServletMapping servletMapping) { 269 servletMappingList.remove(servletMapping); 270 } 271 272 public SessionConfig getSessionConfig() { 273 return sessionConfig; 274 } 275 276 public void setSessionConfig(SessionConfig sessionConfig) { 277 this.sessionConfig = sessionConfig; 278 } 279 280 public List getMimeMappingList() { 281 return mimeMappingList; 282 } 283 284 public void setMimeMappingList(List mimeMappingList) { 285 this.mimeMappingList = mimeMappingList; 286 } 287 288 public void addMimeMapping(MimeMapping mimeMapping) { 289 mimeMappingList.add(mimeMapping); 290 } 291 292 public void removeMimeMapping(MimeMapping mimeMapping) { 293 mimeMappingList.remove(mimeMapping); 294 } 295 296 public WelcomeFileList getWelcomeFileList() { 297 return welcomeFileList; 298 } 299 300 public void setWelcomeFileList(WelcomeFileList welcomeFileList) { 301 this.welcomeFileList = welcomeFileList; 302 } 303 304 public List getErrorPageList() { 305 return errorPageList; 306 } 307 308 public void setErrorPageList(List errorPageList) { 309 this.errorPageList = errorPageList; 310 } 311 312 public void addErrorPage(ErrorPage errorPage) { 313 errorPageList.add(errorPage); 314 } 315 316 public void removeErrorPage(ErrorPage errorPage) { 317 errorPageList.remove(errorPage); 318 } 319 320 public List getTaglibList() { 321 return taglibList; 322 } 323 324 public void setTaglibList(List taglibList) { 325 this.taglibList = taglibList; 326 } 327 328 public void addTaglib(Taglib taglib) { 329 taglibList.add(taglib); 330 } 331 332 public void removeTaglib(Taglib taglib) { 333 taglibList.remove(taglib); 334 } 335 336 public List getResourceEnvRefList() { 337 return resourceEnvRefList; 338 } 339 340 public void setResourceEnvRefList(List resourceEnvRefList) { 341 this.resourceEnvRefList = resourceEnvRefList; 342 } 343 344 public void addResourceEnvRef(ResourceEnvRef resourceEnvRef) { 345 resourceEnvRefList.add(resourceEnvRef); 346 } 347 348 public void removeResourceEnvRef(ResourceEnvRef resourceEnvRef) { 349 resourceEnvRefList.remove(resourceEnvRef); 350 } 351 352 public List getResourceRefList() { 353 return resourceRefList; 354 } 355 356 public void setResourceRefList(List resourceRefList) { 357 this.resourceRefList = resourceRefList; 358 } 359 360 public void addResourceRef(ResourceRef resourceRef) { 361 resourceRefList.add(resourceRef); 362 } 363 364 public void removeResourceRef(ResourceRef resourceRef) { 365 resourceRefList.remove(resourceRef); 366 } 367 368 public List getSecurityConstraintList() { 369 return securityConstraintList; 370 } 371 372 public void setSecurityConstraintList(List securityConstraintList) { 373 this.securityConstraintList = securityConstraintList; 374 } 375 376 public void addSecurityConstraint(SecurityConstraint securityConstraint) { 377 securityConstraintList.add(securityConstraint); 378 } 379 380 public void removeSecurityConstraint(SecurityConstraint securityConstraint) { 381 securityConstraintList.remove(securityConstraint); 382 } 383 384 public LoginConfig getLoginConfig() { 385 return loginConfig; 386 } 387 388 public void setLoginConfig(LoginConfig loginConfig) { 389 this.loginConfig = loginConfig; 390 } 391 392 public List getSecurityRoleList() { 393 return securityRoleList; 394 } 395 396 public void setSecurityRoleList(List securityRoleList) { 397 this.securityRoleList = securityRoleList; 398 } 399 400 public void addSecurityRole(SecurityRole securityRole) { 401 securityRoleList.add(securityRole); 402 } 403 404 public void removeSecurityRole(SecurityRole securityRole) { 405 securityRoleList.remove(securityRole); 406 } 407 408 public List getEnvEntryList() { 409 return envEntryList; 410 } 411 412 public void setEnvEntryList(List envEntryList) { 413 this.envEntryList = envEntryList; 414 } 415 416 public void addEnvEntry(EnvEntry envEntry) { 417 envEntryList.add(envEntry); 418 } 419 420 public void removeEnvEntry(EnvEntry envEntry) { 421 envEntryList.remove(envEntry); 422 } 423 424 public List getEjbRefList() { 425 return ejbRefList; 426 } 427 428 public void setEjbRefList(List ejbRefList) { 429 this.ejbRefList = ejbRefList; 430 } 431 432 public void addEjbRef(EjbRef ejbRef) { 433 ejbRefList.add(ejbRef); 434 } 435 436 public void removeEjbRef(EjbRef ejbRef) { 437 ejbRefList.remove(ejbRef); 438 } 439 440 public List getEjbLocalRefList() { 441 return ejbLocalRefList; 442 } 443 444 public void setEjbLocalRefList(List ejbLocalRefList) { 445 this.ejbLocalRefList = ejbLocalRefList; 446 } 447 448 public void addEjbLocalRef(EjbLocalRef ejbLocalRef) { 449 ejbLocalRefList.add(ejbLocalRef); 450 } 451 452 public void removeEjbLocalRef(EjbLocalRef ejbLocalRef) { 453 ejbLocalRefList.remove(ejbLocalRef); 454 } 455 456 public String getId() { 457 return id; 458 } 459 460 public void setId(String id) { 461 this.id = id; 462 zeus_IdSet = true; 463 } 464 465 public void setDocType(String name, String publicID, String systemID) { 466 try { 467 startDTD(name, publicID, systemID); 468 } catch (SAXException neverHappens) { } 469 } 470 471 public void setOutputEncoding(String outputEncoding) { 472 this.outputEncoding = outputEncoding; 473 } 474 475 public void marshal(File file) throws IOException { 476 marshal(new FileWriter (file)); 478 } 479 480 public void marshal(OutputStream outputStream) throws IOException { 481 marshal(new OutputStreamWriter (outputStream)); 483 } 484 485 public void marshal(Writer writer) throws IOException { 486 writer.write("<?xml version=\"1.0\" "); 488 if (outputEncoding != null) { 489 writer.write("encoding=\""); 490 writer.write(outputEncoding); 491 writer.write("\"?>\n\n"); 492 493 } else { 494 writer.write("encoding=\"UTF-8\"?>\n\n"); 495 496 } 497 writer.write(docTypeString); 499 writer.write("\n"); 500 writeXMLRepresentation(writer, ""); 502 503 writer.flush(); 505 writer.close(); 506 } 507 508 protected void writeXMLRepresentation(Writer writer, 509 String indent) 510 throws IOException { 511 512 writer.write(indent); 513 writer.write("<web-app"); 514 515 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 517 String prefix = (String )i.next(); 518 String uri = (String )namespaceMappings.get(prefix); 519 writer.write(" xmlns"); 520 if (!prefix.trim().equals("")) { 521 writer.write(":"); 522 writer.write(prefix); 523 } 524 writer.write("=\""); 525 writer.write(uri); 526 writer.write("\"\n "); 527 } 528 529 if (zeus_IdSet) { 531 writer.write(" id=\""); 532 writer.write(escapeAttributeValue(id)); 533 writer.write("\""); 534 } 535 writer.write(">"); 536 writer.write("\n"); 537 538 if (icon != null) { 540 ((IconImpl)icon).writeXMLRepresentation(writer, 541 new StringBuffer (indent).append(" ").toString()); 542 } 543 544 if (displayName != null) { 545 ((DisplayNameImpl)displayName).writeXMLRepresentation(writer, 546 new StringBuffer (indent).append(" ").toString()); 547 } 548 549 if (description != null) { 550 ((DescriptionImpl)description).writeXMLRepresentation(writer, 551 new StringBuffer (indent).append(" ").toString()); 552 } 553 554 if (distributable != null) { 555 ((DistributableImpl)distributable).writeXMLRepresentation(writer, 556 new StringBuffer (indent).append(" ").toString()); 557 } 558 559 for (Iterator i=contextParamList.iterator(); i.hasNext(); ) { 560 ContextParamImpl contextParam = (ContextParamImpl)i.next(); 561 contextParam.writeXMLRepresentation(writer, 562 new StringBuffer (indent).append(" ").toString()); 563 } 564 for (Iterator i=filterList.iterator(); i.hasNext(); ) { 565 FilterImpl filter = (FilterImpl)i.next(); 566 filter.writeXMLRepresentation(writer, 567 new StringBuffer (indent).append(" ").toString()); 568 } 569 for (Iterator i=filterMappingList.iterator(); i.hasNext(); ) { 570 FilterMappingImpl filterMapping = (FilterMappingImpl)i.next(); 571 filterMapping.writeXMLRepresentation(writer, 572 new StringBuffer (indent).append(" ").toString()); 573 } 574 for (Iterator i=listenerList.iterator(); i.hasNext(); ) { 575 ListenerImpl listener = (ListenerImpl)i.next(); 576 listener.writeXMLRepresentation(writer, 577 new StringBuffer (indent).append(" ").toString()); 578 } 579 for (Iterator i=servletList.iterator(); i.hasNext(); ) { 580 ServletImpl servlet = (ServletImpl)i.next(); 581 servlet.writeXMLRepresentation(writer, 582 new StringBuffer (indent).append(" ").toString()); 583 } 584 for (Iterator i=servletMappingList.iterator(); i.hasNext(); ) { 585 ServletMappingImpl servletMapping = (ServletMappingImpl)i.next(); 586 servletMapping.writeXMLRepresentation(writer, 587 new StringBuffer (indent).append(" ").toString()); 588 } 589 if (sessionConfig != null) { 590 ((SessionConfigImpl)sessionConfig).writeXMLRepresentation(writer, 591 new StringBuffer (indent).append(" ").toString()); 592 } 593 594 for (Iterator i=mimeMappingList.iterator(); i.hasNext(); ) { 595 MimeMappingImpl mimeMapping = (MimeMappingImpl)i.next(); 596 mimeMapping.writeXMLRepresentation(writer, 597 new StringBuffer (indent).append(" ").toString()); 598 } 599 if (welcomeFileList != null) { 600 ((WelcomeFileListImpl)welcomeFileList).writeXMLRepresentation(writer, 601 new StringBuffer (indent).append(" ").toString()); 602 } 603 604 for (Iterator i=errorPageList.iterator(); i.hasNext(); ) { 605 ErrorPageImpl errorPage = (ErrorPageImpl)i.next(); 606 errorPage.writeXMLRepresentation(writer, 607 new StringBuffer (indent).append(" ").toString()); 608 } 609 for (Iterator i=taglibList.iterator(); i.hasNext(); ) { 610 TaglibImpl taglib = (TaglibImpl)i.next(); 611 taglib.writeXMLRepresentation(writer, 612 new StringBuffer (indent).append(" ").toString()); 613 } 614 for (Iterator i=resourceEnvRefList.iterator(); i.hasNext(); ) { 615 ResourceEnvRefImpl resourceEnvRef = (ResourceEnvRefImpl)i.next(); 616 resourceEnvRef.writeXMLRepresentation(writer, 617 new StringBuffer (indent).append(" ").toString()); 618 } 619 for (Iterator i=resourceRefList.iterator(); i.hasNext(); ) { 620 ResourceRefImpl resourceRef = (ResourceRefImpl)i.next(); 621 resourceRef.writeXMLRepresentation(writer, 622 new StringBuffer (indent).append(" ").toString()); 623 } 624 for (Iterator i=securityConstraintList.iterator(); i.hasNext(); ) { 625 SecurityConstraintImpl securityConstraint = (SecurityConstraintImpl)i.next(); 626 securityConstraint.writeXMLRepresentation(writer, 627 new StringBuffer (indent).append(" ").toString()); 628 } 629 if (loginConfig != null) { 630 ((LoginConfigImpl)loginConfig).writeXMLRepresentation(writer, 631 new StringBuffer (indent).append(" ").toString()); 632 } 633 634 for (Iterator i=securityRoleList.iterator(); i.hasNext(); ) { 635 SecurityRoleImpl securityRole = (SecurityRoleImpl)i.next(); 636 securityRole.writeXMLRepresentation(writer, 637 new StringBuffer (indent).append(" ").toString()); 638 } 639 for (Iterator i=envEntryList.iterator(); i.hasNext(); ) { 640 EnvEntryImpl envEntry = (EnvEntryImpl)i.next(); 641 envEntry.writeXMLRepresentation(writer, 642 new StringBuffer (indent).append(" ").toString()); 643 } 644 for (Iterator i=ejbRefList.iterator(); i.hasNext(); ) { 645 EjbRefImpl ejbRef = (EjbRefImpl)i.next(); 646 ejbRef.writeXMLRepresentation(writer, 647 new StringBuffer (indent).append(" ").toString()); 648 } 649 for (Iterator i=ejbLocalRefList.iterator(); i.hasNext(); ) { 650 EjbLocalRefImpl ejbLocalRef = (EjbLocalRefImpl)i.next(); 651 ejbLocalRef.writeXMLRepresentation(writer, 652 new StringBuffer (indent).append(" ").toString()); 653 } 654 writer.write(indent); 655 writer.write("</web-app>\n"); 656 } 657 658 private String escapeAttributeValue(String attributeValue) { 659 String returnValue = attributeValue; 660 for (int i = 0; i < returnValue.length(); i++) { 661 char ch = returnValue.charAt(i); 662 if (ch == '"') { 663 returnValue = new StringBuffer () 664 .append(returnValue.substring(0, i)) 665 .append(""") 666 .append(returnValue.substring(i+1)) 667 .toString(); 668 } 669 } 670 return returnValue; 671 } 672 673 private String escapeTextValue(String textValue) { 674 String returnValue = textValue; 675 for (int i = 0; i < returnValue.length(); i++) { 676 char ch = returnValue.charAt(i); 677 if (ch == '<') { 678 returnValue = new StringBuffer () 679 .append(returnValue.substring(0, i)) 680 .append("<") 681 .append(returnValue.substring(i+1)) 682 .toString(); 683 } else if (ch == '>') { 684 returnValue = new StringBuffer () 685 .append(returnValue.substring(0, i)) 686 .append(">") 687 .append(returnValue.substring(i+1)) 688 .toString(); 689 } 690 } 691 return returnValue; 692 } 693 694 701 public static void setEntityResolver(EntityResolver resolver) { 702 entityResolver = resolver; 703 } 704 705 712 public static void setErrorHandler(ErrorHandler handler) { 713 errorHandler = handler; 714 } 715 716 public static WebApp unmarshal(File file) throws IOException { 717 return unmarshal(new FileReader (file)); 719 } 720 721 public static WebApp unmarshal(File file, boolean validate) throws IOException { 722 return unmarshal(new FileReader (file), validate); 724 } 725 726 public static WebApp unmarshal(InputStream inputStream) throws IOException { 727 return unmarshal(new InputStreamReader (inputStream)); 729 } 730 731 public static WebApp unmarshal(InputStream inputStream, boolean validate) throws IOException { 732 return unmarshal(new InputStreamReader (inputStream), validate); 734 } 735 736 public static WebApp unmarshal(Reader reader) throws IOException { 737 return unmarshal(reader, false); 739 } 740 741 public |