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