1 57 package com.sun.org.apache.html.internal.dom; 58 59 60 import java.util.Vector ; 61 62 import com.sun.org.apache.xerces.internal.dom.ElementImpl; 63 import com.sun.org.apache.xerces.internal.dom.ProcessingInstructionImpl; 64 import com.sun.org.apache.xerces.internal.dom.TextImpl; 65 import org.w3c.dom.Node ; 66 import org.w3c.dom.html.HTMLDocument; 67 import org.xml.sax.AttributeList ; 68 import org.xml.sax.DocumentHandler ; 69 import org.xml.sax.Locator ; 70 import org.xml.sax.SAXException ; 71 72 73 82 public class HTMLBuilder 83 implements DocumentHandler 84 { 85 86 87 90 protected HTMLDocumentImpl _document; 91 92 93 98 protected ElementImpl _current; 99 100 105 private Locator _locator; 106 107 108 112 private boolean _ignoreWhitespace = true; 113 114 115 119 private boolean _done = true; 120 121 122 127 protected Vector _preRootNodes; 128 129 130 public void startDocument() 131 throws SAXException 132 { 133 if ( ! _done ) 134 throw new SAXException ( "HTM001 State error: startDocument fired twice on one builder." ); 135 _document = null; 136 _done = false; 137 } 138 139 140 public void endDocument() 141 throws SAXException 142 { 143 if ( _document == null ) 144 throw new SAXException ( "HTM002 State error: document never started or missing document element." ); 145 if ( _current != null ) 146 throw new SAXException ( "HTM003 State error: document ended before end of document element." ); 147 _current = null; 148 _done = true; 149 } 150 151 152 public synchronized void startElement( String tagName, AttributeList attrList ) 153 throws SAXException 154 { 155 ElementImpl elem; 156 int i; 157 158 if ( tagName == null ) 159 throw new SAXException ( "HTM004 Argument 'tagName' is null." ); 160 161 if ( _document == null ) 164 { 165 _document = new HTMLDocumentImpl(); 167 elem = (ElementImpl) _document.getDocumentElement(); 168 _current = elem; 169 if ( _current == null ) 170 throw new SAXException ( "HTM005 State error: Document.getDocumentElement returns null." ); 171 172 if ( _preRootNodes != null ) 174 { 175 for ( i = _preRootNodes.size() ; i-- > 0 ; ) 176 _document.insertBefore( (Node ) _preRootNodes.elementAt( i ), elem ); 177 _preRootNodes = null; 178 } 179 180 } 181 else 182 { 183 if ( _current == null ) 186 throw new SAXException ( "HTM006 State error: startElement called after end of document element." ); 187 elem = (ElementImpl) _document.createElement( tagName ); 188 _current.appendChild( elem ); 189 _current = elem; 190 } 191 192 if ( attrList != null ) 194 { 195 for ( i = 0 ; i < attrList.getLength() ; ++ i ) 196 elem.setAttribute( attrList.getName( i ), attrList.getValue( i ) ); 197 } 198 } 199 200 201 public void endElement( String tagName ) 202 throws SAXException 203 { 204 if ( _current == null ) 205 throw new SAXException ( "HTM007 State error: endElement called with no current node." ); 206 if ( ! _current.getNodeName().equalsIgnoreCase( tagName )) 207 throw new SAXException ( "HTM008 State error: mismatch in closing tag name " + tagName + "\n" + tagName); 208 209 if ( _current.getParentNode() == _current.getOwnerDocument() ) 212 _current = null; 213 else 214 _current = (ElementImpl) _current.getParentNode(); 215 } 216 217 218 public void characters( String text ) 219 throws SAXException 220 { 221 if ( _current == null ) 222 throw new SAXException ( "HTM009 State error: character data found outside of root element." ); 223 _current.appendChild( new TextImpl( _document, text ) ); 224 } 225 226 227 public void characters( char[] text, int start, int length ) 228 throws SAXException 229 { 230 if ( _current == null ) 231 throw new SAXException ( "HTM010 State error: character data found outside of root element." ); 232 _current.appendChild( new TextImpl( _document, new String ( text, start, length ) ) ); 233 } 234 235 236 public void ignorableWhitespace( char[] text, int start, int length ) 237 throws SAXException 238 { 239 Node node; 240 241 if ( ! _ignoreWhitespace ) 242 _current.appendChild( new TextImpl( _document, new String ( text, start, length ) ) ); 243 } 244 245 246 public void processingInstruction( String target, String instruction ) 247 throws SAXException 248 { 249 Node node; 250 251 if ( _current == null && _document == null ) 254 { 255 if ( _preRootNodes == null ) 256 _preRootNodes = new Vector (); 257 _preRootNodes.addElement( new ProcessingInstructionImpl( null, target, instruction ) ); 258 } 259 else 260 if ( _current == null && _document != null ) 261 _document.appendChild( new ProcessingInstructionImpl( _document, target, instruction ) ); 262 else 263 _current.appendChild( new ProcessingInstructionImpl( _document, target, instruction ) ); 264 } 265 266 267 public HTMLDocument getHTMLDocument() 268 { 269 return (HTMLDocument) _document; 270 } 271 272 273 public void setDocumentLocator( Locator locator ) 274 { 275 _locator = locator; 276 } 277 278 279 } 280 | Popular Tags |