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 ConstantImpl extends DefaultHandler implements Unmarshallable, LexicalHandler , Constant { 46 47 private String value; 48 private boolean zeus_ValueSet; 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 ConstantImpl() { 81 zeus_ValueSet = false; 82 docTypeString = ""; 83 hasDTD = false; 84 validate = false; 85 namespaceMappings = new HashMap (); 86 } 87 88 public String getValue() { 89 return value; 90 } 91 92 public void setValue(String value) { 93 this.value = value; 94 zeus_ValueSet = 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("<constant"); 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 (getValue() != null) { 163 writer.write(">"); 165 writer.write(getValue()); 166 writer.write("</constant>\n"); 167 } else { 168 writer.write("/>\n"); 169 } 170 } 171 172 private String escapeAttributeValue(String attributeValue) { 173 String returnValue = attributeValue; 174 for (int i = 0; i < returnValue.length(); i++) { 175 char ch = returnValue.charAt(i); 176 if (ch == '"') { 177 returnValue = new StringBuffer () 178 .append(returnValue.substring(0, i)) 179 .append(""") 180 .append(returnValue.substring(i+1)) 181 .toString(); 182 } 183 } 184 return returnValue; 185 } 186 187 194 public static void setEntityResolver(EntityResolver resolver) { 195 entityResolver = resolver; 196 } 197 198 205 public static void setErrorHandler(ErrorHandler handler) { 206 errorHandler = handler; 207 } 208 209 public static Constant unmarshal(File file) throws IOException { 210 return unmarshal(new FileReader (file)); 212 } 213 214 public static Constant unmarshal(File file, boolean validate) throws IOException { 215 return unmarshal(new FileReader (file), validate); 217 } 218 219 public static Constant unmarshal(InputStream inputStream) throws IOException { 220 return unmarshal(new InputStreamReader (inputStream)); 222 } 223 224 public static Constant unmarshal(InputStream inputStream, boolean validate) throws IOException { 225 return unmarshal(new InputStreamReader (inputStream), validate); 227 } 228 229 public static Constant unmarshal(Reader reader) throws IOException { 230 return unmarshal(reader, false); 232 } 233 234 public static Constant unmarshal(Reader reader, boolean validate) throws IOException { 235 ConstantImpl constant = new ConstantImpl(); 236 constant.setValidating(validate); 237 constant.setCurrentUNode(constant); 238 constant.setParentUNode(null); 239 XMLReader parser = null; 241 String parserClass = System.getProperty("org.xml.sax.driver", 242 "org.apache.xerces.parsers.SAXParser"); 243 try { 244 parser = XMLReaderFactory.createXMLReader(parserClass); 245 246 if (entityResolver != null) { 248 parser.setEntityResolver(entityResolver); 249 } 250 251 parser.setErrorHandler(constant); 253 254 parser.setProperty("http://xml.org/sax/properties/lexical-handler", constant); 256 257 parser.setContentHandler(constant); 259 } catch (SAXException e) { 260 throw new IOException ("Could not load XML parser: " + 261 e.getMessage()); 262 } 263 264 InputSource inputSource = new InputSource (reader); 265 try { 266 parser.setFeature("http://xml.org/sax/features/validation", new Boolean (validate).booleanValue()); 267 parser.setFeature("http://xml.org/sax/features/namespaces", true); 268 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 269 parser.parse(inputSource); 270 } catch (SAXException e) { 271 throw new IOException ("Error parsing XML document: " + 272 e.getMessage()); 273 } 274 275 return constant; 277 } 278 279 public Unmarshallable getParentUNode() { 280 return zeus_parentUNode; 281 } 282 283 public void setParentUNode(Unmarshallable parentUNode) { 284 this.zeus_parentUNode = parentUNode; 285 } 286 287 public Unmarshallable getCurrentUNode() { 288 return zeus_currentUNode; 289 } 290 291 public void setCurrentUNode(Unmarshallable currentUNode) { 292 this.zeus_currentUNode = currentUNode; 293 } 294 295 public void setValidating(boolean validate) { 296 this.validate = validate; 297 } 298 299 public void startDocument() throws SAXException { 300 } 302 303 public void setDocumentLocator(Locator locator) { 304 } 306 307 public void startPrefixMapping(String prefix, String uri) 308 throws SAXException { 309 namespaceMappings.put(prefix, uri); 310 } 311 312 public void startElement(String namespaceURI, String localName, 313 String qName, org.xml.sax.Attributes atts) 314 throws SAXException { 315 316 Unmarshallable current = getCurrentUNode(); 318 if (current != this) { 319 current.startElement(namespaceURI, localName, qName, atts); 320 return; 321 } 322 323 if ((localName.equals("constant")) && (!zeus_thisNodeHandled)) { 325 for (int i=0, len=atts.getLength(); i<len; i++) { 327 String attName= atts.getLocalName(i); 328 String attValue = atts.getValue(i); 329 } 330 zeus_thisNodeHandled = true; 331 return; 332 } else { 333 } 335 } 336 337 public void endElement(String namespaceURI, String localName, 338 String qName) 339 throws SAXException { 340 341 Unmarshallable current = getCurrentUNode(); 342 if (current != this) { 343 current.endElement(namespaceURI, localName, qName); 344 return; 345 } 346 347 if (this.value == null) { 349 this.value = ""; 350 } 351 Unmarshallable parent = getCurrentUNode().getParentUNode(); 352 if (parent != null) { 353 parent.setCurrentUNode(parent); 354 } 355 } 356 357 public void characters(char[] ch, int start, int len) 358 throws SAXException { 359 360 Unmarshallable current = getCurrentUNode(); 362 if (current != this) { 363 current.characters(ch, start, len); 364 return; 365 } 366 367 String text = new String (ch, start, len); 368 text = text.trim(); 369 if (this.value == null) { 370 this.value = text; 371 } else { 372 this.value = new StringBuffer (this.value).append(text).toString(); 373 } 374 } 375 376 public void comment(char ch[], int start, int len) throws SAXException { 377 } 379 380 public void warning(SAXParseException e) throws SAXException { 381 if (errorHandler != null) { 382 errorHandler.warning(e); 383 } 384 } 385 386 public void error(SAXParseException e) throws SAXException { 387 if ((validate) && (!hasDTD)) { 388 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."); 389 } 390 if (errorHandler != null) { 391 errorHandler.error(e); 392 } 393 } 394 395 public void fatalError(SAXParseException e) throws SAXException { 396 if ((validate) && (!hasDTD)) { 397 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."); 398 } 399 if (errorHandler != null) { 400 errorHandler.fatalError(e); 401 } 402 } 403 404 public void startDTD(String name, String publicID, String systemID) 405 throws SAXException { 406 407 if ((name == null) || (name.equals(""))) { 408 docTypeString = ""; 409 return; 410 } 411 412 hasDTD = true; 413 StringBuffer docTypeSB = new StringBuffer (); 414 boolean hasPublic = false; 415 416 docTypeSB.append("<!DOCTYPE ") 417 .append(name); 418 419 if ((publicID != null) && (!publicID.equals(""))) { 420 docTypeSB.append(" PUBLIC \"") 421 .append(publicID) 422 .append("\""); 423 hasPublic = true; 424 } 425 426 if ((systemID != null) && (!systemID.equals(""))) { 427 if (!hasPublic) { 428 docTypeSB.append(" SYSTEM"); 429 } 430 docTypeSB.append(" \"") 431 .append(systemID) 432 .append("\""); 433 434 } 435 436 docTypeSB.append(">"); 437 438 docTypeString = docTypeSB.toString(); 439 } 440 441 public void endDTD() throws SAXException { 442 } 444 445 public void startEntity(String name) throws SAXException { 446 } 448 449 public void endEntity(String name) throws SAXException { 450 } 452 453 public void startCDATA() throws SAXException { 454 } 456 457 public void endCDATA() throws SAXException { 458 } 460 461 } 462 | Popular Tags |