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 EjbLocalRefImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , EjbLocalRef { 46 47 private Description description; 48 private EjbRefName ejbRefName; 49 private EjbRefType ejbRefType; 50 private LocalHome localHome; 51 private Local local; 52 private EjbLink ejbLink; 53 private String id; 54 private boolean zeus_IdSet; 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 private static EjbLocalRefImpl prototype = null; 87 88 public static void setPrototype(EjbLocalRefImpl prototype) { 89 EjbLocalRefImpl.prototype = prototype; 90 } 91 public static EjbLocalRefImpl newInstance() { 92 try { 93 return (prototype!=null)?(EjbLocalRefImpl)prototype.clone(): new EjbLocalRefImpl(); 94 } catch (CloneNotSupportedException e) { 95 return null; } 97 } 98 public EjbLocalRefImpl() { 99 zeus_IdSet = false; 100 docTypeString = ""; 101 hasDTD = false; 102 validate = false; 103 namespaceMappings = new HashMap (); 104 } 105 106 public Description getDescription() { 107 return description; 108 } 109 110 public void setDescription(Description description) { 111 this.description = description; 112 } 113 114 public EjbRefName getEjbRefName() { 115 return ejbRefName; 116 } 117 118 public void setEjbRefName(EjbRefName ejbRefName) { 119 this.ejbRefName = ejbRefName; 120 } 121 122 public EjbRefType getEjbRefType() { 123 return ejbRefType; 124 } 125 126 public void setEjbRefType(EjbRefType ejbRefType) { 127 this.ejbRefType = ejbRefType; 128 } 129 130 public LocalHome getLocalHome() { 131 return localHome; 132 } 133 134 public void setLocalHome(LocalHome localHome) { 135 this.localHome = localHome; 136 } 137 138 public Local getLocal() { 139 return local; 140 } 141 142 public void setLocal(Local local) { 143 this.local = local; 144 } 145 146 public EjbLink getEjbLink() { 147 return ejbLink; 148 } 149 150 public void setEjbLink(EjbLink ejbLink) { 151 this.ejbLink = ejbLink; 152 } 153 154 public String getId() { 155 return id; 156 } 157 158 public void setId(String id) { 159 this.id = id; 160 zeus_IdSet = true; 161 } 162 163 public void setDocType(String name, String publicID, String systemID) { 164 try { 165 startDTD(name, publicID, systemID); 166 } catch (SAXException neverHappens) { } 167 } 168 169 public void setOutputEncoding(String outputEncoding) { 170 this.outputEncoding = outputEncoding; 171 } 172 173 public void marshal(File file) throws IOException { 174 marshal(new FileWriter (file)); 176 } 177 178 public void marshal(OutputStream outputStream) throws IOException { 179 marshal(new OutputStreamWriter (outputStream)); 181 } 182 183 public void marshal(Writer writer) throws IOException { 184 writer.write("<?xml version=\"1.0\" "); 186 if (outputEncoding != null) { 187 writer.write("encoding=\""); 188 writer.write(outputEncoding); 189 writer.write("\"?>\n\n"); 190 191 } else { 192 writer.write("encoding=\"UTF-8\"?>\n\n"); 193 194 } 195 writer.write(docTypeString); 197 writer.write("\n"); 198 writeXMLRepresentation(writer, ""); 200 201 writer.flush(); 203 writer.close(); 204 } 205 206 protected void writeXMLRepresentation(Writer writer, 207 String indent) 208 throws IOException { 209 210 writer.write(indent); 211 writer.write("<ejb-local-ref"); 212 213 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 215 String prefix = (String )i.next(); 216 String uri = (String )namespaceMappings.get(prefix); 217 writer.write(" xmlns"); 218 if (!prefix.trim().equals("")) { 219 writer.write(":"); 220 writer.write(prefix); 221 } 222 writer.write("=\""); 223 writer.write(uri); 224 writer.write("\"\n "); 225 } 226 227 if (zeus_IdSet) { 229 writer.write(" id=\""); 230 writer.write(escapeAttributeValue(id)); 231 writer.write("\""); 232 } 233 writer.write(">"); 234 writer.write("\n"); 235 236 if (description != null) { 238 ((DescriptionImpl)description).writeXMLRepresentation(writer, 239 new StringBuffer (indent).append(" ").toString()); 240 } 241 242 if (ejbRefName != null) { 243 ((EjbRefNameImpl)ejbRefName).writeXMLRepresentation(writer, 244 new StringBuffer (indent).append(" ").toString()); 245 } 246 247 if (ejbRefType != null) { 248 ((EjbRefTypeImpl)ejbRefType).writeXMLRepresentation(writer, 249 new StringBuffer (indent).append(" ").toString()); 250 } 251 252 if (localHome != null) { 253 ((LocalHomeImpl)localHome).writeXMLRepresentation(writer, 254 new StringBuffer (indent).append(" ").toString()); 255 } 256 257 if (local != null) { 258 ((LocalImpl)local).writeXMLRepresentation(writer, 259 new StringBuffer (indent).append(" ").toString()); 260 } 261 262 if (ejbLink != null) { 263 ((EjbLinkImpl)ejbLink).writeXMLRepresentation(writer, 264 new StringBuffer (indent).append(" ").toString()); 265 } 266 267 writer.write(indent); 268 writer.write("</ejb-local-ref>\n"); 269 } 270 271 private String escapeAttributeValue(String attributeValue) { 272 String returnValue = attributeValue; 273 for (int i = 0; i < returnValue.length(); i++) { 274 char ch = returnValue.charAt(i); 275 if (ch == '"') { 276 returnValue = new StringBuffer () 277 .append(returnValue.substring(0, i)) 278 .append(""") 279 .append(returnValue.substring(i+1)) 280 .toString(); 281 } 282 } 283 return returnValue; 284 } 285 286 private String escapeTextValue(String textValue) { 287 String returnValue = textValue; 288 for (int i = 0; i < returnValue.length(); i++) { 289 char ch = returnValue.charAt(i); 290 if (ch == '<') { 291 returnValue = new StringBuffer () 292 .append(returnValue.substring(0, i)) 293 .append("<") 294 .append(returnValue.substring(i+1)) 295 .toString(); 296 } else if (ch == '>') { 297 returnValue = new StringBuffer () 298 .append(returnValue.substring(0, i)) 299 .append(">") 300 .append(returnValue.substring(i+1)) 301 .toString(); 302 } 303 } 304 return returnValue; 305 } 306 307 314 public static void setEntityResolver(EntityResolver resolver) { 315 entityResolver = resolver; 316 } 317 318 325 public static void setErrorHandler(ErrorHandler handler) { 326 errorHandler = handler; 327 } 328 329 public static EjbLocalRef unmarshal(File file) throws IOException { 330 return unmarshal(new FileReader (file)); 332 } 333 334 public static EjbLocalRef unmarshal(File file, boolean validate) throws IOException { 335 return unmarshal(new FileReader (file), validate); 337 } 338 339 public static EjbLocalRef unmarshal(InputStream inputStream) throws IOException { 340 return unmarshal(new InputStreamReader (inputStream)); 342 } 343 344 public static EjbLocalRef unmarshal(InputStream inputStream, boolean validate) throws IOException { 345 return unmarshal(new InputStreamReader (inputStream), validate); 347 } 348 349 public static EjbLocalRef unmarshal(Reader reader) throws IOException { 350 return unmarshal(reader, false); 352 } 353 354 public static EjbLocalRef unmarshal(Reader reader, boolean validate) throws IOException { 355 EjbLocalRefImpl ejbLocalRef = EjbLocalRefImpl.newInstance(); 356 ejbLocalRef.setValidating(validate); 357 ejbLocalRef.setCurrentUNode(ejbLocalRef); 358 ejbLocalRef.setParentUNode(null); 359 XMLReader parser = null; 361 String parserClass = System.getProperty("org.xml.sax.driver", 362 "org.apache.xerces.parsers.SAXParser"); 363 try { 364 parser = XMLReaderFactory.createXMLReader(parserClass); 365 366 if (entityResolver != null) { 368 parser.setEntityResolver(entityResolver); 369 } 370 371 parser.setErrorHandler(ejbLocalRef); 373 374 parser.setProperty("http://xml.org/sax/properties/lexical-handler", ejbLocalRef); 376 377 parser.setContentHandler(ejbLocalRef); 379 } catch (SAXException e) { 380 throw new IOException ("Could not load XML parser: " + 381 e.getMessage()); 382 } 383 384 InputSource inputSource = new InputSource (reader); 385 try { 386 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 387 parser.setFeature("http://xml.org/sax/features/namespaces", true); 388 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 389 parser.parse(inputSource); 390 } catch (SAXException e) { 391 throw new IOException ("Error parsing XML document: " + 392 e.getMessage()); 393 } 394 395 return ejbLocalRef; 397 } 398 399 public Unmarshallable getParentUNode() { 400 return zeus_parentUNode; 401 } 402 403 public void setParentUNode(Unmarshallable parentUNode) { 404 this.zeus_parentUNode = parentUNode; 405 } 406 407 public Unmarshallable getCurrentUNode() { 408 return zeus_currentUNode; 409 } 410 411 public void setCurrentUNode(Unmarshallable currentUNode) { 412 this.zeus_currentUNode = currentUNode; 413 } 414 415 public void setValidating(boolean validate) { 416 this.validate = validate; 417 } 418 419 public void startDocument() throws SAXException { 420 } 422 423 public void setDocumentLocator(Locator locator) { 424 } 426 427 public void startPrefixMapping(String prefix, String uri) 428 throws SAXException { 429 namespaceMappings.put(prefix, uri); 430 } 431 432 public void startElement(String namespaceURI, String localName, 433 String qName, org.xml.sax.Attributes atts) 434 throws SAXException { 435 436 Unmarshallable current = getCurrentUNode(); 438 if (current != this) { 439 current.startElement(namespaceURI, localName, qName, atts); 440 return; 441 } 442 443 if ((localName.equals("ejb-local-ref")) && (!zeus_thisNodeHandled)) { 445 for (int i=0, len=atts.getLength(); i<len; i++) { 447 String attName= atts.getLocalName(i); 448 String attValue = atts.getValue(i); 449 if (attName.equals("id")) { 450 setId(attValue); 451 } 452 } 453 zeus_thisNodeHandled = true; 454 return; 455 } else { 456 if (localName.equals("description") && (description==null)) { 458 DescriptionImpl description = DescriptionImpl.newInstance(); 459 current = getCurrentUNode(); 460 description.setParentUNode(current); 461 description.setCurrentUNode(description); 462 this.setCurrentUNode(description); 463 description.startElement(namespaceURI, localName, qName, atts); 464 this.description = description; 466 return; 467 } 468 if (localName.equals("ejb-ref-name") && (ejbRefName==null)) { 469 EjbRefNameImpl ejbRefName = EjbRefNameImpl.newInstance(); 470 current = getCurrentUNode(); 471 ejbRefName.setParentUNode(current); 472 ejbRefName.setCurrentUNode(ejbRefName); 473 this.setCurrentUNode(ejbRefName); 474 ejbRefName.startElement(namespaceURI, localName, qName, atts); 475 this.ejbRefName = ejbRefName; 477 return; 478 } 479 if (localName.equals("ejb-ref-type") && (ejbRefType==null)) { 480 EjbRefTypeImpl ejbRefType = EjbRefTypeImpl.newInstance(); 481 current = getCurrentUNode(); 482 ejbRefType.setParentUNode(current); 483 ejbRefType.setCurrentUNode(ejbRefType); 484 this.setCurrentUNode(ejbRefType); 485 ejbRefType.startElement(namespaceURI, localName, qName, atts); 486 this.ejbRefType = ejbRefType; 488 return; 489 } 490 if (localName.equals("local-home") && (localHome==null)) { 491 LocalHomeImpl localHome = LocalHomeImpl.newInstance(); 492 current = getCurrentUNode(); 493 localHome.setParentUNode(current); 494 localHome.setCurrentUNode(localHome); 495 this.setCurrentUNode(localHome); 496 localHome.startElement(namespaceURI, localName, qName, atts); 497 this.localHome = localHome; 499 return; 500 } 501 if (localName.equals("local") && (local==null)) { 502 LocalImpl local = LocalImpl.newInstance(); 503 current = getCurrentUNode(); 504 local.setParentUNode(current); 505 local.setCurrentUNode(local); 506 this.setCurrentUNode(local); 507 local.startElement(namespaceURI, localName, qName, atts); 508 this.local = local; 510 return; 511 } 512 if (localName.equals("ejb-link") && (ejbLink==null)) { 513 EjbLinkImpl ejbLink = EjbLinkImpl.newInstance(); 514 current = getCurrentUNode(); 515 ejbLink.setParentUNode(current); 516 ejbLink.setCurrentUNode(ejbLink); 517 this.setCurrentUNode(ejbLink); 518 ejbLink.startElement(namespaceURI, localName, qName, atts); 519 this.ejbLink = ejbLink; 521 return; 522 } 523 } 524 } 525 526 public void endElement(String namespaceURI, String localName, 527 String qName) 528 throws SAXException { 529 530 Unmarshallable current = getCurrentUNode(); 531 if (current != this) { 532 current.endElement(namespaceURI, localName, qName); 533 return; 534 } 535 536 Unmarshallable parent = getCurrentUNode().getParentUNode(); 537 if (parent != null) { 538 parent.setCurrentUNode(parent); 539 } 540 } 541 542 public void characters(char[] ch, int start, int len) 543 throws SAXException { 544 545 Unmarshallable current = getCurrentUNode(); 547 if (current != this) { 548 current.characters(ch, start, len); 549 return; 550 } 551 552 String text = new String (ch, start, len); 553 } 554 555 public void comment(char ch[], int start, int len) throws SAXException { 556 } 558 559 public void warning(SAXParseException e) throws SAXException { 560 if (errorHandler != null) { 561 errorHandler.warning(e); 562 } 563 } 564 565 public void error(SAXParseException e) throws SAXException { 566 if ((validate) && (!hasDTD)) { 567 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."); 568 } 569 if (errorHandler != null) { 570 errorHandler.error(e); 571 } 572 } 573 574 public void fatalError(SAXParseException e) throws SAXException { 575 if ((validate) && (!hasDTD)) { 576 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."); 577 } 578 if (errorHandler != null) { 579 errorHandler.fatalError(e); 580 } 581 } 582 583 public void startDTD(String name, String publicID, String systemID) 584 throws SAXException { 585 586 if ((name == null) || (name.equals(""))) { 587 docTypeString = ""; 588 return; 589 } 590 591 hasDTD = true; 592 StringBuffer docTypeSB = new StringBuffer (); 593 boolean hasPublic = false; 594 595 docTypeSB.append("<!DOCTYPE ") 596 .append(name); 597 598 if ((publicID != null) && (!publicID.equals(""))) { 599 docTypeSB.append(" PUBLIC \"") 600 .append(publicID) 601 .append("\""); 602 hasPublic = true; 603 } 604 605 if ((systemID != null) && (!systemID.equals(""))) { 606 if (!hasPublic) { 607 docTypeSB.append(" SYSTEM"); 608 } 609 docTypeSB.append(" \"") 610 .append(systemID) 611 .append("\""); 612 613 } 614 615 docTypeSB.append(">"); 616 617 docTypeString = docTypeSB.toString(); 618 } 619 620 public void endDTD() throws SAXException { 621 } 623 624 public void startEntity(String name) throws SAXException { 625 } 627 628 public void endEntity(String name) throws SAXException { 629 } 631 632 public void startCDATA() throws SAXException { 633 } 635 636 public void endCDATA() throws SAXException { 637 } 639 640 } 641 | Popular Tags |