1 18 package org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd; 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 SwitchImpl extends DefaultHandler implements Unmarshallable, LexicalHandler , Switch { 51 52 private List callList; 53 private List lookupList; 54 private List markupList; 55 private List xmlswitchList; 56 private List constantList; 57 private List dateList; 58 private List countList; 59 60 61 private String docTypeString; 62 63 64 private String outputEncoding; 65 66 67 private Unmarshallable zeus_currentUNode; 68 69 70 private Unmarshallable zeus_parentUNode; 71 72 73 private boolean zeus_thisNodeHandled = false; 74 75 76 private boolean hasDTD; 77 78 79 private boolean validate; 80 81 82 private Map namespaceMappings; 83 84 85 private static EntityResolver entityResolver; 86 87 88 private static ErrorHandler errorHandler; 89 90 public SwitchImpl() { 91 callList = new LinkedList (); 92 lookupList = new LinkedList (); 93 markupList = new LinkedList (); 94 xmlswitchList = new LinkedList (); 95 constantList = new LinkedList (); 96 dateList = new LinkedList (); 97 countList = new LinkedList (); 98 docTypeString = ""; 99 hasDTD = false; 100 validate = false; 101 namespaceMappings = new HashMap (); 102 } 103 104 public List getCallList() { 105 return callList; 106 } 107 108 public void setCallList(List callList) { 109 this.callList = callList; 110 } 111 112 public void addCall(Call call) { 113 callList.add(call); 114 } 115 116 public void removeCall(Call call) { 117 callList.remove(call); 118 } 119 120 public List getLookupList() { 121 return lookupList; 122 } 123 124 public void setLookupList(List lookupList) { 125 this.lookupList = lookupList; 126 } 127 128 public void addLookup(Lookup lookup) { 129 lookupList.add(lookup); 130 } 131 132 public void removeLookup(Lookup lookup) { 133 lookupList.remove(lookup); 134 } 135 136 public List getMarkupList() { 137 return markupList; 138 } 139 140 public void setMarkupList(List markupList) { 141 this.markupList = markupList; 142 } 143 144 public void addMarkup(Markup markup) { 145 markupList.add(markup); 146 } 147 148 public void removeMarkup(Markup markup) { 149 markupList.remove(markup); 150 } 151 152 public List getXmlswitchList() { 153 return xmlswitchList; 154 } 155 156 public void setXmlswitchList(List xmlswitchList) { 157 this.xmlswitchList = xmlswitchList; 158 } 159 160 public void addXmlswitch(Switch xmlswitch) { 161 xmlswitchList.add(xmlswitch); 162 } 163 164 public void removeXmlswitch(Switch xmlswitch) { 165 xmlswitchList.remove(xmlswitch); 166 } 167 168 public List getConstantList() { 169 return constantList; 170 } 171 172 public void setConstantList(List constantList) { 173 this.constantList = constantList; 174 } 175 176 public void addConstant(Constant constant) { 177 constantList.add(constant); 178 } 179 180 public void removeConstant(Constant constant) { 181 constantList.remove(constant); 182 } 183 184 public List getDateList() { 185 return dateList; 186 } 187 188 public void setDateList(List dateList) { 189 this.dateList = dateList; 190 } 191 192 public void addDate(Date date) { 193 dateList.add(date); 194 } 195 196 public void removeDate(Date date) { 197 dateList.remove(date); 198 } 199 200 public List getCountList() { 201 return countList; 202 } 203 204 public void setCountList(List countList) { 205 this.countList = countList; 206 } 207 208 public void addCount(Count count) { 209 countList.add(count); 210 } 211 212 public void removeCount(Count count) { 213 countList.remove(count); 214 } 215 216 public void setDocType(String name, String publicID, String systemID) { 217 try { 218 startDTD(name, publicID, systemID); 219 } catch (SAXException neverHappens) { } 220 } 221 222 public void setOutputEncoding(String outputEncoding) { 223 this.outputEncoding = outputEncoding; 224 } 225 226 public void marshal(File file) throws IOException { 227 marshal(new FileWriter (file)); 229 } 230 231 public void marshal(OutputStream outputStream) throws IOException { 232 marshal(new OutputStreamWriter (outputStream)); 234 } 235 236 public void marshal(Writer writer) throws IOException { 237 writer.write("<?xml version=\"1.0\" "); 239 if (outputEncoding != null) { 240 writer.write("encoding=\""); 241 writer.write(outputEncoding); 242 writer.write("\"?>\n\n"); 243 244 } else { 245 writer.write("encoding=\"UTF-8\"?>\n\n"); 246 247 } 248 writer.write(docTypeString); 250 writer.write("\n"); 251 writeXMLRepresentation(writer, ""); 253 254 writer.flush(); 256 writer.close(); 257 } 258 259 protected void writeXMLRepresentation(Writer writer, 260 String indent) 261 throws IOException { 262 263 writer.write(indent); 264 writer.write("<switch"); 265 266 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 268 String prefix = (String )i.next(); 269 String uri = (String )namespaceMappings.get(prefix); 270 writer.write(" xmlns"); 271 if (!prefix.trim().equals("")) { 272 writer.write(":"); 273 writer.write(prefix); 274 } 275 writer.write("=\""); 276 writer.write(uri); 277 writer.write("\"\n "); 278 } 279 280 writer.write(">"); 282 writer.write("\n"); 283 284 for (Iterator i=callList.iterator(); i.hasNext(); ) { 286 CallImpl call = (CallImpl)i.next(); 287 call.writeXMLRepresentation(writer, 288 new StringBuffer (indent).append(" ").toString()); 289 } 290 for (Iterator i=lookupList.iterator(); i.hasNext(); ) { 291 LookupImpl lookup = (LookupImpl)i.next(); 292 lookup.writeXMLRepresentation(writer, 293 new StringBuffer (indent).append(" ").toString()); 294 } 295 for (Iterator i=markupList.iterator(); i.hasNext(); ) { 296 MarkupImpl markup = (MarkupImpl)i.next(); 297 markup.writeXMLRepresentation(writer, 298 new StringBuffer (indent).append(" ").toString()); 299 } 300 for (Iterator i=xmlswitchList.iterator(); i.hasNext(); ) { 301 SwitchImpl xmlswitch = (SwitchImpl)i.next(); 302 xmlswitch.writeXMLRepresentation(writer, 303 new StringBuffer (indent).append(" ").toString()); 304 } 305 for (Iterator i=constantList.iterator(); i.hasNext(); ) { 306 ConstantImpl constant = (ConstantImpl)i.next(); 307 constant.writeXMLRepresentation(writer, 308 new StringBuffer (indent).append(" ").toString()); 309 } 310 for (Iterator i=dateList.iterator(); i.hasNext(); ) { 311 DateImpl date = (DateImpl)i.next(); 312 date.writeXMLRepresentation(writer, 313 new StringBuffer (indent).append(" ").toString()); 314 } 315 for (Iterator i=countList.iterator(); i.hasNext(); ) { 316 CountImpl count = (CountImpl)i.next(); 317 count.writeXMLRepresentation(writer, 318 new StringBuffer (indent).append(" ").toString()); 319 } 320 writer.write(indent); 321 writer.write("</switch>\n"); 322 } 323 324 private String escapeAttributeValue(String attributeValue) { 325 String returnValue = attributeValue; 326 for (int i = 0; i < returnValue.length(); i++) { 327 char ch = returnValue.charAt(i); 328 if (ch == '"') { 329 returnValue = new StringBuffer () 330 .append(returnValue.substring(0, i)) 331 .append(""") 332 .append(returnValue.substring(i+1)) 333 .toString(); 334 } 335 } 336 return returnValue; 337 } 338 339 346 public static void setEntityResolver(EntityResolver resolver) { 347 entityResolver = resolver; 348 } 349 350 357 public static void setErrorHandler(ErrorHandler handler) { 358 errorHandler = handler; 359 } 360 361 public static Switch unmarshal(File file) throws IOException { 362 return unmarshal(new FileReader (file)); 364 } 365 366 public static Switch unmarshal(File file, boolean validate) throws IOException { 367 return unmarshal(new FileReader (file), validate); 369 } 370 371 public static Switch unmarshal(InputStream inputStream) throws IOException { 372 return unmarshal(new InputStreamReader (inputStream)); 374 } 375 376 public static Switch unmarshal(InputStream inputStream, boolean validate) throws IOException { 377 return unmarshal(new InputStreamReader (inputStream), validate); 379 } 380 381 public static Switch unmarshal(Reader reader) throws IOException { 382 return unmarshal(reader, false); 384 } 385 386 public static Switch unmarshal(Reader reader, boolean validate) throws IOException { 387 SwitchImpl xmlswitch = new SwitchImpl(); 388 xmlswitch.setValidating(validate); 389 xmlswitch.setCurrentUNode(xmlswitch); 390 xmlswitch.setParentUNode(null); 391 XMLReader parser = null; 393 String parserClass = System.getProperty("org.xml.sax.driver", 394 "org.apache.xerces.parsers.SAXParser"); 395 try { 396 parser = XMLReaderFactory.createXMLReader(parserClass); 397 398 if (entityResolver != null) { 400 parser.setEntityResolver(entityResolver); 401 } 402 403 parser.setErrorHandler(xmlswitch); 405 406 parser.setProperty("http://xml.org/sax/properties/lexical-handler", xmlswitch); 408 409 parser.setContentHandler(xmlswitch); 411 } catch (SAXException e) { 412 throw new IOException ("Could not load XML parser: " + 413 e.getMessage()); 414 } 415 416 InputSource inputSource = new InputSource (reader); 417 try { 418 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 419 parser.setFeature("http://xml.org/sax/features/namespaces", true); 420 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 421 parser.parse(inputSource); 422 } catch (SAXException e) { 423 throw new IOException ("Error parsing XML document: " + 424 e.getMessage()); 425 } 426 427 return xmlswitch; 429 } 430 431 public Unmarshallable getParentUNode() { 432 return zeus_parentUNode; 433 } 434 435 public void setParentUNode(Unmarshallable parentUNode) { 436 this.zeus_parentUNode = parentUNode; 437 } 438 439 public Unmarshallable getCurrentUNode() { 440 return zeus_currentUNode; 441 } 442 443 public void setCurrentUNode(Unmarshallable currentUNode) { 444 this.zeus_currentUNode = currentUNode; 445 } 446 447 public void setValidating(boolean validate) { 448 this.validate = validate; 449 } 450 451 public void startDocument() throws SAXException { 452 } 454 455 public void setDocumentLocator(Locator locator) { 456 } 458 459 public void startPrefixMapping(String prefix, String uri) 460 throws SAXException { 461 namespaceMappings.put(prefix, uri); 462 } 463 464 public void startElement(String namespaceURI, String localName, 465 String qName, org.xml.sax.Attributes atts) 466 throws SAXException { 467 468 Unmarshallable current = getCurrentUNode(); 470 if (current != this) { 471 current.startElement(namespaceURI, localName, qName, atts); 472 return; 473 } 474 475 if ((localName.equals("switch")) && (!zeus_thisNodeHandled)) { 477 for (int i=0, len=atts.getLength(); i<len; i++) { 479 String attName= atts.getLocalName(i); 480 String attValue = atts.getValue(i); 481 } 482 zeus_thisNodeHandled = true; 483 return; 484 } else { 485 if (localName.equals("call")) { 487 CallImpl call = new CallImpl(); 488 current = getCurrentUNode(); 489 call.setParentUNode(current); 490 call.setCurrentUNode(call); 491 this.setCurrentUNode(call); 492 call.startElement(namespaceURI, localName, qName, atts); 493 callList.add(call); 495 return; 496 } 497 if (localName.equals("lookup")) { 498 LookupImpl lookup = new LookupImpl(); 499 current = getCurrentUNode(); 500 lookup.setParentUNode(current); 501 lookup.setCurrentUNode(lookup); 502 this.setCurrentUNode(lookup); 503 lookup.startElement(namespaceURI, localName, qName, atts); 504 lookupList.add(lookup); 506 return; 507 } 508 if (localName.equals("markup")) { 509 MarkupImpl markup = new MarkupImpl(); 510 current = getCurrentUNode(); 511 markup.setParentUNode(current); 512 markup.setCurrentUNode(markup); 513 this.setCurrentUNode(markup); 514 markup.startElement(namespaceURI, localName, qName, atts); 515 markupList.add(markup); 517 return; 518 } 519 if (localName.equals("switch")) { 520 SwitchImpl xmlswitch = new SwitchImpl(); 521 current = getCurrentUNode(); 522 xmlswitch.setParentUNode(current); 523 xmlswitch.setCurrentUNode(xmlswitch); 524 this.setCurrentUNode(xmlswitch); 525 xmlswitch.startElement(namespaceURI, localName, qName, atts); 526 xmlswitchList.add(xmlswitch); 528 return; 529 } 530 if (localName.equals("constant")) { 531 ConstantImpl constant = new ConstantImpl(); 532 current = getCurrentUNode(); 533 constant.setParentUNode(current); 534 constant.setCurrentUNode(constant); 535 this.setCurrentUNode(constant); 536 constant.startElement(namespaceURI, localName, qName, atts); 537 constantList.add(constant); 539 return; 540 } 541 if (localName.equals("date")) { 542 DateImpl date = new DateImpl(); 543 current = getCurrentUNode(); 544 date.setParentUNode(current); 545 date.setCurrentUNode(date); 546 this.setCurrentUNode(date); 547 date.startElement(namespaceURI, localName, qName, atts); 548 dateList.add(date); 550 return; 551 } 552 if (localName.equals("count")) { 553 CountImpl count = new CountImpl(); 554 current = getCurrentUNode(); 555 count.setParentUNode(current); 556 count.setCurrentUNode(count); 557 this.setCurrentUNode(count); 558 count.startElement(namespaceURI, localName, qName, atts); 559 countList.add(count); 561 return; 562 } 563 } 564 } 565 566 public void endElement(String namespaceURI, String localName, 567 String qName) 568 throws SAXException { 569 570 Unmarshallable current = getCurrentUNode(); 571 if (current != this) { 572 current.endElement(namespaceURI, localName, qName); 573 return; 574 } 575 576 Unmarshallable parent = getCurrentUNode().getParentUNode(); 577 if (parent != null) { 578 parent.setCurrentUNode(parent); 579 } 580 } 581 582 public void characters(char[] ch, int start, int len) 583 throws SAXException { 584 585 Unmarshallable current = getCurrentUNode(); 587 if (current != this) { 588 current.characters(ch, start, len); 589 return; 590 } 591 592 String text = new String (ch, start, len); 593 text = text.trim(); 594 } 595 596 public void comment(char ch[], int start, int len) throws SAXException { 597 } 599 600 public void warning(SAXParseException e) throws SAXException { 601 if (errorHandler != null) { 602 errorHandler.warning(e); 603 } 604 } 605 606 public void error(SAXParseException e) throws SAXException { 607 if ((validate) && (!hasDTD)) { 608 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."); 609 } 610 if (errorHandler != null) { 611 errorHandler.error(e); 612 } 613 } 614 615 public void fatalError(SAXParseException e) throws SAXException { 616 if ((validate) && (!hasDTD)) { 617 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."); 618 } 619 if (errorHandler != null) { 620 errorHandler.fatalError(e); 621 } 622 } 623 624 public void startDTD(String name, String publicID, String systemID) 625 throws SAXException { 626 627 if ((name == null) || (name.equals(""))) { 628 docTypeString = ""; 629 return; 630 } 631 632 hasDTD = true; 633 StringBuffer docTypeSB = new StringBuffer (); 634 boolean hasPublic = false; 635 636 docTypeSB.append("<!DOCTYPE ") 637 .append(name); 638 639 if ((publicID != null) && (!publicID.equals(""))) { 640 docTypeSB.append(" PUBLIC \"") 641 .append(publicID) 642 .append("\""); 643 hasPublic = true; 644 } 645 646 if ((systemID != null) && (!systemID.equals(""))) { 647 if (!hasPublic) { 648 docTypeSB.append(" SYSTEM"); 649 } 650 docTypeSB.append(" \"") 651 .append(systemID) 652 .append("\""); 653 654 } 655 656 docTypeSB.append(">"); 657 658 docTypeString = docTypeSB.toString(); 659 } 660 661 public void endDTD() throws SAXException { 662 } 664 665 public void startEntity(String name) throws SAXException { 666 } 668 669 public void endEntity(String name) throws SAXException { 670 } 672 673 public void startCDATA() throws SAXException { 674 } 676 677 public void endCDATA() throws SAXException { 678 } 680 681 } 682 | Popular Tags |