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 ServletImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , Servlet { 51 52 private Icon icon; 53 private ServletName servletName; 54 private DisplayName displayName; 55 private Description description; 56 private ServletClass servletClass; 57 private JspFile jspFile; 58 private List initParamList; 59 private LoadOnStartup loadOnStartup; 60 private RunAs runAs; 61 private List securityRoleRefList; 62 private String id; 63 private boolean zeus_IdSet; 64 65 66 private String docTypeString; 67 68 69 private String outputEncoding; 70 71 72 private Unmarshallable zeus_currentUNode; 73 74 75 private Unmarshallable zeus_parentUNode; 76 77 78 private boolean zeus_thisNodeHandled = false; 79 80 81 private boolean hasDTD; 82 83 84 private boolean validate; 85 86 87 private Map namespaceMappings; 88 89 90 private static EntityResolver entityResolver; 91 92 93 private static ErrorHandler errorHandler; 94 95 private static ServletImpl prototype = null; 96 97 public static void setPrototype(ServletImpl prototype) { 98 ServletImpl.prototype = prototype; 99 } 100 public static ServletImpl newInstance() { 101 try { 102 return (prototype!=null)?(ServletImpl)prototype.clone(): new ServletImpl(); 103 } catch (CloneNotSupportedException e) { 104 return null; } 106 } 107 public ServletImpl() { 108 initParamList = new LinkedList (); 109 securityRoleRefList = new LinkedList (); 110 zeus_IdSet = false; 111 docTypeString = ""; 112 hasDTD = false; 113 validate = false; 114 namespaceMappings = new HashMap (); 115 } 116 117 public Icon getIcon() { 118 return icon; 119 } 120 121 public void setIcon(Icon icon) { 122 this.icon = icon; 123 } 124 125 public ServletName getServletName() { 126 return servletName; 127 } 128 129 public void setServletName(ServletName servletName) { 130 this.servletName = servletName; 131 } 132 133 public DisplayName getDisplayName() { 134 return displayName; 135 } 136 137 public void setDisplayName(DisplayName displayName) { 138 this.displayName = displayName; 139 } 140 141 public Description getDescription() { 142 return description; 143 } 144 145 public void setDescription(Description description) { 146 this.description = description; 147 } 148 149 public ServletClass getServletClass() { 150 return servletClass; 151 } 152 153 public void setServletClass(ServletClass servletClass) { 154 this.servletClass = servletClass; 155 } 156 157 public JspFile getJspFile() { 158 return jspFile; 159 } 160 161 public void setJspFile(JspFile jspFile) { 162 this.jspFile = jspFile; 163 } 164 165 public List getInitParamList() { 166 return initParamList; 167 } 168 169 public void setInitParamList(List initParamList) { 170 this.initParamList = initParamList; 171 } 172 173 public void addInitParam(InitParam initParam) { 174 initParamList.add(initParam); 175 } 176 177 public void removeInitParam(InitParam initParam) { 178 initParamList.remove(initParam); 179 } 180 181 public LoadOnStartup getLoadOnStartup() { 182 return loadOnStartup; 183 } 184 185 public void setLoadOnStartup(LoadOnStartup loadOnStartup) { 186 this.loadOnStartup = loadOnStartup; 187 } 188 189 public RunAs getRunAs() { 190 return runAs; 191 } 192 193 public void setRunAs(RunAs runAs) { 194 this.runAs = runAs; 195 } 196 197 public List getSecurityRoleRefList() { 198 return securityRoleRefList; 199 } 200 201 public void setSecurityRoleRefList(List securityRoleRefList) { 202 this.securityRoleRefList = securityRoleRefList; 203 } 204 205 public void addSecurityRoleRef(SecurityRoleRef securityRoleRef) { 206 securityRoleRefList.add(securityRoleRef); 207 } 208 209 public void removeSecurityRoleRef(SecurityRoleRef securityRoleRef) { 210 securityRoleRefList.remove(securityRoleRef); 211 } 212 213 public String getId() { 214 return id; 215 } 216 217 public void setId(String id) { 218 this.id = id; 219 zeus_IdSet = true; 220 } 221 222 public void setDocType(String name, String publicID, String systemID) { 223 try { 224 startDTD(name, publicID, systemID); 225 } catch (SAXException neverHappens) { } 226 } 227 228 public void setOutputEncoding(String outputEncoding) { 229 this.outputEncoding = outputEncoding; 230 } 231 232 public void marshal(File file) throws IOException { 233 marshal(new FileWriter (file)); 235 } 236 237 public void marshal(OutputStream outputStream) throws IOException { 238 marshal(new OutputStreamWriter (outputStream)); 240 } 241 242 public void marshal(Writer writer) throws IOException { 243 writer.write("<?xml version=\"1.0\" "); 245 if (outputEncoding != null) { 246 writer.write("encoding=\""); 247 writer.write(outputEncoding); 248 writer.write("\"?>\n\n"); 249 250 } else { 251 writer.write("encoding=\"UTF-8\"?>\n\n"); 252 253 } 254 writer.write(docTypeString); 256 writer.write("\n"); 257 writeXMLRepresentation(writer, ""); 259 260 writer.flush(); 262 writer.close(); 263 } 264 265 protected void writeXMLRepresentation(Writer writer, 266 String indent) 267 throws IOException { 268 269 writer.write(indent); 270 writer.write("<servlet"); 271 272 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 274 String prefix = (String )i.next(); 275 String uri = (String )namespaceMappings.get(prefix); 276 writer.write(" xmlns"); 277 if (!prefix.trim().equals("")) { 278 writer.write(":"); 279 writer.write(prefix); 280 } 281 writer.write("=\""); 282 writer.write(uri); 283 writer.write("\"\n "); 284 } 285 286 if (zeus_IdSet) { 288 writer.write(" id=\""); 289 writer.write(escapeAttributeValue(id)); 290 writer.write("\""); 291 } 292 writer.write(">"); 293 writer.write("\n"); 294 295 if (icon != null) { 297 ((IconImpl)icon).writeXMLRepresentation(writer, 298 new StringBuffer (indent).append(" ").toString()); 299 } 300 301 if (servletName != null) { 302 ((ServletNameImpl)servletName).writeXMLRepresentation(writer, 303 new StringBuffer (indent).append(" ").toString()); 304 } 305 306 if (displayName != null) { 307 ((DisplayNameImpl)displayName).writeXMLRepresentation(writer, 308 new StringBuffer (indent).append(" ").toString()); 309 } 310 311 if (description != null) { 312 ((DescriptionImpl)description).writeXMLRepresentation(writer, 313 new StringBuffer (indent).append(" ").toString()); 314 } 315 316 if (servletClass != null) { 317 ((ServletClassImpl)servletClass).writeXMLRepresentation(writer, 318 new StringBuffer (indent).append(" ").toString()); 319 } 320 321 if (jspFile != null) { 322 ((JspFileImpl)jspFile).writeXMLRepresentation(writer, 323 new StringBuffer (indent).append(" ").toString()); 324 } 325 326 for (Iterator i=initParamList.iterator(); i.hasNext(); ) { 327 InitParamImpl initParam = (InitParamImpl)i.next(); 328 initParam.writeXMLRepresentation(writer, 329 new StringBuffer (indent).append(" ").toString()); 330 } 331 if (loadOnStartup != null) { 332 ((LoadOnStartupImpl)loadOnStartup).writeXMLRepresentation(writer, 333 new StringBuffer (indent).append(" ").toString()); 334 } 335 336 if (runAs != null) { 337 ((RunAsImpl)runAs).writeXMLRepresentation(writer, 338 new StringBuffer (indent).append(" ").toString()); 339 } 340 341 for (Iterator i=securityRoleRefList.iterator(); i.hasNext(); ) { 342 SecurityRoleRefImpl securityRoleRef = (SecurityRoleRefImpl)i.next(); 343 securityRoleRef.writeXMLRepresentation(writer, 344 new StringBuffer (indent).append(" ").toString()); 345 } 346 writer.write(indent); 347 writer.write("</servlet>\n"); 348 } 349 350 private String escapeAttributeValue(String attributeValue) { 351 String returnValue = attributeValue; 352 for (int i = 0; i < returnValue.length(); i++) { 353 char ch = returnValue.charAt(i); 354 if (ch == '"') { 355 returnValue = new StringBuffer () 356 .append(returnValue.substring(0, i)) 357 .append(""") 358 .append(returnValue.substring(i+1)) 359 .toString(); 360 } 361 } 362 return returnValue; 363 } 364 365 private String escapeTextValue(String textValue) { 366 String returnValue = textValue; 367 for (int i = 0; i < returnValue.length(); i++) { 368 char ch = returnValue.charAt(i); 369 if (ch == '<') { 370 returnValue = new StringBuffer () 371 .append(returnValue.substring(0, i)) 372 .append("<") 373 .append(returnValue.substring(i+1)) 374 .toString(); 375 } else if (ch == '>') { 376 returnValue = new StringBuffer () 377 .append(returnValue.substring(0, i)) 378 .append(">") 379 .append(returnValue.substring(i+1)) 380 .toString(); 381 } 382 } 383 return returnValue; 384 } 385 386 393 public static void setEntityResolver(EntityResolver resolver) { 394 entityResolver = resolver; 395 } 396 397 404 public static void setErrorHandler(ErrorHandler handler) { 405 errorHandler = handler; 406 } 407 408 public static Servlet unmarshal(File file) throws IOException { 409 return unmarshal(new FileReader (file)); 411 } 412 413 public static Servlet unmarshal(File file, boolean validate) throws IOException { 414 return unmarshal(new FileReader (file), validate); 416 } 417 418 public static Servlet unmarshal(InputStream inputStream) throws IOException { 419 return unmarshal(new InputStreamReader (inputStream)); 421 } 422 423 public static Servlet unmarshal(InputStream inputStream, boolean validate) throws IOException { 424 return unmarshal(new InputStreamReader (inputStream), validate); 426 } 427 428 public static Servlet unmarshal(Reader reader) throws IOException { 429 return unmarshal(reader, false); 431 } 432 433 public static Servlet unmarshal(Reader reader, boolean validate) throws IOException { 434 ServletImpl servlet = ServletImpl.newInstance(); 435 servlet.setValidating(validate); 436 servlet.setCurrentUNode(servlet); 437 servlet.setParentUNode(null); 438 XMLReader parser = null; 440 String parserClass = System.getProperty("org.xml.sax.driver", 441 "org.apache.xerces.parsers.SAXParser"); 442 try { 443 parser = XMLReaderFactory.createXMLReader(parserClass); 444 445 if (entityResolver != null) { 447 parser.setEntityResolver(entityResolver); 448 } 449 450 parser.setErrorHandler(servlet); 452 453 parser.setProperty("http://xml.org/sax/properties/lexical-handler", servlet); 455 456 parser.setContentHandler(servlet); 458 } catch (SAXException e) { 459 throw new IOException ("Could not load XML parser: " + 460 e.getMessage()); 461 } 462 463 InputSource inputSource = new InputSource (reader); 464 try { 465 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 466 parser.setFeature("http://xml.org/sax/features/namespaces", true); 467 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 468 parser.parse(inputSource); 469 } catch (SAXException e) { 470 throw new IOException ("Error parsing XML document: " + 471 e.getMessage()); 472 } 473 474 return servlet; 476 } 477 478 public Unmarshallable getParentUNode() { 479 return zeus_parentUNode; 480 } 481 482 public void setParentUNode(Unmarshallable parentUNode) { 483 this.zeus_parentUNode = parentUNode; 484 } 485 486 public Unmarshallable getCurrentUNode() { 487 return zeus_currentUNode; 488 } 489 490 public void setCurrentUNode(Unmarshallable currentUNode) { 491 this.zeus_currentUNode = currentUNode; 492 } 493 494 public void setValidating(boolean validate) { 495 this.validate = validate; 496 } 497 498 public void startDocument() throws SAXException { 499 } 501 502 public void setDocumentLocator(Locator locator) { 503 } 505 506 public void startPrefixMapping(String prefix, String uri) 507 throws SAXException { 508 namespaceMappings.put(prefix, uri); 509 } 510 511 public void startElement(String namespaceURI, String localName, 512 String qName, org.xml.sax.Attributes atts) 513 throws SAXException { 514 515 Unmarshallable current = getCurrentUNode(); 517 if (current != this) { 518 current.startElement(namespaceURI, localName, qName, atts); 519 return; 520 } 521 522 if ((localName.equals("servlet")) && (!zeus_thisNodeHandled)) { 524 for (int i=0, len=atts.getLength(); i<len; i++) { 526 String attName= atts.getLocalName(i); 527 String attValue = atts.getValue(i); 528 if (attName.equals("id")) { 529 setId(attValue); 530 } 531 } 532 zeus_thisNodeHandled = true; 533 return; 534 } else { 535 if (localName.equals("icon") && (icon==null)) { 537 IconImpl icon = IconImpl.newInstance(); 538 current = getCurrentUNode(); 539 icon.setParentUNode(current); 540 icon.setCurrentUNode(icon); 541 this.setCurrentUNode(icon); 542 icon.startElement(namespaceURI, localName, qName, atts); 543 this.icon = icon; 545 return; 546 } 547 if (localName.equals("servlet-name") && (servletName==null)) { 548 ServletNameImpl servletName = ServletNameImpl.newInstance(); 549 current = getCurrentUNode(); 550 servletName.setParentUNode(current); 551 servletName.setCurrentUNode(servletName); 552 this.setCurrentUNode(servletName); 553 servletName.startElement(namespaceURI, localName, qName, atts); 554 this.servletName = servletName; 556 return; 557 } 558 if (localName.equals("display-name") && (displayName==null)) { 559 DisplayNameImpl displayName = DisplayNameImpl.newInstance(); 560 current = getCurrentUNode(); 561 displayName.setParentUNode(current); 562 displayName.setCurrentUNode(displayName); 563 this.setCurrentUNode(displayName); 564 displayName.startElement(namespaceURI, localName, qName, atts); 565 this.displayName = displayName; 567 return; 568 } 569 if (localName.equals("description") && (description==null)) { 570 DescriptionImpl description = DescriptionImpl.newInstance(); 571 current = getCurrentUNode(); 572 description.setParentUNode(current); 573 description.setCurrentUNode(description); 574 this.setCurrentUNode(description); 575 description.startElement(namespaceURI, localName, qName, atts); 576 this.description = description; 578 return; 579 } 580 if (localName.equals("servlet-class") && (servletClass==null)) { 581 ServletClassImpl servletClass = ServletClassImpl.newInstance(); 582 current = getCurrentUNode(); 583 servletClass.setParentUNode(current); 584 servletClass.setCurrentUNode(servletClass); 585 this.setCurrentUNode(servletClass); 586 servletClass.startElement(namespaceURI, localName, qName, atts); 587 this.servletClass = servletClass; 589 return; 590 } 591 if (localName.equals("jsp-file") && (jspFile==null)) { 592 JspFileImpl jspFile = JspFileImpl.newInstance(); 593 current = getCurrentUNode(); 594 jspFile.setParentUNode(current); 595 jspFile.setCurrentUNode(jspFile); 596 this.setCurrentUNode(jspFile); 597 jspFile.startElement(namespaceURI, localName, qName, atts); 598 this.jspFile = jspFile; 600 return; 601 } 602 if (localName.equals("init-param")) { 603 InitParamImpl initParam = InitParamImpl.newInstance(); 604 current = getCurrentUNode(); 605 initParam.setParentUNode(current); 606 initParam.setCurrentUNode(initParam); 607 this.setCurrentUNode(initParam); 608 initParam.startElement(namespaceURI, localName, qName, atts); 609 initParamList.add(initParam); 611 return; 612 } 613 if (localName.equals("load-on-startup") && (loadOnStartup==null)) { 614 LoadOnStartupImpl loadOnStartup = LoadOnStartupImpl.newInstance(); 615 current = getCurrentUNode(); 616 loadOnStartup.setParentUNode(current); 617 loadOnStartup.setCurrentUNode(loadOnStartup); 618 this.setCurrentUNode(loadOnStartup); 619 loadOnStartup.startElement(namespaceURI, localName, qName, atts); 620 this.loadOnStartup = loadOnStartup; 622 return; 623 } 624 if (localName.equals("run-as") && (runAs==null)) { 625 RunAsImpl runAs = RunAsImpl.newInstance(); 626 current = getCurrentUNode(); 627 runAs.setParentUNode(current); 628 runAs.setCurrentUNode(runAs); 629 this.setCurrentUNode(runAs); 630 runAs.startElement(namespaceURI, localName, qName, atts); 631 this.runAs = runAs; 633 return; 634 } 635 if (localName.equals("security-role-ref")) { 636 SecurityRoleRefImpl securityRoleRef = SecurityRoleRefImpl.newInstance(); 637 current = getCurrentUNode(); 638 securityRoleRef.setParentUNode(current); 639 securityRoleRef.setCurrentUNode(securityRoleRef); 640 this.setCurrentUNode(securityRoleRef); 641 securityRoleRef.startElement(namespaceURI, localName, qName, atts); 642 securityRoleRefList.add(securityRoleRef); 644 return; 645 } 646 } 647 } 648 649 public void endElement(String namespaceURI, String localName, 650 String qName) 651 throws SAXException { 652 653 Unmarshallable current = getCurrentUNode(); 654 if (current != this) { 655 current.endElement(namespaceURI, localName, qName); 656 return; 657 } 658 659 Unmarshallable parent = getCurrentUNode().getParentUNode(); 660 if (parent != null) { 661 parent.setCurrentUNode(parent); 662 } 663 } 664 665 public void characters(char[] ch, int start, int len) 666 throws SAXException { 667 668 Unmarshallable current = getCurrentUNode(); 670 if (current != this) { 671 current.characters(ch, start, len); 672 return; 673 } 674 675 String text = new String (ch, start, len); 676 } 677 678 public void comment(char ch[], int start, int len) throws SAXException { 679 } 681 682 public void warning(SAXParseException e) throws SAXException { 683 if (errorHandler != null) { 684 errorHandler.warning(e); 685 } 686 } 687 688 public void error(SAXParseException e) throws SAXException { 689 if ((validate) && (!hasDTD)) { 690 throw new SAXException ("Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference."); 691 } 692 if (errorHandler != null) { 693 errorHandler.error(e); 694 } 695 } 696 697 public void fatalError(SAXParseException e) throws SAXException { 698 if ((validate) && (!hasDTD)) { 699 throw new SAXException ("Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference."); 700 } 701 if (errorHandler != null) { 702 errorHandler.fatalError(e); 703 } 704 } 705 706 public void startDTD(String name, String publicID, String systemID) 707 throws SAXException { 708 709 if ((name == null) || (name.equals(""))) { 710 docTypeString = ""; 711 return; 712 } 713 714 hasDTD = true; 715 StringBuffer docTypeSB = new StringBuffer (); 716 boolean hasPublic = false; 717 718 docTypeSB.append("<!DOCTYPE ") 719 .append(name); 720 721 if ((publicID != null) && (!publicID.equals(""))) { 722 docTypeSB.append(" PUBLIC \"") 723 .append(publicID) 724 .append("\""); 725 hasPublic = true; 726 } 727 728 if ((systemID != null) && (!systemID.equals(""))) { 729 if (!hasPublic) { 730 docTypeSB.append(" SYSTEM"); 731 } 732 docTypeSB.append(" \"") 733 .append(systemID) 734 .append("\""); 735 736 } 737 738 docTypeSB.append(">"); 739 740 docTypeString = docTypeSB.toString(); 741 } 742 743 public void endDTD() throws SAXException { 744 } 746 747 public void startEntity(String name) throws SAXException { 748 } 750 751 public void endEntity(String name) throws SAXException { 752 } 754 755 public void startCDATA() throws SAXException { 756 } 758 759 public void endCDATA() throws SAXException { 760 } 762 763 } 764 | Popular Tags |