1 18 package org.enhydra.convert.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 EnvEntryImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , EnvEntry { 46 47 private Description description; 48 private EnvEntryName envEntryName; 49 private EnvEntryValue envEntryValue; 50 private EnvEntryType envEntryType; 51 private String id; 52 private boolean zeus_IdSet; 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 private static EnvEntryImpl prototype = null; 85 86 public static void setPrototype(EnvEntryImpl prototype) { 87 EnvEntryImpl.prototype = prototype; 88 } 89 public static EnvEntryImpl newInstance() { 90 try { 91 return (prototype!=null)?(EnvEntryImpl)prototype.clone(): new EnvEntryImpl(); 92 } catch (CloneNotSupportedException e) { 93 return null; } 95 } 96 public EnvEntryImpl() { 97 zeus_IdSet = false; 98 docTypeString = ""; 99 hasDTD = false; 100 validate = false; 101 namespaceMappings = new HashMap (); 102 } 103 104 public Description getDescription() { 105 return description; 106 } 107 108 public void setDescription(Description description) { 109 this.description = description; 110 } 111 112 public EnvEntryName getEnvEntryName() { 113 return envEntryName; 114 } 115 116 public void setEnvEntryName(EnvEntryName envEntryName) { 117 this.envEntryName = envEntryName; 118 } 119 120 public EnvEntryValue getEnvEntryValue() { 121 return envEntryValue; 122 } 123 124 public void setEnvEntryValue(EnvEntryValue envEntryValue) { 125 this.envEntryValue = envEntryValue; 126 } 127 128 public EnvEntryType getEnvEntryType() { 129 return envEntryType; 130 } 131 132 public void setEnvEntryType(EnvEntryType envEntryType) { 133 this.envEntryType = envEntryType; 134 } 135 136 public String getId() { 137 return id; 138 } 139 140 public void setId(String id) { 141 this.id = id; 142 zeus_IdSet = true; 143 } 144 145 public void setDocType(String name, String publicID, String systemID) { 146 try { 147 startDTD(name, publicID, systemID); 148 } catch (SAXException neverHappens) { } 149 } 150 151 public void setOutputEncoding(String outputEncoding) { 152 this.outputEncoding = outputEncoding; 153 } 154 155 public void marshal(File file) throws IOException { 156 marshal(new FileWriter (file)); 158 } 159 160 public void marshal(OutputStream outputStream) throws IOException { 161 marshal(new OutputStreamWriter (outputStream)); 163 } 164 165 public void marshal(Writer writer) throws IOException { 166 writer.write("<?xml version=\"1.0\" "); 168 if (outputEncoding != null) { 169 writer.write("encoding=\""); 170 writer.write(outputEncoding); 171 writer.write("\"?>\n\n"); 172 173 } else { 174 writer.write("encoding=\"UTF-8\"?>\n\n"); 175 176 } 177 writer.write(docTypeString); 179 writer.write("\n"); 180 writeXMLRepresentation(writer, ""); 182 183 writer.flush(); 185 writer.close(); 186 } 187 188 protected void writeXMLRepresentation(Writer writer, 189 String indent) 190 throws IOException { 191 192 writer.write(indent); 193 194 197 if (description != null) { 198 if (!description.getValue().equals("")){ 199 writer.write("\n<!--\n" + description.getValue() + "\n-->\n "); 200 description.setValue(""); 201 } 202 } 203 204 writer.write("<env-entry"); 205 206 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 208 String prefix = (String )i.next(); 209 String uri = (String )namespaceMappings.get(prefix); 210 writer.write(" xmlns"); 211 if (!prefix.trim().equals("")) { 212 writer.write(":"); 213 writer.write(prefix); 214 } 215 writer.write("=\""); 216 writer.write(uri); 217 writer.write("\"\n "); 218 } 219 220 if (zeus_IdSet) { 222 writer.write(" id=\""); 223 writer.write(escapeAttributeValue(id)); 224 writer.write("\""); 225 } 226 writer.write(">"); 227 writer.write("\n"); 228 229 if (description != null) { 231 ((DescriptionImpl)description).writeXMLRepresentation(writer, 232 new StringBuffer (indent).append(" ").toString()); 233 } 234 235 if (envEntryName != null) { 236 ((EnvEntryNameImpl)envEntryName).writeXMLRepresentation(writer, 237 new StringBuffer (indent).append(" ").toString()); 238 } 239 240 if (envEntryValue != null) { 241 ((EnvEntryValueImpl)envEntryValue).writeXMLRepresentation(writer, 242 new StringBuffer (indent).append(" ").toString()); 243 } 244 245 if (envEntryType != null) { 246 ((EnvEntryTypeImpl)envEntryType).writeXMLRepresentation(writer, 247 new StringBuffer (indent).append(" ").toString()); 248 } 249 250 writer.write(indent); 251 writer.write("</env-entry>\n"); 252 } 253 254 private String escapeAttributeValue(String attributeValue) { 255 String returnValue = attributeValue; 256 for (int i = 0; i < returnValue.length(); i++) { 257 char ch = returnValue.charAt(i); 258 if (ch == '"') { 259 returnValue = new StringBuffer () 260 .append(returnValue.substring(0, i)) 261 .append(""") 262 .append(returnValue.substring(i+1)) 263 .toString(); 264 } 265 } 266 return returnValue; 267 } 268 269 private String escapeTextValue(String textValue) { 270 String returnValue = textValue; 271 for (int i = 0; i < returnValue.length(); i++) { 272 char ch = returnValue.charAt(i); 273 if (ch == '<') { 274 returnValue = new StringBuffer () 275 .append(returnValue.substring(0, i)) 276 .append("<") 277 .append(returnValue.substring(i+1)) 278 .toString(); 279 } else if (ch == '>') { 280 returnValue = new StringBuffer () 281 .append(returnValue.substring(0, i)) 282 .append(">") 283 .append(returnValue.substring(i+1)) 284 .toString(); 285 } 286 } 287 return returnValue; 288 } 289 290 297 public static void setEntityResolver(EntityResolver resolver) { 298 entityResolver = resolver; 299 } 300 301 308 public static void setErrorHandler(ErrorHandler handler) { 309 errorHandler = handler; 310 } 311 312 public static EnvEntry unmarshal(File file) throws IOException { 313 return unmarshal(new FileReader (file)); 315 } 316 317 public static EnvEntry unmarshal(File file, boolean validate) throws IOException { 318 return unmarshal(new FileReader (file), validate); 320 } 321 322 public static EnvEntry unmarshal(InputStream inputStream) throws IOException { 323 return unmarshal(new InputStreamReader (inputStream)); 325 } 326 327 public static EnvEntry unmarshal(InputStream inputStream, boolean validate) throws IOException { 328 return unmarshal(new InputStreamReader (inputStream), validate); 330 } 331 332 public static EnvEntry unmarshal(Reader reader) throws IOException { 333 return unmarshal(reader, false); 335 } 336 337 public static EnvEntry unmarshal(Reader reader, boolean validate) throws IOException { 338 EnvEntryImpl envEntry = EnvEntryImpl.newInstance(); 339 envEntry.setValidating(validate); 340 envEntry.setCurrentUNode(envEntry); 341 envEntry.setParentUNode(null); 342 XMLReader parser = null; 344 String parserClass = System.getProperty("org.xml.sax.driver", 345 "org.apache.xerces.parsers.SAXParser"); 346 try { 347 parser = XMLReaderFactory.createXMLReader(parserClass); 348 349 if (entityResolver != null) { 351 parser.setEntityResolver(entityResolver); 352 } 353 354 parser.setErrorHandler(envEntry); 356 357 parser.setProperty("http://xml.org/sax/properties/lexical-handler", envEntry); 359 360 parser.setContentHandler(envEntry); 362 } catch (SAXException e) { 363 throw new IOException ("Could not load XML parser: " + 364 e.getMessage()); 365 } 366 367 InputSource inputSource = new InputSource (reader); 368 try { 369 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 370 parser.setFeature("http://xml.org/sax/features/namespaces", true); 371 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 372 parser.parse(inputSource); 373 } catch (SAXException e) { 374 throw new IOException ("Error parsing XML document: " + 375 e.getMessage()); 376 } 377 378 return envEntry; 380 } 381 382 public Unmarshallable getParentUNode() { 383 return zeus_parentUNode; 384 } 385 386 public void setParentUNode(Unmarshallable parentUNode) { 387 this.zeus_parentUNode = parentUNode; 388 } 389 390 public Unmarshallable getCurrentUNode() { 391 return zeus_currentUNode; 392 } 393 394 public void setCurrentUNode(Unmarshallable currentUNode) { 395 this.zeus_currentUNode = currentUNode; 396 } 397 398 public void setValidating(boolean validate) { 399 this.validate = validate; 400 } 401 402 public void startDocument() throws SAXException { 403 } 405 406 public void setDocumentLocator(Locator locator) { 407 } 409 410 public void startPrefixMapping(String prefix, String uri) 411 throws SAXException { 412 namespaceMappings.put(prefix, uri); 413 } 414 415 public void startElement(String namespaceURI, String localName, 416 String qName, org.xml.sax.Attributes atts) 417 throws SAXException { 418 419 Unmarshallable current = getCurrentUNode(); 421 if (current != this) { 422 current.startElement(namespaceURI, localName, qName, atts); 423 return; 424 } 425 426 if ((localName.equals("env-entry")) && (!zeus_thisNodeHandled)) { 428 for (int i=0, len=atts.getLength(); i<len; i++) { 430 String attName= atts.getLocalName(i); 431 String attValue = atts.getValue(i); 432 if (attName.equals("id")) { 433 setId(attValue); 434 } 435 } 436 zeus_thisNodeHandled = true; 437 return; 438 } else { 439 if (localName.equals("description") && (description==null)) { 441 DescriptionImpl description = DescriptionImpl.newInstance(); 442 current = getCurrentUNode(); 443 description.setParentUNode(current); 444 description.setCurrentUNode(description); 445 this.setCurrentUNode(description); 446 description.startElement(namespaceURI, localName, qName, atts); 447 this.description = description; 449 return; 450 } 451 if (localName.equals("env-entry-name") && (envEntryName==null)) { 452 EnvEntryNameImpl envEntryName = EnvEntryNameImpl.newInstance(); 453 current = getCurrentUNode(); 454 envEntryName.setParentUNode(current); 455 envEntryName.setCurrentUNode(envEntryName); 456 this.setCurrentUNode(envEntryName); 457 envEntryName.startElement(namespaceURI, localName, qName, atts); 458 this.envEntryName = envEntryName; 460 return; 461 } 462 if (localName.equals("env-entry-value") && (envEntryValue==null)) { 463 EnvEntryValueImpl envEntryValue = EnvEntryValueImpl.newInstance(); 464 current = getCurrentUNode(); 465 envEntryValue.setParentUNode(current); 466 envEntryValue.setCurrentUNode(envEntryValue); 467 this.setCurrentUNode(envEntryValue); 468 envEntryValue.startElement(namespaceURI, localName, qName, atts); 469 this.envEntryValue = envEntryValue; 471 return; 472 } 473 if (localName.equals("env-entry-type") && (envEntryType==null)) { 474 EnvEntryTypeImpl envEntryType = EnvEntryTypeImpl.newInstance(); 475 current = getCurrentUNode(); 476 envEntryType.setParentUNode(current); 477 envEntryType.setCurrentUNode(envEntryType); 478 this.setCurrentUNode(envEntryType); 479 envEntryType.startElement(namespaceURI, localName, qName, atts); 480 this.envEntryType = envEntryType; 482 return; 483 } 484 } 485 } 486 487 public void endElement(String namespaceURI, String localName, 488 String qName) 489 throws SAXException { 490 491 Unmarshallable current = getCurrentUNode(); 492 if (current != this) { 493 current.endElement(namespaceURI, localName, qName); 494 return; 495 } 496 497 Unmarshallable parent = getCurrentUNode().getParentUNode(); 498 if (parent != null) { 499 parent.setCurrentUNode(parent); 500 } 501 } 502 503 public void characters(char[] ch, int start, int len) 504 throws SAXException { 505 506 Unmarshallable current = getCurrentUNode(); 508 if (current != this) { 509 current.characters(ch, start, len); 510 return; 511 } 512 513 String text = new String (ch, start, len); 514 } 515 516 public void comment(char ch[], int start, int len) throws SAXException { 517 } 519 520 public void warning(SAXParseException e) throws SAXException { 521 if (errorHandler != null) { 522 errorHandler.warning(e); 523 } 524 } 525 526 public void error(SAXParseException e) throws SAXException { 527 if ((validate) && (!hasDTD)) { 528 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."); 529 } 530 if (errorHandler != null) { 531 errorHandler.error(e); 532 } 533 } 534 535 public void fatalError(SAXParseException e) throws SAXException { 536 if ((validate) && (!hasDTD)) { 537 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."); 538 } 539 if (errorHandler != null) { 540 errorHandler.fatalError(e); 541 } 542 } 543 544 public void startDTD(String name, String publicID, String systemID) 545 throws SAXException { 546 547 if ((name == null) || (name.equals(""))) { 548 docTypeString = ""; 549 return; 550 } 551 552 hasDTD = true; 553 StringBuffer docTypeSB = new StringBuffer (); 554 boolean hasPublic = false; 555 556 docTypeSB.append("<!DOCTYPE ") 557 .append(name); 558 559 if ((publicID != null) && (!publicID.equals(""))) { 560 docTypeSB.append(" PUBLIC \"") 561 .append(publicID) 562 .append("\""); 563 hasPublic = true; 564 } 565 566 if ((systemID != null) && (!systemID.equals(""))) { 567 if (!hasPublic) { 568 docTypeSB.append(" SYSTEM"); 569 } 570 docTypeSB.append(" \"") 571 .append(systemID) 572 .append("\""); 573 574 } 575 576 docTypeSB.append(">"); 577 578 docTypeString = docTypeSB.toString(); 579 } 580 581 public void endDTD() throws SAXException { 582 } 584 585 public void startEntity(String name) throws SAXException { 586 } 588 589 public void endEntity(String name) throws SAXException { 590 } 592 593 public void startCDATA() throws SAXException { 594 } 596 597 public void endCDATA() throws SAXException { 598 } 600 601 } 602 | Popular Tags |