1 16 19 package com.sun.org.apache.xml.internal.utils; 20 21 import java.util.Stack ; 22 23 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 24 import com.sun.org.apache.xml.internal.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:") || attrQName.equals("xmlns")) { 305 attrNS = "http://www.w3.org/2000/xmlns/"; 306 } 307 308 elem.setAttributeNS(attrNS,attrQName, atts.getValue(i)); 310 } 311 } 312 313 315 m_elemStack.push(elem); 316 317 m_currentNode = elem; 318 319 } 321 catch(java.lang.Exception de) 322 { 323 throw new org.xml.sax.SAXException (de); 325 } 326 327 } 328 329 348 public void endElement(String ns, String localName, String name) 349 throws org.xml.sax.SAXException 350 { 351 m_elemStack.pop(); 352 m_currentNode = m_elemStack.isEmpty() ? null : (Node )m_elemStack.peek(); 353 } 354 355 361 public void setIDAttribute(String id, Element elem) 362 { 363 364 } 366 367 390 public void characters(char ch[], int start, int length) throws org.xml.sax.SAXException 391 { 392 if(isOutsideDocElem() 393 && com.sun.org.apache.xml.internal.utils.XMLCharacterRecognizer.isWhiteSpace(ch, start, length)) 394 return; 396 if (m_inCData) 397 { 398 cdata(ch, start, length); 399 400 return; 401 } 402 403 String s = new String (ch, start, length); 404 Node childNode; 405 childNode = m_currentNode != null ? m_currentNode.getLastChild(): null; 406 if( childNode != null && childNode.getNodeType() == Node.TEXT_NODE ){ 407 ((Text )childNode).appendData(s); 408 } 409 else{ 410 Text text = m_doc.createTextNode(s); 411 append(text); 412 } 413 } 414 415 425 public void charactersRaw(char ch[], int start, int length) 426 throws org.xml.sax.SAXException 427 { 428 if(isOutsideDocElem() 429 && com.sun.org.apache.xml.internal.utils.XMLCharacterRecognizer.isWhiteSpace(ch, start, length)) 430 return; 432 433 String s = new String (ch, start, length); 434 435 append(m_doc.createProcessingInstruction("xslt-next-is-raw", 436 "formatter-to-dom")); 437 append(m_doc.createTextNode(s)); 438 } 439 440 454 public void startEntity(String name) throws org.xml.sax.SAXException 455 { 456 457 } 460 461 467 public void endEntity(String name) throws org.xml.sax.SAXException {} 468 469 474 public void entityReference(String name) throws org.xml.sax.SAXException 475 { 476 append(m_doc.createEntityReference(name)); 477 } 478 479 501 public void ignorableWhitespace(char ch[], int start, int length) 502 throws org.xml.sax.SAXException 503 { 504 if(isOutsideDocElem()) 505 return; 507 String s = new String (ch, start, length); 508 509 append(m_doc.createTextNode(s)); 510 } 511 512 517 private boolean isOutsideDocElem() 518 { 519 return (null == m_docFrag) && m_elemStack.size() == 0 && (null == m_currentNode || m_currentNode.getNodeType() == Node.DOCUMENT_NODE); 520 } 521 522 537 public void processingInstruction(String target, String data) 538 throws org.xml.sax.SAXException 539 { 540 append(m_doc.createProcessingInstruction(target, data)); 541 } 542 543 554 public void comment(char ch[], int start, int length) throws org.xml.sax.SAXException 555 { 556 append(m_doc.createComment(new String (ch, start, length))); 557 } 558 559 560 protected boolean m_inCData = false; 561 562 567 public void startCDATA() throws org.xml.sax.SAXException 568 { 569 m_inCData = true; 570 append(m_doc.createCDATASection("")); 571 } 572 573 578 public void endCDATA() throws org.xml.sax.SAXException 579 { 580 m_inCData = false; 581 } 582 583 606 public void cdata(char ch[], int start, int length) throws org.xml.sax.SAXException 607 { 608 if(isOutsideDocElem() 609 && com.sun.org.apache.xml.internal.utils.XMLCharacterRecognizer.isWhiteSpace(ch, start, length)) 610 return; 612 String s = new String (ch, start, length); 613 614 CDATASection section =(CDATASection ) m_currentNode.getLastChild(); 615 section.appendData(s); 616 } 617 618 632 public void startDTD(String name, String publicId, String systemId) 633 throws org.xml.sax.SAXException 634 { 635 636 } 638 639 644 public void endDTD() throws org.xml.sax.SAXException 645 { 646 647 } 649 650 678 public void startPrefixMapping(String prefix, String uri) 679 throws org.xml.sax.SAXException 680 { 681 682 704 } 705 706 718 public void endPrefixMapping(String prefix) throws org.xml.sax.SAXException {} 719 720 735 public void skippedEntity(String name) throws org.xml.sax.SAXException {} 736 } 737 | Popular Tags |