1 51 52 59 60 package org.ozoneDB.xml.util; 61 62 import java.io.OutputStream ; 63 import java.io.IOException ; 64 import java.io.Serializable ; 65 66 import org.xml.sax.SAXException ; 67 import org.xml.sax.ContentHandler ; 68 import org.xml.sax.ext.LexicalHandler ; 69 import org.xml.sax.Attributes ; 70 import org.xml.sax.Locator ; 71 72 73 75 class CXMLContentHandler implements ContentHandler , LexicalHandler , Serializable { 76 77 public final static boolean debug = false; 78 79 public final static int START_DOCUMENT = 0; 80 public final static int END_DOCUMENT = 1; 81 public final static int START_PREFIX_MAPPING = 2; 82 public final static int END_PREFIX_MAPPING = 3; 83 public final static int START_ELEMENT = 4; 84 public final static int END_ELEMENT = 5; 85 public final static int CHARACTERS = 6; 86 public final static int IGNORABLE_WHITESPACE = 7; 87 public final static int PROCESSING_INSTRUCTION = 8; 88 public final static int COMMENT = 9; 89 public final static int START_CDATA = 10; 90 public final static int END_CDATA = 11; 91 92 private final CompiledXMLOutputStream out; 93 94 95 public CXMLContentHandler( OutputStream stream ) throws IOException { 96 out = new CompiledXMLOutputStream( stream ); 97 } 98 99 100 public void startDocument() throws SAXException { 101 if (debug) { 102 System.out.println(this.getClass().getName() + ": startDocument()"); 103 } 104 try { 105 out.writeEvent( START_DOCUMENT ); 106 } catch (Exception e) { 107 throw new SAXException ( e ); 108 } 109 } 110 111 112 public void endDocument() throws SAXException { 113 if (debug) { 114 System.out.println(this.getClass().getName() + ": endDocument()"); 115 } 116 try { 117 out.writeEvent( END_DOCUMENT ); 118 } catch (Exception e) { 119 throw new SAXException ( e ); 120 } 121 } 122 123 124 public void startPrefixMapping( java.lang.String prefix, java.lang.String uri ) throws SAXException { 125 if (debug) { 126 System.out.println(this.getClass().getName() + ": startPrefixMapping(...)"); 127 } 128 try { 129 out.writeEvent( START_PREFIX_MAPPING ); 130 out.writeString( prefix ); 131 out.writeString( uri ); 132 } catch (Exception e) { 133 throw new SAXException ( e ); 134 } 135 } 136 137 138 public void endPrefixMapping( java.lang.String prefix ) throws SAXException { 139 if (debug) { 140 System.out.println(this.getClass().getName() + ": endPrefixMapping(...)"); 141 } 142 try { 143 out.writeEvent( END_PREFIX_MAPPING ); 144 out.writeString( prefix ); 145 } catch (Exception e) { 146 throw new SAXException ( e ); 147 } 148 } 149 150 151 public void startElement( java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, 152 Attributes atts ) throws SAXException { 153 if (debug) { 154 System.out.println(this.getClass().getName() + ": startElement(...)"); 155 } 156 try { 157 int length = atts.getLength(); 158 out.writeEvent( START_ELEMENT ); 159 out.writeAttributes( length ); 160 for (int i = 0; i < length; i++) { 161 out.writeString( atts.getURI( i ) ); 162 out.writeString( atts.getLocalName( i ) ); 163 out.writeString( atts.getQName( i ) ); 164 out.writeString( atts.getType( i ) ); 165 166 String attValue = atts.getValue( i ); 169 out.writeChars( attValue.toCharArray(), 0, attValue.length() ); 170 171 } 173 out.writeString( namespaceURI ); 174 out.writeString( localName ); 175 out.writeString( qName ); 176 } catch (Exception e) { 177 throw new SAXException ( e ); 178 } 179 } 180 181 182 public void endElement( java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName ) 183 throws SAXException { 184 if (debug) { 185 System.out.println(this.getClass().getName() + ": endElement(...)"); 186 } 187 try { 188 out.writeEvent( END_ELEMENT ); 189 out.writeString( namespaceURI ); 190 out.writeString( localName ); 191 out.writeString( qName ); 192 } catch (Exception e) { 193 throw new SAXException ( e ); 194 } 195 } 196 197 198 public void characters( char[] ch, int start, int length ) throws SAXException { 199 if (debug) { 200 System.out.println(this.getClass().getName() + ": characters(...)"); 201 } 202 try { 203 out.writeEvent( CHARACTERS ); 204 out.writeChars( ch, start, length ); 205 } catch (Exception e) { 206 throw new SAXException ( e ); 207 } 208 } 209 210 211 public void ignorableWhitespace( char[] ch, int start, int length ) throws SAXException { 212 if (debug) { 213 System.out.println(this.getClass().getName() + ": ignorableWhitespace(...)"); 214 } 215 try { 216 out.writeEvent( IGNORABLE_WHITESPACE ); 217 out.writeChars( ch, start, length ); 218 } catch (Exception e) { 219 throw new SAXException ( e ); 220 } 221 } 222 223 224 public void processingInstruction( java.lang.String target, java.lang.String data ) throws SAXException { 225 if (debug) { 226 System.out.println(this.getClass().getName() + ": processingInstruction(...)"); 227 } 228 try { 229 out.writeEvent( PROCESSING_INSTRUCTION ); 230 out.writeString( target ); 231 out.writeString( data ); 232 } catch (Exception e) { 233 throw new SAXException ( e ); 234 } 235 } 236 237 238 public void comment( char[] ch, int start, int length ) throws SAXException { 239 if (debug) { 240 System.out.println(this.getClass().getName() + ": comment(...)"); 241 } 242 try { 243 out.writeEvent(COMMENT); 244 out.writeChars(ch, start, length); 245 } catch (Exception e) { 246 throw new SAXException (e); 247 } 248 } 249 250 251 public void startCDATA() throws SAXException { 252 if (debug) { 253 System.out.println(this.getClass().getName() + ": startCDATA()"); 254 } 255 try { 256 out.writeEvent(START_CDATA); 257 } catch (Exception e) { 258 throw new SAXException (e); 259 } 260 } 261 262 263 public void endCDATA() throws SAXException { 264 if (debug) { 265 System.out.println(this.getClass().getName() + ": endCDATA()"); 266 } 267 try { 268 out.writeEvent(END_CDATA); 269 } catch (Exception e) { 270 throw new SAXException (e); 271 } 272 } 273 274 public void startDTD(String name, String publicId, String systemId) 275 throws SAXException { 276 if (debug) { 277 System.out.println(this.getClass().getName() + ": startDTD()"); 278 } 279 } 282 283 284 public void endDTD() throws SAXException { 285 if (debug) { 286 System.out.println(this.getClass().getName() + ": endDTD()"); 287 } 288 } 291 292 293 public void startEntity(String name) throws SAXException { 294 if (debug) { 295 System.out.println(this.getClass().getName() + ": startEntity()"); 296 } 297 } 300 301 302 public void endEntity(String name) throws SAXException { 303 if (debug) { 304 System.out.println(this.getClass().getName() + ": endEntity()"); 305 } 306 } 309 310 311 public void setDocumentLocator( Locator locator ) { 312 } 314 315 316 public void skippedEntity( java.lang.String name ) throws SAXException { 317 if (debug) { 318 System.out.println(this.getClass().getName() + ": skippedEntity(...)[ignored]"); 319 } 320 } 322 } 323 | Popular Tags |