1 17 18 19 package org.apache.commons.digester; 20 21 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 26 import org.w3c.dom.Attr ; 27 import org.w3c.dom.DOMException ; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.ContentHandler ; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.helpers.DefaultHandler ; 35 36 37 74 75 public class NodeCreateRule extends Rule { 76 77 78 80 81 85 private class NodeBuilder 86 extends DefaultHandler { 87 88 89 91 92 106 public NodeBuilder(Document doc, Node root) 107 throws ParserConfigurationException , SAXException { 108 109 this.doc = doc; 110 this.root = root; 111 this.top = root; 112 113 oldContentHandler = digester.getCustomContentHandler(); 114 115 } 116 117 118 120 121 125 protected ContentHandler oldContentHandler = null; 126 127 128 132 protected int depth = 0; 133 134 135 138 protected Document doc = null; 139 140 141 144 protected Node root = null; 145 146 147 150 protected Node top = null; 151 152 153 155 156 164 public void characters(char[] ch, int start, int length) 165 throws SAXException { 166 167 try { 168 String str = new String (ch, start, length); 169 if (str.trim().length() > 0) { 170 top.appendChild(doc.createTextNode(str)); 171 } 172 } catch (DOMException e) { 173 throw new SAXException (e.getMessage()); 174 } 175 176 } 177 178 179 187 public void endElement(String namespaceURI, String localName, 188 String qName) 189 throws SAXException { 190 191 try { 192 if (depth == 0) { 193 getDigester().setCustomContentHandler(oldContentHandler); 194 getDigester().push(root); 195 getDigester().endElement(namespaceURI, localName, qName); 196 } 197 198 top = top.getParentNode(); 199 depth--; 200 } catch (DOMException e) { 201 throw new SAXException (e.getMessage()); 202 } 203 204 } 205 206 207 217 public void processingInstruction(String target, String data) 218 throws SAXException { 219 220 try { 221 top.appendChild(doc.createProcessingInstruction(target, data)); 222 } catch (DOMException e) { 223 throw new SAXException (e.getMessage()); 224 } 225 226 } 227 228 229 239 public void startElement(String namespaceURI, String localName, 240 String qName, Attributes atts) 241 throws SAXException { 242 243 try { 244 Node previousTop = top; 245 if ((localName == null) || (localName.length() == 0)) { 246 top = doc.createElement(qName); 247 } else { 248 top = doc.createElementNS(namespaceURI, localName); 249 } 250 for (int i = 0; i < atts.getLength(); i++) { 251 Attr attr = null; 252 if ((atts.getLocalName(i) == null) || 253 (atts.getLocalName(i).length() == 0)) { 254 attr = doc.createAttribute(atts.getQName(i)); 255 attr.setNodeValue(atts.getValue(i)); 256 ((Element )top).setAttributeNode(attr); 257 } else { 258 attr = doc.createAttributeNS(atts.getURI(i), 259 atts.getLocalName(i)); 260 attr.setNodeValue(atts.getValue(i)); 261 ((Element )top).setAttributeNodeNS(attr); 262 } 263 } 264 previousTop.appendChild(top); 265 depth++; 266 } catch (DOMException e) { 267 throw new SAXException (e.getMessage()); 268 } 269 270 } 271 272 } 273 274 275 277 278 282 public NodeCreateRule() throws ParserConfigurationException { 283 284 this(Node.ELEMENT_NODE); 285 286 } 287 288 289 297 public NodeCreateRule(DocumentBuilder documentBuilder) { 298 299 this(Node.ELEMENT_NODE, documentBuilder); 300 301 } 302 303 304 315 public NodeCreateRule(int nodeType) throws ParserConfigurationException { 316 317 this(nodeType, 318 DocumentBuilderFactory.newInstance().newDocumentBuilder()); 319 320 } 321 322 323 337 public NodeCreateRule(int nodeType, DocumentBuilder documentBuilder) { 338 339 if (!((nodeType == Node.DOCUMENT_FRAGMENT_NODE) || 340 (nodeType == Node.ELEMENT_NODE))) { 341 throw new IllegalArgumentException ( 342 "Can only create nodes of type DocumentFragment and Element"); 343 } 344 this.nodeType = nodeType; 345 this.documentBuilder = documentBuilder; 346 347 } 348 349 350 352 353 356 private DocumentBuilder documentBuilder = null; 357 358 359 366 private int nodeType = Node.ELEMENT_NODE; 367 368 369 371 372 388 public void begin(String namespaceURI, String name, Attributes attributes) 389 throws Exception { 390 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 getDigester().setCustomContentHandler(builder); 419 } 420 421 422 425 public void end() throws Exception { 426 427 Object top = digester.pop(); 428 429 } 430 431 432 } 433 | Popular Tags |