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 public class Ref_idImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , Ref_id { 46 47 private String input_id; 48 private boolean zeus_Input_idSet; 49 private String type; 50 private static boolean zeus_TypeInitialized; 51 private static java.util.Vector zeus_validType; 52 private boolean zeus_TypeSet; 53 private String label_id; 54 private boolean zeus_Label_idSet; 55 private String name_id; 56 private boolean zeus_Name_idSet; 57 private String id; 58 private boolean zeus_IdSet; 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 Ref_idImpl prototype = null; 91 92 public static void setPrototype(Ref_idImpl prototype) { 93 Ref_idImpl.prototype = prototype; 94 } 95 public static Ref_idImpl newInstance() { 96 try { 97 return (prototype!=null)?(Ref_idImpl)prototype.clone(): new Ref_idImpl(); 98 } catch (CloneNotSupportedException e) { 99 return null; } 101 } 102 public Ref_idImpl() { 103 zeus_Input_idSet = false; 104 zeus_TypeInitialized = false; 105 zeus_validType = null; 106 zeus_TypeSet = false; 107 zeus_Label_idSet = false; 108 zeus_Name_idSet = false; 109 zeus_IdSet = false; 110 docTypeString = ""; 111 hasDTD = false; 112 validate = false; 113 namespaceMappings = new HashMap (); 114 } 115 116 public String getInput_id() { 117 return input_id; 118 } 119 120 public void setInput_id(String input_id) { 121 this.input_id = input_id; 122 zeus_Input_idSet = true; 123 } 124 125 public String getType() { 126 return type; 127 } 128 129 public void setType(String type) 130 throws IllegalArgumentException { 131 if (!zeus_TypeInitialized) { 132 zeus_validType = new java.util.Vector (); 133 zeus_validType.addElement("text"); 134 zeus_validType.addElement("textarea"); 135 zeus_validType.addElement("select"); 136 zeus_validType.addElement("radio"); 137 zeus_validType.addElement("hidden"); 138 zeus_TypeInitialized = true; 139 } 140 if (!zeus_validType.contains(type)) { 141 throw new IllegalArgumentException ("Illegal value for attribute 'type'"); 142 } else { 143 this.type = type; 144 this.zeus_TypeSet = true; 145 } 146 } 147 148 public String getLabel_id() { 149 return label_id; 150 } 151 152 public void setLabel_id(String label_id) { 153 this.label_id = label_id; 154 zeus_Label_idSet = true; 155 } 156 157 public String getName_id() { 158 return name_id; 159 } 160 161 public void setName_id(String name_id) { 162 this.name_id = name_id; 163 zeus_Name_idSet = true; 164 } 165 166 public String getId() { 167 return id; 168 } 169 170 public void setId(String id) { 171 this.id = id; 172 zeus_IdSet = true; 173 } 174 175 public void setDocType(String name, String publicID, String systemID) { 176 try { 177 startDTD(name, publicID, systemID); 178 } catch (SAXException neverHappens) { } 179 } 180 181 public void setOutputEncoding(String outputEncoding) { 182 this.outputEncoding = outputEncoding; 183 } 184 185 public void marshal(File file) throws IOException { 186 marshal(new FileWriter (file)); 188 } 189 190 public void marshal(OutputStream outputStream) throws IOException { 191 marshal(new OutputStreamWriter (outputStream)); 193 } 194 195 public void marshal(Writer writer) throws IOException { 196 writer.write("<?xml version=\"1.0\" "); 198 if (outputEncoding != null) { 199 writer.write("encoding=\""); 200 writer.write(outputEncoding); 201 writer.write("\"?>\n\n"); 202 203 } else { 204 writer.write("encoding=\"UTF-8\"?>\n\n"); 205 206 } 207 writer.write(docTypeString); 209 writer.write("\n"); 210 writeXMLRepresentation(writer, ""); 212 213 writer.flush(); 215 writer.close(); 216 } 217 218 protected void writeXMLRepresentation(Writer writer, 219 String indent) 220 throws IOException { 221 222 writer.write(indent); 223 writer.write("<ref_id"); 224 225 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 227 String prefix = (String )i.next(); 228 String uri = (String )namespaceMappings.get(prefix); 229 writer.write(" xmlns"); 230 if (!prefix.trim().equals("")) { 231 writer.write(":"); 232 writer.write(prefix); 233 } 234 writer.write("=\""); 235 writer.write(uri); 236 writer.write("\"\n "); 237 } 238 239 if (zeus_Input_idSet) { 241 writer.write(" input_id=\""); 242 writer.write(escapeAttributeValue(input_id)); 243 writer.write("\""); 244 } 245 if (zeus_TypeSet) { 246 writer.write(" type=\""); 247 writer.write(escapeAttributeValue(type)); 248 writer.write("\""); 249 } 250 if (zeus_Label_idSet) { 251 writer.write(" label_id=\""); 252 writer.write(escapeAttributeValue(label_id)); 253 writer.write("\""); 254 } 255 if (zeus_Name_idSet) { 256 writer.write(" name_id=\""); 257 writer.write(escapeAttributeValue(name_id)); 258 writer.write("\""); 259 } 260 if (zeus_IdSet) { 261 writer.write(" id=\""); 262 writer.write(escapeAttributeValue(id)); 263 writer.write("\""); 264 } 265 writer.write("/>\n"); 266 } 267 268 private String escapeAttributeValue(String attributeValue) { 269 String returnValue = attributeValue; 270 for (int i = 0; i < returnValue.length(); i++) { 271 char ch = returnValue.charAt(i); 272 if (ch == '"') { 273 returnValue = new StringBuffer () 274 .append(returnValue.substring(0, i)) 275 .append(""") 276 .append(returnValue.substring(i+1)) 277 .toString(); 278 } 279 } 280 return returnValue; 281 } 282 283 private String escapeTextValue(String textValue) { 284 String returnValue = textValue; 285 for (int i = 0; i < returnValue.length(); i++) { 286 char ch = returnValue.charAt(i); 287 if (ch == '<') { 288 returnValue = new StringBuffer () 289 .append(returnValue.substring(0, i)) 290 .append("<") 291 .append(returnValue.substring(i+1)) 292 .toString(); 293 } else 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 311 public static void setEntityResolver(EntityResolver resolver) { 312 entityResolver = resolver; 313 } 314 315 322 public static void setErrorHandler(ErrorHandler handler) { 323 errorHandler = handler; 324 } 325 326 public static Ref_id unmarshal(File file) throws IOException { 327 return unmarshal(new FileReader (file)); 329 } 330 331 public static Ref_id unmarshal(File file, boolean validate) throws IOException { 332 return unmarshal(new FileReader (file), validate); 334 } 335 336 public static Ref_id unmarshal(InputStream inputStream) throws IOException { 337 return unmarshal(new InputStreamReader (inputStream)); 339 } 340 341 public static Ref_id unmarshal(InputStream inputStream, boolean validate) throws IOException { 342 return unmarshal(new InputStreamReader (inputStream), validate); 344 } 345 346 public static Ref_id unmarshal(Reader reader) throws IOException { 347 return unmarshal(reader, false); 349 } 350 351 public static Ref_id unmarshal(Reader reader, boolean validate) throws IOException { 352 Ref_idImpl ref_id = Ref_idImpl.newInstance(); 353 ref_id.setValidating(validate); 354 ref_id.setCurrentUNode(ref_id); 355 ref_id.setParentUNode(null); 356 XMLReader parser = null; 358 String parserClass = System.getProperty("org.xml.sax.driver", 359 "org.apache.xerces.parsers.SAXParser"); 360 try { 361 parser = XMLReaderFactory.createXMLReader(parserClass); 362 363 if (entityResolver != null) { 365 parser.setEntityResolver(entityResolver); 366 } 367 368 parser.setErrorHandler(ref_id); 370 371 parser.setProperty("http://xml.org/sax/properties/lexical-handler", ref_id); 373 374 parser.setContentHandler(ref_id); 376 } catch (SAXException e) { 377 throw new IOException ("Could not load XML parser: " + 378 e.getMessage()); 379 } 380 381 InputSource inputSource = new InputSource (reader); 382 try { 383 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 384 parser.setFeature("http://xml.org/sax/features/namespaces", true); 385 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 386 parser.parse(inputSource); 387 } catch (SAXException e) { 388 throw new IOException ("Error parsing XML document: " + 389 e.getMessage()); 390 } 391 392 return ref_id; 394 } 395 396 public Unmarshallable getParentUNode() { 397 return zeus_parentUNode; 398 } 399 400 public void setParentUNode(Unmarshallable parentUNode) { 401 this.zeus_parentUNode = parentUNode; 402 } 403 404 public Unmarshallable getCurrentUNode() { 405 return zeus_currentUNode; 406 } 407 408 public void setCurrentUNode(Unmarshallable currentUNode) { 409 this.zeus_currentUNode = currentUNode; 410 } 411 412 public void setValidating(boolean validate) { 413 this.validate = validate; 414 } 415 416 public void startDocument() throws SAXException { 417 } 419 420 public void setDocumentLocator(Locator locator) { 421 } 423 424 public void startPrefixMapping(String prefix, String uri) 425 throws SAXException { 426 namespaceMappings.put(prefix, uri); 427 } 428 429 public void startElement(String namespaceURI, String localName, 430 String qName, org.xml.sax.Attributes atts) 431 throws SAXException { 432 433 Unmarshallable current = getCurrentUNode(); 435 if (current != this) { 436 current.startElement(namespaceURI, localName, qName, atts); 437 return; 438 } 439 440 if ((localName.equals("ref_id")) && (!zeus_thisNodeHandled)) { 442 for (int i=0, len=atts.getLength(); i<len; i++) { 444 String attName= atts.getLocalName(i); 445 String attValue = atts.getValue(i); 446 if (attName.equals("input_id")) { 447 setInput_id(attValue); 448 } 449 if (attName.equals("type")) { 450 setType(attValue); 451 } 452 if (attName.equals("label_id")) { 453 setLabel_id(attValue); 454 } 455 if (attName.equals("name_id")) { 456 setName_id(attValue); 457 } 458 if (attName.equals("id")) { 459 setId(attValue); 460 } 461 } 462 zeus_thisNodeHandled = true; 463 return; 464 } else { 465 } 467 } 468 469 public void endElement(String namespaceURI, String localName, 470 String qName) 471 throws SAXException { 472 473 Unmarshallable current = getCurrentUNode(); 474 if (current != this) { 475 current.endElement(namespaceURI, localName, qName); 476 return; 477 } 478 479 Unmarshallable parent = getCurrentUNode().getParentUNode(); 480 if (parent != null) { 481 parent.setCurrentUNode(parent); 482 } 483 } 484 485 public void characters(char[] ch, int start, int len) 486 throws SAXException { 487 488 Unmarshallable current = getCurrentUNode(); 490 if (current != this) { 491 current.characters(ch, start, len); 492 return; 493 } 494 495 String text = new String (ch, start, len); 496 text = text.trim(); 497 } 498 499 public void comment(char ch[], int start, int len) throws SAXException { 500 } 502 503 public void warning(SAXParseException e) throws SAXException { 504 if (errorHandler != null) { 505 errorHandler.warning(e); 506 } 507 } 508 509 public void error(SAXParseException e) throws SAXException { 510 if ((validate) && (!hasDTD)) { 511 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."); 512 } 513 if (errorHandler != null) { 514 errorHandler.error(e); 515 } 516 } 517 518 public void fatalError(SAXParseException e) throws SAXException { 519 if ((validate) && (!hasDTD)) { 520 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."); 521 } 522 if (errorHandler != null) { 523 errorHandler.fatalError(e); 524 } 525 } 526 527 public void startDTD(String name, String publicID, String systemID) 528 throws SAXException { 529 530 if ((name == null) || (name.equals(""))) { 531 docTypeString = ""; 532 return; 533 } 534 535 hasDTD = true; 536 StringBuffer docTypeSB = new StringBuffer (); 537 boolean hasPublic = false; 538 539 docTypeSB.append("<!DOCTYPE ") 540 .append(name); 541 542 if ((publicID != null) && (!publicID.equals(""))) { 543 docTypeSB.append(" PUBLIC \"") 544 .append(publicID) 545 .append("\""); 546 hasPublic = true; 547 } 548 549 if ((systemID != null) && (!systemID.equals(""))) { 550 if (!hasPublic) { 551 docTypeSB.append(" SYSTEM"); 552 } 553 docTypeSB.append(" \"") 554 .append(systemID) 555 .append("\""); 556 557 } 558 559 docTypeSB.append(">"); 560 561 docTypeString = docTypeSB.toString(); 562 } 563 564 public void endDTD() throws SAXException { 565 } 567 568 public void startEntity(String name) throws SAXException { 569 } 571 572 public void endEntity(String name) throws SAXException { 573 } 575 576 public void startCDATA() throws SAXException { 577 } 579 580 public void endCDATA() throws SAXException { 581 } 583 584 } 585 | Popular Tags |