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 SessionConfigImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , SessionConfig { 46 47 private SessionTimeout sessionTimeout; 48 private String id; 49 private boolean zeus_IdSet; 50 51 52 private String docTypeString; 53 54 55 private String outputEncoding; 56 57 58 private Unmarshallable zeus_currentUNode; 59 60 61 private Unmarshallable zeus_parentUNode; 62 63 64 private boolean zeus_thisNodeHandled = false; 65 66 67 private boolean hasDTD; 68 69 70 private boolean validate; 71 72 73 private Map namespaceMappings; 74 75 76 private static EntityResolver entityResolver; 77 78 79 private static ErrorHandler errorHandler; 80 81 private static SessionConfigImpl prototype = null; 82 83 public static void setPrototype(SessionConfigImpl prototype) { 84 SessionConfigImpl.prototype = prototype; 85 } 86 public static SessionConfigImpl newInstance() { 87 try { 88 return (prototype!=null)?(SessionConfigImpl)prototype.clone(): new SessionConfigImpl(); 89 } catch (CloneNotSupportedException e) { 90 return null; } 92 } 93 public SessionConfigImpl() { 94 zeus_IdSet = false; 95 docTypeString = ""; 96 hasDTD = false; 97 validate = false; 98 namespaceMappings = new HashMap (); 99 } 100 101 public SessionTimeout getSessionTimeout() { 102 return sessionTimeout; 103 } 104 105 public void setSessionTimeout(SessionTimeout sessionTimeout) { 106 this.sessionTimeout = sessionTimeout; 107 } 108 109 public String getId() { 110 return id; 111 } 112 113 public void setId(String id) { 114 this.id = id; 115 zeus_IdSet = true; 116 } 117 118 public void setDocType(String name, String publicID, String systemID) { 119 try { 120 startDTD(name, publicID, systemID); 121 } catch (SAXException neverHappens) { } 122 } 123 124 public void setOutputEncoding(String outputEncoding) { 125 this.outputEncoding = outputEncoding; 126 } 127 128 public void marshal(File file) throws IOException { 129 marshal(new FileWriter (file)); 131 } 132 133 public void marshal(OutputStream outputStream) throws IOException { 134 marshal(new OutputStreamWriter (outputStream)); 136 } 137 138 public void marshal(Writer writer) throws IOException { 139 writer.write("<?xml version=\"1.0\" "); 141 if (outputEncoding != null) { 142 writer.write("encoding=\""); 143 writer.write(outputEncoding); 144 writer.write("\"?>\n\n"); 145 146 } else { 147 writer.write("encoding=\"UTF-8\"?>\n\n"); 148 149 } 150 writer.write(docTypeString); 152 writer.write("\n"); 153 writeXMLRepresentation(writer, ""); 155 156 writer.flush(); 158 writer.close(); 159 } 160 161 protected void writeXMLRepresentation(Writer writer, 162 String indent) 163 throws IOException { 164 165 writer.write(indent); 166 writer.write("<session-config"); 167 168 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 170 String prefix = (String )i.next(); 171 String uri = (String )namespaceMappings.get(prefix); 172 writer.write(" xmlns"); 173 if (!prefix.trim().equals("")) { 174 writer.write(":"); 175 writer.write(prefix); 176 } 177 writer.write("=\""); 178 writer.write(uri); 179 writer.write("\"\n "); 180 } 181 182 if (zeus_IdSet) { 184 writer.write(" id=\""); 185 writer.write(escapeAttributeValue(id)); 186 writer.write("\""); 187 } 188 writer.write(">"); 189 writer.write("\n"); 190 191 if (sessionTimeout != null) { 193 ((SessionTimeoutImpl)sessionTimeout).writeXMLRepresentation(writer, 194 new StringBuffer (indent).append(" ").toString()); 195 } 196 197 writer.write(indent); 198 writer.write("</session-config>\n"); 199 } 200 201 private String escapeAttributeValue(String attributeValue) { 202 String returnValue = attributeValue; 203 for (int i = 0; i < returnValue.length(); i++) { 204 char ch = returnValue.charAt(i); 205 if (ch == '"') { 206 returnValue = new StringBuffer () 207 .append(returnValue.substring(0, i)) 208 .append(""") 209 .append(returnValue.substring(i+1)) 210 .toString(); 211 } 212 } 213 return returnValue; 214 } 215 216 private String escapeTextValue(String textValue) { 217 String returnValue = textValue; 218 for (int i = 0; i < returnValue.length(); i++) { 219 char ch = returnValue.charAt(i); 220 if (ch == '<') { 221 returnValue = new StringBuffer () 222 .append(returnValue.substring(0, i)) 223 .append("<") 224 .append(returnValue.substring(i+1)) 225 .toString(); 226 } else if (ch == '>') { 227 returnValue = new StringBuffer () 228 .append(returnValue.substring(0, i)) 229 .append(">") 230 .append(returnValue.substring(i+1)) 231 .toString(); 232 } 233 } 234 return returnValue; 235 } 236 237 244 public static void setEntityResolver(EntityResolver resolver) { 245 entityResolver = resolver; 246 } 247 248 255 public static void setErrorHandler(ErrorHandler handler) { 256 errorHandler = handler; 257 } 258 259 public static SessionConfig unmarshal(File file) throws IOException { 260 return unmarshal(new FileReader (file)); 262 } 263 264 public static SessionConfig unmarshal(File file, boolean validate) throws IOException { 265 return unmarshal(new FileReader (file), validate); 267 } 268 269 public static SessionConfig unmarshal(InputStream inputStream) throws IOException { 270 return unmarshal(new InputStreamReader (inputStream)); 272 } 273 274 public static SessionConfig unmarshal(InputStream inputStream, boolean validate) throws IOException { 275 return unmarshal(new InputStreamReader (inputStream), validate); 277 } 278 279 public static SessionConfig unmarshal(Reader reader) throws IOException { 280 return unmarshal(reader, false); 282 } 283 284 public static SessionConfig unmarshal(Reader reader, boolean validate) throws IOException { 285 SessionConfigImpl sessionConfig = SessionConfigImpl.newInstance(); 286 sessionConfig.setValidating(validate); 287 sessionConfig.setCurrentUNode(sessionConfig); 288 sessionConfig.setParentUNode(null); 289 XMLReader parser = null; 291 String parserClass = System.getProperty("org.xml.sax.driver", 292 "org.apache.xerces.parsers.SAXParser"); 293 try { 294 parser = XMLReaderFactory.createXMLReader(parserClass); 295 296 if (entityResolver != null) { 298 parser.setEntityResolver(entityResolver); 299 } 300 301 parser.setErrorHandler(sessionConfig); 303 304 parser.setProperty("http://xml.org/sax/properties/lexical-handler", sessionConfig); 306 307 parser.setContentHandler(sessionConfig); 309 } catch (SAXException e) { 310 throw new IOException ("Could not load XML parser: " + 311 e.getMessage()); 312 } 313 314 InputSource inputSource = new InputSource (reader); 315 try { 316 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 317 parser.setFeature("http://xml.org/sax/features/namespaces", true); 318 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 319 parser.parse(inputSource); 320 } catch (SAXException e) { 321 throw new IOException ("Error parsing XML document: " + 322 e.getMessage()); 323 } 324 325 return sessionConfig; 327 } 328 329 public Unmarshallable getParentUNode() { 330 return zeus_parentUNode; 331 } 332 333 public void setParentUNode(Unmarshallable parentUNode) { 334 this.zeus_parentUNode = parentUNode; 335 } 336 337 public Unmarshallable getCurrentUNode() { 338 return zeus_currentUNode; 339 } 340 341 public void setCurrentUNode(Unmarshallable currentUNode) { 342 this.zeus_currentUNode = currentUNode; 343 } 344 345 public void setValidating(boolean validate) { 346 this.validate = validate; 347 } 348 349 public void startDocument() throws SAXException { 350 } 352 353 public void setDocumentLocator(Locator locator) { 354 } 356 357 public void startPrefixMapping(String prefix, String uri) 358 throws SAXException { 359 namespaceMappings.put(prefix, uri); 360 } 361 362 public void startElement(String namespaceURI, String localName, 363 String qName, org.xml.sax.Attributes atts) 364 throws SAXException { 365 366 Unmarshallable current = getCurrentUNode(); 368 if (current != this) { 369 current.startElement(namespaceURI, localName, qName, atts); 370 return; 371 } 372 373 if ((localName.equals("session-config")) && (!zeus_thisNodeHandled)) { 375 for (int i=0, len=atts.getLength(); i<len; i++) { 377 String attName= atts.getLocalName(i); 378 String attValue = atts.getValue(i); 379 if (attName.equals("id")) { 380 setId(attValue); 381 } 382 } 383 zeus_thisNodeHandled = true; 384 return; 385 } else { 386 if (localName.equals("session-timeout") && (sessionTimeout==null)) { 388 SessionTimeoutImpl sessionTimeout = SessionTimeoutImpl.newInstance(); 389 current = getCurrentUNode(); 390 sessionTimeout.setParentUNode(current); 391 sessionTimeout.setCurrentUNode(sessionTimeout); 392 this.setCurrentUNode(sessionTimeout); 393 sessionTimeout.startElement(namespaceURI, localName, qName, atts); 394 this.sessionTimeout = sessionTimeout; 396 return; 397 } 398 } 399 } 400 401 public void endElement(String namespaceURI, String localName, 402 String qName) 403 throws SAXException { 404 405 Unmarshallable current = getCurrentUNode(); 406 if (current != this) { 407 current.endElement(namespaceURI, localName, qName); 408 return; 409 } 410 411 Unmarshallable parent = getCurrentUNode().getParentUNode(); 412 if (parent != null) { 413 parent.setCurrentUNode(parent); 414 } 415 } 416 417 public void characters(char[] ch, int start, int len) 418 throws SAXException { 419 420 Unmarshallable current = getCurrentUNode(); 422 if (current != this) { 423 current.characters(ch, start, len); 424 return; 425 } 426 427 String text = new String (ch, start, len); 428 } 429 430 public void comment(char ch[], int start, int len) throws SAXException { 431 } 433 434 public void warning(SAXParseException e) throws SAXException { 435 if (errorHandler != null) { 436 errorHandler.warning(e); 437 } 438 } 439 440 public void error(SAXParseException e) throws SAXException { 441 if ((validate) && (!hasDTD)) { 442 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."); 443 } 444 if (errorHandler != null) { 445 errorHandler.error(e); 446 } 447 } 448 449 public void fatalError(SAXParseException e) throws SAXException { 450 if ((validate) && (!hasDTD)) { 451 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."); 452 } 453 if (errorHandler != null) { 454 errorHandler.fatalError(e); 455 } 456 } 457 458 public void startDTD(String name, String publicID, String systemID) 459 throws SAXException { 460 461 if ((name == null) || (name.equals(""))) { 462 docTypeString = ""; 463 return; 464 } 465 466 hasDTD = true; 467 StringBuffer docTypeSB = new StringBuffer (); 468 boolean hasPublic = false; 469 470 docTypeSB.append("<!DOCTYPE ") 471 .append(name); 472 473 if ((publicID != null) && (!publicID.equals(""))) { 474 docTypeSB.append(" PUBLIC \"") 475 .append(publicID) 476 .append("\""); 477 hasPublic = true; 478 } 479 480 if ((systemID != null) && (!systemID.equals(""))) { 481 if (!hasPublic) { 482 docTypeSB.append(" SYSTEM"); 483 } 484 docTypeSB.append(" \"") 485 .append(systemID) 486 .append("\""); 487 488 } 489 490 docTypeSB.append(">"); 491 492 docTypeString = docTypeSB.toString(); 493 } 494 495 public void endDTD() throws SAXException { 496 } 498 499 public void startEntity(String name) throws SAXException { 500 } 502 503 public void endEntity(String name) throws SAXException { 504 } 506 507 public void startCDATA() throws SAXException { 508 } 510 511 public void endCDATA() throws SAXException { 512 } 514 515 } 516 | Popular Tags |