1 22 23 package org.xquark.util; 24 25 import java.io.IOException ; 26 import java.util.Iterator ; 27 28 import org.w3c.dom.*; 29 import org.xml.sax.*; 30 import org.xml.sax.ext.LexicalHandler ; 31 32 42 public class DOMReader implements XMLReader, Locator, SAXConstants 43 { 44 private static final String RCSRevision = "$Revision: 1.3 $"; 45 private static final String RCSName = "$Name: $"; 46 47 protected boolean fragment = false; 48 51 protected Node root; 52 53 56 protected ContentHandler contentHandler = null; 57 58 61 protected DTDHandler dtdHandler = null; 62 63 66 protected ErrorHandler errorHandler = null; 67 68 71 protected EntityResolver entityResolver = null; 72 73 76 protected LexicalHandler lexicalHandler = null; 77 78 81 protected DOM2SAXAttrs wAtts = new DOM2SAXAttrs(); 82 83 public DOMReader(){} 84 85 public DOMReader(Document doc) 86 {setDocument(doc);} 88 public DOMReader(Element root) 89 {setRoot(root);} 91 public void setDocument(Document doc) 92 {this.root = doc;} 93 94 public void setRoot(Node root) 95 {this.root = root;} 96 97 public ContentHandler getContentHandler() 98 { 99 return contentHandler; 100 } 101 102 public DTDHandler getDTDHandler() 103 { 104 return dtdHandler; 105 } 106 107 public EntityResolver getEntityResolver() 108 { 109 return entityResolver; 110 } 111 112 public ErrorHandler getErrorHandler() 113 { 114 return errorHandler; 115 } 116 117 public boolean getFeature(String name) 118 throws SAXNotRecognizedException, 119 SAXNotSupportedException 120 { 121 if (name.equals(FRAGMENT_FEATURE)) 122 return fragment; 123 if (name.equals(SAX_NAMESPACE_FEATURE)) 124 return true; 125 if (name.equals(SAX_PREFIX_FEATURE)) 126 return false; 127 throw new SAXNotRecognizedException("Don't recognize feature \""+name+"\""); 128 } 129 130 133 139 public Object getProperty(String name) 140 throws SAXNotRecognizedException, 141 SAXNotSupportedException 142 { 143 if (name.equals(SAX_LEXICAL_PROPERTY)) 144 return lexicalHandler; 145 else 146 throw new SAXNotRecognizedException("Property \""+name+"\" not recognized"); 147 } 148 149 public void parse(InputSource input) throws SAXException, 151 IOException 152 { 153 parse(""); 155 } 156 157 public void parse(String systemId) 158 throws SAXException, 159 IOException 160 { 161 if ((contentHandler != null) && !fragment) 163 { 164 contentHandler.setDocumentLocator(this); 165 contentHandler.startDocument(); 166 } 167 if (root == null) 168 throw new SAXException("A DOM document or element must be provided."); 169 else 170 { 171 switch (root.getNodeType()) 172 { 173 case Node.ELEMENT_NODE: 174 case Node.TEXT_NODE: 176 parseNode(root); 177 break; 178 case Node.DOCUMENT_FRAGMENT_NODE: 179 case Node.DOCUMENT_NODE: 180 parseChildNodes(root); 181 break; 182 default: 183 throw new SAXException("The root node provided for browsing must be an Element, a Document or a Document fragment."); 184 } 185 if ((contentHandler != null) && !fragment) 186 contentHandler.endDocument(); 187 } 188 } 189 190 195 protected void parseChildNodes(Node node) throws SAXException { 196 NodeList nl=node.getChildNodes(); 197 int nbchilds=nl.getLength(); 198 for (int i=0; i<nbchilds; i++) 199 parseNode(nl.item(i)); 200 } 201 202 207 protected void parseNode(Node node) throws SAXException { 208 switch (node.getNodeType()) 209 { 210 case Node.ELEMENT_NODE: 211 if (contentHandler != null) { 212 generateStartElement(node); 213 } 214 parseChildNodes(node); 215 if (contentHandler != null) 216 { 217 generateEndElement(node); 218 } 219 break; 220 case Node.PROCESSING_INSTRUCTION_NODE: 221 if (contentHandler != null) 222 generateProcessingInstruction(node); 223 break; 224 case Node.TEXT_NODE: 225 if (contentHandler != null) 226 generateText(node); 227 break; 228 case Node.CDATA_SECTION_NODE: 229 if (contentHandler != null) 230 generateCDataSection(node); 231 break; 232 case Node.COMMENT_NODE: 233 if (lexicalHandler != null) 234 generateComment(node); 235 break; 236 case Node.ENTITY_REFERENCE_NODE: 237 if (contentHandler != null) 238 generateEntityReference(node); 239 break; 240 case Node.DOCUMENT_FRAGMENT_NODE: 241 case Node.DOCUMENT_NODE: 242 case Node.DOCUMENT_TYPE_NODE: 243 case Node.ENTITY_NODE: 244 case Node.NOTATION_NODE: 245 case Node.ATTRIBUTE_NODE: 246 break; 247 } 248 } 249 250 protected void generateStartElement(Node node) throws SAXException { 251 String uri = node.getNamespaceURI(); 252 if (uri == null) uri = ""; 253 String local = node.getLocalName(); 254 if (local == null) local = ""; 255 String prefix = node.getPrefix(); 256 String qname = prefix == null ? local : prefix + ":" +local; 257 wAtts.setDOMAttributes(node.getAttributes()); 259 Iterator it = wAtts.getPrefixIterator(); if (it != null) 261 while (it.hasNext()) { 262 Attr a = (Attr)it.next(); 263 contentHandler.startPrefixMapping( 264 a.getLocalName().equals(XMLNS_PREFIX) ? "": a.getLocalName(), a.getValue()); 266 } 267 contentHandler.startElement(uri, local, qname, wAtts); 268 } 269 270 protected void generateEndElement(Node node) throws SAXException { 271 String uri = node.getNamespaceURI(); 272 if (uri == null) uri = ""; 273 String local = node.getLocalName(); 274 if (local == null) local = ""; 275 String prefix = node.getPrefix(); 276 String qname = prefix == null ? local : prefix + ":" +local; 277 contentHandler.endElement(uri, local, qname); 278 wAtts.setDOMAttributes(node.getAttributes()); 280 Iterator it = wAtts.getPrefixIterator(); if (it != null) 282 while (it.hasNext()) { 284 Attr a = (Attr)it.next(); 285 contentHandler.endPrefixMapping(a.getLocalName().equals(XMLNS_PREFIX) ? "": a.getLocalName()); 286 } 287 } 288 289 protected void generateProcessingInstruction(Node node) throws SAXException { 290 contentHandler.processingInstruction(node.getNodeName(), node.getNodeValue()); 291 } 292 293 protected void generateText(Node node) throws SAXException { 294 String value = node.getNodeValue(); 295 if (value != null) 296 contentHandler.characters(value.toCharArray(), 0, value.length()); 297 } 298 299 protected void generateComment(Node node) throws SAXException { 300 String value = node.getNodeValue(); 301 if (value != null) 302 lexicalHandler.comment(value.toCharArray(), 0, value.length()); 303 } 304 305 protected void generateCDataSection(Node node) throws SAXException { 306 if (lexicalHandler != null) lexicalHandler.startCDATA(); 307 String value = node.getNodeValue(); 308 if (value != null) 309 contentHandler.characters(value.toCharArray(), 0, value.length()); 310 if (lexicalHandler != null) lexicalHandler.endCDATA(); 311 } 312 313 protected void generateEntityReference(Node node) throws SAXException { 314 contentHandler.skippedEntity(node.getNodeName()); 315 } 316 317 public void setContentHandler(ContentHandler handler) 318 { 319 contentHandler = handler; 320 } 321 322 public void setDTDHandler(DTDHandler handler) 323 { 324 dtdHandler = handler; 325 } 326 327 public void setEntityResolver(EntityResolver resolver) 328 { 329 entityResolver = resolver; 330 } 331 332 public void setErrorHandler(ErrorHandler handler) 333 { 334 errorHandler = handler; 335 } 336 337 public void setFeature(String name, boolean value) 338 throws SAXNotRecognizedException, 339 SAXNotSupportedException 340 { 341 if (name.equals(FRAGMENT_FEATURE)) 342 fragment = value; 343 else if (name.equals(SAX_NAMESPACE_FEATURE)) 344 { 345 if (!value) 346 throw new SAXNotSupportedException 347 ("Feature: \"" 348 + name 349 + "=" 350 + value 351 + "\" not supported"); 352 } 353 else if (name.equals(SAX_PREFIX_FEATURE)) 354 { 355 if (value) 356 throw new SAXNotSupportedException 357 ("Feature: \"" 358 + name 359 + "=" 360 + value 361 + "\" not supported"); 362 } 363 else 364 throw new SAXNotRecognizedException("Feature \""+name+"\" not recognized."); 365 } 366 367 370 376 public void setProperty(String name, Object value) 377 throws SAXNotRecognizedException, 378 SAXNotSupportedException 379 { 380 if (name.equals("http://xml.org/sax/properties/lexical-handler")) 381 lexicalHandler = (LexicalHandler )value; 382 else 383 throw new SAXNotRecognizedException("Don't recognize property \""+name+"\""); 384 } 385 386 public int getColumnNumber() 387 { return -1; } 388 389 public int getLineNumber() 390 { return -1; } 391 392 public String getPublicId() 393 { return null; } 394 395 public String getSystemId() 396 { return null; } 397 } 398 399 400 | Popular Tags |