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