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