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 SecurityConstraintImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , SecurityConstraint { 51 52 private DisplayName displayName; 53 private List webResourceCollectionList; 54 private AuthConstraint authConstraint; 55 private UserDataConstraint userDataConstraint; 56 private String id; 57 private boolean zeus_IdSet; 58 59 60 private String docTypeString; 61 62 63 private String outputEncoding; 64 65 66 private Unmarshallable zeus_currentUNode; 67 68 69 private Unmarshallable zeus_parentUNode; 70 71 72 private boolean zeus_thisNodeHandled = false; 73 74 75 private boolean hasDTD; 76 77 78 private boolean validate; 79 80 81 private Map namespaceMappings; 82 83 84 private static EntityResolver entityResolver; 85 86 87 private static ErrorHandler errorHandler; 88 89 private static SecurityConstraintImpl prototype = null; 90 91 public static void setPrototype(SecurityConstraintImpl prototype) { 92 SecurityConstraintImpl.prototype = prototype; 93 } 94 public static SecurityConstraintImpl newInstance() { 95 try { 96 return (prototype!=null)?(SecurityConstraintImpl)prototype.clone(): new SecurityConstraintImpl(); 97 } catch (CloneNotSupportedException e) { 98 return null; } 100 } 101 public SecurityConstraintImpl() { 102 webResourceCollectionList = new LinkedList (); 103 zeus_IdSet = false; 104 docTypeString = ""; 105 hasDTD = false; 106 validate = false; 107 namespaceMappings = new HashMap (); 108 } 109 110 public DisplayName getDisplayName() { 111 return displayName; 112 } 113 114 public void setDisplayName(DisplayName displayName) { 115 this.displayName = displayName; 116 } 117 118 public List getWebResourceCollectionList() { 119 return webResourceCollectionList; 120 } 121 122 public void setWebResourceCollectionList(List webResourceCollectionList) { 123 this.webResourceCollectionList = webResourceCollectionList; 124 } 125 126 public void addWebResourceCollection(WebResourceCollection webResourceCollection) { 127 webResourceCollectionList.add(webResourceCollection); 128 } 129 130 public void removeWebResourceCollection(WebResourceCollection webResourceCollection) { 131 webResourceCollectionList.remove(webResourceCollection); 132 } 133 134 public AuthConstraint getAuthConstraint() { 135 return authConstraint; 136 } 137 138 public void setAuthConstraint(AuthConstraint authConstraint) { 139 this.authConstraint = authConstraint; 140 } 141 142 public UserDataConstraint getUserDataConstraint() { 143 return userDataConstraint; 144 } 145 146 public void setUserDataConstraint(UserDataConstraint userDataConstraint) { 147 this.userDataConstraint = userDataConstraint; 148 } 149 150 public String getId() { 151 return id; 152 } 153 154 public void setId(String id) { 155 this.id = id; 156 zeus_IdSet = true; 157 } 158 159 public void setDocType(String name, String publicID, String systemID) { 160 try { 161 startDTD(name, publicID, systemID); 162 } catch (SAXException neverHappens) { } 163 } 164 165 public void setOutputEncoding(String outputEncoding) { 166 this.outputEncoding = outputEncoding; 167 } 168 169 public void marshal(File file) throws IOException { 170 marshal(new FileWriter (file)); 172 } 173 174 public void marshal(OutputStream outputStream) throws IOException { 175 marshal(new OutputStreamWriter (outputStream)); 177 } 178 179 public void marshal(Writer writer) throws IOException { 180 writer.write("<?xml version=\"1.0\" "); 182 if (outputEncoding != null) { 183 writer.write("encoding=\""); 184 writer.write(outputEncoding); 185 writer.write("\"?>\n\n"); 186 187 } else { 188 writer.write("encoding=\"UTF-8\"?>\n\n"); 189 190 } 191 writer.write(docTypeString); 193 writer.write("\n"); 194 writeXMLRepresentation(writer, ""); 196 197 writer.flush(); 199 writer.close(); 200 } 201 202 protected void writeXMLRepresentation(Writer writer, 203 String indent) 204 throws IOException { 205 206 writer.write(indent); 207 writer.write("<security-constraint"); 208 209 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 211 String prefix = (String )i.next(); 212 String uri = (String )namespaceMappings.get(prefix); 213 writer.write(" xmlns"); 214 if (!prefix.trim().equals("")) { 215 writer.write(":"); 216 writer.write(prefix); 217 } 218 writer.write("=\""); 219 writer.write(uri); 220 writer.write("\"\n "); 221 } 222 223 if (zeus_IdSet) { 225 writer.write(" id=\""); 226 writer.write(escapeAttributeValue(id)); 227 writer.write("\""); 228 } 229 writer.write(">"); 230 writer.write("\n"); 231 232 if (displayName != null) { 234 ((DisplayNameImpl)displayName).writeXMLRepresentation(writer, 235 new StringBuffer (indent).append(" ").toString()); 236 } 237 238 for (Iterator i=webResourceCollectionList.iterator(); i.hasNext(); ) { 239 WebResourceCollectionImpl webResourceCollection = (WebResourceCollectionImpl)i.next(); 240 webResourceCollection.writeXMLRepresentation(writer, 241 new StringBuffer (indent).append(" ").toString()); 242 } 243 if (authConstraint != null) { 244 ((AuthConstraintImpl)authConstraint).writeXMLRepresentation(writer, 245 new StringBuffer (indent).append(" ").toString()); 246 } 247 248 if (userDataConstraint != null) { 249 ((UserDataConstraintImpl)userDataConstraint).writeXMLRepresentation(writer, 250 new StringBuffer (indent).append(" ").toString()); 251 } 252 253 writer.write(indent); 254 writer.write("</security-constraint>\n"); 255 } 256 257 private String escapeAttributeValue(String attributeValue) { 258 String returnValue = attributeValue; 259 for (int i = 0; i < returnValue.length(); i++) { 260 char ch = returnValue.charAt(i); 261 if (ch == '"') { 262 returnValue = new StringBuffer () 263 .append(returnValue.substring(0, i)) 264 .append(""") 265 .append(returnValue.substring(i+1)) 266 .toString(); 267 } 268 } 269 return returnValue; 270 } 271 272 private String escapeTextValue(String textValue) { 273 String returnValue = textValue; 274 for (int i = 0; i < returnValue.length(); i++) { 275 char ch = returnValue.charAt(i); 276 if (ch == '<') { 277 returnValue = new StringBuffer () 278 .append(returnValue.substring(0, i)) 279 .append("<") 280 .append(returnValue.substring(i+1)) 281 .toString(); 282 } else if (ch == '>') { 283 returnValue = new StringBuffer () 284 .append(returnValue.substring(0, i)) 285 .append(">") 286 .append(returnValue.substring(i+1)) 287 .toString(); 288 } 289 } 290 return returnValue; 291 } 292 293 300 public static void setEntityResolver(EntityResolver resolver) { 301 entityResolver = resolver; 302 } 303 304 311 public static void setErrorHandler(ErrorHandler handler) { 312 errorHandler = handler; 313 } 314 315 public static SecurityConstraint unmarshal(File file) throws IOException { 316 return unmarshal(new FileReader (file)); 318 } 319 320 public static SecurityConstraint unmarshal(File file, boolean validate) throws IOException { 321 return unmarshal(new FileReader (file), validate); 323 } 324 325 public static SecurityConstraint unmarshal(InputStream inputStream) throws IOException { 326 return unmarshal(new InputStreamReader (inputStream)); 328 } 329 330 public static SecurityConstraint unmarshal(InputStream inputStream, boolean validate) throws IOException { 331 return unmarshal(new InputStreamReader (inputStream), validate); 333 } 334 335 public static SecurityConstraint unmarshal(Reader reader) throws IOException { 336 return unmarshal(reader, false); 338 } 339 340 public static SecurityConstraint unmarshal(Reader reader, boolean validate) throws IOException { 341 SecurityConstraintImpl securityConstraint = SecurityConstraintImpl.newInstance(); 342 securityConstraint.setValidating(validate); 343 securityConstraint.setCurrentUNode(securityConstraint); 344 securityConstraint.setParentUNode(null); 345 XMLReader parser = null; 347 String parserClass = System.getProperty("org.xml.sax.driver", 348 "org.apache.xerces.parsers.SAXParser"); 349 try { 350 parser = XMLReaderFactory.createXMLReader(parserClass); 351 352 if (entityResolver != null) { 354 parser.setEntityResolver(entityResolver); 355 } 356 357 parser.setErrorHandler(securityConstraint); 359 360 parser.setProperty("http://xml.org/sax/properties/lexical-handler", securityConstraint); 362 363 parser.setContentHandler(securityConstraint); 365 } catch (SAXException e) { 366 throw new IOException ("Could not load XML parser: " + 367 e.getMessage()); 368 } 369 370 InputSource inputSource = new InputSource (reader); 371 try { 372 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 373 parser.setFeature("http://xml.org/sax/features/namespaces", true); 374 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 375 parser.parse(inputSource); 376 } catch (SAXException e) { 377 throw new IOException ("Error parsing XML document: " + 378 e.getMessage()); 379 } 380 381 return securityConstraint; 383 } 384 385 public Unmarshallable getParentUNode() { 386 return zeus_parentUNode; 387 } 388 389 public void setParentUNode(Unmarshallable parentUNode) { 390 this.zeus_parentUNode = parentUNode; 391 } 392 393 public Unmarshallable getCurrentUNode() { 394 return zeus_currentUNode; 395 } 396 397 public void setCurrentUNode(Unmarshallable currentUNode) { 398 this.zeus_currentUNode = currentUNode; 399 } 400 401 public void setValidating(boolean validate) { 402 this.validate = validate; 403 } 404 405 public void startDocument() throws SAXException { 406 } 408 409 public void setDocumentLocator(Locator locator) { 410 } 412 413 public void startPrefixMapping(String prefix, String uri) 414 throws SAXException { 415 namespaceMappings.put(prefix, uri); 416 } 417 418 public void startElement(String namespaceURI, String localName, 419 String qName, org.xml.sax.Attributes atts) 420 throws SAXException { 421 422 Unmarshallable current = getCurrentUNode(); 424 if (current != this) { 425 current.startElement(namespaceURI, localName, qName, atts); 426 return; 427 } 428 429 if ((localName.equals("security-constraint")) && (!zeus_thisNodeHandled)) { 431 for (int i=0, len=atts.getLength(); i<len; i++) { 433 String attName= atts.getLocalName(i); 434 String attValue = atts.getValue(i); 435 if (attName.equals("id")) { 436 setId(attValue); 437 } 438 } 439 zeus_thisNodeHandled = true; 440 return; 441 } else { 442 if (localName.equals("display-name") && (displayName==null)) { 444 DisplayNameImpl displayName = DisplayNameImpl.newInstance(); 445 current = getCurrentUNode(); 446 displayName.setParentUNode(current); 447 displayName.setCurrentUNode(displayName); 448 this.setCurrentUNode(displayName); 449 displayName.startElement(namespaceURI, localName, qName, atts); 450 this.displayName = displayName; 452 return; 453 } 454 if (localName.equals("web-resource-collection")) { 455 WebResourceCollectionImpl webResourceCollection = WebResourceCollectionImpl.newInstance(); 456 current = getCurrentUNode(); 457 webResourceCollection.setParentUNode(current); 458 webResourceCollection.setCurrentUNode(webResourceCollection); 459 this.setCurrentUNode(webResourceCollection); 460 webResourceCollection.startElement(namespaceURI, localName, qName, atts); 461 webResourceCollectionList.add(webResourceCollection); 463 return; 464 } 465 if (localName.equals("auth-constraint") && (authConstraint==null)) { 466 AuthConstraintImpl authConstraint = AuthConstraintImpl.newInstance(); 467 current = getCurrentUNode(); 468 authConstraint.setParentUNode(current); 469 authConstraint.setCurrentUNode(authConstraint); 470 this.setCurrentUNode(authConstraint); 471 authConstraint.startElement(namespaceURI, localName, qName, atts); 472 this.authConstraint = authConstraint; 474 return; 475 } 476 if (localName.equals("user-data-constraint") && (userDataConstraint==null)) { 477 UserDataConstraintImpl userDataConstraint = UserDataConstraintImpl.newInstance(); 478 current = getCurrentUNode(); 479 userDataConstraint.setParentUNode(current); 480 userDataConstraint.setCurrentUNode(userDataConstraint); 481 this.setCurrentUNode(userDataConstraint); 482 userDataConstraint.startElement(namespaceURI, localName, qName, atts); 483 this.userDataConstraint = userDataConstraint; 485 return; 486 } 487 } 488 } 489 490 public void endElement(String namespaceURI, String localName, 491 String qName) 492 throws SAXException { 493 494 Unmarshallable current = getCurrentUNode(); 495 if (current != this) { 496 current.endElement(namespaceURI, localName, qName); 497 return; 498 } 499 500 Unmarshallable parent = getCurrentUNode().getParentUNode(); 501 if (parent != null) { 502 parent.setCurrentUNode(parent); 503 } 504 } 505 506 public void characters(char[] ch, int start, int len) 507 throws SAXException { 508 509 Unmarshallable current = getCurrentUNode(); 511 if (current != this) { 512 current.characters(ch, start, len); 513 return; 514 } 515 516 String text = new String (ch, start, len); 517 } 518 519 public void comment(char ch[], int start, int len) throws SAXException { 520 } 522 523 public void warning(SAXParseException e) throws SAXException { 524 if (errorHandler != null) { 525 errorHandler.warning(e); 526 } 527 } 528 529 public void error(SAXParseException e) throws SAXException { 530 if ((validate) && (!hasDTD)) { 531 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."); 532 } 533 if (errorHandler != null) { 534 errorHandler.error(e); 535 } 536 } 537 538 public void fatalError(SAXParseException e) throws SAXException { 539 if ((validate) && (!hasDTD)) { 540 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."); 541 } 542 if (errorHandler != null) { 543 errorHandler.fatalError(e); 544 } 545 } 546 547 public void startDTD(String name, String publicID, String systemID) 548 throws SAXException { 549 550 if ((name == null) || (name.equals(""))) { 551 docTypeString = ""; 552 return; 553 } 554 555 hasDTD = true; 556 StringBuffer docTypeSB = new StringBuffer (); 557 boolean hasPublic = false; 558 559 docTypeSB.append("<!DOCTYPE ") 560 .append(name); 561 562 if ((publicID != null) && (!publicID.equals(""))) { 563 docTypeSB.append(" PUBLIC \"") 564 .append(publicID) 565 .append("\""); 566 hasPublic = true; 567 } 568 569 if ((systemID != null) && (!systemID.equals(""))) { 570 if (!hasPublic) { 571 docTypeSB.append(" SYSTEM"); 572 } 573 docTypeSB.append(" \"") 574 .append(systemID) 575 .append("\""); 576 577 } 578 579 docTypeSB.append(">"); 580 581 docTypeString = docTypeSB.toString(); 582 } 583 584 public void endDTD() throws SAXException { 585 } 587 588 public void startEntity(String name) throws SAXException { 589 } 591 592 public void endEntity(String name) throws SAXException { 593 } 595 596 public void startCDATA() throws SAXException { 597 } 599 600 public void endCDATA() throws SAXException { 601 } 603 604 } 605 | Popular Tags |