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 public class CountImpl extends DefaultHandler implements Unmarshallable, LexicalHandler , Count { 46 47 48 49 private String docTypeString; 50 51 52 private String outputEncoding; 53 54 55 private Unmarshallable zeus_currentUNode; 56 57 58 private Unmarshallable zeus_parentUNode; 59 60 61 private boolean zeus_thisNodeHandled = false; 62 63 64 private boolean hasDTD; 65 66 67 private boolean validate; 68 69 70 private Map namespaceMappings; 71 72 73 private static EntityResolver entityResolver; 74 75 76 private static ErrorHandler errorHandler; 77 78 public CountImpl() { 79 docTypeString = ""; 80 hasDTD = false; 81 validate = false; 82 namespaceMappings = new HashMap (); 83 } 84 85 public void setDocType(String name, String publicID, String systemID) { 86 try { 87 startDTD(name, publicID, systemID); 88 } catch (SAXException neverHappens) { } 89 } 90 91 public void setOutputEncoding(String outputEncoding) { 92 this.outputEncoding = outputEncoding; 93 } 94 95 public void marshal(File file) throws IOException { 96 marshal(new FileWriter (file)); 98 } 99 100 public void marshal(OutputStream outputStream) throws IOException { 101 marshal(new OutputStreamWriter (outputStream)); 103 } 104 105 public void marshal(Writer writer) throws IOException { 106 writer.write("<?xml version=\"1.0\" "); 108 if (outputEncoding != null) { 109 writer.write("encoding=\""); 110 writer.write(outputEncoding); 111 writer.write("\"?>\n\n"); 112 113 } else { 114 writer.write("encoding=\"UTF-8\"?>\n\n"); 115 116 } 117 writer.write(docTypeString); 119 writer.write("\n"); 120 writeXMLRepresentation(writer, ""); 122 123 writer.flush(); 125 writer.close(); 126 } 127 128 protected void writeXMLRepresentation(Writer writer, 129 String indent) 130 throws IOException { 131 132 writer.write(indent); 133 writer.write("<count"); 134 135 for (Iterator i = namespaceMappings.keySet().iterator(); i.hasNext(); ) { 137 String prefix = (String )i.next(); 138 String uri = (String )namespaceMappings.get(prefix); 139 writer.write(" xmlns"); 140 if (!prefix.trim().equals("")) { 141 writer.write(":"); 142 writer.write(prefix); 143 } 144 writer.write("=\""); 145 writer.write(uri); 146 writer.write("\"\n "); 147 } 148 149 writer.write("/>\n"); 151 } 152 153 private String escapeAttributeValue(String attributeValue) { 154 String returnValue = attributeValue; 155 for (int i = 0; i < returnValue.length(); i++) { 156 char ch = returnValue.charAt(i); 157 if (ch == '"') { 158 returnValue = new StringBuffer () 159 .append(returnValue.substring(0, i)) 160 .append(""") 161 .append(returnValue.substring(i+1)) 162 .toString(); 163 } 164 } 165 return returnValue; 166 } 167 168 175 public static void setEntityResolver(EntityResolver resolver) { 176 entityResolver = resolver; 177 } 178 179 186 public static void setErrorHandler(ErrorHandler handler) { 187 errorHandler = handler; 188 } 189 190 public static Count unmarshal(File file) throws IOException { 191 return unmarshal(new FileReader (file)); 193 } 194 195 public static Count unmarshal(File file, boolean validate) throws IOException { 196 return unmarshal(new FileReader (file), validate); 198 } 199 200 public static Count unmarshal(InputStream inputStream) throws IOException { 201 return unmarshal(new InputStreamReader (inputStream)); 203 } 204 205 public static Count unmarshal(InputStream inputStream, boolean validate) throws IOException { 206 return unmarshal(new InputStreamReader (inputStream), validate); 208 } 209 210 public static Count unmarshal(Reader reader) throws IOException { 211 return unmarshal(reader, false); 213 } 214 215 public static Count unmarshal(Reader reader, boolean validate) throws IOException { 216 CountImpl count = new CountImpl(); 217 count.setValidating(validate); 218 count.setCurrentUNode(count); 219 count.setParentUNode(null); 220 XMLReader parser = null; 222 String parserClass = System.getProperty("org.xml.sax.driver", 223 "org.apache.xerces.parsers.SAXParser"); 224 try { 225 parser = XMLReaderFactory.createXMLReader(parserClass); 226 227 if (entityResolver != null) { 229 parser.setEntityResolver(entityResolver); 230 } 231 232 parser.setErrorHandler(count); 234 235 parser.setProperty("http://xml.org/sax/properties/lexical-handler", count); 237 238 parser.setContentHandler(count); 240 } catch (SAXException e) { 241 throw new IOException ("Could not load XML parser: " + 242 e.getMessage()); 243 } 244 245 InputSource inputSource = new InputSource (reader); 246 try { 247 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 248 parser.setFeature("http://xml.org/sax/features/namespaces", true); 249 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 250 parser.parse(inputSource); 251 } catch (SAXException e) { 252 throw new IOException ("Error parsing XML document: " + 253 e.getMessage()); 254 } 255 256 return count; 258 } 259 260 public Unmarshallable getParentUNode() { 261 return zeus_parentUNode; 262 } 263 264 public void setParentUNode(Unmarshallable parentUNode) { 265 this.zeus_parentUNode = parentUNode; 266 } 267 268 public Unmarshallable getCurrentUNode() { 269 return zeus_currentUNode; 270 } 271 272 public void setCurrentUNode(Unmarshallable currentUNode) { 273 this.zeus_currentUNode = currentUNode; 274 } 275 276 public void setValidating(boolean validate) { 277 this.validate = validate; 278 } 279 280 public void startDocument() throws SAXException { 281 } 283 284 public void setDocumentLocator(Locator locator) { 285 } 287 288 public void startPrefixMapping(String prefix, String uri) 289 throws SAXException { 290 namespaceMappings.put(prefix, uri); 291 } 292 293 public void startElement(String namespaceURI, String localName, 294 String qName, org.xml.sax.Attributes atts) 295 throws SAXException { 296 297 Unmarshallable current = getCurrentUNode(); 299 if (current != this) { 300 current.startElement(namespaceURI, localName, qName, atts); 301 return; 302 } 303 304 if ((localName.equals("count")) && (!zeus_thisNodeHandled)) { 306 for (int i=0, len=atts.getLength(); i<len; i++) { 308 String attName= atts.getLocalName(i); 309 String attValue = atts.getValue(i); 310 } 311 zeus_thisNodeHandled = true; 312 return; 313 } else { 314 } 316 } 317 318 public void endElement(String namespaceURI, String localName, 319 String qName) 320 throws SAXException { 321 322 Unmarshallable current = getCurrentUNode(); 323 if (current != this) { 324 current.endElement(namespaceURI, localName, qName); 325 return; 326 } 327 328 Unmarshallable parent = getCurrentUNode().getParentUNode(); 329 if (parent != null) { 330 parent.setCurrentUNode(parent); 331 } 332 } 333 334 public void characters(char[] ch, int start, int len) 335 throws SAXException { 336 337 Unmarshallable current = getCurrentUNode(); 339 if (current != this) { 340 current.characters(ch, start, len); 341 return; 342 } 343 344 String text = new String (ch, start, len); 345 text = text.trim(); 346 } 347 348 public void comment(char ch[], int start, int len) throws SAXException { 349 } 351 352 public void warning(SAXParseException e) throws SAXException { 353 if (errorHandler != null) { 354 errorHandler.warning(e); 355 } 356 } 357 358 public void error(SAXParseException e) throws SAXException { 359 if ((validate) && (!hasDTD)) { 360 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."); 361 } 362 if (errorHandler != null) { 363 errorHandler.error(e); 364 } 365 } 366 367 public void fatalError(SAXParseException e) throws SAXException { 368 if ((validate) && (!hasDTD)) { 369 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."); 370 } 371 if (errorHandler != null) { 372 errorHandler.fatalError(e); 373 } 374 } 375 376 public void startDTD(String name, String publicID, String systemID) 377 throws SAXException { 378 379 if ((name == null) || (name.equals(""))) { 380 docTypeString = ""; 381 return; 382 } 383 384 hasDTD = true; 385 StringBuffer docTypeSB = new StringBuffer (); 386 boolean hasPublic = false; 387 388 docTypeSB.append("<!DOCTYPE ") 389 .append(name); 390 391 if ((publicID != null) && (!publicID.equals(""))) { 392 docTypeSB.append(" PUBLIC \"") 393 .append(publicID) 394 .append("\""); 395 hasPublic = true; 396 } 397 398 if ((systemID != null) && (!systemID.equals(""))) { 399 if (!hasPublic) { 400 docTypeSB.append(" SYSTEM"); 401 } 402 docTypeSB.append(" \"") 403 .append(systemID) 404 .append("\""); 405 406 } 407 408 docTypeSB.append(">"); 409 410 docTypeString = docTypeSB.toString(); 411 } 412 413 public void endDTD() throws SAXException { 414 } 416 417 public void startEntity(String name) throws SAXException { 418 } 420 421 public void endEntity(String name) throws SAXException { 422 } 424 425 public void startCDATA() throws SAXException { 426 } 428 429 public void endCDATA() throws SAXException { 430 } 432 433 } 434 | Popular Tags |