1 18 package org.enhydra.snapper.business.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 SiteImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , Site { 51 52 private Name name; 53 private ID iD; 54 private IndexDir indexDir; 55 private Download download; 56 private Searched searched; 57 private FileNR fileNR; 58 private QueryNR queryNR; 59 private LastQuery lastQuery; 60 private LastUpdate lastUpdate; 61 private List PathList; 62 63 64 private String docTypeString; 65 66 67 private String outputEncoding; 68 69 70 private Unmarshallable zeus_currentUNode; 71 72 73 private Unmarshallable zeus_parentUNode; 74 75 76 private boolean zeus_thisNodeHandled = false; 77 78 79 private boolean hasDTD; 80 81 82 private boolean validate; 83 84 85 private Map namespaceMappings; 86 87 88 private static EntityResolver entityResolver; 89 90 91 private static ErrorHandler errorHandler; 92 93 private static SiteImpl prototype = null; 94 95 public static void setPrototype(SiteImpl prototype) { 96 SiteImpl.prototype = prototype; 97 } 98 public static SiteImpl newInstance() { 99 try { 100 return (prototype!=null)?(SiteImpl)prototype.clone(): new SiteImpl(); 101 } catch (CloneNotSupportedException e) { 102 return null; } 104 } 105 public SiteImpl() { 106 PathList = new LinkedList (); 107 docTypeString = ""; 108 hasDTD = false; 109 validate = false; 110 namespaceMappings = new HashMap (); 111 } 112 113 public Name getName() { 114 return name; 115 } 116 117 public void setName(Name name) { 118 this.name = name; 119 } 120 121 public ID getID() { 122 return iD; 123 } 124 125 public void setID(ID iD) { 126 this.iD = iD; 127 } 128 129 public IndexDir getIndexDir() { 130 return indexDir; 131 } 132 133 public void setIndexDir(IndexDir indexDir) { 134 this.indexDir = indexDir; 135 } 136 137 public Download getDownload() { 138 return download; 139 } 140 141 public void setDownload(Download download) { 142 this.download = download; 143 } 144 145 public Searched getSearched() { 146 return searched; 147 } 148 149 public void setSearched(Searched searched) { 150 this.searched = searched; 151 } 152 153 public FileNR getFileNR() { 154 return fileNR; 155 } 156 157 public void setFileNR(FileNR fileNR) { 158 this.fileNR = fileNR; 159 } 160 161 public QueryNR getQueryNR() { 162 return queryNR; 163 } 164 165 public void setQueryNR(QueryNR queryNR) { 166 this.queryNR = queryNR; 167 } 168 169 public LastQuery getLastQuery() { 170 return lastQuery; 171 } 172 173 public void setLastQuery(LastQuery lastQuery) { 174 this.lastQuery = lastQuery; 175 } 176 177 public LastUpdate getLastUpdate() { 178 return lastUpdate; 179 } 180 181 public void setLastUpdate(LastUpdate lastUpdate) { 182 this.lastUpdate = lastUpdate; 183 } 184 185 public List getPathList() { 186 return PathList; 187 } 188 189 public void setPathList(List PathList) { 190 this.PathList = PathList; 191 } 192 193 public void addPath(Path Path) { 194 PathList.add(Path); 195 } 196 197 public void removePath(Path Path) { 198 PathList.remove(Path); 199 } 200 201 public void setDocType(String name, String publicID, String systemID) { 202 try { 203 startDTD(name, publicID, systemID); 204 } catch (SAXException neverHappens) { } 205 } 206 207 public void setOutputEncoding(String outputEncoding) { 208 this.outputEncoding = outputEncoding; 209 } 210 211 public void marshal(File file) throws IOException { 212 marshal(new FileWriter (file)); 214 } 215 216 public void marshal(OutputStream outputStream) throws IOException { 217 marshal(new OutputStreamWriter (outputStream)); 219 } 220 221 public void marshal(Writer writer) throws IOException { 222 writer.write("<?xml version=\"1.0\" "); 224 if (outputEncoding != null) { 225 writer.write("encoding=\""); 226 writer.write(outputEncoding); 227 writer.write("\"?>\n\n"); 228 229 } else { 230 writer.write("encoding=\"UTF-8\"?>\n\n"); 231 232 } 233 writer.write(docTypeString); 235 writer.write("\n"); 236 writeXMLRepresentation(writer, ""); 238 239 writer.flush(); 241 writer.close(); 242 } 243 244 protected void writeXMLRepresentation(Writer writer, 245 String indent) 246 throws IOException { 247 248 writer.write(indent); 249 writer.write("<Site"); 250 251 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 253 String prefix = (String )i.next(); 254 String uri = (String )namespaceMappings.get(prefix); 255 writer.write(" xmlns"); 256 if (!prefix.trim().equals("")) { 257 writer.write(":"); 258 writer.write(prefix); 259 } 260 writer.write("=\""); 261 writer.write(uri); 262 writer.write("\"\n "); 263 } 264 265 writer.write(">"); 267 writer.write("\n"); 268 269 if (name != null) { 271 ((NameImpl)name).writeXMLRepresentation(writer, 272 new StringBuffer (indent).append(" ").toString()); 273 } 274 275 if (iD != null) { 276 ((IDImpl)iD).writeXMLRepresentation(writer, 277 new StringBuffer (indent).append(" ").toString()); 278 } 279 280 if (indexDir != null) { 281 ((IndexDirImpl)indexDir).writeXMLRepresentation(writer, 282 new StringBuffer (indent).append(" ").toString()); 283 } 284 285 if (download != null) { 286 ((DownloadImpl)download).writeXMLRepresentation(writer, 287 new StringBuffer (indent).append(" ").toString()); 288 } 289 290 if (searched != null) { 291 ((SearchedImpl)searched).writeXMLRepresentation(writer, 292 new StringBuffer (indent).append(" ").toString()); 293 } 294 295 if (fileNR != null) { 296 ((FileNRImpl)fileNR).writeXMLRepresentation(writer, 297 new StringBuffer (indent).append(" ").toString()); 298 } 299 300 if (queryNR != null) { 301 ((QueryNRImpl)queryNR).writeXMLRepresentation(writer, 302 new StringBuffer (indent).append(" ").toString()); 303 } 304 305 if (lastQuery != null) { 306 ((LastQueryImpl)lastQuery).writeXMLRepresentation(writer, 307 new StringBuffer (indent).append(" ").toString()); 308 } 309 310 if (lastUpdate != null) { 311 ((LastUpdateImpl)lastUpdate).writeXMLRepresentation(writer, 312 new StringBuffer (indent).append(" ").toString()); 313 } 314 315 for (Iterator i=PathList.iterator(); i.hasNext(); ) { 316 PathImpl path = (PathImpl)i.next(); 317 path.writeXMLRepresentation(writer, 318 new StringBuffer (indent).append(" ").toString()); 319 } 320 writer.write(indent); 321 writer.write("</Site>\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 private String escapeTextValue(String textValue) { 340 String returnValue = textValue; 341 for (int i = 0; i < returnValue.length(); i++) { 342 char ch = returnValue.charAt(i); 343 if (ch == '<') { 344 returnValue = new StringBuffer () 345 .append(returnValue.substring(0, i)) 346 .append("<") 347 .append(returnValue.substring(i+1)) 348 .toString(); 349 } else if (ch == '>') { 350 returnValue = new StringBuffer () 351 .append(returnValue.substring(0, i)) 352 .append(">") 353 .append(returnValue.substring(i+1)) 354 .toString(); 355 } 356 } 357 return returnValue; 358 } 359 360 367 public static void setEntityResolver(EntityResolver resolver) { 368 entityResolver = resolver; 369 } 370 371 378 public static void setErrorHandler(ErrorHandler handler) { 379 errorHandler = handler; 380 } 381 382 public static Site unmarshal(File file) throws IOException { 383 return unmarshal(new FileReader (file)); 385 } 386 387 public static Site unmarshal(File file, boolean validate) throws IOException { 388 return unmarshal(new FileReader (file), validate); 390 } 391 392 public static Site unmarshal(InputStream inputStream) throws IOException { 393 return unmarshal(new InputStreamReader (inputStream)); 395 } 396 397 public static Site unmarshal(InputStream inputStream, boolean validate) throws IOException { 398 return unmarshal(new InputStreamReader (inputStream), validate); 400 } 401 402 public static Site unmarshal(Reader reader) throws IOException { 403 return unmarshal(reader, false); 405 } 406 407 public static Site unmarshal(Reader reader, boolean validate) throws IOException { 408 SiteImpl site = SiteImpl.newInstance(); 409 site.setValidating(validate); 410 site.setCurrentUNode(site); 411 site.setParentUNode(null); 412 XMLReader parser = null; 414 String parserClass = System.getProperty("org.xml.sax.driver", 415 "org.apache.xerces.parsers.SAXParser"); 416 try { 417 parser = XMLReaderFactory.createXMLReader(parserClass); 418 419 if (entityResolver != null) { 421 parser.setEntityResolver(entityResolver); 422 } 423 424 parser.setErrorHandler(site); 426 427 parser.setProperty("http://xml.org/sax/properties/lexical-handler", site); 429 430 parser.setContentHandler(site); 432 } catch (SAXException e) { 433 throw new IOException ("Could not load XML parser: " + 434 e.getMessage()); 435 } 436 437 InputSource inputSource = new InputSource (reader); 438 try { 439 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 440 parser.setFeature("http://xml.org/sax/features/namespaces", true); 441 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 442 parser.parse(inputSource); 443 } catch (SAXException e) { 444 throw new IOException ("Error parsing XML document: " + 445 e.getMessage()); 446 } 447 448 return site; 450 } 451 452 public Unmarshallable getParentUNode() { 453 return zeus_parentUNode; 454 } 455 456 public void setParentUNode(Unmarshallable parentUNode) { 457 this.zeus_parentUNode = parentUNode; 458 } 459 460 public Unmarshallable getCurrentUNode() { 461 return zeus_currentUNode; 462 } 463 464 public void setCurrentUNode(Unmarshallable currentUNode) { 465 this.zeus_currentUNode = currentUNode; 466 } 467 468 public void setValidating(boolean validate) { 469 this.validate = validate; 470 } 471 472 public void startDocument() throws SAXException { 473 } 475 476 public void setDocumentLocator(Locator locator) { 477 } 479 480 public void startPrefixMapping(String prefix, String uri) 481 throws SAXException { 482 namespaceMappings.put(prefix, uri); 483 } 484 485 public void startElement(String namespaceURI, String localName, 486 String qName, org.xml.sax.Attributes atts) 487 throws SAXException { 488 489 Unmarshallable current = getCurrentUNode(); 491 if (current != this) { 492 current.startElement(namespaceURI, localName, qName, atts); 493 return; 494 } 495 496 if ((localName.equals("Site")) && (!zeus_thisNodeHandled)) { 498 for (int i=0, len=atts.getLength(); i<len; i++) { 500 String attName= atts.getLocalName(i); 501 String attValue = atts.getValue(i); 502 } 503 zeus_thisNodeHandled = true; 504 return; 505 } else { 506 if (localName.equals("Name") && (name==null)) { 508 NameImpl name = NameImpl.newInstance(); 509 current = getCurrentUNode(); 510 name.setParentUNode(current); 511 name.setCurrentUNode(name); 512 this.setCurrentUNode(name); 513 name.startElement(namespaceURI, localName, qName, atts); 514 this.name = name; 516 return; 517 } 518 if (localName.equals("ID") && (iD==null)) { 519 IDImpl iD = IDImpl.newInstance(); 520 current = getCurrentUNode(); 521 iD.setParentUNode(current); 522 iD.setCurrentUNode(iD); 523 this.setCurrentUNode(iD); 524 iD.startElement(namespaceURI, localName, qName, atts); 525 this.iD = iD; 527 return; 528 } 529 if (localName.equals("IndexDir") && (indexDir==null)) { 530 IndexDirImpl indexDir = IndexDirImpl.newInstance(); 531 current = getCurrentUNode(); 532 indexDir.setParentUNode(current); 533 indexDir.setCurrentUNode(indexDir); 534 this.setCurrentUNode(indexDir); 535 indexDir.startElement(namespaceURI, localName, qName, atts); 536 this.indexDir = indexDir; 538 return; 539 } 540 if (localName.equals("Download") && (download==null)) { 541 DownloadImpl download = DownloadImpl.newInstance(); 542 current = getCurrentUNode(); 543 download.setParentUNode(current); 544 download.setCurrentUNode(download); 545 this.setCurrentUNode(download); 546 download.startElement(namespaceURI, localName, qName, atts); 547 this.download = download; 549 return; 550 } 551 if (localName.equals("Searched") && (searched==null)) { 552 SearchedImpl searched = SearchedImpl.newInstance(); 553 current = getCurrentUNode(); 554 searched.setParentUNode(current); 555 searched.setCurrentUNode(searched); 556 this.setCurrentUNode(searched); 557 searched.startElement(namespaceURI, localName, qName, atts); 558 this.searched = searched; 560 return; 561 } 562 if (localName.equals("FileNR") && (fileNR==null)) { 563 FileNRImpl fileNR = FileNRImpl.newInstance(); 564 current = getCurrentUNode(); 565 fileNR.setParentUNode(current); 566 fileNR.setCurrentUNode(fileNR); 567 this.setCurrentUNode(fileNR); 568 fileNR.startElement(namespaceURI, localName, qName, atts); 569 this.fileNR = fileNR; 571 return; 572 } 573 if (localName.equals("QueryNR") && (queryNR==null)) { 574 QueryNRImpl queryNR = QueryNRImpl.newInstance(); 575 current = getCurrentUNode(); 576 queryNR.setParentUNode(current); 577 queryNR.setCurrentUNode(queryNR); 578 this.setCurrentUNode(queryNR); 579 queryNR.startElement(namespaceURI, localName, qName, atts); 580 this.queryNR = queryNR; 582 return; 583 } 584 if (localName.equals("LastQuery") && (lastQuery==null)) { 585 LastQueryImpl lastQuery = LastQueryImpl.newInstance(); 586 current = getCurrentUNode(); 587 lastQuery.setParentUNode(current); 588 lastQuery.setCurrentUNode(lastQuery); 589 this.setCurrentUNode(lastQuery); 590 lastQuery.startElement(namespaceURI, localName, qName, atts); 591 this.lastQuery = lastQuery; 593 return; 594 } 595 if (localName.equals("LastUpdate") && (lastUpdate==null)) { 596 LastUpdateImpl lastUpdate = LastUpdateImpl.newInstance(); 597 current = getCurrentUNode(); 598 lastUpdate.setParentUNode(current); 599 lastUpdate.setCurrentUNode(lastUpdate); 600 this.setCurrentUNode(lastUpdate); 601 lastUpdate.startElement(namespaceURI, localName, qName, atts); 602 this.lastUpdate = lastUpdate; 604 return; 605 } 606 if (localName.equals("Path")) { 607 PathImpl path = PathImpl.newInstance(); 608 current = getCurrentUNode(); 609 path.setParentUNode(current); 610 path.setCurrentUNode(path); 611 this.setCurrentUNode(path); 612 path.startElement(namespaceURI, localName, qName, atts); 613 PathList.add(path); 615 return; 616 } 617 } 618 } 619 620 public void endElement(String namespaceURI, String localName, 621 String qName) 622 throws SAXException { 623 624 Unmarshallable current = getCurrentUNode(); 625 if (current != this) { 626 current.endElement(namespaceURI, localName, qName); 627 return; 628 } 629 630 Unmarshallable parent = getCurrentUNode().getParentUNode(); 631 if (parent != null) { 632 parent.setCurrentUNode(parent); 633 } 634 } 635 636 public void characters(char[] ch, int start, int len) 637 throws SAXException { 638 639 Unmarshallable current = getCurrentUNode(); 641 if (current != this) { 642 current.characters(ch, start, len); 643 return; 644 } 645 646 String text = new String (ch, start, len); 647 } 648 649 public void comment(char ch[], int start, int len) throws SAXException { 650 } 652 653 public void warning(SAXParseException e) throws SAXException { 654 if (errorHandler != null) { 655 errorHandler.warning(e); 656 } 657 } 658 659 public void error(SAXParseException e) throws SAXException { 660 if ((validate) && (!hasDTD)) { 661 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."); 662 } 663 if (errorHandler != null) { 664 errorHandler.error(e); 665 } 666 } 667 668 public void fatalError(SAXParseException e) throws SAXException { 669 if ((validate) && (!hasDTD)) { 670 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."); 671 } 672 if (errorHandler != null) { 673 errorHandler.fatalError(e); 674 } 675 } 676 677 public void startDTD(String name, String publicID, String systemID) 678 throws SAXException { 679 680 if ((name == null) || (name.equals(""))) { 681 docTypeString = ""; 682 return; 683 } 684 685 hasDTD = true; 686 StringBuffer docTypeSB = new StringBuffer (); 687 boolean hasPublic = false; 688 689 docTypeSB.append("<!DOCTYPE ") 690 .append(name); 691 692 if ((publicID != null) && (!publicID.equals(""))) { 693 docTypeSB.append(" PUBLIC \"") 694 .append(publicID) 695 .append("\""); 696 hasPublic = true; 697 } 698 699 if ((systemID != null) && (!systemID.equals(""))) { 700 if (!hasPublic) { 701 docTypeSB.append(" SYSTEM"); 702 } 703 docTypeSB.append(" \"") 704 .append(systemID) 705 .append("\""); 706 707 } 708 709 docTypeSB.append(">"); 710 711 docTypeString = docTypeSB.toString(); 712 } 713 714 public void endDTD() throws SAXException { 715 } 717 718 public void startEntity(String name) throws SAXException { 719 } 721 722 public void endEntity(String name) throws SAXException { 723 } 725 726 public void startCDATA() throws SAXException { 727 } 729 730 public void endCDATA() throws SAXException { 731 } 733 734 } 735 | Popular Tags |