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