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 DateImpl extends DefaultHandler implements Unmarshallable, LexicalHandler , Date { 46 47 private String value; 48 private boolean zeus_ValueSet; 49 private String locale; 50 private static boolean zeus_LocaleInitialized; 51 private static java.util.Vector zeus_validLocale; 52 private boolean zeus_LocaleSet; 53 54 55 private String docTypeString; 56 57 58 private String outputEncoding; 59 60 61 private Unmarshallable zeus_currentUNode; 62 63 64 private Unmarshallable zeus_parentUNode; 65 66 67 private boolean zeus_thisNodeHandled = false; 68 69 70 private boolean hasDTD; 71 72 73 private boolean validate; 74 75 76 private Map namespaceMappings; 77 78 79 private static EntityResolver entityResolver; 80 81 82 private static ErrorHandler errorHandler; 83 84 public DateImpl() { 85 zeus_ValueSet = false; 86 locale = "DEFAULT"; 87 zeus_LocaleInitialized = false; 88 zeus_validLocale = null; 89 zeus_LocaleSet = false; 90 docTypeString = ""; 91 hasDTD = false; 92 validate = false; 93 namespaceMappings = new HashMap (); 94 } 95 96 public String getValue() { 97 return value; 98 } 99 100 public void setValue(String value) { 101 this.value = value; 102 zeus_ValueSet = true; 103 } 104 105 public String getLocale() { 106 return locale; 107 } 108 109 public void setLocale(String locale) 110 throws IllegalArgumentException { 111 if (!zeus_LocaleInitialized) { 112 zeus_validLocale = new java.util.Vector (); 113 zeus_validLocale.addElement("DEFAULT"); 114 zeus_validLocale.addElement("CANADA"); 115 zeus_validLocale.addElement("CANADA_FRENCH"); 116 zeus_validLocale.addElement("CHINA"); 117 zeus_validLocale.addElement("CHINESE"); 118 zeus_validLocale.addElement("ENGLISH"); 119 zeus_validLocale.addElement("FRANCE"); 120 zeus_validLocale.addElement("FRENCH"); 121 zeus_validLocale.addElement("GERMANY"); 122 zeus_validLocale.addElement("ITALIAN"); 123 zeus_validLocale.addElement("JAPAN"); 124 zeus_validLocale.addElement("JAPANESE"); 125 zeus_validLocale.addElement("KOREA"); 126 zeus_validLocale.addElement("KOREAN"); 127 zeus_validLocale.addElement("PRC"); 128 zeus_validLocale.addElement("SIMPLIFIED_CHINESE"); 129 zeus_validLocale.addElement("TAIWAN"); 130 zeus_validLocale.addElement("TRADITIONAL_CHINESE"); 131 zeus_validLocale.addElement("UK"); 132 zeus_validLocale.addElement("US"); 133 zeus_LocaleInitialized = true; 134 } 135 if (!zeus_validLocale.contains(locale)) { 136 throw new IllegalArgumentException ("Illegal value for attribute 'locale'"); 137 } else { 138 this.locale = locale; 139 this.zeus_LocaleSet = true; 140 } 141 } 142 143 public void setDocType(String name, String publicID, String systemID) { 144 try { 145 startDTD(name, publicID, systemID); 146 } catch (SAXException neverHappens) { } 147 } 148 149 public void setOutputEncoding(String outputEncoding) { 150 this.outputEncoding = outputEncoding; 151 } 152 153 public void marshal(File file) throws IOException { 154 marshal(new FileWriter (file)); 156 } 157 158 public void marshal(OutputStream outputStream) throws IOException { 159 marshal(new OutputStreamWriter (outputStream)); 161 } 162 163 public void marshal(Writer writer) throws IOException { 164 writer.write("<?xml version=\"1.0\" "); 166 if (outputEncoding != null) { 167 writer.write("encoding=\""); 168 writer.write(outputEncoding); 169 writer.write("\"?>\n\n"); 170 171 } else { 172 writer.write("encoding=\"UTF-8\"?>\n\n"); 173 174 } 175 writer.write(docTypeString); 177 writer.write("\n"); 178 writeXMLRepresentation(writer, ""); 180 181 writer.flush(); 183 writer.close(); 184 } 185 186 protected void writeXMLRepresentation(Writer writer, 187 String indent) 188 throws IOException { 189 190 writer.write(indent); 191 writer.write("<date"); 192 193 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 195 String prefix = (String )i.next(); 196 String uri = (String )namespaceMappings.get(prefix); 197 writer.write(" xmlns"); 198 if (!prefix.trim().equals("")) { 199 writer.write(":"); 200 writer.write(prefix); 201 } 202 writer.write("=\""); 203 writer.write(uri); 204 writer.write("\"\n "); 205 } 206 207 if (zeus_LocaleSet) { 209 writer.write(" locale=\""); 210 writer.write(escapeAttributeValue(locale)); 211 writer.write("\""); 212 } 213 if (getValue() != null) { 214 writer.write(">"); 216 writer.write(getValue()); 217 writer.write("</date>\n"); 218 } else { 219 writer.write("/>\n"); 220 } 221 } 222 223 private String escapeAttributeValue(String attributeValue) { 224 String returnValue = attributeValue; 225 for (int i = 0; i < returnValue.length(); i++) { 226 char ch = returnValue.charAt(i); 227 if (ch == '"') { 228 returnValue = new StringBuffer () 229 .append(returnValue.substring(0, i)) 230 .append(""") 231 .append(returnValue.substring(i+1)) 232 .toString(); 233 } 234 } 235 return returnValue; 236 } 237 238 245 public static void setEntityResolver(EntityResolver resolver) { 246 entityResolver = resolver; 247 } 248 249 256 public static void setErrorHandler(ErrorHandler handler) { 257 errorHandler = handler; 258 } 259 260 public static Date unmarshal(File file) throws IOException { 261 return unmarshal(new FileReader (file)); 263 } 264 265 public static Date unmarshal(File file, boolean validate) throws IOException { 266 return unmarshal(new FileReader (file), validate); 268 } 269 270 public static Date unmarshal(InputStream inputStream) throws IOException { 271 return unmarshal(new InputStreamReader (inputStream)); 273 } 274 275 public static Date unmarshal(InputStream inputStream, boolean validate) throws IOException { 276 return unmarshal(new InputStreamReader (inputStream), validate); 278 } 279 280 public static Date unmarshal(Reader reader) throws IOException { 281 return unmarshal(reader, false); 283 } 284 285 public static Date unmarshal(Reader reader, boolean validate) throws IOException { 286 DateImpl date = new DateImpl(); 287 date.setValidating(validate); 288 date.setCurrentUNode(date); 289 date.setParentUNode(null); 290 XMLReader parser = null; 292 String parserClass = System.getProperty("org.xml.sax.driver", 293 "org.apache.xerces.parsers.SAXParser"); 294 try { 295 parser = XMLReaderFactory.createXMLReader(parserClass); 296 297 if (entityResolver != null) { 299 parser.setEntityResolver(entityResolver); 300 } 301 302 parser.setErrorHandler(date); 304 305 parser.setProperty("http://xml.org/sax/properties/lexical-handler", date); 307 308 parser.setContentHandler(date); 310 } catch (SAXException e) { 311 throw new IOException ("Could not load XML parser: " + 312 e.getMessage()); 313 } 314 315 InputSource inputSource = new InputSource (reader); 316 try { 317 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 318 parser.setFeature("http://xml.org/sax/features/namespaces", true); 319 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 320 parser.parse(inputSource); 321 } catch (SAXException e) { 322 throw new IOException ("Error parsing XML document: " + 323 e.getMessage()); 324 } 325 326 return date; 328 } 329 330 public Unmarshallable getParentUNode() { 331 return zeus_parentUNode; 332 } 333 334 public void setParentUNode(Unmarshallable parentUNode) { 335 this.zeus_parentUNode = parentUNode; 336 } 337 338 public Unmarshallable getCurrentUNode() { 339 return zeus_currentUNode; 340 } 341 342 public void setCurrentUNode(Unmarshallable currentUNode) { 343 this.zeus_currentUNode = currentUNode; 344 } 345 346 public void setValidating(boolean validate) { 347 this.validate = validate; 348 } 349 350 public void startDocument() throws SAXException { 351 } 353 354 public void setDocumentLocator(Locator locator) { 355 } 357 358 public void startPrefixMapping(String prefix, String uri) 359 throws SAXException { 360 namespaceMappings.put(prefix, uri); 361 } 362 363 public void startElement(String namespaceURI, String localName, 364 String qName, org.xml.sax.Attributes atts) 365 throws SAXException { 366 367 Unmarshallable current = getCurrentUNode(); 369 if (current != this) { 370 current.startElement(namespaceURI, localName, qName, atts); 371 return; 372 } 373 374 if ((localName.equals("date")) && (!zeus_thisNodeHandled)) { 376 for (int i=0, len=atts.getLength(); i<len; i++) { 378 String attName= atts.getLocalName(i); 379 String attValue = atts.getValue(i); 380 if (attName.equals("locale")) { 381 setLocale(attValue); 382 } 383 } 384 zeus_thisNodeHandled = true; 385 return; 386 } else { 387 } 389 } 390 391 public void endElement(String namespaceURI, String localName, 392 String qName) 393 throws SAXException { 394 395 Unmarshallable current = getCurrentUNode(); 396 if (current != this) { 397 current.endElement(namespaceURI, localName, qName); 398 return; 399 } 400 401 if (this.value == null) { 403 this.value = ""; 404 } 405 Unmarshallable parent = getCurrentUNode().getParentUNode(); 406 if (parent != null) { 407 parent.setCurrentUNode(parent); 408 } 409 } 410 411 public void characters(char[] ch, int start, int len) 412 throws SAXException { 413 414 Unmarshallable current = getCurrentUNode(); 416 if (current != this) { 417 current.characters(ch, start, len); 418 return; 419 } 420 421 String text = new String (ch, start, len); 422 text = text.trim(); 423 if (this.value == null) { 424 this.value = text; 425 } else { 426 this.value = new StringBuffer (this.value).append(text).toString(); 427 } 428 } 429 430 public void comment(char ch[], int start, int len) throws SAXException { 431 } 433 434 public void warning(SAXParseException e) throws SAXException { 435 if (errorHandler != null) { 436 errorHandler.warning(e); 437 } 438 } 439 440 public void error(SAXParseException e) throws SAXException { 441 if ((validate) && (!hasDTD)) { 442 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."); 443 } 444 if (errorHandler != null) { 445 errorHandler.error(e); 446 } 447 } 448 449 public void fatalError(SAXParseException e) throws SAXException { 450 if ((validate) && (!hasDTD)) { 451 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."); 452 } 453 if (errorHandler != null) { 454 errorHandler.fatalError(e); 455 } 456 } 457 458 public void startDTD(String name, String publicID, String systemID) 459 throws SAXException { 460 461 if ((name == null) || (name.equals(""))) { 462 docTypeString = ""; 463 return; 464 } 465 466 hasDTD = true; 467 StringBuffer docTypeSB = new StringBuffer (); 468 boolean hasPublic = false; 469 470 docTypeSB.append("<!DOCTYPE ") 471 .append(name); 472 473 if ((publicID != null) && (!publicID.equals(""))) { 474 docTypeSB.append(" PUBLIC \"") 475 .append(publicID) 476 .append("\""); 477 hasPublic = true; 478 } 479 480 if ((systemID != null) && (!systemID.equals(""))) { 481 if (!hasPublic) { 482 docTypeSB.append(" SYSTEM"); 483 } 484 docTypeSB.append(" \"") 485 .append(systemID) 486 .append("\""); 487 488 } 489 490 docTypeSB.append(">"); 491 492 docTypeString = docTypeSB.toString(); 493 } 494 495 public void endDTD() throws SAXException { 496 } 498 499 public void startEntity(String name) throws SAXException { 500 } 502 503 public void endEntity(String name) throws SAXException { 504 } 506 507 public void startCDATA() throws SAXException { 508 } 510 511 public void endCDATA() throws SAXException { 512 } 514 515 } 516 | Popular Tags |