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