1 18 package org.enhydra.barracuda.contrib.sam.xmlform.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 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.LinkedList ; 49 50 public class FormImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , Form { 51 52 private List ref_idList; 53 private InsertBefore insertBefore; 54 private AppendChild appendChild; 55 private List elementList; 56 private List validatorList; 57 private String repeat_prefix; 58 private boolean zeus_Repeat_prefixSet; 59 60 61 private String docTypeString; 62 63 64 private String outputEncoding; 65 66 67 private Unmarshallable zeus_currentUNode; 68 69 70 private Unmarshallable zeus_parentUNode; 71 72 73 private boolean zeus_thisNodeHandled = false; 74 75 76 private boolean hasDTD; 77 78 79 private boolean validate; 80 81 82 private Map namespaceMappings; 83 84 85 private static EntityResolver entityResolver; 86 87 88 private static ErrorHandler errorHandler; 89 90 private static FormImpl prototype = null; 91 92 public static void setPrototype(FormImpl prototype) { 93 FormImpl.prototype = prototype; 94 } 95 public static FormImpl newInstance() { 96 try { 97 return (prototype!=null)?(FormImpl)prototype.clone(): new FormImpl(); 98 } catch (CloneNotSupportedException e) { 99 return null; } 101 } 102 public FormImpl() { 103 ref_idList = new LinkedList (); 104 elementList = new LinkedList (); 105 validatorList = new LinkedList (); 106 zeus_Repeat_prefixSet = false; 107 docTypeString = ""; 108 hasDTD = false; 109 validate = false; 110 namespaceMappings = new HashMap (); 111 } 112 113 public List getRef_idList() { 114 return ref_idList; 115 } 116 117 public void setRef_idList(List ref_idList) { 118 this.ref_idList = ref_idList; 119 } 120 121 public void addRef_id(Ref_id ref_id) { 122 ref_idList.add(ref_id); 123 } 124 125 public void removeRef_id(Ref_id ref_id) { 126 ref_idList.remove(ref_id); 127 } 128 129 public InsertBefore getInsertBefore() { 130 return insertBefore; 131 } 132 133 public void setInsertBefore(InsertBefore insertBefore) { 134 this.insertBefore = insertBefore; 135 } 136 137 public AppendChild getAppendChild() { 138 return appendChild; 139 } 140 141 public void setAppendChild(AppendChild appendChild) { 142 this.appendChild = appendChild; 143 } 144 145 public List getElementList() { 146 return elementList; 147 } 148 149 public void setElementList(List elementList) { 150 this.elementList = elementList; 151 } 152 153 public void addElement(Element element) { 154 elementList.add(element); 155 } 156 157 public void removeElement(Element element) { 158 elementList.remove(element); 159 } 160 161 public List getValidatorList() { 162 return validatorList; 163 } 164 165 public void setValidatorList(List validatorList) { 166 this.validatorList = validatorList; 167 } 168 169 public void addValidator(Validator validator) { 170 validatorList.add(validator); 171 } 172 173 public void removeValidator(Validator validator) { 174 validatorList.remove(validator); 175 } 176 177 public String getRepeat_prefix() { 178 return repeat_prefix; 179 } 180 181 public void setRepeat_prefix(String repeat_prefix) { 182 this.repeat_prefix = repeat_prefix; 183 zeus_Repeat_prefixSet = true; 184 } 185 186 public void setDocType(String name, String publicID, String systemID) { 187 try { 188 startDTD(name, publicID, systemID); 189 } catch (SAXException neverHappens) { } 190 } 191 192 public void setOutputEncoding(String outputEncoding) { 193 this.outputEncoding = outputEncoding; 194 } 195 196 public void marshal(File file) throws IOException { 197 marshal(new FileWriter (file)); 199 } 200 201 public void marshal(OutputStream outputStream) throws IOException { 202 marshal(new OutputStreamWriter (outputStream)); 204 } 205 206 public void marshal(Writer writer) throws IOException { 207 writer.write("<?xml version=\"1.0\" "); 209 if (outputEncoding != null) { 210 writer.write("encoding=\""); 211 writer.write(outputEncoding); 212 writer.write("\"?>\n\n"); 213 214 } else { 215 writer.write("encoding=\"UTF-8\"?>\n\n"); 216 217 } 218 writer.write(docTypeString); 220 writer.write("\n"); 221 writeXMLRepresentation(writer, ""); 223 224 writer.flush(); 226 writer.close(); 227 } 228 229 protected void writeXMLRepresentation(Writer writer, 230 String indent) 231 throws IOException { 232 233 writer.write(indent); 234 writer.write("<form"); 235 236 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 238 String prefix = (String )i.next(); 239 String uri = (String )namespaceMappings.get(prefix); 240 writer.write(" xmlns"); 241 if (!prefix.trim().equals("")) { 242 writer.write(":"); 243 writer.write(prefix); 244 } 245 writer.write("=\""); 246 writer.write(uri); 247 writer.write("\"\n "); 248 } 249 250 if (zeus_Repeat_prefixSet) { 252 writer.write(" repeat_prefix=\""); 253 writer.write(escapeAttributeValue(repeat_prefix)); 254 writer.write("\""); 255 } 256 writer.write(">"); 257 writer.write("\n"); 258 259 for (Iterator i=ref_idList.iterator(); i.hasNext(); ) { 261 Ref_idImpl ref_id = (Ref_idImpl)i.next(); 262 ref_id.writeXMLRepresentation(writer, 263 new StringBuffer (indent).append(" ").toString()); 264 } 265 if (insertBefore != null) { 266 ((InsertBeforeImpl)insertBefore).writeXMLRepresentation(writer, 267 new StringBuffer (indent).append(" ").toString()); 268 } 269 270 if (appendChild != null) { 271 ((AppendChildImpl)appendChild).writeXMLRepresentation(writer, 272 new StringBuffer (indent).append(" ").toString()); 273 } 274 275 for (Iterator i=elementList.iterator(); i.hasNext(); ) { 276 ElementImpl element = (ElementImpl)i.next(); 277 element.writeXMLRepresentation(writer, 278 new StringBuffer (indent).append(" ").toString()); 279 } 280 for (Iterator i=validatorList.iterator(); i.hasNext(); ) { 281 ValidatorImpl validator = (ValidatorImpl)i.next(); 282 validator.writeXMLRepresentation(writer, 283 new StringBuffer (indent).append(" ").toString()); 284 } 285 writer.write(indent); 286 writer.write("</form>\n"); 287 } 288 289 private String escapeAttributeValue(String attributeValue) { 290 String returnValue = attributeValue; 291 for (int i = 0; i < returnValue.length(); i++) { 292 char ch = returnValue.charAt(i); 293 if (ch == '"') { 294 returnValue = new StringBuffer () 295 .append(returnValue.substring(0, i)) 296 .append(""") 297 .append(returnValue.substring(i+1)) 298 .toString(); 299 } 300 } 301 return returnValue; 302 } 303 304 private String escapeTextValue(String textValue) { 305 String returnValue = textValue; 306 for (int i = 0; i < returnValue.length(); i++) { 307 char ch = returnValue.charAt(i); 308 if (ch == '<') { 309 returnValue = new StringBuffer () 310 .append(returnValue.substring(0, i)) 311 .append("<") 312 .append(returnValue.substring(i+1)) 313 .toString(); 314 } else if (ch == '>') { 315 returnValue = new StringBuffer () 316 .append(returnValue.substring(0, i)) 317 .append(">") 318 .append(returnValue.substring(i+1)) 319 .toString(); 320 } 321 } 322 return returnValue; 323 } 324 325 332 public static void setEntityResolver(EntityResolver resolver) { 333 entityResolver = resolver; 334 } 335 336 343 public static void setErrorHandler(ErrorHandler handler) { 344 errorHandler = handler; 345 } 346 347 public static Form unmarshal(File file) throws IOException { 348 return unmarshal(new FileReader (file)); 350 } 351 352 public static Form unmarshal(File file, boolean validate) throws IOException { 353 return unmarshal(new FileReader (file), validate); 355 } 356 357 public static Form unmarshal(InputStream inputStream) throws IOException { 358 return unmarshal(new InputStreamReader (inputStream)); 360 } 361 362 public static Form unmarshal(InputStream inputStream, boolean validate) throws IOException { 363 return unmarshal(new InputStreamReader (inputStream), validate); 365 } 366 367 public static Form unmarshal(Reader reader) throws IOException { 368 return unmarshal(reader, false); 370 } 371 372 public static Form unmarshal(Reader reader, boolean validate) throws IOException { 373 FormImpl form = FormImpl.newInstance(); 374 form.setValidating(validate); 375 form.setCurrentUNode(form); 376 form.setParentUNode(null); 377 XMLReader parser = null; 379 String parserClass = System.getProperty("org.xml.sax.driver", 380 "org.apache.xerces.parsers.SAXParser"); 381 try { 382 parser = XMLReaderFactory.createXMLReader(parserClass); 383 384 if (entityResolver != null) { 386 parser.setEntityResolver(entityResolver); 387 } 388 389 parser.setErrorHandler(form); 391 392 parser.setProperty("http://xml.org/sax/properties/lexical-handler", form); 394 395 parser.setContentHandler(form); 397 } catch (SAXException e) { 398 throw new IOException ("Could not load XML parser: " + 399 e.getMessage()); 400 } 401 402 InputSource inputSource = new InputSource (reader); 403 try { 404 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 405 parser.setFeature("http://xml.org/sax/features/namespaces", true); 406 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 407 parser.parse(inputSource); 408 } catch (SAXException e) { 409 throw new IOException ("Error parsing XML document: " + 410 e.getMessage()); 411 } 412 413 return form; 415 } 416 417 public Unmarshallable getParentUNode() { 418 return zeus_parentUNode; 419 } 420 421 public void setParentUNode(Unmarshallable parentUNode) { 422 this.zeus_parentUNode = parentUNode; 423 } 424 425 public Unmarshallable getCurrentUNode() { 426 return zeus_currentUNode; 427 } 428 429 public void setCurrentUNode(Unmarshallable currentUNode) { 430 this.zeus_currentUNode = currentUNode; 431 } 432 433 public void setValidating(boolean validate) { 434 this.validate = validate; 435 } 436 437 public void startDocument() throws SAXException { 438 } 440 441 public void setDocumentLocator(Locator locator) { 442 } 444 445 public void startPrefixMapping(String prefix, String uri) 446 throws SAXException { 447 namespaceMappings.put(prefix, uri); 448 } 449 450 public void startElement(String namespaceURI, String localName, 451 String qName, org.xml.sax.Attributes atts) 452 throws SAXException { 453 454 Unmarshallable current = getCurrentUNode(); 456 if (current != this) { 457 current.startElement(namespaceURI, localName, qName, atts); 458 return; 459 } 460 461 if ((localName.equals("form")) && (!zeus_thisNodeHandled)) { 463 for (int i=0, len=atts.getLength(); i<len; i++) { 465 String attName= atts.getLocalName(i); 466 String attValue = atts.getValue(i); 467 if (attName.equals("repeat_prefix")) { 468 setRepeat_prefix(attValue); 469 } 470 } 471 zeus_thisNodeHandled = true; 472 return; 473 } else { 474 if (localName.equals("ref_id")) { 476 Ref_idImpl ref_id = Ref_idImpl.newInstance(); 477 current = getCurrentUNode(); 478 ref_id.setParentUNode(current); 479 ref_id.setCurrentUNode(ref_id); 480 this.setCurrentUNode(ref_id); 481 ref_id.startElement(namespaceURI, localName, qName, atts); 482 ref_idList.add(ref_id); 484 return; 485 } 486 if (localName.equals("insertBefore") && (insertBefore==null)) { 487 InsertBeforeImpl insertBefore = InsertBeforeImpl.newInstance(); 488 current = getCurrentUNode(); 489 insertBefore.setParentUNode(current); 490 insertBefore.setCurrentUNode(insertBefore); 491 this.setCurrentUNode(insertBefore); 492 insertBefore.startElement(namespaceURI, localName, qName, atts); 493 this.insertBefore = insertBefore; 495 return; 496 } 497 if (localName.equals("appendChild") && (appendChild==null)) { 498 AppendChildImpl appendChild = AppendChildImpl.newInstance(); 499 current = getCurrentUNode(); 500 appendChild.setParentUNode(current); 501 appendChild.setCurrentUNode(appendChild); 502 this.setCurrentUNode(appendChild); 503 appendChild.startElement(namespaceURI, localName, qName, atts); 504 this.appendChild = appendChild; 506 return; 507 } 508 if (localName.equals("element")) { 509 ElementImpl element = ElementImpl.newInstance(); 510 current = getCurrentUNode(); 511 element.setParentUNode(current); 512 element.setCurrentUNode(element); 513 this.setCurrentUNode(element); 514 element.startElement(namespaceURI, localName, qName, atts); 515 elementList.add(element); 517 return; 518 } 519 if (localName.equals("validator")) { 520 ValidatorImpl validator = ValidatorImpl.newInstance(); 521 current = getCurrentUNode(); 522 validator.setParentUNode(current); 523 validator.setCurrentUNode(validator); 524 this.setCurrentUNode(validator); 525 validator.startElement(namespaceURI, localName, qName, atts); 526 validatorList.add(validator); 528 return; 529 } 530 } 531 } 532 533 public void endElement(String namespaceURI, String localName, 534 String qName) 535 throws SAXException { 536 537 Unmarshallable current = getCurrentUNode(); 538 if (current != this) { 539 current.endElement(namespaceURI, localName, qName); 540 return; 541 } 542 543 Unmarshallable parent = getCurrentUNode().getParentUNode(); 544 if (parent != null) { 545 parent.setCurrentUNode(parent); 546 } 547 } 548 549 public void characters(char[] ch, int start, int len) 550 throws SAXException { 551 552 Unmarshallable current = getCurrentUNode(); 554 if (current != this) { 555 current.characters(ch, start, len); 556 return; 557 } 558 559 String text = new String (ch, start, len); 560 text = text.trim(); 561 } 562 563 public void comment(char ch[], int start, int len) throws SAXException { 564 } 566 567 public void warning(SAXParseException e) throws SAXException { 568 if (errorHandler != null) { 569 errorHandler.warning(e); 570 } 571 } 572 573 public void error(SAXParseException e) throws SAXException { 574 if ((validate) && (!hasDTD)) { 575 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."); 576 } 577 if (errorHandler != null) { 578 errorHandler.error(e); 579 } 580 } 581 582 public void fatalError(SAXParseException e) throws SAXException { 583 if ((validate) && (!hasDTD)) { 584 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."); 585 } 586 if (errorHandler != null) { 587 errorHandler.fatalError(e); 588 } 589 } 590 591 public void startDTD(String name, String publicID, String systemID) 592 throws SAXException { 593 594 if ((name == null) || (name.equals(""))) { 595 docTypeString = ""; 596 return; 597 } 598 599 hasDTD = true; 600 StringBuffer docTypeSB = new StringBuffer (); 601 boolean hasPublic = false; 602 603 docTypeSB.append("<!DOCTYPE ") 604 .append(name); 605 606 if ((publicID != null) && (!publicID.equals(""))) { 607 docTypeSB.append(" PUBLIC \"") 608 .append(publicID) 609 .append("\""); 610 hasPublic = true; 611 } 612 613 if ((systemID != null) && (!systemID.equals(""))) { 614 if (!hasPublic) { 615 docTypeSB.append(" SYSTEM"); 616 } 617 docTypeSB.append(" \"") 618 .append(systemID) 619 .append("\""); 620 621 } 622 623 docTypeSB.append(">"); 624 625 docTypeString = docTypeSB.toString(); 626 } 627 628 public void endDTD() throws SAXException { 629 } 631 632 public void startEntity(String name) throws SAXException { 633 } 635 636 public void endEntity(String name) throws SAXException { 637 } 639 640 public void startCDATA() throws SAXException { 641 } 643 644 public void endCDATA() throws SAXException { 645 } 647 648 } 649 | Popular Tags |