1 57 package org.enhydra.apache.html.dom; 58 59 60 import java.util.Vector ; 61 62 import org.enhydra.apache.xerces.dom.ElementImpl; 63 import org.enhydra.apache.xerces.dom.ProcessingInstructionImpl; 64 import org.enhydra.apache.xerces.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 205 if ( _current == null ) 206 throw new SAXException ( "HTM007 State error: endElement called with no current node." ); 207 if ( ! _current.getNodeName().equals( tagName ) ) 208 throw new SAXException ( "HTM008 State error: mismatch in closing tag name " + tagName + "\n" + tagName); 209 210 if ( _current.getParentNode() == _current.getOwnerDocument() ) 213 _current = null; 214 else 215 _current = (ElementImpl) _current.getParentNode(); 216 } 217 218 219 public void characters( String text ) 220 throws SAXException 221 { 222 if ( _current == null ) 223 throw new SAXException ( "HTM009 State error: character data found outside of root element." ); 224 _current.appendChild( new TextImpl( _document, text ) ); 225 } 226 227 228 public void characters( char[] text, int start, int length ) 229 throws SAXException 230 { 231 if ( _current == null ) 232 throw new SAXException ( "HTM010 State error: character data found outside of root element." ); 233 _current.appendChild( new TextImpl( _document, new String ( text, start, length ) ) ); 234 } 235 236 237 public void ignorableWhitespace( char[] text, int start, int length ) 238 throws SAXException 239 { 240 Node node; 241 242 if ( ! _ignoreWhitespace ) 243 _current.appendChild( new TextImpl( _document, new String ( text, start, length ) ) ); 244 } 245 246 247 public void processingInstruction( String target, String instruction ) 248 throws SAXException 249 { 250 Node node; 251 252 if ( _current == null && _document == null ) 255 { 256 if ( _preRootNodes == null ) 257 _preRootNodes = new Vector (); 258 _preRootNodes.addElement( new ProcessingInstructionImpl( null, target, instruction ) ); 259 } 260 else 261 if ( _current == null && _document != null ) 262 _document.appendChild( new ProcessingInstructionImpl( _document, target, instruction ) ); 263 else 264 _current.appendChild( new ProcessingInstructionImpl( _document, target, instruction ) ); 265 } 266 267 268 public HTMLDocument getHTMLDocument() 269 { 270 return (HTMLDocument) _document; 271 } 272 273 274 public void setDocumentLocator( Locator locator ) 275 { 276 _locator = locator; 277 } 278 279 280 } 281 | Popular Tags |