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 public class FilterImpl extends DefaultHandler implements Unmarshallable, LexicalHandler , Filter { 46 47 private Call call; 48 private Lookup lookup; 49 private Markup markup; 50 private Switch xmlswitch; 51 private Constant constant; 52 private Date date; 53 private Count count; 54 private String name; 55 private boolean zeus_NameSet; 56 57 58 private String docTypeString; 59 60 61 private String outputEncoding; 62 63 64 private Unmarshallable zeus_currentUNode; 65 66 67 private Unmarshallable zeus_parentUNode; 68 69 70 private boolean zeus_thisNodeHandled = false; 71 72 73 private boolean hasDTD; 74 75 76 private boolean validate; 77 78 79 private Map namespaceMappings; 80 81 82 private static EntityResolver entityResolver; 83 84 85 private static ErrorHandler errorHandler; 86 87 public FilterImpl() { 88 zeus_NameSet = false; 89 docTypeString = ""; 90 hasDTD = false; 91 validate = false; 92 namespaceMappings = new HashMap (); 93 } 94 95 public Call getCall() { 96 return call; 97 } 98 99 public void setCall(Call call) { 100 this.call = call; 101 } 102 103 public Lookup getLookup() { 104 return lookup; 105 } 106 107 public void setLookup(Lookup lookup) { 108 this.lookup = lookup; 109 } 110 111 public Markup getMarkup() { 112 return markup; 113 } 114 115 public void setMarkup(Markup markup) { 116 this.markup = markup; 117 } 118 119 public Switch getXmlswitch() { 120 return xmlswitch; 121 } 122 123 public void setXmlswitch(Switch xmlswitch) { 124 this.xmlswitch = xmlswitch; 125 } 126 127 public Constant getConstant() { 128 return constant; 129 } 130 131 public void setConstant(Constant constant) { 132 this.constant = constant; 133 } 134 135 public Date getDate() { 136 return date; 137 } 138 139 public void setDate(Date date) { 140 this.date = date; 141 } 142 143 public Count getCount() { 144 return count; 145 } 146 147 public void setCount(Count count) { 148 this.count = count; 149 } 150 151 public String getName() { 152 return name; 153 } 154 155 public void setName(String name) { 156 this.name = name; 157 zeus_NameSet = true; 158 } 159 160 public void setDocType(String name, String publicID, String systemID) { 161 try { 162 startDTD(name, publicID, systemID); 163 } catch (SAXException neverHappens) { } 164 } 165 166 public void setOutputEncoding(String outputEncoding) { 167 this.outputEncoding = outputEncoding; 168 } 169 170 public void marshal(File file) throws IOException { 171 marshal(new FileWriter (file)); 173 } 174 175 public void marshal(OutputStream outputStream) throws IOException { 176 marshal(new OutputStreamWriter (outputStream)); 178 } 179 180 public void marshal(Writer writer) throws IOException { 181 writer.write("<?xml version=\"1.0\" "); 183 if (outputEncoding != null) { 184 writer.write("encoding=\""); 185 writer.write(outputEncoding); 186 writer.write("\"?>\n\n"); 187 188 } else { 189 writer.write("encoding=\"UTF-8\"?>\n\n"); 190 191 } 192 writer.write(docTypeString); 194 writer.write("\n"); 195 writeXMLRepresentation(writer, ""); 197 198 writer.flush(); 200 writer.close(); 201 } 202 203 protected void writeXMLRepresentation(Writer writer, 204 String indent) 205 throws IOException { 206 207 writer.write(indent); 208 writer.write("<filter"); 209 210 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 212 String prefix = (String )i.next(); 213 String uri = (String )namespaceMappings.get(prefix); 214 writer.write(" xmlns"); 215 if (!prefix.trim().equals("")) { 216 writer.write(":"); 217 writer.write(prefix); 218 } 219 writer.write("=\""); 220 writer.write(uri); 221 writer.write("\"\n "); 222 } 223 224 if (zeus_NameSet) { 226 writer.write(" name=\""); 227 writer.write(escapeAttributeValue(name)); 228 writer.write("\""); 229 } 230 writer.write(">"); 231 writer.write("\n"); 232 233 if (call != null) { 235 ((CallImpl)call).writeXMLRepresentation(writer, 236 new StringBuffer (indent).append(" ").toString()); 237 } 238 239 if (lookup != null) { 240 ((LookupImpl)lookup).writeXMLRepresentation(writer, 241 new StringBuffer (indent).append(" ").toString()); 242 } 243 244 if (markup != null) { 245 ((MarkupImpl)markup).writeXMLRepresentation(writer, 246 new StringBuffer (indent).append(" ").toString()); 247 } 248 249 if (xmlswitch != null) { 250 ((SwitchImpl)xmlswitch).writeXMLRepresentation(writer, 251 new StringBuffer (indent).append(" ").toString()); 252 } 253 254 if (constant != null) { 255 ((ConstantImpl)constant).writeXMLRepresentation(writer, 256 new StringBuffer (indent).append(" ").toString()); 257 } 258 259 if (date != null) { 260 ((DateImpl)date).writeXMLRepresentation(writer, 261 new StringBuffer (indent).append(" ").toString()); 262 } 263 264 if (count != null) { 265 ((CountImpl)count).writeXMLRepresentation(writer, 266 new StringBuffer (indent).append(" ").toString()); 267 } 268 269 writer.write(indent); 270 writer.write("</filter>\n"); 271 } 272 273 private String escapeAttributeValue(String attributeValue) { 274 String returnValue = attributeValue; 275 for (int i = 0; i < returnValue.length(); i++) { 276 char ch = returnValue.charAt(i); 277 if (ch == '"') { 278 returnValue = new StringBuffer () 279 .append(returnValue.substring(0, i)) 280 .append(""") 281 .append(returnValue.substring(i+1)) 282 .toString(); 283 } 284 } 285 return returnValue; 286 } 287 288 295 public static void setEntityResolver(EntityResolver resolver) { 296 entityResolver = resolver; 297 } 298 299 306 public static void setErrorHandler(ErrorHandler handler) { 307 errorHandler = handler; 308 } 309 310 public static Filter unmarshal(File file) throws IOException { 311 return unmarshal(new FileReader (file)); 313 } 314 315 public static Filter unmarshal(File file, boolean validate) throws IOException { 316 return unmarshal(new FileReader (file), validate); 318 } 319 320 public static Filter unmarshal(InputStream inputStream) throws IOException { 321 return unmarshal(new InputStreamReader (inputStream)); 323 } 324 325 public static Filter unmarshal(InputStream inputStream, boolean validate) throws IOException { 326 return unmarshal(new InputStreamReader (inputStream), validate); 328 } 329 330 public static Filter unmarshal(Reader reader) throws IOException { 331 return unmarshal(reader, false); 333 } 334 335 public static Filter unmarshal(Reader reader, boolean validate) throws IOException { 336 FilterImpl filter = new FilterImpl(); 337 filter.setValidating(validate); 338 filter.setCurrentUNode(filter); 339 filter.setParentUNode(null); 340 XMLReader parser = null; 342 String parserClass = System.getProperty("org.xml.sax.driver", 343 "org.apache.xerces.parsers.SAXParser"); 344 try { 345 parser = XMLReaderFactory.createXMLReader(parserClass); 346 347 if (entityResolver != null) { 349 parser.setEntityResolver(entityResolver); 350 } 351 352 parser.setErrorHandler(filter); 354 355 parser.setProperty("http://xml.org/sax/properties/lexical-handler", filter); 357 358 parser.setContentHandler(filter); 360 } catch (SAXException e) { 361 throw new IOException ("Could not load XML parser: " + 362 e.getMessage()); 363 } 364 365 InputSource inputSource = new InputSource (reader); 366 try { 367 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 368 parser.setFeature("http://xml.org/sax/features/namespaces", true); 369 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 370 parser.parse(inputSource); 371 } catch (SAXException e) { 372 throw new IOException ("Error parsing XML document: " + 373 e.getMessage()); 374 } 375 376 return filter; 378 } 379 380 public Unmarshallable getParentUNode() { 381 return zeus_parentUNode; 382 } 383 384 public void setParentUNode(Unmarshallable parentUNode) { 385 this.zeus_parentUNode = parentUNode; 386 } 387 388 public Unmarshallable getCurrentUNode() { 389 return zeus_currentUNode; 390 } 391 392 public void setCurrentUNode(Unmarshallable currentUNode) { 393 this.zeus_currentUNode = currentUNode; 394 } 395 396 public void setValidating(boolean validate) { 397 this.validate = validate; 398 } 399 400 public void startDocument() throws SAXException { 401 } 403 404 public void setDocumentLocator(Locator locator) { 405 } 407 408 public void startPrefixMapping(String prefix, String uri) 409 throws SAXException { 410 namespaceMappings.put(prefix, uri); 411 } 412 413 public void startElement(String namespaceURI, String localName, 414 String qName, org.xml.sax.Attributes atts) 415 throws SAXException { 416 417 Unmarshallable current = getCurrentUNode(); 419 if (current != this) { 420 current.startElement(namespaceURI, localName, qName, atts); 421 return; 422 } 423 424 if ((localName.equals("filter")) && (!zeus_thisNodeHandled)) { 426 for (int i=0, len=atts.getLength(); i<len; i++) { 428 String attName= atts.getLocalName(i); 429 String attValue = atts.getValue(i); 430 if (attName.equals("name")) { 431 setName(attValue); 432 } 433 } 434 zeus_thisNodeHandled = true; 435 return; 436 } else { 437 if (localName.equals("call")) { 439 CallImpl call = new CallImpl(); 440 current = getCurrentUNode(); 441 call.setParentUNode(current); 442 call.setCurrentUNode(call); 443 this.setCurrentUNode(call); 444 call.startElement(namespaceURI, localName, qName, atts); 445 this.call = call; 447 return; 448 } 449 if (localName.equals("lookup")) { 450 LookupImpl lookup = new LookupImpl(); 451 current = getCurrentUNode(); 452 lookup.setParentUNode(current); 453 lookup.setCurrentUNode(lookup); 454 this.setCurrentUNode(lookup); 455 lookup.startElement(namespaceURI, localName, qName, atts); 456 this.lookup = lookup; 458 return; 459 } 460 if (localName.equals("markup")) { 461 MarkupImpl markup = new MarkupImpl(); 462 current = getCurrentUNode(); 463 markup.setParentUNode(current); 464 markup.setCurrentUNode(markup); 465 this.setCurrentUNode(markup); 466 markup.startElement(namespaceURI, localName, qName, atts); 467 this.markup = markup; 469 return; 470 } 471 if (localName.equals("switch")) { 472 SwitchImpl xmlswitch = new SwitchImpl(); 473 current = getCurrentUNode(); 474 xmlswitch.setParentUNode(current); 475 xmlswitch.setCurrentUNode(xmlswitch); 476 this.setCurrentUNode(xmlswitch); 477 xmlswitch.startElement(namespaceURI, localName, qName, atts); 478 this.xmlswitch = xmlswitch; 480 return; 481 } 482 if (localName.equals("constant")) { 483 ConstantImpl constant = new ConstantImpl(); 484 current = getCurrentUNode(); 485 constant.setParentUNode(current); 486 constant.setCurrentUNode(constant); 487 this.setCurrentUNode(constant); 488 constant.startElement(namespaceURI, localName, qName, atts); 489 this.constant = constant; 491 return; 492 } 493 if (localName.equals("date")) { 494 DateImpl date = new DateImpl(); 495 current = getCurrentUNode(); 496 date.setParentUNode(current); 497 date.setCurrentUNode(date); 498 this.setCurrentUNode(date); 499 date.startElement(namespaceURI, localName, qName, atts); 500 this.date = date; 502 return; 503 } 504 if (localName.equals("count")) { 505 CountImpl count = new CountImpl(); 506 current = getCurrentUNode(); 507 count.setParentUNode(current); 508 count.setCurrentUNode(count); 509 this.setCurrentUNode(count); 510 count.startElement(namespaceURI, localName, qName, atts); 511 this.count = count; 513 return; 514 } 515 } 516 } 517 518 public void endElement(String namespaceURI, String localName, 519 String qName) 520 throws SAXException { 521 522 Unmarshallable current = getCurrentUNode(); 523 if (current != this) { 524 current.endElement(namespaceURI, localName, qName); 525 return; 526 } 527 528 Unmarshallable parent = getCurrentUNode().getParentUNode(); 529 if (parent != null) { 530 parent.setCurrentUNode(parent); 531 } 532 } 533 534 public void characters(char[] ch, int start, int len) 535 throws SAXException { 536 537 Unmarshallable current = getCurrentUNode(); 539 if (current != this) { 540 current.characters(ch, start, len); 541 return; 542 } 543 544 String text = new String (ch, start, len); 545 text = text.trim(); 546 } 547 548 public void comment(char ch[], int start, int len) throws SAXException { 549 } 551 552 public void warning(SAXParseException e) throws SAXException { 553 if (errorHandler != null) { 554 errorHandler.warning(e); 555 } 556 } 557 558 public void error(SAXParseException e) throws SAXException { 559 if ((validate) && (!hasDTD)) { 560 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."); 561 } 562 if (errorHandler != null) { 563 errorHandler.error(e); 564 } 565 } 566 567 public void fatalError(SAXParseException e) throws SAXException { 568 if ((validate) && (!hasDTD)) { 569 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."); 570 } 571 if (errorHandler != null) { 572 errorHandler.fatalError(e); 573 } 574 } 575 576 public void startDTD(String name, String publicID, String systemID) 577 throws SAXException { 578 579 if ((name == null) || (name.equals(""))) { 580 docTypeString = ""; 581 return; 582 } 583 584 hasDTD = true; 585 StringBuffer docTypeSB = new StringBuffer (); 586 boolean hasPublic = false; 587 588 docTypeSB.append("<!DOCTYPE ") 589 .append(name); 590 591 if ((publicID != null) && (!publicID.equals(""))) { 592 docTypeSB.append(" PUBLIC \"") 593 .append(publicID) 594 .append("\""); 595 hasPublic = true; 596 } 597 598 if ((systemID != null) && (!systemID.equals(""))) { 599 if (!hasPublic) { 600 docTypeSB.append(" SYSTEM"); 601 } 602 docTypeSB.append(" \"") 603 .append(systemID) 604 .append("\""); 605 606 } 607 608 docTypeSB.append(">"); 609 610 docTypeString = docTypeSB.toString(); 611 } 612 613 public void endDTD() throws SAXException { 614 } 616 617 public void startEntity(String name) throws SAXException { 618 } 620 621 public void endEntity(String name) throws SAXException { 622 } 624 625 public void startCDATA() throws SAXException { 626 } 628 629 public void endCDATA() throws SAXException { 630 } 632 633 } 634 | Popular Tags |