1 18 package org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd; 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 ClassdescriptorImpl extends DefaultHandler implements Unmarshallable, LexicalHandler , Classdescriptor { 51 52 private List maprefList; 53 private String xmlextends; 54 private boolean zeus_XmlextendsSet; 55 private String classname; 56 private boolean zeus_ClassnameSet; 57 58 59 private String docTypeString; 60 61 62 private String outputEncoding; 63 64 65 private Unmarshallable zeus_currentUNode; 66 67 68 private Unmarshallable zeus_parentUNode; 69 70 71 private boolean zeus_thisNodeHandled = false; 72 73 74 private boolean hasDTD; 75 76 77 private boolean validate; 78 79 80 private Map namespaceMappings; 81 82 83 private static EntityResolver entityResolver; 84 85 86 private static ErrorHandler errorHandler; 87 88 public ClassdescriptorImpl() { 89 maprefList = new LinkedList (); 90 zeus_XmlextendsSet = false; 91 zeus_ClassnameSet = false; 92 docTypeString = ""; 93 hasDTD = false; 94 validate = false; 95 namespaceMappings = new HashMap (); 96 } 97 98 public List getMaprefList() { 99 return maprefList; 100 } 101 102 public void setMaprefList(List maprefList) { 103 this.maprefList = maprefList; 104 } 105 106 public void addMapref(Mapref mapref) { 107 maprefList.add(mapref); 108 } 109 110 public void removeMapref(Mapref mapref) { 111 maprefList.remove(mapref); 112 } 113 114 public String getXmlextends() { 115 return xmlextends; 116 } 117 118 public void setXmlextends(String xmlextends) { 119 this.xmlextends = xmlextends; 120 zeus_XmlextendsSet = true; 121 } 122 123 public String getClassname() { 124 return classname; 125 } 126 127 public void setClassname(String classname) { 128 this.classname = classname; 129 zeus_ClassnameSet = 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("<classdescriptor"); 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_XmlextendsSet) { 198 writer.write(" extends=\""); 199 writer.write(escapeAttributeValue(xmlextends)); 200 writer.write("\""); 201 } 202 if (zeus_ClassnameSet) { 203 writer.write(" classname=\""); 204 writer.write(escapeAttributeValue(classname)); 205 writer.write("\""); 206 } 207 writer.write(">"); 208 writer.write("\n"); 209 210 for (Iterator i=maprefList.iterator(); i.hasNext(); ) { 212 MaprefImpl mapref = (MaprefImpl)i.next(); 213 mapref.writeXMLRepresentation(writer, 214 new StringBuffer (indent).append(" ").toString()); 215 } 216 writer.write(indent); 217 writer.write("</classdescriptor>\n"); 218 } 219 220 private String escapeAttributeValue(String attributeValue) { 221 String returnValue = attributeValue; 222 for (int i = 0; i < returnValue.length(); i++) { 223 char ch = returnValue.charAt(i); 224 if (ch == '"') { 225 returnValue = new StringBuffer () 226 .append(returnValue.substring(0, i)) 227 .append(""") 228 .append(returnValue.substring(i+1)) 229 .toString(); 230 } 231 } 232 return returnValue; 233 } 234 235 242 public static void setEntityResolver(EntityResolver resolver) { 243 entityResolver = resolver; 244 } 245 246 253 public static void setErrorHandler(ErrorHandler handler) { 254 errorHandler = handler; 255 } 256 257 public static Classdescriptor unmarshal(File file) throws IOException { 258 return unmarshal(new FileReader (file)); 260 } 261 262 public static Classdescriptor unmarshal(File file, boolean validate) throws IOException { 263 return unmarshal(new FileReader (file), validate); 265 } 266 267 public static Classdescriptor unmarshal(InputStream inputStream) throws IOException { 268 return unmarshal(new InputStreamReader (inputStream)); 270 } 271 272 public static Classdescriptor unmarshal(InputStream inputStream, boolean validate) throws IOException { 273 return unmarshal(new InputStreamReader (inputStream), validate); 275 } 276 277 public static Classdescriptor unmarshal(Reader reader) throws IOException { 278 return unmarshal(reader, false); 280 } 281 282 public static Classdescriptor unmarshal(Reader reader, boolean validate) throws IOException { 283 ClassdescriptorImpl classdescriptor = new ClassdescriptorImpl(); 284 classdescriptor.setValidating(validate); 285 classdescriptor.setCurrentUNode(classdescriptor); 286 classdescriptor.setParentUNode(null); 287 XMLReader parser = null; 289 String parserClass = System.getProperty("org.xml.sax.driver", 290 "org.apache.xerces.parsers.SAXParser"); 291 try { 292 parser = XMLReaderFactory.createXMLReader(parserClass); 293 294 if (entityResolver != null) { 296 parser.setEntityResolver(entityResolver); 297 } 298 299 parser.setErrorHandler(classdescriptor); 301 302 parser.setProperty("http://xml.org/sax/properties/lexical-handler", classdescriptor); 304 305 parser.setContentHandler(classdescriptor); 307 } catch (SAXException e) { 308 throw new IOException ("Could not load XML parser: " + 309 e.getMessage()); 310 } 311 312 InputSource inputSource = new InputSource (reader); 313 try { 314 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 315 parser.setFeature("http://xml.org/sax/features/namespaces", true); 316 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 317 parser.parse(inputSource); 318 } catch (SAXException e) { 319 throw new IOException ("Error parsing XML document: " + 320 e.getMessage()); 321 } 322 323 return classdescriptor; 325 } 326 327 public Unmarshallable getParentUNode() { 328 return zeus_parentUNode; 329 } 330 331 public void setParentUNode(Unmarshallable parentUNode) { 332 this.zeus_parentUNode = parentUNode; 333 } 334 335 public Unmarshallable getCurrentUNode() { 336 return zeus_currentUNode; 337 } 338 339 public void setCurrentUNode(Unmarshallable currentUNode) { 340 this.zeus_currentUNode = currentUNode; 341 } 342 343 public void setValidating(boolean validate) { 344 this.validate = validate; 345 } 346 347 public void startDocument() throws SAXException { 348 } 350 351 public void setDocumentLocator(Locator locator) { 352 } 354 355 public void startPrefixMapping(String prefix, String uri) 356 throws SAXException { 357 namespaceMappings.put(prefix, uri); 358 } 359 360 public void startElement(String namespaceURI, String localName, 361 String qName, org.xml.sax.Attributes atts) 362 throws SAXException { 363 364 Unmarshallable current = getCurrentUNode(); 366 if (current != this) { 367 current.startElement(namespaceURI, localName, qName, atts); 368 return; 369 } 370 371 if ((localName.equals("classdescriptor")) && (!zeus_thisNodeHandled)) { 373 for (int i=0, len=atts.getLength(); i<len; i++) { 375 String attName= atts.getLocalName(i); 376 String attValue = atts.getValue(i); 377 if (attName.equals("extends")) { 378 setXmlextends(attValue); 379 } 380 if (attName.equals("classname")) { 381 setClassname(attValue); 382 } 383 } 384 zeus_thisNodeHandled = true; 385 return; 386 } else { 387 if (localName.equals("mapref")) { 389 MaprefImpl mapref = new MaprefImpl(); 390 current = getCurrentUNode(); 391 mapref.setParentUNode(current); 392 mapref.setCurrentUNode(mapref); 393 this.setCurrentUNode(mapref); 394 mapref.startElement(namespaceURI, localName, qName, atts); 395 maprefList.add(mapref); 397 return; 398 } 399 } 400 } 401 402 public void endElement(String namespaceURI, String localName, 403 String qName) 404 throws SAXException { 405 406 Unmarshallable current = getCurrentUNode(); 407 if (current != this) { 408 current.endElement(namespaceURI, localName, qName); 409 return; 410 } 411 412 Unmarshallable parent = getCurrentUNode().getParentUNode(); 413 if (parent != null) { 414 parent.setCurrentUNode(parent); 415 } 416 } 417 418 public void characters(char[] ch, int start, int len) 419 throws SAXException { 420 421 Unmarshallable current = getCurrentUNode(); 423 if (current != this) { 424 current.characters(ch, start, len); 425 return; 426 } 427 428 String text = new String (ch, start, len); 429 text = text.trim(); 430 } 431 432 public void comment(char ch[], int start, int len) throws SAXException { 433 } 435 436 public void warning(SAXParseException e) throws SAXException { 437 if (errorHandler != null) { 438 errorHandler.warning(e); 439 } 440 } 441 442 public void error(SAXParseException e) throws SAXException { 443 if ((validate) && (!hasDTD)) { 444 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."); 445 } 446 if (errorHandler != null) { 447 errorHandler.error(e); 448 } 449 } 450 451 public void fatalError(SAXParseException e) throws SAXException { 452 if ((validate) && (!hasDTD)) { 453 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."); 454 } 455 if (errorHandler != null) { 456 errorHandler.fatalError(e); 457 } 458 } 459 460 public void startDTD(String name, String publicID, String systemID) 461 throws SAXException { 462 463 if ((name == null) || (name.equals(""))) { 464 docTypeString = ""; 465 return; 466 } 467 468 hasDTD = true; 469 StringBuffer docTypeSB = new StringBuffer (); 470 boolean hasPublic = false; 471 472 docTypeSB.append("<!DOCTYPE ") 473 .append(name); 474 475 if ((publicID != null) && (!publicID.equals(""))) { 476 docTypeSB.append(" PUBLIC \"") 477 .append(publicID) 478 .append("\""); 479 hasPublic = true; 480 } 481 482 if ((systemID != null) && (!systemID.equals(""))) { 483 if (!hasPublic) { 484 docTypeSB.append(" SYSTEM"); 485 } 486 docTypeSB.append(" \"") 487 .append(systemID) 488 .append("\""); 489 490 } 491 492 docTypeSB.append(">"); 493 494 docTypeString = docTypeSB.toString(); 495 } 496 497 public void endDTD() throws SAXException { 498 } 500 501 public void startEntity(String name) throws SAXException { 502 } 504 505 public void endEntity(String name) throws SAXException { 506 } 508 509 public void startCDATA() throws SAXException { 510 } 512 513 public void endCDATA() throws SAXException { 514 } 516 517 } 518 | Popular Tags |