1 18 19 20 package org.apache.tomcat.util.digester; 21 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 27 import org.w3c.dom.Attr ; 28 import org.w3c.dom.DOMException ; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 import org.xml.sax.Attributes ; 33 import org.xml.sax.ContentHandler ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.XMLReader ; 36 import org.xml.sax.helpers.DefaultHandler ; 37 38 39 76 77 public class NodeCreateRule extends Rule { 78 79 80 82 83 87 private class NodeBuilder 88 extends DefaultHandler { 89 90 91 93 94 108 public NodeBuilder(Document doc, Node root) 109 throws ParserConfigurationException , SAXException { 110 111 this.doc = doc; 112 this.root = root; 113 this.top = root; 114 115 oldContentHandler = digester.getXMLReader().getContentHandler(); 116 117 } 118 119 120 122 123 127 protected ContentHandler oldContentHandler = null; 128 129 130 134 protected int depth = 0; 135 136 137 140 protected Document doc = null; 141 142 143 146 protected Node root = null; 147 148 149 152 protected Node top = null; 153 154 155 157 158 166 public void characters(char[] ch, int start, int length) 167 throws SAXException { 168 169 try { 170 String str = new String (ch, start, length); 171 if (str.trim().length() > 0) { 172 top.appendChild(doc.createTextNode(str)); 173 } 174 } catch (DOMException e) { 175 throw new SAXException (e.getMessage()); 176 } 177 178 } 179 180 181 189 public void endElement(String namespaceURI, String localName, 190 String qName) 191 throws SAXException { 192 193 try { 194 if (depth == 0) { 195 getDigester().getXMLReader().setContentHandler( 196 oldContentHandler); 197 getDigester().push(root); 198 getDigester().endElement(namespaceURI, localName, qName); 199 } 200 201 top = top.getParentNode(); 202 depth--; 203 } catch (DOMException e) { 204 throw new SAXException (e.getMessage()); 205 } 206 207 } 208 209 210 220 public void processingInstruction(String target, String data) 221 throws SAXException { 222 223 try { 224 top.appendChild(doc.createProcessingInstruction(target, data)); 225 } catch (DOMException e) { 226 throw new SAXException (e.getMessage()); 227 } 228 229 } 230 231 232 242 public void startElement(String namespaceURI, String localName, 243 String qName, Attributes atts) 244 throws SAXException { 245 246 try { 247 Node previousTop = top; 248 if ((localName == null) || (localName.length() == 0)) { 249 top = doc.createElement(qName); 250 } else { 251 top = doc.createElementNS(namespaceURI, localName); 252 } 253 for (int i = 0; i < atts.getLength(); i++) { 254 Attr attr = null; 255 if ((atts.getLocalName(i) == null) || 256 (atts.getLocalName(i).length() == 0)) { 257 attr = doc.createAttribute(atts.getQName(i)); 258 attr.setNodeValue(atts.getValue(i)); 259 ((Element )top).setAttributeNode(attr); 260 } else { 261 attr = doc.createAttributeNS(atts.getURI(i), 262 atts.getLocalName(i)); 263 attr.setNodeValue(atts.getValue(i)); 264 ((Element )top).setAttributeNodeNS(attr); 265 } 266 } 267 previousTop.appendChild(top); 268 depth++; 269 } catch (DOMException e) { 270 throw new SAXException (e.getMessage()); 271 } 272 273 } 274 275 } 276 277 278 280 281 285 public NodeCreateRule() throws ParserConfigurationException { 286 287 this(Node.ELEMENT_NODE); 288 289 } 290 291 292 300 public NodeCreateRule(DocumentBuilder documentBuilder) { 301 302 this(Node.ELEMENT_NODE, documentBuilder); 303 304 } 305 306 307 318 public NodeCreateRule(int nodeType) throws ParserConfigurationException { 319 320 this(nodeType, 321 DocumentBuilderFactory.newInstance().newDocumentBuilder()); 322 323 } 324 325 326 340 public NodeCreateRule(int nodeType, DocumentBuilder documentBuilder) { 341 342 if (!((nodeType == Node.DOCUMENT_FRAGMENT_NODE) || 343 (nodeType == Node.ELEMENT_NODE))) { 344 throw new IllegalArgumentException ( 345 "Can only create nodes of type DocumentFragment and Element"); 346 } 347 this.nodeType = nodeType; 348 this.documentBuilder = documentBuilder; 349 350 } 351 352 353 355 356 359 private DocumentBuilder documentBuilder = null; 360 361 362 369 private int nodeType = Node.ELEMENT_NODE; 370 371 372 374 375 387 public void begin(String namespaceURI, String name, Attributes attributes) 388 throws Exception { 389 390 XMLReader xmlReader = getDigester().getXMLReader(); 391 Document doc = documentBuilder.newDocument(); 392 NodeBuilder builder = null; 393 if (nodeType == Node.ELEMENT_NODE) { 394 Element element = null; 395 if (getDigester().getNamespaceAware()) { 396 element = 397 doc.createElementNS(namespaceURI, name); 398 for (int i = 0; i < attributes.getLength(); i++) { 399 element.setAttributeNS(attributes.getURI(i), 400 attributes.getLocalName(i), 401 attributes.getValue(i)); 402 } 403 } else { 404 element = doc.createElement(name); 405 for (int i = 0; i < attributes.getLength(); i++) { 406 element.setAttribute(attributes.getQName(i), 407 attributes.getValue(i)); 408 } 409 } 410 builder = new NodeBuilder(doc, element); 411 } else { 412 builder = new NodeBuilder(doc, doc.createDocumentFragment()); 413 } 414 xmlReader.setContentHandler(builder); 415 416 } 417 418 419 422 public void end() throws Exception { 423 424 Object top = digester.pop(); 425 426 } 427 428 429 } 430 | Popular Tags |