1 16 package org.apache.cocoon.xml.dom; 17 18 import org.apache.avalon.framework.CascadingRuntimeException; 19 20 import org.apache.cocoon.xml.AbstractXMLPipe; 21 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.Node ; 24 import org.xml.sax.SAXException ; 25 26 import javax.xml.transform.TransformerFactory ; 27 import javax.xml.transform.dom.DOMResult ; 28 import javax.xml.transform.sax.SAXTransformerFactory ; 29 import javax.xml.transform.sax.TransformerHandler ; 30 31 38 public class DOMBuilder extends AbstractXMLPipe { 39 40 41 protected static final SAXTransformerFactory FACTORY = (SAXTransformerFactory ) TransformerFactory.newInstance(); 42 43 44 protected SAXTransformerFactory factory; 45 46 47 protected Listener listener; 48 49 50 protected DOMResult result; 51 52 53 protected Node parentNode; 54 55 58 public DOMBuilder() { 59 this((Listener) null, (Node ) null); 60 } 61 62 65 public DOMBuilder(SAXTransformerFactory factory) { 66 this(factory, null, null); 67 } 68 69 73 public DOMBuilder(DOMFactory factory) { 74 this((Listener) null, (Node ) null); 75 } 76 77 80 public DOMBuilder(Listener listener) { 81 this(listener, null); 82 } 83 84 88 public DOMBuilder(DOMFactory factory, Listener listener) { 89 this(listener, null); 90 } 91 92 96 public DOMBuilder(DOMFactory domFactory, Listener listener, Node parentNode) { 97 this(listener, parentNode); 98 } 99 100 107 public DOMBuilder(Node parentNode) { 108 this(null, parentNode); 109 } 110 111 114 public DOMBuilder(Listener listener, Node parentNode) { 115 this((SAXTransformerFactory ) null, listener, parentNode); 116 } 117 118 121 public DOMBuilder(SAXTransformerFactory factory, Listener listener, Node parentNode) { 122 super(); 123 this.factory = factory == null? FACTORY: factory; 124 this.listener = listener; 125 this.parentNode = parentNode; 126 setup(); 127 } 128 129 132 private void setup() { 133 try { 134 TransformerHandler handler = this.factory.newTransformerHandler(); 135 setContentHandler(handler); 136 setLexicalHandler(handler); 137 if (this.parentNode != null) { 138 this.result = new DOMResult (this.parentNode); 139 } else { 140 this.result = new DOMResult (); 141 } 142 handler.setResult(this.result); 143 } catch (javax.xml.transform.TransformerException local) { 144 throw new CascadingRuntimeException("Fatal-Error: Unable to get transformer handler", local); 145 } 146 } 147 148 151 public void recycle() { 152 super.recycle(); 153 setup(); 154 } 155 156 159 public Document getDocument() { 160 if (this.result == null || this.result.getNode() == null) { 161 return null; 162 } else if (this.result.getNode().getNodeType() == Node.DOCUMENT_NODE) { 163 return (Document ) this.result.getNode(); 164 } else { 165 return this.result.getNode().getOwnerDocument(); 166 } 167 } 168 169 174 public void endDocument() throws SAXException { 175 super.endDocument(); 176 notifyListener(); 178 } 179 180 183 protected void notifyListener() throws SAXException { 184 if (this.listener != null) { 185 this.listener.notify(getDocument()); 186 } 187 } 188 189 193 public interface Listener { 194 195 198 void notify(Document doc) throws SAXException ; 199 } 200 } 201 | Popular Tags |