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 import java.util.Iterator ; 47 import java.util.List ; 48 import java.util.LinkedList ; 49 50 public class WelcomeFileListImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , WelcomeFileList { 51 52 private List welcomeFileList; 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 WelcomeFileListImpl prototype = null; 87 88 public static void setPrototype(WelcomeFileListImpl prototype) { 89 WelcomeFileListImpl.prototype = prototype; 90 } 91 public static WelcomeFileListImpl newInstance() { 92 try { 93 return (prototype!=null)?(WelcomeFileListImpl)prototype.clone(): new WelcomeFileListImpl(); 94 } catch (CloneNotSupportedException e) { 95 return null; } 97 } 98 public WelcomeFileListImpl() { 99 welcomeFileList = new LinkedList (); 100 zeus_IdSet = false; 101 docTypeString = ""; 102 hasDTD = false; 103 validate = false; 104 namespaceMappings = new HashMap (); 105 } 106 107 public List getWelcomeFileList() { 108 return welcomeFileList; 109 } 110 111 public void setWelcomeFileList(List welcomeFileList) { 112 this.welcomeFileList = welcomeFileList; 113 } 114 115 public void addWelcomeFile(WelcomeFile welcomeFile) { 116 welcomeFileList.add(welcomeFile); 117 } 118 119 public void removeWelcomeFile(WelcomeFile welcomeFile) { 120 welcomeFileList.remove(welcomeFile); 121 } 122 123 public String getId() { 124 return id; 125 } 126 127 public void setId(String id) { 128 this.id = id; 129 zeus_IdSet = true; 130 } 131 132 public void setDocType(String name, String publicID, String systemID) { 133 try { 134 startDTD(name, publicID, systemID); 135 } catch (SAXException neverHappens) { } 136 } 137 138 public void setOutputEncoding(String outputEncoding) { 139 this.outputEncoding = outputEncoding; 140 } 141 142 public void marshal(File file) throws IOException { 143 marshal(new FileWriter (file)); 145 } 146 147 public void marshal(OutputStream outputStream) throws IOException { 148 marshal(new OutputStreamWriter (outputStream)); 150 } 151 152 public void marshal(Writer writer) throws IOException { 153 writer.write("<?xml version=\"1.0\" "); 155 if (outputEncoding != null) { 156 writer.write("encoding=\""); 157 writer.write(outputEncoding); 158 writer.write("\"?>\n\n"); 159 160 } else { 161 writer.write("encoding=\"UTF-8\"?>\n\n"); 162 163 } 164 writer.write(docTypeString); 166 writer.write("\n"); 167 writeXMLRepresentation(writer, ""); 169 170 writer.flush(); 172 writer.close(); 173 } 174 175 protected void writeXMLRepresentation(Writer writer, 176 String indent) 177 throws IOException { 178 179 writer.write(indent); 180 writer.write("<welcome-file-list"); 181 182 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 184 String prefix = (String )i.next(); 185 String uri = (String )namespaceMappings.get(prefix); 186 writer.write(" xmlns"); 187 if (!prefix.trim().equals("")) { 188 writer.write(":"); 189 writer.write(prefix); 190 } 191 writer.write("=\""); 192 writer.write(uri); 193 writer.write("\"\n "); 194 } 195 196 if (zeus_IdSet) { 198 writer.write(" id=\""); 199 writer.write(escapeAttributeValue(id)); 200 writer.write("\""); 201 } 202 writer.write(">"); 203 writer.write("\n"); 204 205 for (Iterator i=welcomeFileList.iterator(); i.hasNext(); ) { 207 WelcomeFileImpl welcomeFile = (WelcomeFileImpl)i.next(); 208 welcomeFile.writeXMLRepresentation(writer, 209 new StringBuffer (indent).append(" ").toString()); 210 } 211 writer.write(indent); 212 writer.write("</welcome-file-list>\n"); 213 } 214 215 private String escapeAttributeValue(String attributeValue) { 216 String returnValue = attributeValue; 217 for (int i = 0; i < returnValue.length(); i++) { 218 char ch = returnValue.charAt(i); 219 if (ch == '"') { 220 returnValue = new StringBuffer () 221 .append(returnValue.substring(0, i)) 222 .append(""") 223 .append(returnValue.substring(i+1)) 224 .toString(); 225 } 226 } 227 return returnValue; 228 } 229 230 private String escapeTextValue(String textValue) { 231 String returnValue = textValue; 232 for (int i = 0; i < returnValue.length(); i++) { 233 char ch = returnValue.charAt(i); 234 if (ch == '<') { 235 returnValue = new StringBuffer () 236 .append(returnValue.substring(0, i)) 237 .append("<") 238 .append(returnValue.substring(i+1)) 239 .toString(); 240 } else if (ch == '>') { 241 returnValue = new StringBuffer () 242 .append(returnValue.substring(0, i)) 243 .append(">") 244 .append(returnValue.substring(i+1)) 245 .toString(); 246 } 247 } 248 return returnValue; 249 } 250 251 258 public static void setEntityResolver(EntityResolver resolver) { 259 entityResolver = resolver; 260 } 261 262 269 public static void setErrorHandler(ErrorHandler handler) { 270 errorHandler = handler; 271 } 272 273 public static WelcomeFileList unmarshal(File file) throws IOException { 274 return unmarshal(new FileReader (file)); 276 } 277 278 public static WelcomeFileList unmarshal(File file, boolean validate) throws IOException { 279 return unmarshal(new FileReader (file), validate); 281 } 282 283 public static WelcomeFileList unmarshal(InputStream inputStream) throws IOException { 284 return unmarshal(new InputStreamReader (inputStream)); 286 } 287 288 public static WelcomeFileList unmarshal(InputStream inputStream, boolean validate) throws IOException { 289 return unmarshal(new InputStreamReader (inputStream), validate); 291 } 292 293 public static WelcomeFileList unmarshal(Reader reader) throws IOException { 294 return unmarshal(reader, false); 296 } 297 298 public static WelcomeFileList unmarshal(Reader reader, boolean validate) throws IOException { 299 WelcomeFileListImpl welcomeFileList = WelcomeFileListImpl.newInstance(); 300 welcomeFileList.setValidating(validate); 301 welcomeFileList.setCurrentUNode(welcomeFileList); 302 welcomeFileList.setParentUNode(null); 303 XMLReader parser = null; 305 String parserClass = System.getProperty("org.xml.sax.driver", 306 "org.apache.xerces.parsers.SAXParser"); 307 try { 308 parser = XMLReaderFactory.createXMLReader(parserClass); 309 310 if (entityResolver != null) { 312 parser.setEntityResolver(entityResolver); 313 } 314 315 parser.setErrorHandler(welcomeFileList); 317 318 parser.setProperty("http://xml.org/sax/properties/lexical-handler", welcomeFileList); 320 321 parser.setContentHandler(welcomeFileList); 323 } catch (SAXException e) { 324 throw new IOException ("Could not load XML parser: " + 325 e.getMessage()); 326 } 327 328 InputSource inputSource = new InputSource (reader); 329 try { 330 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 331 parser.setFeature("http://xml.org/sax/features/namespaces", true); 332 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 333 parser.parse(inputSource); 334 } catch (SAXException e) { 335 throw new IOException ("Error parsing XML document: " + 336 e.getMessage()); 337 } 338 339 return welcomeFileList; 341 } 342 343 public Unmarshallable getParentUNode() { 344 return zeus_parentUNode; 345 } 346 347 public void setParentUNode(Unmarshallable parentUNode) { 348 this.zeus_parentUNode = parentUNode; 349 } 350 351 public Unmarshallable getCurrentUNode() { 352 return zeus_currentUNode; 353 } 354 355 public void setCurrentUNode(Unmarshallable currentUNode) { 356 this.zeus_currentUNode = currentUNode; 357 } 358 359 public void setValidating(boolean validate) { 360 this.validate = validate; 361 } 362 363 public void startDocument() throws SAXException { 364 } 366 367 public void setDocumentLocator(Locator locator) { 368 } 370 371 public void startPrefixMapping(String prefix, String uri) 372 throws SAXException { 373 namespaceMappings.put(prefix, uri); 374 } 375 376 public void startElement(String namespaceURI, String localName, 377 String qName, org.xml.sax.Attributes atts) 378 throws SAXException { 379 380 Unmarshallable current = getCurrentUNode(); 382 if (current != this) { 383 current.startElement(namespaceURI, localName, qName, atts); 384 return; 385 } 386 387 if ((localName.equals("welcome-file-list")) && (!zeus_thisNodeHandled)) { 389 for (int i=0, len=atts.getLength(); i<len; i++) { 391 String attName= atts.getLocalName(i); 392 String attValue = atts.getValue(i); 393 if (attName.equals("id")) { 394 setId(attValue); 395 } 396 } 397 zeus_thisNodeHandled = true; 398 return; 399 } else { 400 if (localName.equals("welcome-file")) { 402 WelcomeFileImpl welcomeFile = WelcomeFileImpl.newInstance(); 403 current = getCurrentUNode(); 404 welcomeFile.setParentUNode(current); 405 welcomeFile.setCurrentUNode(welcomeFile); 406 this.setCurrentUNode(welcomeFile); 407 welcomeFile.startElement(namespaceURI, localName, qName, atts); 408 welcomeFileList.add(welcomeFile); 410 return; 411 } 412 } 413 } 414 415 public void endElement(String namespaceURI, String localName, 416 String qName) 417 throws SAXException { 418 419 Unmarshallable current = getCurrentUNode(); 420 if (current != this) { 421 current.endElement(namespaceURI, localName, qName); 422 return; 423 } 424 425 Unmarshallable parent = getCurrentUNode().getParentUNode(); 426 if (parent != null) { 427 parent.setCurrentUNode(parent); 428 } 429 } 430 431 public void characters(char[] ch, int start, int len) 432 throws SAXException { 433 434 Unmarshallable current = getCurrentUNode(); 436 if (current != this) { 437 current.characters(ch, start, len); 438 return; 439 } 440 441 String text = new String (ch, start, len); 442 } 443 444 public void comment(char ch[], int start, int len) throws SAXException { 445 } 447 448 public void warning(SAXParseException e) throws SAXException { 449 if (errorHandler != null) { 450 errorHandler.warning(e); 451 } 452 } 453 454 public void error(SAXParseException e) throws SAXException { 455 if ((validate) && (!hasDTD)) { 456 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."); 457 } 458 if (errorHandler != null) { 459 errorHandler.error(e); 460 } 461 } 462 463 public void fatalError(SAXParseException e) throws SAXException { 464 if ((validate) && (!hasDTD)) { 465 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."); 466 } 467 if (errorHandler != null) { 468 errorHandler.fatalError(e); 469 } 470 } 471 472 public void startDTD(String name, String publicID, String systemID) 473 throws SAXException { 474 475 if ((name == null) || (name.equals(""))) { 476 docTypeString = ""; 477 return; 478 } 479 480 hasDTD = true; 481 StringBuffer docTypeSB = new StringBuffer (); 482 boolean hasPublic = false; 483 484 docTypeSB.append("<!DOCTYPE ") 485 .append(name); 486 487 if ((publicID != null) && (!publicID.equals(""))) { 488 docTypeSB.append(" PUBLIC \"") 489 .append(publicID) 490 .append("\""); 491 hasPublic = true; 492 } 493 494 if ((systemID != null) && (!systemID.equals(""))) { 495 if (!hasPublic) { 496 docTypeSB.append(" SYSTEM"); 497 } 498 docTypeSB.append(" \"") 499 .append(systemID) 500 .append("\""); 501 502 } 503 504 docTypeSB.append(">"); 505 506 docTypeString = docTypeSB.toString(); 507 } 508 509 public void endDTD() throws SAXException { 510 } 512 513 public void startEntity(String name) throws SAXException { 514 } 516 517 public void endEntity(String name) throws SAXException { 518 } 520 521 public void startCDATA() throws SAXException { 522 } 524 525 public void endCDATA() throws SAXException { 526 } 528 529 } 530 | Popular Tags |