1 package org.mr.core.util.xml.sax; 2 3 import org.xml.sax.Attributes ; 4 import org.xml.sax.ContentHandler ; 5 import org.xml.sax.Locator ; 6 import org.xml.sax.SAXException ; 7 import org.xml.sax.ext.LexicalHandler ; 8 import org.xml.sax.helpers.AttributesImpl ; 9 10 import java.io.Serializable ; 11 import java.util.Iterator ; 12 import java.util.LinkedList ; 13 14 59 60 67 public class QueueContentHandler implements ContentHandler , LexicalHandler , Serializable { 68 69 private LinkedList m_q = new LinkedList (); 70 71 74 public void fireInto(ContentHandler i_handler) 75 throws SAXException { 76 Iterator events = m_q.iterator(); 77 while (events.hasNext()) { 78 SaxEvent event = (SaxEvent) events.next(); 79 event.fire(i_handler); 80 81 } 82 } 83 84 public int getSize(){ 85 return m_q.size(); 86 } 87 88 91 public void fireInto(QueueContentHandler i_handler) { 92 i_handler.m_q.addAll(m_q); 93 } 94 95 98 public void clear() { 99 m_q.clear(); 100 } 101 102 public final boolean hasEvents() { 103 return m_q.size() > 0; 104 } 105 106 112 public void setDocumentLocator(Locator i_locator) { 113 m_q.add(new SetDocumentLocator(i_locator)); 114 } 115 116 public void startDocument() throws SAXException { 117 m_q.add(new StartDocument()); 118 } 119 120 public void endDocument() throws SAXException { 121 m_q.add(new EndDocument()); 122 } 123 124 public void startPrefixMapping(String i_prefix, String i_uri) throws SAXException { 125 m_q.add(new StartPrefixMapping(i_prefix, i_uri)); 126 } 127 128 public void endPrefixMapping(String i_prefix) throws SAXException { 129 m_q.add(new EndPrefixMapping(i_prefix)); 130 } 131 132 public void startElement(String i_namespaceURI, String i_localName, String i_qName, Attributes i_atts) throws SAXException { 133 m_q.add(new StartElement(i_namespaceURI, i_localName, i_qName, i_atts)); 134 } 135 136 public void endElement(String i_namespaceURI, String i_localName, String i_qName) throws SAXException { 137 m_q.add(new EndElement(i_namespaceURI, i_localName, i_qName)); 138 } 139 140 public void ignorableWhitespace(char[] i_chars, int i_start, int i_length) throws SAXException { 141 m_q.add(new IgnorableWhitespace(i_chars, i_start, i_length)); 142 } 143 144 public void processingInstruction(String i_target, String i_data) throws SAXException { 145 m_q.add(new ProcessingInstruction(i_target, i_data)); 146 } 147 148 public void skippedEntity(String i_name) throws SAXException { 149 m_q.add(new SkippedEntity(i_name)); 150 } 151 152 public void characters(char[] i_chars, int i_start, int i_length) throws SAXException { 153 m_q.add(new Characters(i_chars, i_start, i_length)); 154 } 155 156 public void startDTD(String s, String s1, String s2) throws SAXException { 162 m_q.add(new StartDTD(s, s1, s2)); 163 } 164 165 public void endDTD() throws SAXException { 166 m_q.add(new EndDTD()); 167 } 168 169 public void startEntity(String s) throws SAXException { 170 m_q.add(new StartEntity(s)); 171 } 172 173 public void endEntity(String s) throws SAXException { 174 m_q.add(new EndEntity(s)); 175 } 176 177 public void startCDATA() throws SAXException { 178 m_q.add(new StartCDATA()); 179 } 180 181 public void endCDATA() throws SAXException { 182 m_q.add(new EndCDATA()); 183 } 184 185 public void comment(char[] i_chars, int i, int i1) throws SAXException { 186 m_q.add(new Comment(i_chars, i, i1)); 187 } 188 189 195 198 private static interface SaxEvent extends Serializable { 199 200 205 public void fire(ContentHandler i_handler) 206 throws SAXException ; 207 } 208 209 protected static abstract class SaxLexicalEvent implements SaxEvent { 210 public void fire(ContentHandler i_handler) 211 throws SAXException { 212 if (i_handler instanceof LexicalHandler ) { 213 fire((LexicalHandler )i_handler); 214 } 215 } 216 217 public abstract void fire(LexicalHandler i_handler) throws SAXException ; 218 } 219 220 public static class SetDocumentLocator implements SaxEvent { 221 private Locator m_docLocator; 222 223 public SetDocumentLocator(Locator i_docLocator) { 224 m_docLocator = i_docLocator; 225 } 226 227 public void fire(ContentHandler i_handler) throws SAXException { 228 i_handler.setDocumentLocator(m_docLocator); 229 } 230 } 231 232 public static class StartDocument implements SaxEvent { 233 public void fire(ContentHandler i_handler) throws SAXException { 234 i_handler.startDocument(); 235 } 236 } 237 238 public static class EndDocument implements SaxEvent { 239 public void fire(ContentHandler i_handler) throws SAXException { 240 i_handler.endDocument(); 241 } 242 } 243 244 public static class StartPrefixMapping implements SaxEvent { 245 private String m_prefix; 246 private String m_uri; 247 248 public StartPrefixMapping(String i_prefix, String i_uri) { 249 m_prefix = i_prefix; 250 m_uri = i_uri; 251 } 252 253 public void fire(ContentHandler i_handler) throws SAXException { 254 i_handler.startPrefixMapping(m_prefix, m_uri); 255 } 256 } 257 258 public static class EndPrefixMapping implements SaxEvent { 259 private String m_prefix; 260 261 public EndPrefixMapping(String i_prefix) { 262 m_prefix = i_prefix; 263 } 264 265 public void fire(ContentHandler i_handler) throws SAXException { 266 i_handler.endPrefixMapping(m_prefix); 267 } 268 } 269 270 public static class StartElement implements SaxEvent { 271 private String m_namespaceURI; 272 private String m_localName; 273 private String m_qName; 274 private Attributes m_atts; 275 276 public StartElement(String i_namespaceURI, String i_localName, String i_qName, Attributes i_atts) { 277 m_namespaceURI = i_namespaceURI; 278 m_localName = i_localName; 279 m_qName = i_qName; 280 m_atts = new AttributesImpl (i_atts); 281 } 282 283 public void fire(ContentHandler i_handler) throws SAXException { 284 i_handler.startElement(m_namespaceURI, m_localName, m_qName, m_atts); 285 } 286 } 287 288 public static class EndElement implements SaxEvent { 289 private String m_namespaceURI; 290 private String m_localName; 291 private String m_qName; 292 293 public EndElement(String i_namespaceURI, String i_localName, String i_qName) { 294 m_namespaceURI = i_namespaceURI; 295 m_localName = i_localName; 296 m_qName = i_qName; 297 } 298 299 public void fire(ContentHandler i_handler) throws SAXException { 300 i_handler.endElement(m_namespaceURI, m_localName, m_qName); 301 } 302 } 303 304 public static class IgnorableWhitespace implements SaxEvent { 305 private String m_chars; 306 307 public IgnorableWhitespace(char[] i_chars, int i_start, int i_length) { 308 m_chars = new String (i_chars, i_start, i_length); 309 } 310 311 public void fire(ContentHandler i_handler) 312 throws SAXException { 313 i_handler.ignorableWhitespace(m_chars.toCharArray(), 0, m_chars.length()); 314 } 315 } 316 317 public static class ProcessingInstruction implements SaxEvent { 318 private String m_target; 319 private String m_data; 320 321 public ProcessingInstruction(String i_target, String i_data) { 322 m_target = i_target; 323 m_data = i_data; 324 } 325 326 public void fire(ContentHandler i_handler) throws SAXException { 327 i_handler.processingInstruction(m_target, m_data); 328 } 329 } 330 331 public static class SkippedEntity implements SaxEvent { 332 private String m_name; 333 334 public SkippedEntity(String i_name) { 335 m_name = i_name; 336 } 337 338 public void fire(ContentHandler i_handler) throws SAXException { 339 i_handler.skippedEntity(m_name); 340 } 341 } 342 343 public static class Characters implements SaxEvent { 344 private String m_chars; 345 346 public Characters(char[] i_chars, int i_start, int i_length) { 347 m_chars = new String (i_chars, i_start, i_length); 348 } 349 350 public void fire(ContentHandler i_handler) 351 throws SAXException { 352 i_handler.characters(m_chars.toCharArray(), 0, m_chars.length()); 353 } 354 } 355 356 public static class Comment extends SaxLexicalEvent { 357 private String m_chars; 358 359 public Comment(char[] i_chars, int i_start, int i_length) { 360 m_chars = new String (i_chars, i_start, i_length); 361 } 362 363 public void fire(LexicalHandler i_handler) 364 throws SAXException { 365 i_handler.comment(m_chars.toCharArray(), 0, m_chars.length()); 366 } 367 } 368 369 public static class StartDTD extends SaxLexicalEvent { 370 private String m_name; 371 private String m_publicId; 372 private String m_systemId; 373 374 public StartDTD(String i_name, String i_publicId, String i_systemId) { 375 m_name = i_name; 376 m_publicId = i_publicId; 377 m_systemId = i_systemId; 378 } 379 380 public void fire(LexicalHandler i_handler) throws SAXException { 381 i_handler.startDTD(m_name, m_publicId, m_systemId); 382 } 383 } 384 385 public static class EndDTD extends SaxLexicalEvent { 386 public void fire(LexicalHandler i_handler) throws SAXException { 387 i_handler.endDTD(); 388 } 389 } 390 391 public static class StartCDATA extends SaxLexicalEvent { 392 public void fire(LexicalHandler i_handler) throws SAXException { 393 i_handler.startCDATA(); 394 } 395 } 396 397 public static class EndCDATA extends SaxLexicalEvent { 398 public void fire(LexicalHandler i_handler) throws SAXException { 399 i_handler.endCDATA(); 400 } 401 } 402 403 public static class StartEntity extends SaxLexicalEvent { 404 private String m_name; 405 406 public StartEntity(String i_name) { 407 m_name = i_name; 408 } 409 410 public void fire(LexicalHandler i_handler) throws SAXException { 411 i_handler.startEntity(m_name); 412 } 413 } 414 415 public static class EndEntity extends SaxLexicalEvent { 416 private String m_name; 417 418 public EndEntity(String i_name) { 419 m_name = i_name; 420 } 421 422 public void fire(LexicalHandler i_handler) throws SAXException { 423 i_handler.endEntity(m_name); 424 } 425 } 426 427 } 428 | Popular Tags |