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 FiltermapImpl extends DefaultHandler implements Unmarshallable, LexicalHandler , Filtermap { 51 52 private List remapList; 53 private List removeList; 54 private List copyList; 55 private List filterList; 56 private String name; 57 private boolean zeus_NameSet; 58 private String xmlextends; 59 private boolean zeus_XmlextendsSet; 60 61 62 private String docTypeString; 63 64 65 private String outputEncoding; 66 67 68 private Unmarshallable zeus_currentUNode; 69 70 71 private Unmarshallable zeus_parentUNode; 72 73 74 private boolean zeus_thisNodeHandled = false; 75 76 77 private boolean hasDTD; 78 79 80 private boolean validate; 81 82 83 private Map namespaceMappings; 84 85 86 private static EntityResolver entityResolver; 87 88 89 private static ErrorHandler errorHandler; 90 91 public FiltermapImpl() { 92 remapList = new LinkedList (); 93 removeList = new LinkedList (); 94 copyList = new LinkedList (); 95 filterList = new LinkedList (); 96 zeus_NameSet = false; 97 zeus_XmlextendsSet = false; 98 docTypeString = ""; 99 hasDTD = false; 100 validate = false; 101 namespaceMappings = new HashMap (); 102 } 103 104 public List getRemapList() { 105 return remapList; 106 } 107 108 public void setRemapList(List remapList) { 109 this.remapList = remapList; 110 } 111 112 public void addRemap(Remap remap) { 113 remapList.add(remap); 114 } 115 116 public void removeRemap(Remap remap) { 117 remapList.remove(remap); 118 } 119 120 public List getRemoveList() { 121 return removeList; 122 } 123 124 public void setRemoveList(List removeList) { 125 this.removeList = removeList; 126 } 127 128 public void addRemove(Remove remove) { 129 removeList.add(remove); 130 } 131 132 public void removeRemove(Remove remove) { 133 removeList.remove(remove); 134 } 135 136 public List getCopyList() { 137 return copyList; 138 } 139 140 public void setCopyList(List copyList) { 141 this.copyList = copyList; 142 } 143 144 public void addCopy(Copy copy) { 145 copyList.add(copy); 146 } 147 148 public void removeCopy(Copy copy) { 149 copyList.remove(copy); 150 } 151 152 public List getFilterList() { 153 return filterList; 154 } 155 156 public void setFilterList(List filterList) { 157 this.filterList = filterList; 158 } 159 160 public void addFilter(Filter filter) { 161 filterList.add(filter); 162 } 163 164 public void removeFilter(Filter filter) { 165 filterList.remove(filter); 166 } 167 168 public String getName() { 169 return name; 170 } 171 172 public void setName(String name) { 173 this.name = name; 174 zeus_NameSet = true; 175 } 176 177 public String getXmlextends() { 178 return xmlextends; 179 } 180 181 public void setXmlextends(String xmlextends) { 182 this.xmlextends = xmlextends; 183 zeus_XmlextendsSet = true; 184 } 185 186 public void setDocType(String name, String publicID, String systemID) { 187 try { 188 startDTD(name, publicID, systemID); 189 } catch (SAXException neverHappens) { } 190 } 191 192 public void setOutputEncoding(String outputEncoding) { 193 this.outputEncoding = outputEncoding; 194 } 195 196 public void marshal(File file) throws IOException { 197 marshal(new FileWriter (file)); 199 } 200 201 public void marshal(OutputStream outputStream) throws IOException { 202 marshal(new OutputStreamWriter (outputStream)); 204 } 205 206 public void marshal(Writer writer) throws IOException { 207 writer.write("<?xml version=\"1.0\" "); 209 if (outputEncoding != null) { 210 writer.write("encoding=\""); 211 writer.write(outputEncoding); 212 writer.write("\"?>\n\n"); 213 214 } else { 215 writer.write("encoding=\"UTF-8\"?>\n\n"); 216 217 } 218 writer.write(docTypeString); 220 writer.write("\n"); 221 writeXMLRepresentation(writer, ""); 223 224 writer.flush(); 226 writer.close(); 227 } 228 229 protected void writeXMLRepresentation(Writer writer, 230 String indent) 231 throws IOException { 232 233 writer.write(indent); 234 writer.write("<filtermap"); 235 236 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 238 String prefix = (String )i.next(); 239 String uri = (String )namespaceMappings.get(prefix); 240 writer.write(" xmlns"); 241 if (!prefix.trim().equals("")) { 242 writer.write(":"); 243 writer.write(prefix); 244 } 245 writer.write("=\""); 246 writer.write(uri); 247 writer.write("\"\n "); 248 } 249 250 if (zeus_NameSet) { 252 writer.write(" name=\""); 253 writer.write(escapeAttributeValue(name)); 254 writer.write("\""); 255 } 256 if (zeus_XmlextendsSet) { 257 writer.write(" extends=\""); 258 writer.write(escapeAttributeValue(xmlextends)); 259 writer.write("\""); 260 } 261 writer.write(">"); 262 writer.write("\n"); 263 264 for (Iterator i=remapList.iterator(); i.hasNext(); ) { 266 RemapImpl remap = (RemapImpl)i.next(); 267 remap.writeXMLRepresentation(writer, 268 new StringBuffer (indent).append(" ").toString()); 269 } 270 for (Iterator i=removeList.iterator(); i.hasNext(); ) { 271 RemoveImpl remove = (RemoveImpl)i.next(); 272 remove.writeXMLRepresentation(writer, 273 new StringBuffer (indent).append(" ").toString()); 274 } 275 for (Iterator i=copyList.iterator(); i.hasNext(); ) { 276 CopyImpl copy = (CopyImpl)i.next(); 277 copy.writeXMLRepresentation(writer, 278 new StringBuffer (indent).append(" ").toString()); 279 } 280 for (Iterator i=filterList.iterator(); i.hasNext(); ) { 281 FilterImpl filter = (FilterImpl)i.next(); 282 filter.writeXMLRepresentation(writer, 283 new StringBuffer (indent).append(" ").toString()); 284 } 285 writer.write(indent); 286 writer.write("</filtermap>\n"); 287 } 288 289 private String escapeAttributeValue(String attributeValue) { 290 String returnValue = attributeValue; 291 for (int i = 0; i < returnValue.length(); i++) { 292 char ch = returnValue.charAt(i); 293 if (ch == '"') { 294 returnValue = new StringBuffer () 295 .append(returnValue.substring(0, i)) 296 .append(""") 297 .append(returnValue.substring(i+1)) 298 .toString(); 299 } 300 } 301 return returnValue; 302 } 303 304 311 public static void setEntityResolver(EntityResolver resolver) { 312 entityResolver = resolver; 313 } 314 315 322 public static void setErrorHandler(ErrorHandler handler) { 323 errorHandler = handler; 324 } 325 326 public static Filtermap unmarshal(File file) throws IOException { 327 return unmarshal(new FileReader (file)); 329 } 330 331 public static Filtermap unmarshal(File file, boolean validate) throws IOException { 332 return unmarshal(new FileReader (file), validate); 334 } 335 336 public static Filtermap unmarshal(InputStream inputStream) throws IOException { 337 return unmarshal(new InputStreamReader (inputStream)); 339 } 340 341 public static Filtermap unmarshal(InputStream inputStream, boolean validate) throws IOException { 342 return unmarshal(new InputStreamReader (inputStream), validate); 344 } 345 346 public static Filtermap unmarshal(Reader reader) throws IOException { 347 return unmarshal(reader, false); 349 } 350 351 public static Filtermap unmarshal(Reader reader, boolean validate) throws IOException { 352 FiltermapImpl filtermap = new FiltermapImpl(); 353 filtermap.setValidating(validate); 354 filtermap.setCurrentUNode(filtermap); 355 filtermap.setParentUNode(null); 356 XMLReader parser = null; 358 String parserClass = System.getProperty("org.xml.sax.driver", 359 "org.apache.xerces.parsers.SAXParser"); 360 try { 361 parser = XMLReaderFactory.createXMLReader(parserClass); 362 363 if (entityResolver != null) { 365 parser.setEntityResolver(entityResolver); 366 } 367 368 parser.setErrorHandler(filtermap); 370 371 parser.setProperty("http://xml.org/sax/properties/lexical-handler", filtermap); 373 374 parser.setContentHandler(filtermap); 376 } catch (SAXException e) { 377 throw new IOException ("Could not load XML parser: " + 378 e.getMessage()); 379 } 380 381 InputSource inputSource = new InputSource (reader); 382 try { 383 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 384 parser.setFeature("http://xml.org/sax/features/namespaces", true); 385 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 386 parser.parse(inputSource); 387 } catch (SAXException e) { 388 throw new IOException ("Error parsing XML document: " + 389 e.getMessage()); 390 } 391 392 return filtermap; 394 } 395 396 public Unmarshallable getParentUNode() { 397 return zeus_parentUNode; 398 } 399 400 public void setParentUNode(Unmarshallable parentUNode) { 401 this.zeus_parentUNode = parentUNode; 402 } 403 404 public Unmarshallable getCurrentUNode() { 405 return zeus_currentUNode; 406 } 407 408 public void setCurrentUNode(Unmarshallable currentUNode) { 409 this.zeus_currentUNode = currentUNode; 410 } 411 412 public void setValidating(boolean validate) { 413 this.validate = validate; 414 } 415 416 public void startDocument() throws SAXException { 417 } 419 420 public void setDocumentLocator(Locator locator) { 421 } 423 424 public void startPrefixMapping(String prefix, String uri) 425 throws SAXException { 426 namespaceMappings.put(prefix, uri); 427 } 428 429 public void startElement(String namespaceURI, String localName, 430 String qName, org.xml.sax.Attributes atts) 431 throws SAXException { 432 433 Unmarshallable current = getCurrentUNode(); 435 if (current != this) { 436 current.startElement(namespaceURI, localName, qName, atts); 437 return; 438 } 439 440 if ((localName.equals("filtermap")) && (!zeus_thisNodeHandled)) { 442 for (int i=0, len=atts.getLength(); i<len; i++) { 444 String attName= atts.getLocalName(i); 445 String attValue = atts.getValue(i); 446 if (attName.equals("name")) { 447 setName(attValue); 448 } 449 if (attName.equals("extends")) { 450 setXmlextends(attValue); 451 } 452 } 453 zeus_thisNodeHandled = true; 454 return; 455 } else { 456 if (localName.equals("remap")) { 458 RemapImpl remap = new RemapImpl(); 459 current = getCurrentUNode(); 460 remap.setParentUNode(current); 461 remap.setCurrentUNode(remap); 462 this.setCurrentUNode(remap); 463 remap.startElement(namespaceURI, localName, qName, atts); 464 remapList.add(remap); 466 return; 467 } 468 if (localName.equals("remove")) { 469 RemoveImpl remove = new RemoveImpl(); 470 current = getCurrentUNode(); 471 remove.setParentUNode(current); 472 remove.setCurrentUNode(remove); 473 this.setCurrentUNode(remove); 474 remove.startElement(namespaceURI, localName, qName, atts); 475 removeList.add(remove); 477 return; 478 } 479 if (localName.equals("copy")) { 480 CopyImpl copy = new CopyImpl(); 481 current = getCurrentUNode(); 482 copy.setParentUNode(current); 483 copy.setCurrentUNode(copy); 484 this.setCurrentUNode(copy); 485 copy.startElement(namespaceURI, localName, qName, atts); 486 copyList.add(copy); 488 return; 489 } 490 if (localName.equals("filter")) { 491 FilterImpl filter = new FilterImpl(); 492 current = getCurrentUNode(); 493 filter.setParentUNode(current); 494 filter.setCurrentUNode(filter); 495 this.setCurrentUNode(filter); 496 filter.startElement(namespaceURI, localName, qName, atts); 497 filterList.add(filter); 499 return; 500 } 501 } 502 } 503 504 public void endElement(String namespaceURI, String localName, 505 String qName) 506 throws SAXException { 507 508 Unmarshallable current = getCurrentUNode(); 509 if (current != this) { 510 current.endElement(namespaceURI, localName, qName); 511 return; 512 } 513 514 Unmarshallable parent = getCurrentUNode().getParentUNode(); 515 if (parent != null) { 516 parent.setCurrentUNode(parent); 517 } 518 } 519 520 public void characters(char[] ch, int start, int len) 521 throws SAXException { 522 523 Unmarshallable current = getCurrentUNode(); 525 if (current != this) { 526 current.characters(ch, start, len); 527 return; 528 } 529 530 String text = new String (ch, start, len); 531 text = text.trim(); 532 } 533 534 public void comment(char ch[], int start, int len) throws SAXException { 535 } 537 538 public void warning(SAXParseException e) throws SAXException { 539 if (errorHandler != null) { 540 errorHandler.warning(e); 541 } 542 } 543 544 public void error(SAXParseException e) throws SAXException { 545 if ((validate) && (!hasDTD)) { 546 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."); 547 } 548 if (errorHandler != null) { 549 errorHandler.error(e); 550 } 551 } 552 553 public void fatalError(SAXParseException e) throws SAXException { 554 if ((validate) && (!hasDTD)) { 555 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."); 556 } 557 if (errorHandler != null) { 558 errorHandler.fatalError(e); 559 } 560 } 561 562 public void startDTD(String name, String publicID, String systemID) 563 throws SAXException { 564 565 if ((name == null) || (name.equals(""))) { 566 docTypeString = ""; 567 return; 568 } 569 570 hasDTD = true; 571 StringBuffer docTypeSB = new StringBuffer (); 572 boolean hasPublic = false; 573 574 docTypeSB.append("<!DOCTYPE ") 575 .append(name); 576 577 if ((publicID != null) && (!publicID.equals(""))) { 578 docTypeSB.append(" PUBLIC \"") 579 .append(publicID) 580 .append("\""); 581 hasPublic = true; 582 } 583 584 if ((systemID != null) && (!systemID.equals(""))) { 585 if (!hasPublic) { 586 docTypeSB.append(" SYSTEM"); 587 } 588 docTypeSB.append(" \"") 589 .append(systemID) 590 .append("\""); 591 592 } 593 594 docTypeSB.append(">"); 595 596 docTypeString = docTypeSB.toString(); 597 } 598 599 public void endDTD() throws SAXException { 600 } 602 603 public void startEntity(String name) throws SAXException { 604 } 606 607 public void endEntity(String name) throws SAXException { 608 } 610 611 public void startCDATA() throws SAXException { 612 } 614 615 public void endCDATA() throws SAXException { 616 } 618 619 } 620 | Popular Tags |