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