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