1 16 19 20 package com.sun.org.apache.xml.internal.dtm.ref; 21 22 import java.io.IOException ; 23 24 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 25 import com.sun.org.apache.xml.internal.res.XMLMessages; 26 import com.sun.org.apache.xml.internal.utils.ThreadControllerWrapper; 27 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.ContentHandler ; 30 import org.xml.sax.DTDHandler ; 31 import org.xml.sax.ErrorHandler ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.Locator ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.SAXNotRecognizedException ; 36 import org.xml.sax.SAXNotSupportedException ; 37 import org.xml.sax.SAXParseException ; 38 import org.xml.sax.XMLReader ; 39 import org.xml.sax.ext.LexicalHandler ; 40 41 69 final class IncrementalSAXSource_Filter 70 implements IncrementalSAXSource, ContentHandler , DTDHandler , LexicalHandler , ErrorHandler , Runnable 71 { 72 boolean DEBUG=false; 74 private CoroutineManager fCoroutineManager = null; 78 private int fControllerCoroutineID = -1; 79 private int fSourceCoroutineID = -1; 80 81 private ContentHandler clientContentHandler=null; private LexicalHandler clientLexicalHandler=null; private DTDHandler clientDTDHandler=null; private ErrorHandler clientErrorHandler=null; private int eventcounter; 86 private int frequency=5; 87 88 private boolean fNoMoreEvents=false; 92 93 private XMLReader fXMLReader=null; 95 private InputSource fXMLReaderInputSource=null; 96 97 101 public IncrementalSAXSource_Filter() { 102 this.init( new CoroutineManager(), -1, -1); 103 } 104 105 108 public IncrementalSAXSource_Filter(CoroutineManager co, int controllerCoroutineID) 109 { 110 this.init( co, controllerCoroutineID, -1 ); 111 } 112 113 static public IncrementalSAXSource createIncrementalSAXSource(CoroutineManager co, int controllerCoroutineID) { 117 return new IncrementalSAXSource_Filter(co, controllerCoroutineID); 118 } 119 120 124 public void init( CoroutineManager co, int controllerCoroutineID, 125 int sourceCoroutineID) 126 { 127 if(co==null) 128 co = new CoroutineManager(); 129 fCoroutineManager = co; 130 fControllerCoroutineID = co.co_joinCoroutineSet(controllerCoroutineID); 131 fSourceCoroutineID = co.co_joinCoroutineSet(sourceCoroutineID); 132 if (fControllerCoroutineID == -1 || fSourceCoroutineID == -1) 133 throw new RuntimeException (XMLMessages.createXMLMessage(XMLErrorResources.ER_COJOINROUTINESET_FAILED, null)); 135 fNoMoreEvents=false; 136 eventcounter=frequency; 137 } 138 139 144 public void setXMLReader(XMLReader eventsource) 145 { 146 fXMLReader=eventsource; 147 eventsource.setContentHandler(this); 148 eventsource.setDTDHandler(this); 149 eventsource.setErrorHandler(this); 151 try 153 { 154 eventsource. 155 setProperty("http://xml.org/sax/properties/lexical-handler", 156 this); 157 } 158 catch(SAXNotRecognizedException e) 159 { 160 } 162 catch(SAXNotSupportedException e) 163 { 164 } 166 167 } 170 171 public void setContentHandler(ContentHandler handler) 173 { 174 clientContentHandler=handler; 175 } 176 public void setDTDHandler(DTDHandler handler) 178 { 179 clientDTDHandler=handler; 180 } 181 public void setLexicalHandler(LexicalHandler handler) 186 { 187 clientLexicalHandler=handler; 188 } 189 public void setErrHandler(ErrorHandler handler) 192 { 193 clientErrorHandler=handler; 194 } 195 196 public void setReturnFrequency(int events) 199 { 200 if(events<1) events=1; 201 frequency=eventcounter=events; 202 } 203 204 public void characters(char[] ch, int start, int length) 224 throws org.xml.sax.SAXException 225 { 226 if(--eventcounter<=0) 227 { 228 co_yield(true); 229 eventcounter=frequency; 230 } 231 if(clientContentHandler!=null) 232 clientContentHandler.characters(ch,start,length); 233 } 234 public void endDocument() 235 throws org.xml.sax.SAXException 236 { 237 if(clientContentHandler!=null) 239 clientContentHandler.endDocument(); 240 241 eventcounter=0; 242 co_yield(false); 243 } 244 public void endElement(java.lang.String namespaceURI, java.lang.String localName, 245 java.lang.String qName) 246 throws org.xml.sax.SAXException 247 { 248 if(--eventcounter<=0) 249 { 250 co_yield(true); 251 eventcounter=frequency; 252 } 253 if(clientContentHandler!=null) 254 clientContentHandler.endElement(namespaceURI,localName,qName); 255 } 256 public void endPrefixMapping(java.lang.String prefix) 257 throws org.xml.sax.SAXException 258 { 259 if(--eventcounter<=0) 260 { 261 co_yield(true); 262 eventcounter=frequency; 263 } 264 if(clientContentHandler!=null) 265 clientContentHandler.endPrefixMapping(prefix); 266 } 267 public void ignorableWhitespace(char[] ch, int start, int length) 268 throws org.xml.sax.SAXException 269 { 270 if(--eventcounter<=0) 271 { 272 co_yield(true); 273 eventcounter=frequency; 274 } 275 if(clientContentHandler!=null) 276 clientContentHandler.ignorableWhitespace(ch,start,length); 277 } 278 public void processingInstruction(java.lang.String target, java.lang.String data) 279 throws org.xml.sax.SAXException 280 { 281 if(--eventcounter<=0) 282 { 283 co_yield(true); 284 eventcounter=frequency; 285 } 286 if(clientContentHandler!=null) 287 clientContentHandler.processingInstruction(target,data); 288 } 289 public void setDocumentLocator(Locator locator) 290 { 291 if(--eventcounter<=0) 292 { 293 eventcounter=frequency; 296 } 297 if(clientContentHandler!=null) 298 clientContentHandler.setDocumentLocator(locator); 299 } 300 public void skippedEntity(java.lang.String name) 301 throws org.xml.sax.SAXException 302 { 303 if(--eventcounter<=0) 304 { 305 co_yield(true); 306 eventcounter=frequency; 307 } 308 if(clientContentHandler!=null) 309 clientContentHandler.skippedEntity(name); 310 } 311 public void startDocument() 312 throws org.xml.sax.SAXException 313 { 314 co_entry_pause(); 315 316 if(--eventcounter<=0) 318 { 319 co_yield(true); 320 eventcounter=frequency; 321 } 322 if(clientContentHandler!=null) 323 clientContentHandler.startDocument(); 324 } 325 public void startElement(java.lang.String namespaceURI, java.lang.String localName, 326 java.lang.String qName, Attributes atts) 327 throws org.xml.sax.SAXException 328 { 329 if(--eventcounter<=0) 330 { 331 co_yield(true); 332 eventcounter=frequency; 333 } 334 if(clientContentHandler!=null) 335 clientContentHandler.startElement(namespaceURI, localName, qName, atts); 336 } 337 public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) 338 throws org.xml.sax.SAXException 339 { 340 if(--eventcounter<=0) 341 { 342 co_yield(true); 343 eventcounter=frequency; 344 } 345 if(clientContentHandler!=null) 346 clientContentHandler.startPrefixMapping(prefix,uri); 347 } 348 349 public void comment(char[] ch, int start, int length) 360 throws org.xml.sax.SAXException 361 { 362 if(null!=clientLexicalHandler) 363 clientLexicalHandler.comment(ch,start,length); 364 } 365 public void endCDATA() 366 throws org.xml.sax.SAXException 367 { 368 if(null!=clientLexicalHandler) 369 clientLexicalHandler.endCDATA(); 370 } 371 public void endDTD() 372 throws org.xml.sax.SAXException 373 { 374 if(null!=clientLexicalHandler) 375 clientLexicalHandler.endDTD(); 376 } 377 public void endEntity(java.lang.String name) 378 throws org.xml.sax.SAXException 379 { 380 if(null!=clientLexicalHandler) 381 clientLexicalHandler.endEntity(name); 382 } 383 public void startCDATA() 384 throws org.xml.sax.SAXException 385 { 386 if(null!=clientLexicalHandler) 387 clientLexicalHandler.startCDATA(); 388 } 389 public void startDTD(java.lang.String name, java.lang.String publicId, 390 java.lang.String systemId) 391 throws org.xml.sax.SAXException 392 { 393 if(null!=clientLexicalHandler) 394 clientLexicalHandler. startDTD(name, publicId, systemId); 395 } 396 public void startEntity(java.lang.String name) 397 throws org.xml.sax.SAXException 398 { 399 if(null!=clientLexicalHandler) 400 clientLexicalHandler.startEntity(name); 401 } 402 403 406 public void notationDecl(String a, String b, String c) throws SAXException 407 { 408 if(null!=clientDTDHandler) 409 clientDTDHandler.notationDecl(a,b,c); 410 } 411 public void unparsedEntityDecl(String a, String b, String c, String d) throws SAXException 412 { 413 if(null!=clientDTDHandler) 414 clientDTDHandler.unparsedEntityDecl(a,b,c,d); 415 } 416 417 public void error(SAXParseException exception) throws SAXException 433 { 434 if(null!=clientErrorHandler) 435 clientErrorHandler.error(exception); 436 } 437 438 public void fatalError(SAXParseException exception) throws SAXException 439 { 440 if(null!=clientErrorHandler) 443 clientErrorHandler.error(exception); 444 445 eventcounter=0; 446 co_yield(false); 447 448 } 449 450 public void warning(SAXParseException exception) throws SAXException 451 { 452 if(null!=clientErrorHandler) 453 clientErrorHandler.error(exception); 454 } 455 456 457 461 public int getSourceCoroutineID() { 462 return fSourceCoroutineID; 463 } 464 public int getControllerCoroutineID() { 465 return fControllerCoroutineID; 466 } 467 468 473 public CoroutineManager getCoroutineManager() 474 { 475 return fCoroutineManager; 476 } 477 478 489 protected void count_and_yield(boolean moreExpected) throws SAXException 490 { 491 if(!moreExpected) eventcounter=0; 492 493 if(--eventcounter<=0) 494 { 495 co_yield(true); 496 eventcounter=frequency; 497 } 498 } 499 500 507 private void co_entry_pause() throws SAXException 508 { 509 if(fCoroutineManager==null) 510 { 511 init(null,-1,-1); 513 } 514 515 try 516 { 517 Object arg=fCoroutineManager.co_entry_pause(fSourceCoroutineID); 518 if(arg==Boolean.FALSE) 519 co_yield(false); 520 } 521 catch(NoSuchMethodException e) 522 { 523 if(DEBUG) e.printStackTrace(); 526 throw new SAXException (e); 527 } 528 } 529 530 552 private void co_yield(boolean moreRemains) throws SAXException 553 { 554 if(fNoMoreEvents) 556 return; 557 558 try { 560 Object arg=Boolean.FALSE; 561 if(moreRemains) 562 { 563 arg = fCoroutineManager.co_resume(Boolean.TRUE, fSourceCoroutineID, 565 fControllerCoroutineID); 566 567 } 568 569 if(arg==Boolean.FALSE) 571 { 572 fNoMoreEvents=true; 573 574 if(fXMLReader!=null) throw new StopException(); 577 fCoroutineManager.co_exit_to(Boolean.FALSE, fSourceCoroutineID, 579 fControllerCoroutineID); 580 } 581 } 582 catch(NoSuchMethodException e) 583 { 584 fNoMoreEvents=true; 587 fCoroutineManager.co_exit(fSourceCoroutineID); 588 throw new SAXException (e); 589 } 590 } 591 592 596 605 public void startParse(InputSource source) throws SAXException 606 { 607 if(fNoMoreEvents) 608 throw new SAXException (XMLMessages.createXMLMessage(XMLErrorResources.ER_INCRSAXSRCFILTER_NOT_RESTARTABLE, null)); if(fXMLReader==null) 610 throw new SAXException (XMLMessages.createXMLMessage(XMLErrorResources.ER_XMLRDR_NOT_BEFORE_STARTPARSE, null)); 612 fXMLReaderInputSource=source; 613 614 ThreadControllerWrapper.runThread(this, -1); 617 } 618 619 621 public void run() 622 { 623 if(fXMLReader==null) return; 625 626 if(DEBUG)System.out.println("IncrementalSAXSource_Filter parse thread launched"); 627 628 Object arg=Boolean.FALSE; 630 631 try 636 { 637 fXMLReader.parse(fXMLReaderInputSource); 638 } 639 catch(IOException ex) 640 { 641 arg=ex; 642 } 643 catch(StopException ex) 644 { 645 if(DEBUG)System.out.println("Active IncrementalSAXSource_Filter normal stop exception"); 647 } 648 catch (SAXException ex) 649 { 650 Exception inner=ex.getException(); 651 if(inner instanceof StopException){ 652 if(DEBUG)System.out.println("Active IncrementalSAXSource_Filter normal stop exception"); 654 } 655 else 656 { 657 if(DEBUG) 659 { 660 System.out.println("Active IncrementalSAXSource_Filter UNEXPECTED SAX exception: "+inner); 661 inner.printStackTrace(); 662 } 663 arg=ex; 664 } 665 } 667 fXMLReader=null; 669 670 try 671 { 672 fNoMoreEvents=true; 674 fCoroutineManager.co_exit_to(arg, fSourceCoroutineID, 675 fControllerCoroutineID); 676 } 677 catch(java.lang.NoSuchMethodException e) 678 { 679 e.printStackTrace(System.err); 682 fCoroutineManager.co_exit(fSourceCoroutineID); 683 } 684 } 685 686 688 class StopException extends RuntimeException 689 { 690 } 691 692 709 public Object deliverMoreNodes(boolean parsemore) 710 { 711 if(fNoMoreEvents) 713 return Boolean.FALSE; 714 715 try 716 { 717 Object result = 718 fCoroutineManager.co_resume(parsemore?Boolean.TRUE:Boolean.FALSE, 719 fControllerCoroutineID, fSourceCoroutineID); 720 if(result==Boolean.FALSE) 721 fCoroutineManager.co_exit(fControllerCoroutineID); 722 723 return result; 724 } 725 726 catch(NoSuchMethodException e) 730 { 731 return e; 732 } 733 } 734 735 736 740 808 } | Popular Tags |