1 16 19 20 21 package org.apache.xalan.xsltc.trax; 22 23 import java.util.Stack ; 24 import java.util.Vector ; 25 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 29 import org.apache.xalan.xsltc.runtime.Constants; 30 31 import org.w3c.dom.Comment ; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.ProcessingInstruction ; 36 import org.xml.sax.Attributes ; 37 import org.xml.sax.ContentHandler ; 38 import org.xml.sax.Locator ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.ext.LexicalHandler ; 41 42 45 public class SAX2DOM implements ContentHandler , LexicalHandler , Constants { 46 47 private Node _root = null; 48 private Document _document = null; 49 private Stack _nodeStk = new Stack (); 50 private Vector _namespaceDecls = null; 51 52 public SAX2DOM() throws ParserConfigurationException { 53 final DocumentBuilderFactory factory = 54 DocumentBuilderFactory.newInstance(); 55 _document = factory.newDocumentBuilder().newDocument(); 56 _root = _document; 57 } 58 59 public SAX2DOM(Node root) throws ParserConfigurationException { 60 _root = root; 61 if (root instanceof Document ) { 62 _document = (Document )root; 63 } 64 else if (root != null) { 65 _document = root.getOwnerDocument(); 66 } 67 else { 68 final DocumentBuilderFactory factory = 69 DocumentBuilderFactory.newInstance(); 70 _document = factory.newDocumentBuilder().newDocument(); 71 _root = _document; 72 } 73 } 74 75 public Node getDOM() { 76 return _root; 77 } 78 79 public void characters(char[] ch, int start, int length) { 80 final Node last = (Node )_nodeStk.peek(); 81 82 if (last != _document) { 84 final String text = new String (ch, start, length); 85 last.appendChild(_document.createTextNode(text)); 86 } 87 } 88 89 public void startDocument() { 90 _nodeStk.push(_root); 91 } 92 93 public void endDocument() { 94 _nodeStk.pop(); 95 } 96 97 public void startElement(String namespace, String localName, String qName, 98 Attributes attrs) 99 { 100 final Element tmp = (Element )_document.createElementNS(namespace, qName); 101 102 if (_namespaceDecls != null) { 104 final int nDecls = _namespaceDecls.size(); 105 for (int i = 0; i < nDecls; i++) { 106 final String prefix = (String ) _namespaceDecls.elementAt(i++); 107 108 if (prefix == null || prefix.equals(EMPTYSTRING)) { 109 tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX, 110 (String ) _namespaceDecls.elementAt(i)); 111 } 112 else { 113 tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix, 114 (String ) _namespaceDecls.elementAt(i)); 115 } 116 } 117 _namespaceDecls.clear(); 118 } 119 120 final int nattrs = attrs.getLength(); 122 for (int i = 0; i < nattrs; i++) { 123 if (attrs.getLocalName(i) == null) { 124 tmp.setAttribute(attrs.getQName(i), attrs.getValue(i)); 125 } 126 else { 127 tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i), 128 attrs.getValue(i)); 129 } 130 } 131 132 Node last = (Node )_nodeStk.peek(); 134 last.appendChild(tmp); 135 136 _nodeStk.push(tmp); 138 } 139 140 public void endElement(String namespace, String localName, String qName) { 141 _nodeStk.pop(); 142 } 143 144 public void startPrefixMapping(String prefix, String uri) { 145 if (_namespaceDecls == null) { 146 _namespaceDecls = new Vector (2); 147 } 148 _namespaceDecls.addElement(prefix); 149 _namespaceDecls.addElement(uri); 150 } 151 152 public void endPrefixMapping(String prefix) { 153 } 155 156 160 public void ignorableWhitespace(char[] ch, int start, int length) { 161 } 162 163 166 public void processingInstruction(String target, String data) { 167 final Node last = (Node )_nodeStk.peek(); 168 ProcessingInstruction pi = _document.createProcessingInstruction( 169 target, data); 170 if (pi != null) last.appendChild(pi); 171 } 172 173 177 public void setDocumentLocator(Locator locator) { 178 } 179 180 184 public void skippedEntity(String name) { 185 } 186 187 188 191 public void comment(char[] ch, int start, int length) { 192 final Node last = (Node )_nodeStk.peek(); 193 Comment comment = _document.createComment(new String (ch,start,length)); 194 if (comment != null) last.appendChild(comment); 195 } 196 197 public void startCDATA() { } 199 public void endCDATA() { } 200 public void startEntity(java.lang.String name) { } 201 public void endDTD() { } 202 public void endEntity(String name) { } 203 public void startDTD(String name, String publicId, String systemId) 204 throws SAXException { } 205 206 } 207 | Popular Tags |