1 16 19 package org.apache.xml.utils; 20 21 import java.util.Stack ; 22 23 import org.apache.xml.res.XMLErrorResources; 24 import org.apache.xml.res.XMLMessages; 25 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.DocumentFragment ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.Text ; 31 import org.w3c.dom.CDATASection ; 32 33 import org.xml.sax.Attributes ; 34 import org.xml.sax.ContentHandler ; 35 import org.xml.sax.Locator ; 36 import org.xml.sax.ext.LexicalHandler ; 37 43 public class DOMBuilder 44 implements ContentHandler , LexicalHandler 45 { 46 47 48 public Document m_doc; 49 50 51 protected Node m_currentNode = null; 52 53 54 public DocumentFragment m_docFrag = null; 55 56 57 protected Stack m_elemStack = new Stack (); 58 59 66 public DOMBuilder(Document doc, Node node) 67 { 68 m_doc = doc; 69 m_currentNode = node; 70 } 71 72 79 public DOMBuilder(Document doc, DocumentFragment docFrag) 80 { 81 m_doc = doc; 82 m_docFrag = docFrag; 83 } 84 85 91 public DOMBuilder(Document doc) 92 { 93 m_doc = doc; 94 } 95 96 102 public Node getRootNode() 103 { 104 return (null != m_docFrag) ? (Node ) m_docFrag : (Node ) m_doc; 105 } 106 107 112 public Node getCurrentNode() 113 { 114 return m_currentNode; 115 } 116 117 122 public java.io.Writer getWriter() 123 { 124 return null; 125 } 126 127 132 protected void append(Node newNode) throws org.xml.sax.SAXException 133 { 134 135 Node currentNode = m_currentNode; 136 137 if (null != currentNode) 138 { 139 currentNode.appendChild(newNode); 140 141 } 143 else if (null != m_docFrag) 144 { 145 m_docFrag.appendChild(newNode); 146 } 147 else 148 { 149 boolean ok = true; 150 short type = newNode.getNodeType(); 151 152 if (type == Node.TEXT_NODE) 153 { 154 String data = newNode.getNodeValue(); 155 156 if ((null != data) && (data.trim().length() > 0)) 157 { 158 throw new org.xml.sax.SAXException ( 159 XMLMessages.createXMLMessage( 160 XMLErrorResources.ER_CANT_OUTPUT_TEXT_BEFORE_DOC, null)); } 162 163 ok = false; 164 } 165 else if (type == Node.ELEMENT_NODE) 166 { 167 if (m_doc.getDocumentElement() != null) 168 { 169 throw new org.xml.sax.SAXException ( 170 XMLMessages.createXMLMessage( 171 XMLErrorResources.ER_CANT_HAVE_MORE_THAN_ONE_ROOT, null)); } 173 } 174 175 if (ok) 176 m_doc.appendChild(newNode); 177 } 178 } 179 180 205 public void setDocumentLocator(Locator locator) 206 { 207 208 } 210 211 218 public void startDocument() throws org.xml.sax.SAXException 219 { 220 221 } 223 224 233 public void endDocument() throws org.xml.sax.SAXException 234 { 235 236 } 238 239 262 public void startElement( 263 String ns, String localName, String name, Attributes atts) 264 throws org.xml.sax.SAXException 265 { 266 267 Element elem; 268 269 if ((null == ns) || (ns.length() == 0)) 272 elem = m_doc.createElementNS(null,name); 273 else 274 elem = m_doc.createElementNS(ns, name); 275 276 append(elem); 277 278 try 279 { 280 int nAtts = atts.getLength(); 281 282 if (0 != nAtts) 283 { 284 for (int i = 0; i < nAtts; i++) 285 { 286 287 if (atts.getType(i).equalsIgnoreCase("ID")) 290 setIDAttribute(atts.getValue(i), elem); 291 292 String attrNS = atts.getURI(i); 293 294 if("".equals(attrNS)) 295 attrNS = null; 297 String attrQName = atts.getQName(i); 301 302 if (attrQName.startsWith("xmlns:")) 304 attrNS = "http://www.w3.org/2000/xmlns/"; 305 306 elem.setAttributeNS(attrNS,attrQName, atts.getValue(i)); 308 } 309 } 310 311 313 m_elemStack.push(elem); 314 315 m_currentNode = elem; 316 317 } 319 catch(java.lang.Exception de) 320 { 321 throw new org.xml.sax.SAXException (de); 323 } 324 325 } 326 327 346 public void endElement(String ns, String localName, String name) 347 throws org.xml.sax.SAXException 348 { 349 m_elemStack.pop(); 350 m_currentNode = m_elemStack.isEmpty() ? null : (Node )m_elemStack.peek(); 351 } 352 353 359 public void setIDAttribute(String id, Element elem) 360 { 361 362 } 364 365
|