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 SecurityRoleImpl extends DefaultHandler implements Cloneable , Unmarshallable, LexicalHandler , SecurityRole { 46 47 private Description description; 48 private RoleName roleName; 49 private String id; 50 private boolean zeus_IdSet; 51 52 53 private String docTypeString; 54 55 56 private String outputEncoding; 57 58 59 private Unmarshallable zeus_currentUNode; 60 61 62 private Unmarshallable zeus_parentUNode; 63 64 65 private boolean zeus_thisNodeHandled = false; 66 67 68 private boolean hasDTD; 69 70 71 private boolean validate; 72 73 74 private Map namespaceMappings; 75 76 77 private static EntityResolver entityResolver; 78 79 80 private static ErrorHandler errorHandler; 81 82 private static SecurityRoleImpl prototype = null; 83 84 public static void setPrototype(SecurityRoleImpl prototype) { 85 SecurityRoleImpl.prototype = prototype; 86 } 87 public static SecurityRoleImpl newInstance() { 88 try { 89 return (prototype!=null)?(SecurityRoleImpl)prototype.clone(): new SecurityRoleImpl(); 90 } catch (CloneNotSupportedException e) { 91 return null; } 93 } 94 public SecurityRoleImpl() { 95 zeus_IdSet = false; 96 docTypeString = ""; 97 hasDTD = false; 98 validate = false; 99 namespaceMappings = new HashMap (); 100 } 101 102 public Description getDescription() { 103 return description; 104 } 105 106 public void setDescription(Description description) { 107 this.description = description; 108 } 109 110 public RoleName getRoleName() { 111 return roleName; 112 } 113 114 public void setRoleName(RoleName roleName) { 115 this.roleName = roleName; 116 } 117 118 public String getId() { 119 return id; 120 } 121 122 public void setId(String id) { 123 this.id = id; 124 zeus_IdSet = true; 125 } 126 127 public void setDocType(String name, String publicID, String systemID) { 128 try { 129 startDTD(name, publicID, systemID); 130 } catch (SAXException neverHappens) { } 131 } 132 133 public void setOutputEncoding(String outputEncoding) { 134 this.outputEncoding = outputEncoding; 135 } 136 137 public void marshal(File file) throws IOException { 138 marshal(new FileWriter (file)); 140 } 141 142 public void marshal(OutputStream outputStream) throws IOException { 143 marshal(new OutputStreamWriter (outputStream)); 145 } 146 147 public void marshal(Writer writer) throws IOException { 148 writer.write("<?xml version=\"1.0\" "); 150 if (outputEncoding != null) { 151 writer.write("encoding=\""); 152 writer.write(outputEncoding); 153 writer.write("\"?>\n\n"); 154 155 } else { 156 writer.write("encoding=\"UTF-8\"?>\n\n"); 157 158 } 159 writer.write(docTypeString); 161 writer.write("\n"); 162 writeXMLRepresentation(writer, ""); 164 165 writer.flush(); 167 writer.close(); 168 } 169 170 protected void writeXMLRepresentation(Writer writer, 171 String indent) 172 throws IOException { 173 174 writer.write(indent); 175 writer.write("<security-role"); 176 177 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 179 String prefix = (String )i.next(); 180 String uri = (String )namespaceMappings.get(prefix); 181 writer.write(" xmlns"); 182 if (!prefix.trim().equals("")) { 183 writer.write(":"); 184 writer.write(prefix); 185 } 186 writer.write("=\""); 187 writer.write(uri); 188 writer.write("\"\n "); 189 } 190 191 if (zeus_IdSet) { 193 writer.write(" id=\""); 194 writer.write(escapeAttributeValue(id)); 195 writer.write("\""); 196 } 197 writer.write(">"); 198 writer.write("\n"); 199 200 if (description != null) { 202 ((DescriptionImpl)description).writeXMLRepresentation(writer, 203 new StringBuffer (indent).append(" ").toString()); 204 } 205 206 if (roleName != null) { 207 ((RoleNameImpl)roleName).writeXMLRepresentation(writer, 208 new StringBuffer (indent).append(" ").toString()); 209 } 210 211 writer.write(indent); 212 writer.write("</security-role>\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 SecurityRole unmarshal(File file) throws IOException { 274 return unmarshal(new FileReader (file)); 276 } 277 278 public static SecurityRole unmarshal(File file, boolean validate) throws IOException { 279 return unmarshal(new FileReader (file), validate); 281 } 282 283 public static SecurityRole unmarshal(InputStream inputStream) throws IOException { 284 return unmarshal(new InputStreamReader (inputStream)); 286 } 287 288 public static SecurityRole unmarshal(InputStream inputStream, boolean validate) throws IOException { 289 return unmarshal(new InputStreamReader (inputStream), validate); 291 } 292 293 public static SecurityRole unmarshal(Reader reader) throws IOException { 294 return unmarshal(reader, false); 296 } 297 298 public static SecurityRole unmarshal(Reader reader, boolean validate) throws IOException { 299 SecurityRoleImpl securityRole = SecurityRoleImpl.newInstance(); 300 securityRole.setValidating(validate); 301 securityRole.setCurrentUNode(securityRole); 302 securityRole.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(securityRole); 317 318 parser.setProperty("http://xml.org/sax/properties/lexical-handler", securityRole); 320 321 parser.setContentHandler(securityRole); 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 securityRole; 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("security-role")) && (!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("description") && (description==null)) { 402 DescriptionImpl description = DescriptionImpl.newInstance(); 403 current = getCurrentUNode(); 404 description.setParentUNode(current); 405 description.setCurrentUNode(description); 406 this.setCurrentUNode(description); 407 description.startElement(namespaceURI, localName, qName, atts); 408 this.description = description; 410 return; 411 } 412 if (localName.equals("role-name") && (roleName==null)) { 413 RoleNameImpl roleName = RoleNameImpl.newInstance(); 414 current = getCurrentUNode(); 415 roleName.setParentUNode(current); 416 roleName.setCurrentUNode(roleName); 417 this.setCurrentUNode(roleName); 418 roleName.startElement(namespaceURI, localName, qName, atts); 419 this.roleName = roleName; 421 return; 422 } 423 } 424 } 425 426 public void endElement(String namespaceURI, String localName, 427 String qName) 428 throws SAXException { 429 430 Unmarshallable current = getCurrentUNode(); 431 if (current != this) { 432 current.endElement(namespaceURI, localName, qName); 433 return; 434 } 435 436 Unmarshallable parent = getCurrentUNode().getParentUNode(); 437 if (parent != null) { 438 parent.setCurrentUNode(parent); 439 } 440 } 441 442 public void characters(char[] ch, int start, int len) 443 throws SAXException { 444 445 Unmarshallable current = getCurrentUNode(); 447 if (current != this) { 448 current.characters(ch, start, len); 449 return; 450 } 451 452 String text = new String (ch, start, len); 453 } 454 455 public void comment(char ch[], int start, int len) throws SAXException { 456 } 458 459 public void warning(SAXParseException e) throws SAXException { 460 if (errorHandler != null) { 461 errorHandler.warning(e); 462 } 463 } 464 465 public void error(SAXParseException e) throws SAXException { 466 if ((validate) && (!hasDTD)) { 467 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."); 468 } 469 if (errorHandler != null) { 470 errorHandler.error(e); 471 } 472 } 473 474 public void fatalError(SAXParseException e) throws SAXException { 475 if ((validate) && (!hasDTD)) { 476 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."); 477 } 478 if (errorHandler != null) { 479 errorHandler.fatalError(e); 480 } 481 } 482 483 public void startDTD(String name, String publicID, String systemID) 484 throws SAXException { 485 486 if ((name == null) || (name.equals(""))) { 487 docTypeString = ""; 488 return; 489 } 490 491 hasDTD = true; 492 StringBuffer docTypeSB = new StringBuffer (); 493 boolean hasPublic = false; 494 495 docTypeSB.append("<!DOCTYPE ") 496 .append(name); 497 498 if ((publicID != null) && (!publicID.equals(""))) { 499 docTypeSB.append(" PUBLIC \"") 500 .append(publicID) 501 .append("\""); 502 hasPublic = true; 503 } 504 505 if ((systemID != null) && (!systemID.equals(""))) { 506 if (!hasPublic) { 507 docTypeSB.append(" SYSTEM"); 508 } 509 docTypeSB.append(" \"") 510 .append(systemID) 511 .append("\""); 512 513 } 514 515 docTypeSB.append(">"); 516 517 docTypeString = docTypeSB.toString(); 518 } 519 520 public void endDTD() throws SAXException { 521 } 523 524 public void startEntity(String name) throws SAXException { 525 } 527 528 public void endEntity(String name) throws SAXException { 529 } 531 532 public void startCDATA() throws SAXException { 533 } 535 536 public void endCDATA() throws SAXException { 537 } 539 540 } 541 | Popular Tags |