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 SummaryImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , Summary { 46 47 private Configuration configuration; 48 private SearchedParameters searchedParameters; 49 private Search search; 50 51 52 private String docTypeString; 53 54 55 private String outputEncoding; 56 57 58 private Unmarshallable zeus_currentUNode; 59 60 61 private Unmarshallable zeus_parentUNode; 62 63 64 private boolean zeus_thisNodeHandled = false; 65 66 67 private boolean hasDTD; 68 69 70 private boolean validate; 71 72 73 private Map namespaceMappings; 74 75 76 private static EntityResolver entityResolver; 77 78 79 private static ErrorHandler errorHandler; 80 81 private static SummaryImpl prototype = null; 82 83 public static void setPrototype(SummaryImpl prototype) { 84 SummaryImpl.prototype = prototype; 85 } 86 public static SummaryImpl newInstance() { 87 try { 88 return (prototype!=null)?(SummaryImpl)prototype.clone(): new SummaryImpl(); 89 } catch (CloneNotSupportedException e) { 90 return null; } 92 } 93 public SummaryImpl() { 94 docTypeString = ""; 95 hasDTD = false; 96 validate = false; 97 namespaceMappings = new HashMap (); 98 } 99 100 public Configuration getConfiguration() { 101 return configuration; 102 } 103 104 public void setConfiguration(Configuration configuration) { 105 this.configuration = configuration; 106 } 107 108 public SearchedParameters getSearchedParameters() { 109 return searchedParameters; 110 } 111 112 public void setSearchedParameters(SearchedParameters searchedParameters) { 113 this.searchedParameters = searchedParameters; 114 } 115 116 public Search getSearch() { 117 return search; 118 } 119 120 public void setSearch(Search search) { 121 this.search = search; 122 } 123 124 public void setDocType(String name, String publicID, String systemID) { 125 try { 126 startDTD(name, publicID, systemID); 127 } catch (SAXException neverHappens) { } 128 } 129 130 public void setOutputEncoding(String outputEncoding) { 131 this.outputEncoding = outputEncoding; 132 } 133 134 public void marshal(File file) throws IOException { 135 marshal(new FileWriter (file)); 137 } 138 139 public void marshal(OutputStream outputStream) throws IOException { 140 marshal(new OutputStreamWriter (outputStream,"UTF-8")); 142 } 143 144 public void marshal(Writer writer) throws IOException { 145 writer.write("<?xml version=\"1.0\" "); 147 if (outputEncoding != null) { 148 writer.write("encoding=\""); 149 writer.write(outputEncoding); 150 writer.write("\"?>\n\n"); 151 152 } else { 153 writer.write("encoding=\"UTF-8\"?>\n\n"); 154 155 } 156 writer.write(docTypeString); 158 writer.write("\n"); 159 writeXMLRepresentation(writer, ""); 161 162 writer.flush(); 164 } 166 167 protected void writeXMLRepresentation(Writer writer, 168 String indent) 169 throws IOException { 170 171 writer.write(indent); 172 writer.write("<Summary"); 173 174 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 176 String prefix = (String )i.next(); 177 String uri = (String )namespaceMappings.get(prefix); 178 writer.write(" xmlns"); 179 if (!prefix.trim().equals("")) { 180 writer.write(":"); 181 writer.write(prefix); 182 } 183 writer.write("=\""); 184 writer.write(uri); 185 writer.write("\"\n "); 186 } 187 188 writer.write(">"); 190 writer.write("\n"); 191 192 if (configuration != null) { 194 ((ConfigurationImpl)configuration).writeXMLRepresentation(writer, 195 new StringBuffer (indent).append(" ").toString()); 196 } 197 198 if (searchedParameters != null) { 199 ((SearchedParametersImpl)searchedParameters).writeXMLRepresentation(writer, 200 new StringBuffer (indent).append(" ").toString()); 201 } 202 203 if (search != null) { 204 ((SearchImpl)search).writeXMLRepresentation(writer, 205 new StringBuffer (indent).append(" ").toString()); 206 } 207 208 writer.write(indent); 209 writer.write("</Summary>\n"); 210 } 211 212 private String escapeAttributeValue(String attributeValue) { 213 String returnValue = attributeValue; 214 for (int i = 0; i < returnValue.length(); i++) { 215 char ch = returnValue.charAt(i); 216 if (ch == '"') { 217 returnValue = new StringBuffer () 218 .append(returnValue.substring(0, i)) 219 .append(""") 220 .append(returnValue.substring(i+1)) 221 .toString(); 222 } 223 } 224 return returnValue; 225 } 226 227 private String escapeTextValue(String textValue) { 228 String returnValue = textValue; 229 for (int i = 0; i < returnValue.length(); i++) { 230 char ch = returnValue.charAt(i); 231 if (ch == '<') { 232 returnValue = new StringBuffer () 233 .append(returnValue.substring(0, i)) 234 .append("<") 235 .append(returnValue.substring(i+1)) 236 .toString(); 237 } else if (ch == '>') { 238 returnValue = new StringBuffer () 239 .append(returnValue.substring(0, i)) 240 .append(">") 241 .append(returnValue.substring(i+1)) 242 .toString(); 243 } 244 } 245 return returnValue; 246 } 247 248 255 public static void setEntityResolver(EntityResolver resolver) { 256 entityResolver = resolver; 257 } 258 259 266 public static void setErrorHandler(ErrorHandler handler) { 267 errorHandler = handler; 268 } 269 270 public static Summary unmarshal(File file) throws IOException { 271 return unmarshal(new FileReader (file)); 273 } 274 275 public static Summary unmarshal(File file, boolean validate) throws IOException { 276 return unmarshal(new FileReader (file), validate); 278 } 279 280 public static Summary unmarshal(InputStream inputStream) throws IOException { 281 return unmarshal(new InputStreamReader (inputStream)); 283 } 284 285 public static Summary unmarshal(InputStream inputStream, boolean validate) throws IOException { 286 return unmarshal(new InputStreamReader (inputStream), validate); 288 } 289 290 public static Summary unmarshal(Reader reader) throws IOException { 291 return unmarshal(reader, false); 293 } 294 295 public static Summary unmarshal(Reader reader, boolean validate) throws IOException { 296 SummaryImpl summary = SummaryImpl.newInstance(); 297 summary.setValidating(validate); 298 summary.setCurrentUNode(summary); 299 summary.setParentUNode(null); 300 XMLReader parser = null; 302 String parserClass = System.getProperty("org.xml.sax.driver", 303 "org.apache.xerces.parsers.SAXParser"); 304 try { 305 parser = XMLReaderFactory.createXMLReader(parserClass); 306 307 if (entityResolver != null) { 309 parser.setEntityResolver(entityResolver); 310 } 311 312 parser.setErrorHandler(summary); 314 315 parser.setProperty("http://xml.org/sax/properties/lexical-handler", summary); 317 318 parser.setContentHandler(summary); 320 } catch (SAXException e) { 321 throw new IOException ("Could not load XML parser: " + 322 e.getMessage()); 323 } 324 325 InputSource inputSource = new InputSource (reader); 326 try { 327 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 328 parser.setFeature("http://xml.org/sax/features/namespaces", true); 329 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 330 parser.parse(inputSource); 331 } catch (SAXException e) { 332 throw new IOException ("Error parsing XML document: " + 333 e.getMessage()); 334 } 335 336 return summary; 338 } 339 340 public Unmarshallable getParentUNode() { 341 return zeus_parentUNode; 342 } 343 344 public void setParentUNode(Unmarshallable parentUNode) { 345 this.zeus_parentUNode = parentUNode; 346 } 347 348 public Unmarshallable getCurrentUNode() { 349 return zeus_currentUNode; 350 } 351 352 public void setCurrentUNode(Unmarshallable currentUNode) { 353 this.zeus_currentUNode = currentUNode; 354 } 355 356 public void setValidating(boolean validate) { 357 this.validate = validate; 358 } 359 360 public void startDocument() throws SAXException { 361 } 363 364 public void setDocumentLocator(Locator locator) { 365 } 367 368 public void startPrefixMapping(String prefix, String uri) 369 throws SAXException { 370 namespaceMappings.put(prefix, uri); 371 } 372 373 public void startElement(String namespaceURI, String localName, 374 String qName, org.xml.sax.Attributes atts) 375 throws SAXException { 376 377 Unmarshallable current = getCurrentUNode(); 379 if (current != this) { 380 current.startElement(namespaceURI, localName, qName, atts); 381 return; 382 } 383 384 if ((localName.equals("Summary")) && (!zeus_thisNodeHandled)) { 386 for (int i=0, len=atts.getLength(); i<len; i++) { 388 String attName= atts.getLocalName(i); 389 String attValue = atts.getValue(i); 390 } 391 zeus_thisNodeHandled = true; 392 return; 393 } else { 394 if (localName.equals("Configuration") && (configuration==null)) { 396 ConfigurationImpl configuration = ConfigurationImpl.newInstance(); 397 current = getCurrentUNode(); 398 configuration.setParentUNode(current); 399 configuration.setCurrentUNode(configuration); 400 this.setCurrentUNode(configuration); 401 configuration.startElement(namespaceURI, localName, qName, atts); 402 this.configuration = configuration; 404 return; 405 } 406 if (localName.equals("SearchedParameters") && (searchedParameters==null)) { 407 SearchedParametersImpl searchedParameters = SearchedParametersImpl.newInstance(); 408 current = getCurrentUNode(); 409 searchedParameters.setParentUNode(current); 410 searchedParameters.setCurrentUNode(searchedParameters); 411 this.setCurrentUNode(searchedParameters); 412 searchedParameters.startElement(namespaceURI, localName, qName, atts); 413 this.searchedParameters = searchedParameters; 415 return; 416 } 417 if (localName.equals("Search") && (search==null)) { 418 SearchImpl search = SearchImpl.newInstance(); 419 current = getCurrentUNode(); 420 search.setParentUNode(current); 421 search.setCurrentUNode(search); 422 this.setCurrentUNode(search); 423 search.startElement(namespaceURI, localName, qName, atts); 424 this.search = search; 426 return; 427 } 428 } 429 } 430 431 public void endElement(String namespaceURI, String localName, 432 String qName) 433 throws SAXException { 434 435 Unmarshallable current = getCurrentUNode(); 436 if (current != this) { 437 current.endElement(namespaceURI, localName, qName); 438 return; 439 } 440 441 Unmarshallable parent = getCurrentUNode().getParentUNode(); 442 if (parent != null) { 443 parent.setCurrentUNode(parent); 444 } 445 } 446 447 public void characters(char[] ch, int start, int len) 448 throws SAXException { 449 450 Unmarshallable current = getCurrentUNode(); 452 if (current != this) { 453 current.characters(ch, start, len); 454 return; 455 } 456 457 String text = new String (ch, start, len); 458 } 459 460 public void comment(char ch[], int start, int len) throws SAXException { 461 } 463 464 public void warning(SAXParseException e) throws SAXException { 465 if (errorHandler != null) { 466 errorHandler.warning(e); 467 } 468 } 469 470 public void error(SAXParseException e) throws SAXException { 471 if ((validate) && (!hasDTD)) { 472 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."); 473 } 474 if (errorHandler != null) { 475 errorHandler.error(e); 476 } 477 } 478 479 public void fatalError(SAXParseException e) throws SAXException { 480 if ((validate) && (!hasDTD)) { 481 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."); 482 } 483 if (errorHandler != null) { 484 errorHandler.fatalError(e); 485 } 486 } 487 488 public void startDTD(String name, String publicID, String systemID) 489 throws SAXException { 490 491 if ((name == null) || (name.equals(""))) { 492 docTypeString = ""; 493 return; 494 } 495 496 hasDTD = true; 497 StringBuffer docTypeSB = new StringBuffer (); 498 boolean hasPublic = false; 499 500 docTypeSB.append("<!DOCTYPE ") 501 .append(name); 502 503 if ((publicID != null) && (!publicID.equals(""))) { 504 docTypeSB.append(" PUBLIC \"") 505 .append(publicID) 506 .append("\""); 507 hasPublic = true; 508 } 509 510 if ((systemID != null) && (!systemID.equals(""))) { 511 if (!hasPublic) { 512 docTypeSB.append(" SYSTEM"); 513 } 514 docTypeSB.append(" \"") 515 .append(systemID) 516 .append("\""); 517 518 } 519 520 docTypeSB.append(">"); 521 522 docTypeString = docTypeSB.toString(); 523 } 524 525 public void endDTD() throws SAXException { 526 } 528 529 public void startEntity(String name) throws SAXException { 530 } 532 533 public void endEntity(String name) throws SAXException { 534 } 536 537 public void startCDATA() throws SAXException { 538 } 540 541 public void endCDATA() throws SAXException { 542 } 544 545 } 546 | Popular Tags |