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