1 16 19 20 21 package com.sun.org.apache.xalan.internal.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 com.sun.org.apache.xalan.internal.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.Text ; 36 import org.w3c.dom.ProcessingInstruction ; 37 import org.xml.sax.Attributes ; 38 import org.xml.sax.ContentHandler ; 39 import org.xml.sax.Locator ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.ext.LexicalHandler ; 42 43 46 public class SAX2DOM implements ContentHandler , LexicalHandler , Constants { 47 48 private Node _root = null; 49 private Document _document = null; 50 private Stack _nodeStk = new Stack (); 51 private Vector _namespaceDecls = null; 52 private Node _lastSibling = null; 53 54 public SAX2DOM() throws ParserConfigurationException { 55 final DocumentBuilderFactory factory = 56 DocumentBuilderFactory.newInstance(); 57 _document = factory.newDocumentBuilder().newDocument(); 58 _root = _document; 59 } 60 61 public SAX2DOM(Node root) throws ParserConfigurationException { 62 _root = root; 63 if (root instanceof Document ) { 64 _document = (Document )root; 65 } 66 else if (root != null) { 67 _document = root.getOwnerDocument(); 68 } 69 else { 70 final DocumentBuilderFactory factory = 71 DocumentBuilderFactory.newInstance(); 72 _document = factory.newDocumentBuilder().newDocument(); 73 _root = _document; 74 } 75 } 76 77 public Node getDOM() { 78 return _root; 79 } 80 81 public void characters(char[] ch, int start, int length) { 82 final Node last = (Node )_nodeStk.peek(); 83 84 if (last != _document) { 86 final String text = new String (ch, start, length); 87 if( _lastSibling != null && _lastSibling.getNodeType() == Node.TEXT_NODE ){ 88 ((Text )_lastSibling).appendData(text); 89 } 90 else{ 91 _lastSibling = last.appendChild(_document.createTextNode(text)); 92 } 93 } 94 } 95 96 public void startDocument() { 97 _nodeStk.push(_root); 98 } 99 100 public void endDocument() { 101 _nodeStk.pop(); 102 } 103 104 public void startElement(String namespace, String localName, String qName, 105 Attributes attrs) 106 { 107 final Element tmp = (Element )_document.createElementNS(namespace, qName); 108 109 if (_namespaceDecls != null) { 111 final int nDecls = _namespaceDecls.size(); 112 for (int i = 0; i < nDecls; i++) { 113 final String prefix = (String ) _namespaceDecls.elementAt(i++); 114 115 if (prefix == null || prefix.equals(EMPTYSTRING)) { 116 tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX, 117 (String ) _namespaceDecls.elementAt(i)); 118 } 119 else { 120 tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix, 121 (String ) _namespaceDecls.elementAt(i)); 122 } 123 } 124 _namespaceDecls.clear(); 125 } 126 127 final int nattrs = attrs.getLength(); 129 for (int i = 0; i < nattrs; i++) { 130 String attQName = attrs.getQName(i); 132 String attURI = attrs.getURI(i); 133 String attrLocalName = attrs.getLocalName(i); 134 135 if (attrLocalName == null || attrLocalName.equals(EMPTYSTRING)) { 136 tmp.setAttribute(attQName, attrs.getValue(i)); 137 if (attrs.getType(i).equals("ID")) { 138 tmp.setIdAttribute(attQName, true); 139 } 140 } else { 141 tmp.setAttributeNS(attURI, attQName, attrs.getValue(i)); 142 if (attrs.getType(i).equals("ID")) { 143 tmp.setIdAttributeNS(attURI, attrLocalName, true); 144 } 145 } 146 } 147 148 Node last = (Node )_nodeStk.peek(); 150 last.appendChild(tmp); 151 152 _nodeStk.push(tmp); 154 _lastSibling = null; 155 } 156 157 public void endElement(String namespace, String localName, String qName) { 158 _nodeStk.pop(); 159 _lastSibling = null; 160 } 161 162 public void startPrefixMapping(String prefix, String uri) { 163 if (_namespaceDecls == null) { 164 _namespaceDecls = new Vector (2); 165 } 166 _namespaceDecls.addElement(prefix); 167 _namespaceDecls.addElement(uri); 168 } 169 170 public void endPrefixMapping(String prefix) { 171 } 173 174 178 public void ignorableWhitespace(char[] ch, int start, int length) { 179 } 180 181 184 public void processingInstruction(String target, String data) { 185 final Node last = (Node )_nodeStk.peek(); 186 ProcessingInstruction pi = _document.createProcessingInstruction( 187 target, data); 188 if (pi != null){ 189 last.appendChild(pi); 190 _lastSibling = pi; 191 } 192 } 193 194 198 public void setDocumentLocator(Locator locator) { 199 } 200 201 205 public void skippedEntity(String name) { 206 } 207 208 209 212 public void comment(char[] ch, int start, int length) { 213 final Node last = (Node )_nodeStk.peek(); 214 Comment comment = _document.createComment(new String (ch,start,length)); 215 if (comment != null){ 216 last.appendChild(comment); 217 _lastSibling = comment; 218 } 219 } 220 221 public void startCDATA() { } 223 public void endCDATA() { } 224 public void startEntity(java.lang.String name) { } 225 public void endDTD() { } 226 public void endEntity(String name) { } 227 public void startDTD(String name, String publicId, String systemId) 228 throws SAXException { } 229 230 } 231 | Popular Tags |