1 16 package org.apache.html.dom; 17 18 19 import java.util.Vector ; 20 21 import org.apache.xerces.dom.ElementImpl; 22 import org.apache.xerces.dom.ProcessingInstructionImpl; 23 import org.apache.xerces.dom.TextImpl; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.html.HTMLDocument; 26 import org.xml.sax.AttributeList ; 27 import org.xml.sax.DocumentHandler ; 28 import org.xml.sax.Locator ; 29 import org.xml.sax.SAXException ; 30 31 32 41 public class HTMLBuilder 42 implements DocumentHandler 43 { 44 45 46 49 protected HTMLDocumentImpl _document; 50 51 52 57 protected ElementImpl _current; 58 59 64 private Locator _locator; 65 66 67 71 private boolean _ignoreWhitespace = true; 72 73 74 78 private boolean _done = true; 79 80 81 86 protected Vector _preRootNodes; 87 88 89 public void startDocument() 90 throws SAXException 91 { 92 if ( ! _done ) 93 throw new SAXException ( "HTM001 State error: startDocument fired twice on one builder." ); 94 _document = null; 95 _done = false; 96 } 97 98 99 public void endDocument() 100 throws SAXException 101 { 102 if ( _document == null ) 103 throw new SAXException ( "HTM002 State error: document never started or missing document element." ); 104 if ( _current != null ) 105 throw new SAXException ( "HTM003 State error: document ended before end of document element." ); 106 _current = null; 107 _done = true; 108 } 109 110 111 public synchronized void startElement( String tagName, AttributeList attrList ) 112 throws SAXException 113 { 114 ElementImpl elem; 115 int i; 116 117 if ( tagName == null ) 118 throw new SAXException ( "HTM004 Argument 'tagName' is null." ); 119 120 if ( _document == null ) 123 { 124 _document = new HTMLDocumentImpl(); 126 elem = (ElementImpl) _document.getDocumentElement(); 127 _current = elem; 128 if ( _current == null ) 129 throw new SAXException ( "HTM005 State error: Document.getDocumentElement returns null." ); 130 131 if ( _preRootNodes != null ) 133 { 134 for ( i = _preRootNodes.size() ; i-- > 0 ; ) 135 _document.insertBefore( (Node ) _preRootNodes.elementAt( i ), elem ); 136 _preRootNodes = null; 137 } 138 139 } 140 else 141 { 142 if ( _current == null ) 145 throw new SAXException ( "HTM006 State error: startElement called after end of document element." ); 146 elem = (ElementImpl) _document.createElement( tagName ); 147 _current.appendChild( elem ); 148 _current = elem; 149 } 150 151 if ( attrList != null ) 153 { 154 for ( i = 0 ; i < attrList.getLength() ; ++ i ) 155 elem.setAttribute( attrList.getName( i ), attrList.getValue( i ) ); 156 } 157 } 158 159 160 public void endElement( String tagName ) 161 throws SAXException 162 { 163 if ( _current == null ) 164 throw new SAXException ( "HTM007 State error: endElement called with no current node." ); 165 if ( ! _current.getNodeName().equalsIgnoreCase( tagName )) 166 throw new SAXException ( "HTM008 State error: mismatch in closing tag name " + tagName + "\n" + tagName); 167 168 if ( _current.getParentNode() == _current.getOwnerDocument() ) 171 _current = null; 172 else 173 _current = (ElementImpl) _current.getParentNode(); 174 } 175 176 177 public void characters( String text ) 178 throws SAXException 179 { 180 if ( _current == null ) 181 throw new SAXException ( "HTM009 State error: character data found outside of root element." ); 182 _current.appendChild( new TextImpl( _document, text ) ); 183 } 184 185 186 public void characters( char[] text, int start, int length ) 187 throws SAXException 188 { 189 if ( _current == null ) 190 throw new SAXException ( "HTM010 State error: character data found outside of root element." ); 191 _current.appendChild( new TextImpl( _document, new String ( text, start, length ) ) ); 192 } 193 194 195 public void ignorableWhitespace( char[] text, int start, int length ) 196 throws SAXException 197 { 198 Node node; 199 200 if ( ! _ignoreWhitespace ) 201 _current.appendChild( new TextImpl( _document, new String ( text, start, length ) ) ); 202 } 203 204 205 public void processingInstruction( String target, String instruction ) 206 throws SAXException 207 { 208 Node node; 209 210 if ( _current == null && _document == null ) 213 { 214 if ( _preRootNodes == null ) 215 _preRootNodes = new Vector (); 216 _preRootNodes.addElement( new ProcessingInstructionImpl( null, target, instruction ) ); 217 } 218 else 219 if ( _current == null && _document != null ) 220 _document.appendChild( new ProcessingInstructionImpl( _document, target, instruction ) ); 221 else 222 _current.appendChild( new ProcessingInstructionImpl( _document, target, instruction ) ); 223 } 224 225 226 public HTMLDocument getHTMLDocument() 227 { 228 return (HTMLDocument) _document; 229 } 230 231 232 public void setDocumentLocator( Locator locator ) 233 { 234 _locator = locator; 235 } 236 237 238 } 239 | Popular Tags |