You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by jk...@apache.org on 2001/05/16 23:57:31 UTC

cvs commit: xml-xalan/java/src/org/apache/xml/dtm CoroutineParser.java CoroutineSAXParser_Xerces.java

jkesselm    01/05/16 14:57:30

  Modified:    java/src/org/apache/xml/dtm Tag: DTM_EXP
                        CoroutineParser.java CoroutineSAXParser_Xerces.java
  Log:
  The Xerces-specific CoroutineParser now avoids the need for a
  second thread, by taking full advantage of CoroutineParser's
  encapsulation and Xerces' incremental parsing mode.
  
  Note: I'm using IncrementalXerces to expose one method which
  is not currently in the public Xerces API, and which I've been
  told by a Reliable Source (hi, Glenn!) really does have to be
  invoked in order to run the parser in this mode. Presumably
  future versions of Xerces will either make this officially available
  or make calling it unnecessary.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.8   +5 -28     xml-xalan/java/src/org/apache/xml/dtm/Attic/CoroutineParser.java
  
  Index: CoroutineParser.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xml/dtm/Attic/CoroutineParser.java,v
  retrieving revision 1.1.2.7
  retrieving revision 1.1.2.8
  diff -u -r1.1.2.7 -r1.1.2.8
  --- CoroutineParser.java	2001/05/14 19:08:58	1.1.2.7
  +++ CoroutineParser.java	2001/05/16 21:57:27	1.1.2.8
  @@ -82,7 +82,7 @@
    *
    * <p>Status: In progress</p>
    * */
  -public interface CoroutineParser extends Runnable {
  +public interface CoroutineParser {
   
       /** @return the coroutine ID number for this CoroutineParser object.
        * Note that this isn't useful unless you know which CoroutineManager
  @@ -110,33 +110,10 @@
      */
     public void setLexHandler(org.xml.sax.ext.LexicalHandler handler);
   
  -
  -    /**
  -     * This coroutine (thread) can be resumed with the following arguments:
  -     *
  -     *      null            terminate this coroutine.
  -     *                      exits with:
  -     *                          co_exit_to(null, ...)
  -     *                      expects next:
  -     *                          nothing, we have terminated the thread.
  -     *
  -     *      InputSource     setup to read from this source.
  -     *                      resumes with:
  -     *                          co_resume(Boolean.TRUE, ...) on success.
  -     *                          co_resume(Boolean.FALSE, ...) on failure. (not sure why?)
  -     *                          co_resume(Exception, ...) on error. (what I expect for failure)
  -     *
  -     *      Boolean.TRUE    indication to continue parsing the current document.
  -     *                      resumes with:
  -     *                          co_resume(Boolean.TRUE, ...) on success with more to parse.
  -     *                          co_resume(Boolean.FALSE, ...) on success when finished.
  -     *                          co_resume(Exception, ...) on error.
  -     *
  -     *      Boolean.FALSE   indication to discontinue parsing and reset.
  -     *                      resumes with:
  -     *                          co_resume(Boolean.FALSE, ...) always.
  -     */
  -    public void run();
  +  /* The run() method is required in CoroutineParsers that run as
  +   * threads (of course)... but it isn't part of our API, and
  +   * shouldn't be declared here.
  +   * */
   
     //================================================================
     /** doParse() is a simple API which tells the coroutine parser
  
  
  
  1.1.2.7   +74 -170   xml-xalan/java/src/org/apache/xml/dtm/Attic/CoroutineSAXParser_Xerces.java
  
  Index: CoroutineSAXParser_Xerces.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xml/dtm/Attic/CoroutineSAXParser_Xerces.java,v
  retrieving revision 1.1.2.6
  retrieving revision 1.1.2.7
  diff -u -r1.1.2.6 -r1.1.2.7
  --- CoroutineSAXParser_Xerces.java	2001/05/16 05:33:19	1.1.2.6
  +++ CoroutineSAXParser_Xerces.java	2001/05/16 21:57:28	1.1.2.7
  @@ -61,29 +61,38 @@
   import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
   import java.io.IOException;
  -import org.apache.xml.dtm.CoroutineManager;
  +//import org.apache.xml.dtm.CoroutineManager;
   
  -/** <p>CoroutineSAXParser_Xerces illustrates how to run Xerces's incremental
  - * parsing feature (parseSome()) in a coroutine. Output from Xerces
  - * will still be issued via callbacks, which will need to be recieved
  - * and acted upon by an appopriate handler.</p>
  +/** <p>CoroutineSAXParser_Xerces takes advantage of the fact that Xerces
  + * incremental mode is already a coroutine of sorts, and just wraps our
  + * CoroutineParser API around it.</p>
    *
  + * NOTE that this requires access to a few features which are not yet
  + * part of the public Xerces API. Glenn solved that by making this class
  + * a subclass of xerces.parsers.SAXParser. A better solution may be to
  + * make a separate subclass thereof which exposes the relevant methods as
  + * public -- both because it provides more explicit control over what
  + * is and isn't exposed, and because it holds out the hope of dropping that
  + * subclass and plugging in a normal instance of Xerces if/when these methods
  + * become official API calls.
  + *
    * <p>Usage example: See main().</p>
    *
  - * <p>Status: Passes simple main() unit-test</p>
  + * <p>Status: Passes simple main() unit-test. NEEDS JAVADOC.</p>
    * */
   public class CoroutineSAXParser_Xerces
  -extends org.apache.xerces.parsers.SAXParser
  -implements CoroutineParser, Runnable
  +// extends org.apache.xerces.parsers.SAXParser
  +implements CoroutineParser
   {
  +  IncrementalXercesSaxParser incrementalParser;
   
       //
       // Data
       //
   
  -    private CoroutineManager fCoroutineManager = null;
  -    private int fAppCoroutineID = -1;
  -    private int fParserCoroutineID = -1;
  +  //private CoroutineManager fCoroutineManager = null;
  +  //private int fAppCoroutineID = -1;
  +  //private int fParserCoroutineID = -1;
       private boolean fParseInProgress=false;
   
       //
  @@ -92,16 +101,14 @@
   
       public CoroutineSAXParser_Xerces(CoroutineManager co, int appCoroutineID) {
   
  -        initHandlers(true, this, this);
  +        incrementalParser=new IncrementalXercesSaxParser();
   
  -        fCoroutineManager = co;
  -        fAppCoroutineID = appCoroutineID;
  -        fParserCoroutineID = co.co_joinCoroutineSet(-1);
  -        if (fParserCoroutineID == -1)
  -            throw new RuntimeException("co_joinCoroutineSet() failed");
  -        Thread t = new Thread(this);
  -        t.setDaemon(false);
  -        t.start();
  +        incrementalParser.initHandlers(true, incrementalParser, incrementalParser);
  +	// fCoroutineManager = co;
  +	// fAppCoroutineID = appCoroutineID;
  +	// fParserCoroutineID = co.co_joinCoroutineSet(-1);
  +	// if (fParserCoroutineID == -1)
  +	//   throw new RuntimeException("co_joinCoroutineSet() failed");
       }
   
       //
  @@ -118,7 +125,8 @@
       // coroutine support
   
       public int getParserCoroutineID() {
  -        return fParserCoroutineID;
  +      // return fParserCoroutineID;
  +      return -1;
       }
   
     /** @return the CoroutineManager this CoroutineParser object is bound to.
  @@ -128,9 +136,16 @@
      * */
     public CoroutineManager getCoroutineManager()
     {
  -    return fCoroutineManager;
  +    // return fCoroutineManager;
  +    return null;
     }
   
  +  // Register handler directly with the incremental parser
  +  public void setContentHandler(org.xml.sax.ContentHandler handler)
  +  {
  +    incrementalParser.setContentHandler(handler);
  +  }
  +
     // Note name, needed to dodge the inherited Xerces setLexicalHandler
     // which isn't public.
     public void setLexHandler(org.xml.sax.ext.LexicalHandler handler)
  @@ -138,8 +153,8 @@
       // Not supported by all SAX2 parsers but should work in Xerces:
       try 
         {
  -        setProperty("http://xml.org/sax/properties/lexical-handler",
  -                    this);
  +        incrementalParser.setProperty("http://xml.org/sax/properties/lexical-handler",
  +                    handler);
         }
       catch(org.xml.sax.SAXNotRecognizedException e)
         {
  @@ -151,88 +166,6 @@
         }
     }
     
  -
  -
  -    /**
  -     * This coroutine (thread) can be resumed with the following arguments:
  -     *
  -     *      null            terminate this coroutine.
  -     *                      exits with:
  -     *                          co_exit_to(null, ...)
  -     *                      expects next:
  -     *                          nothing, we have terminated the thread.
  -     *
  -     *      InputSource     setup to read from this source.
  -     *                      resumes with:
  -     *                          co_resume(Boolean.TRUE, ...) on success.
  -     *                          co_resume(Boolean.FALSE, ...) on failure. (not sure why?)
  -     *                          co_resume(Exception, ...) on error. (what I expect for failure)
  -     *
  -     *      Boolean.TRUE    indication to continue parsing the current document.
  -     *                      resumes with:
  -     *                          co_resume(Boolean.TRUE, ...) on success with more to parse.
  -     *                          co_resume(Boolean.FALSE, ...) on success when finished.
  -     *                          co_resume(Exception, ...) on error.
  -     *
  -     *      Boolean.FALSE   indication to discontinue parsing and reset.
  -     *                      resumes with:
  -     *                          co_resume(Boolean.FALSE, ...) always.
  -     */
  -    public void run() {
  -      try {
  -          
  -        Object arg = null;
  -        while (true) {
  -            arg = fCoroutineManager.co_resume(arg, fParserCoroutineID, fAppCoroutineID);
  -            if (arg == null) {
  -                fCoroutineManager.co_exit_to(arg, fParserCoroutineID, fAppCoroutineID);
  -                break;
  -            }
  -            if (arg instanceof InputSource) {
  -                if (fParseInProgress) {
  -                    arg = new SAXException("parse may not be called while parsing.");
  -                }
  -                else {
  -                    try {
  -                        boolean ok = parseSomeSetup((InputSource)arg);
  -                        arg = ok ? Boolean.TRUE : Boolean.FALSE;
  -                    }
  -                    catch (Exception ex) {
  -                        arg = ex;
  -                    }
  -                }
  -            }
  -            else if (arg instanceof Boolean) {
  -                boolean keepgoing = ((Boolean)arg).booleanValue();
  -                if (!keepgoing) {
  -                    fParseInProgress = false;
  -                    arg = Boolean.FALSE;
  -                }
  -                else {
  -                    try {
  -                        keepgoing = parseSome();
  -                        arg = keepgoing ? Boolean.TRUE : Boolean.FALSE;
  -                    } catch (SAXException ex) {
  -                        arg = ex;
  -                    } catch (IOException ex) {
  -                        arg = ex;
  -                    } catch (Exception ex) {
  -                        arg = new SAXException(ex);
  -                    }
  -                }
  -            }
  -        }
  -        
  -      }
  -      catch(java.lang.NoSuchMethodException e)
  -        {
  -          // Shouldn't happen unless we've miscoded our coroutine logic
  -          // "Shut down the garbage smashers on the detention level!"
  -          e.printStackTrace(System.err);
  -          fCoroutineManager.co_exit(fParserCoroutineID);
  -        }
  -    }
  -
     //================================================================
     /** doParse() is a simple API which tells the coroutine parser
      * to begin reading from a file.  This is intended to be called from one
  @@ -243,9 +176,6 @@
      * %REVIEW% Can/should this unify with doMore? (if URI hasn't changed,
      * parse more from same file, else end and restart parsing...?
      *
  -   * %REVIEW% With this encapsulation, and Xerces incremental parsing,
  -   * we may be able to encapsulate entirely and avoid the second thread.
  -   *
      * @param source The InputSource to parse from.
      * @param appCoroutineID The coroutine ID number of the coroutine invoking
      * this method, so it can be resumed after the parser has responded to the
  @@ -256,31 +186,23 @@
      * */
     public Object doParse(InputSource source, int appCoroutineID)
     {
  -    try 
  -      {
  -        Object result=    
  -          fCoroutineManager.co_resume(source, appCoroutineID, fParserCoroutineID);
  -        
  -        // %REVIEW% Better error reporting needed... though most of these
  -        // should never arise during normal operation.
  -        // Should this rethrow the parse exception?
  -        if (result instanceof Exception) {
  -          System.out.println("\nParser threw exception:");
  -          ((Exception)result).printStackTrace();
  -        }
  -
  -        return result;
  -      }
  -
  -    // SHOULD NEVER OCCUR, since the coroutine number and coroutine manager
  -    // are those previously established for this CoroutineSAXParser...
  -    // So I'm just going to return it as a parsing exception, for now.
  -    catch(NoSuchMethodException e)
  -      {
  -        return e;
  -      }
  +    if (fParseInProgress) {
  +      // %review% -- We never set this flag in the previous version of
  +      // this class, we still don't set it here... Discard, or fix?
  +      return new SAXException("parse may not be called while parsing.");
  +    }
  +    
  +    Object arg;
  +    try {
  +      boolean ok = incrementalParser.parseSomeSetup(source);
  +      arg = ok ? Boolean.TRUE : Boolean.FALSE;
  +    }
  +    catch (Exception ex) {
  +      arg = ex;
  +    }
  +    return arg;
     }
  -  
  +
     
     /** doMore() is a simple API which tells the coroutine parser
      * that we need more nodes.  This is intended to be called from one
  @@ -300,35 +222,26 @@
      * */
     public Object doMore (boolean parsemore, int appCoroutineID)
     {
  -    try 
  -      {
  -        Object result =
  -          fCoroutineManager.co_resume(parsemore?Boolean.TRUE:Boolean.FALSE,
  -                                      appCoroutineID, fParserCoroutineID);
  -        
  -        // %REVIEW% Better error reporting needed
  -        if (result == null)
  -          {
  -            System.out.println("\nUNEXPECTED: Parser doMore says shut down prematurely.\n");
  -          }
  -        else if (result instanceof Exception) {
  -          System.out.println("\nParser threw exception:");
  -          ((Exception)result).printStackTrace();
  -        }
  -        
  -        return result;
  -      }
  -  
  -    // SHOULD NEVER OCCUR, since the coroutine number and coroutine manager
  -    // are those previously established for this CoroutineSAXParser...
  -    // So I'm just going to return it as a parsing exception, for now.
  -    catch(NoSuchMethodException e)
  +    if(!parsemore)
         {
  -        return e;
  +	fParseInProgress=false;
  +	return Boolean.FALSE;
         }
  +
  +    Object arg;
  +    try {
  +      boolean keepgoing = incrementalParser.parseSome();
  +      arg = keepgoing ? Boolean.TRUE : Boolean.FALSE;
  +    } catch (SAXException ex) {
  +      arg = ex;
  +    } catch (IOException ex) {
  +      arg = ex;
  +    } catch (Exception ex) {
  +      arg = new SAXException(ex);
  +    }
  +    return arg;
     }
     
  -  
     /** doTerminate() is a simple API which tells the coroutine
      * parser to terminate itself.  This is intended to be called from
      * one of our partner coroutines, and serves both to encapsulate the
  @@ -343,19 +256,10 @@
      * */
     public void doTerminate(int appCoroutineID)
     {
  -    try
  -      {
  -        Object result =
  -          fCoroutineManager.co_resume(null, appCoroutineID, fParserCoroutineID);
  +    // No-op in IncrementalXerces?
   
  -        // Debugging; shouldn't arise in normal operation
  -        if(result!=null)
  -          System.out.println("\nUNEXPECTED: Parser doTerminate answers "+result);
  -      }
  -    catch(java.lang.NoSuchMethodException e)
  -      {
  -        // That's OK; if it doesn't exist, we don't need to terminate it
  -      }
  +    // Could release some data structures to promote garbage collection...
  +    incrementalParser=null;
     }
   
     //================================================================
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org