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 public class SearchSummaryImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , SearchSummary { 46 47 private SearchedTerm searchedTerm; 48 private BeginIndex beginIndex; 49 private EndIndex endIndex; 50 private DocsNumber docsNumber; 51 private SearchedTime searchedTime; 52 53 54 private String docTypeString; 55 56 57 private String outputEncoding; 58 59 60 private Unmarshallable zeus_currentUNode; 61 62 63 private Unmarshallable zeus_parentUNode; 64 65 66 private boolean zeus_thisNodeHandled = false; 67 68 69 private boolean hasDTD; 70 71 72 private boolean validate; 73 74 75 private Map namespaceMappings; 76 77 78 private static EntityResolver entityResolver; 79 80 81 private static ErrorHandler errorHandler; 82 83 private static SearchSummaryImpl prototype = null; 84 85 public static void setPrototype(SearchSummaryImpl prototype) { 86 SearchSummaryImpl.prototype = prototype; 87 } 88 public static SearchSummaryImpl newInstance() { 89 try { 90 return (prototype!=null)?(SearchSummaryImpl)prototype.clone(): new SearchSummaryImpl(); 91 } catch (CloneNotSupportedException e) { 92 return null; } 94 } 95 public SearchSummaryImpl() { 96 docTypeString = ""; 97 hasDTD = false; 98 validate = false; 99 namespaceMappings = new HashMap (); 100 } 101 102 public SearchedTerm getSearchedTerm() { 103 return searchedTerm; 104 } 105 106 public void setSearchedTerm(SearchedTerm searchedTerm) { 107 this.searchedTerm = searchedTerm; 108 } 109 110 public BeginIndex getBeginIndex() { 111 return beginIndex; 112 } 113 114 public void setBeginIndex(BeginIndex beginIndex) { 115 this.beginIndex = beginIndex; 116 } 117 118 public EndIndex getEndIndex() { 119 return endIndex; 120 } 121 122 public void setEndIndex(EndIndex endIndex) { 123 this.endIndex = endIndex; 124 } 125 126 public DocsNumber getDocsNumber() { 127 return docsNumber; 128 } 129 130 public void setDocsNumber(DocsNumber docsNumber) { 131 this.docsNumber = docsNumber; 132 } 133 134 public SearchedTime getSearchedTime() { 135 return searchedTime; 136 } 137 138 public void setSearchedTime(SearchedTime searchedTime) { 139 this.searchedTime = searchedTime; 140 } 141 142 public void setDocType(String name, String publicID, String systemID) { 143 try { 144 startDTD(name, publicID, systemID); 145 } catch (SAXException neverHappens) { } 146 } 147 148 public void setOutputEncoding(String outputEncoding) { 149 this.outputEncoding = outputEncoding; 150 } 151 152 public void marshal(File file) throws IOException { 153 marshal(new FileWriter (file)); 155 } 156 157 public void marshal(OutputStream outputStream) throws IOException { 158 marshal(new OutputStreamWriter (outputStream)); 160 } 161 162 public void marshal(Writer writer) throws IOException { 163 writer.write("<?xml version=\"1.0\" "); 165 if (outputEncoding != null) { 166 writer.write("encoding=\""); 167 writer.write(outputEncoding); 168 writer.write("\"?>\n\n"); 169 170 } else { 171 writer.write("encoding=\"UTF-8\"?>\n\n"); 172 173 } 174 writer.write(docTypeString); 176 writer.write("\n"); 177 writeXMLRepresentation(writer, ""); 179 180 writer.flush(); 182 writer.close(); 183 } 184 185 protected void writeXMLRepresentation(Writer writer, 186 String indent) 187 throws IOException { 188 189 writer.write(indent); 190 writer.write("<SearchSummary"); 191 192 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 194 String prefix = (String )i.next(); 195 String uri = (String )namespaceMappings.get(prefix); 196 writer.write(" xmlns"); 197 if (!prefix.trim().equals("")) { 198 writer.write(":"); 199 writer.write(prefix); 200 } 201 writer.write("=\""); 202 writer.write(uri); 203 writer.write("\"\n "); 204 } 205 206 writer.write(">"); 208 writer.write("\n"); 209 210 if (searchedTerm != null) { 212 ((SearchedTermImpl)searchedTerm).writeXMLRepresentation(writer, 213 new StringBuffer (indent).append(" ").toString()); 214 } 215 216 if (beginIndex != null) { 217 ((BeginIndexImpl)beginIndex).writeXMLRepresentation(writer, 218 new StringBuffer (indent).append(" ").toString()); 219 } 220 221 if (endIndex != null) { 222 ((EndIndexImpl)endIndex).writeXMLRepresentation(writer, 223 new StringBuffer (indent).append(" ").toString()); 224 } 225 226 if (docsNumber != null) { 227 ((DocsNumberImpl)docsNumber).writeXMLRepresentation(writer, 228 new StringBuffer (indent).append(" ").toString()); 229 } 230 231 if (searchedTime != null) { 232 ((SearchedTimeImpl)searchedTime).writeXMLRepresentation(writer, 233 new StringBuffer (indent).append(" ").toString()); 234 } 235 236 writer.write(indent); 237 writer.write("</SearchSummary>\n"); 238 } 239 240 private String escapeAttributeValue(String attributeValue) { 241 String returnValue = attributeValue; 242 for (int i = 0; i < returnValue.length(); i++) { 243 char ch = returnValue.charAt(i); 244 if (ch == '"') { 245 returnValue = new StringBuffer () 246 .append(returnValue.substring(0, i)) 247 .append(""") 248 .append(returnValue.substring(i+1)) 249 .toString(); 250 } 251 } 252 return returnValue; 253 } 254 255 private String escapeTextValue(String textValue) { 256 String returnValue = textValue; 257 for (int i = 0; i < returnValue.length(); i++) { 258 char ch = returnValue.charAt(i); 259 if (ch == '<') { 260 returnValue = new StringBuffer () 261 .append(returnValue.substring(0, i)) 262 .append("<") 263 .append(returnValue.substring(i+1)) 264 .toString(); 265 } else if (ch == '>') { 266 returnValue = new StringBuffer () 267 .append(returnValue.substring(0, i)) 268 .append(">") 269 .append(returnValue.substring(i+1)) 270 .toString(); 271 } 272 } 273 return returnValue; 274 } 275 276 283 public static void setEntityResolver(EntityResolver resolver) { 284 entityResolver = resolver; 285 } 286 287 294 public static void setErrorHandler(ErrorHandler handler) { 295 errorHandler = handler; 296 } 297 298 public static SearchSummary unmarshal(File file) throws IOException { 299 return unmarshal(new FileReader (file)); 301 } 302 303 public static SearchSummary unmarshal(File file, boolean validate) throws IOException { 304 return unmarshal(new FileReader (file), validate); 306 } 307 308 public static SearchSummary unmarshal(InputStream inputStream) throws IOException { 309 return unmarshal(new InputStreamReader (inputStream)); 311 } 312 313 public static SearchSummary unmarshal(InputStream inputStream, boolean validate) throws IOException { 314 return unmarshal(new InputStreamReader (inputStream), validate); 316 } 317 318 public static SearchSummary unmarshal(Reader reader) throws IOException { 319 return unmarshal(reader, false); 321 } 322 323 public static SearchSummary unmarshal(Reader reader, boolean validate) throws IOException { 324 SearchSummaryImpl searchSummary = SearchSummaryImpl.newInstance(); 325 searchSummary.setValidating(validate); 326 searchSummary.setCurrentUNode(searchSummary); 327 searchSummary.setParentUNode(null); 328 XMLReader parser = null; 330 String parserClass = System.getProperty("org.xml.sax.driver", 331 "org.apache.xerces.parsers.SAXParser"); 332 try { 333 parser = XMLReaderFactory.createXMLReader(parserClass); 334 335 if (entityResolver != null) { 337 parser.setEntityResolver(entityResolver); 338 } 339 340 parser.setErrorHandler(searchSummary); 342 343 parser.setProperty("http://xml.org/sax/properties/lexical-handler", searchSummary); 345 346 parser.setContentHandler(searchSummary); 348 } catch (SAXException e) { 349 throw new IOException ("Could not load XML parser: " + 350 e.getMessage()); 351 } 352 353 InputSource inputSource = new InputSource (reader); 354 try { 355 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 356 parser.setFeature("http://xml.org/sax/features/namespaces", true); 357 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 358 parser.parse(inputSource); 359 } catch (SAXException e) { 360 throw new IOException ("Error parsing XML document: " + 361 e.getMessage()); 362 } 363 364 return searchSummary; 366 } 367 368 public Unmarshallable getParentUNode() { 369 return zeus_parentUNode; 370 } 371 372 public void setParentUNode(Unmarshallable parentUNode) { 373 this.zeus_parentUNode = parentUNode; 374 } 375 376 public Unmarshallable getCurrentUNode() { 377 return zeus_currentUNode; 378 } 379 380 public void setCurrentUNode(Unmarshallable currentUNode) { 381 this.zeus_currentUNode = currentUNode; 382 } 383 384 public void setValidating(boolean validate) { 385 this.validate = validate; 386 } 387 388 public void startDocument() throws SAXException { 389 } 391 392 public void setDocumentLocator(Locator locator) { 393 } 395 396 public void startPrefixMapping(String prefix, String uri) 397 throws SAXException { 398 namespaceMappings.put(prefix, uri); 399 } 400 401 public void startElement(String namespaceURI, String localName, 402 String qName, org.xml.sax.Attributes atts) 403 throws SAXException { 404 405 Unmarshallable current = getCurrentUNode(); 407 if (current != this) { 408 current.startElement(namespaceURI, localName, qName, atts); 409 return; 410 } 411 412 if ((localName.equals("SearchSummary")) && (!zeus_thisNodeHandled)) { 414 for (int i=0, len=atts.getLength(); i<len; i++) { 416 String attName= atts.getLocalName(i); 417 String attValue = atts.getValue(i); 418 } 419 zeus_thisNodeHandled = true; 420 return; 421 } else { 422 if (localName.equals("SearchedTerm") && (searchedTerm==null)) { 424 SearchedTermImpl searchedTerm = SearchedTermImpl.newInstance(); 425 current = getCurrentUNode(); 426 searchedTerm.setParentUNode(current); 427 searchedTerm.setCurrentUNode(searchedTerm); 428 this.setCurrentUNode(searchedTerm); 429 searchedTerm.startElement(namespaceURI, localName, qName, atts); 430 this.searchedTerm = searchedTerm; 432 return; 433 } 434 if (localName.equals("BeginIndex") && (beginIndex==null)) { 435 BeginIndexImpl beginIndex = BeginIndexImpl.newInstance(); 436 current = getCurrentUNode(); 437 beginIndex.setParentUNode(current); 438 beginIndex.setCurrentUNode(beginIndex); 439 this.setCurrentUNode(beginIndex); 440 beginIndex.startElement(namespaceURI, localName, qName, atts); 441 this.beginIndex = beginIndex; 443 return; 444 } 445 if (localName.equals("EndIndex") && (endIndex==null)) { 446 EndIndexImpl endIndex = EndIndexImpl.newInstance(); 447 current = getCurrentUNode(); 448 endIndex.setParentUNode(current); 449 endIndex.setCurrentUNode(endIndex); 450 this.setCurrentUNode(endIndex); 451 endIndex.startElement(namespaceURI, localName, qName, atts); 452 this.endIndex = endIndex; 454 return; 455 } 456 if (localName.equals("DocsNumber") && (docsNumber==null)) { 457 DocsNumberImpl docsNumber = DocsNumberImpl.newInstance(); 458 current = getCurrentUNode(); 459 docsNumber.setParentUNode(current); 460 docsNumber.setCurrentUNode(docsNumber); 461 this.setCurrentUNode(docsNumber); 462 docsNumber.startElement(namespaceURI, localName, qName, atts); 463 this.docsNumber = docsNumber; 465 return; 466 } 467 if (localName.equals("SearchedTime") && (searchedTime==null)) { 468 SearchedTimeImpl searchedTime = SearchedTimeImpl.newInstance(); 469 current = getCurrentUNode(); 470 searchedTime.setParentUNode(current); 471 searchedTime.setCurrentUNode(searchedTime); 472 this.setCurrentUNode(searchedTime); 473 searchedTime.startElement(namespaceURI, localName, qName, atts); 474 this.searchedTime = searchedTime; 476 return; 477 } 478 } 479 } 480 481 public void endElement(String namespaceURI, String localName, 482 String qName) 483 throws SAXException { 484 485 Unmarshallable current = getCurrentUNode(); 486 if (current != this) { 487 current.endElement(namespaceURI, localName, qName); 488 return; 489 } 490 491 Unmarshallable parent = getCurrentUNode().getParentUNode(); 492 if (parent != null) { 493 parent.setCurrentUNode(parent); 494 } 495 } 496 497 public void characters(char[] ch, int start, int len) 498 throws SAXException { 499 500 Unmarshallable current = getCurrentUNode(); 502 if (current != this) { 503 current.characters(ch, start, len); 504 return; 505 } 506 507 String text = new String (ch, start, len); 508 } 509 510 public void comment(char ch[], int start, int len) throws SAXException { 511 } 513 514 public void warning(SAXParseException e) throws SAXException { 515 if (errorHandler != null) { 516 errorHandler.warning(e); 517 } 518 } 519 520 public void error(SAXParseException e) throws SAXException { 521 if ((validate) && (!hasDTD)) { 522 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."); 523 } 524 if (errorHandler != null) { 525 errorHandler.error(e); 526 } 527 } 528 529 public void fatalError(SAXParseException e) throws SAXException { 530 if ((validate) && (!hasDTD)) { 531 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."); 532 } 533 if (errorHandler != null) { 534 errorHandler.fatalError(e); 535 } 536 } 537 538 public void startDTD(String name, String publicID, String systemID) 539 throws SAXException { 540 541 if ((name == null) || (name.equals(""))) { 542 docTypeString = ""; 543 return; 544 } 545 546 hasDTD = true; 547 StringBuffer docTypeSB = new StringBuffer (); 548 boolean hasPublic = false; 549 550 docTypeSB.append("<!DOCTYPE ") 551 .append(name); 552 553 if ((publicID != null) && (!publicID.equals(""))) { 554 docTypeSB.append(" PUBLIC \"") 555 .append(publicID) 556 .append("\""); 557 hasPublic = true; 558 } 559 560 if ((systemID != null) && (!systemID.equals(""))) { 561 if (!hasPublic) { 562 docTypeSB.append(" SYSTEM"); 563 } 564 docTypeSB.append(" \"") 565 .append(systemID) 566 .append("\""); 567 568 } 569 570 docTypeSB.append(">"); 571 572 docTypeString = docTypeSB.toString(); 573 } 574 575 public void endDTD() throws SAXException { 576 } 578 579 public void startEntity(String name) throws SAXException { 580 } 582 583 public void endEntity(String name) throws SAXException { 584 } 586 587 public void startCDATA() throws SAXException { 588 } 590 591 public void endCDATA() throws SAXException { 592 } 594 595 } 596 | Popular Tags |