Class NGCCRuntime

  • All Implemented Interfaces:
    NGCCEventSource, org.xml.sax.ContentHandler
    Direct Known Subclasses:
    NGCCRuntimeEx

    public class NGCCRuntime
    extends java.lang.Object
    implements org.xml.sax.ContentHandler, NGCCEventSource
    Runtime Engine for RELAXNGCC execution. This class has the following functionalities:
    1. Managing a stack of NGCCHandler objects and switching between them appropriately.
    2. Keep track of all Attributes.
    3. manage mapping between namespace URIs and prefixes.
    4. TODO: provide support for interleaving.
    Version:
    $Id: NGCCRuntime.java,v 1.15 2002/09/29 02:55:48 okajima Exp $
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Stack attStack
      stack of Attributes.
      private AttributesImpl currentAtts
      current attributes set.
      private NGCCEventReceiver currentHandler
      The current NGCCHandler.
      (package private) static java.lang.String IMPOSSIBLE
      Impossible token.
      private int indent  
      private org.xml.sax.Locator locator  
      private java.util.ArrayList namespaces
      in-scope namespace mapping.
      private boolean needIndent  
      private int nsEffectivePtr
      Index on the namespaces array, which points to the top of the effective bindings.
      private java.util.Stack nsEffectiveStack
      Stack to preserve old nsEffectivePtr values.
      private org.xml.sax.ContentHandler redirect
      When redirecting a sub-tree, this value will be non-null.
      private int redirectionDepth
      Counts the depth of the elements when we are re-directing a sub-tree to another ContentHandler.
      private java.lang.StringBuffer text
      accumulated text.
    • Constructor Summary

      Constructors 
      Constructor Description
      NGCCRuntime()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void characters​(char[] ch, int start, int length)  
      void consumeAttribute​(int index)  
      void endDocument()  
      void endElement​(java.lang.String uri, java.lang.String localname, java.lang.String qname)  
      void endPrefixMapping​(java.lang.String prefix)  
      int getAttributeIndex​(java.lang.String uri, java.lang.String localname)  
      org.xml.sax.Attributes getCurrentAttributes()
      Attributes that belong to the current element.
      org.xml.sax.Locator getLocator()
      Gets the source location of the current event.
      void ignorableWhitespace​(char[] ch, int start, int length)  
      void onEnterElementConsumed​(java.lang.String uri, java.lang.String localName, java.lang.String qname, org.xml.sax.Attributes atts)
      Called by the generated handler code when an enter element event is consumed.
      void onLeaveElementConsumed​(java.lang.String uri, java.lang.String localName, java.lang.String qname)  
      private void printIndent()  
      void processingInstruction​(java.lang.String target, java.lang.String data)  
      void processList​(java.lang.String str)  
      private void processPendingText​(boolean ignorable)
      Processes buffered text.
      void redirectSubtree​(org.xml.sax.ContentHandler child, java.lang.String uri, java.lang.String local, java.lang.String qname)
      This method can be called only from the enterElement handler.
      int replace​(NGCCEventReceiver o, NGCCEventReceiver n)
      Replaces an old handler with a new handler, and returns ID of the EventReceiver thread.
      void reset()
      Cleans up all the data structure so that the object can be reused later.
      java.lang.String resolveNamespacePrefix​(java.lang.String prefix)  
      void sendEnterAttribute​(int threadId, java.lang.String uri, java.lang.String local, java.lang.String qname)  
      void sendEnterElement​(int threadId, java.lang.String uri, java.lang.String local, java.lang.String qname, org.xml.sax.Attributes atts)
      Sends an enter element event to the specified EventReceiver thread.
      void sendLeaveAttribute​(int threadId, java.lang.String uri, java.lang.String local, java.lang.String qname)  
      void sendLeaveElement​(int threadId, java.lang.String uri, java.lang.String local, java.lang.String qname)  
      void sendText​(int threadId, java.lang.String value)  
      void setDocumentLocator​(org.xml.sax.Locator _loc)  
      void setRootHandler​(NGCCHandler rootHandler)
      Sets the root handler, which will be used to parse the root element.
      void skippedEntity​(java.lang.String name)  
      void startDocument()  
      void startElement​(java.lang.String uri, java.lang.String localname, java.lang.String qname, org.xml.sax.Attributes atts)  
      void startPrefixMapping​(java.lang.String prefix, java.lang.String uri)  
      void trace​(java.lang.String s)  
      void traceln​(java.lang.String s)  
      protected void unexpectedX​(java.lang.String token)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • locator

        private org.xml.sax.Locator locator
      • attStack

        private final java.util.Stack attStack
        stack of Attributes.
      • currentAtts

        private AttributesImpl currentAtts
        current attributes set. always equal to attStack.peek()
      • text

        private java.lang.StringBuffer text
        accumulated text.
      • currentHandler

        private NGCCEventReceiver currentHandler
        The current NGCCHandler. Always equals to handlerStack.peek()
      • IMPOSSIBLE

        static final java.lang.String IMPOSSIBLE
        Impossible token. This value can never be a valid XML name.
        See Also:
        Constant Field Values
      • redirect

        private org.xml.sax.ContentHandler redirect
        When redirecting a sub-tree, this value will be non-null.
      • redirectionDepth

        private int redirectionDepth
        Counts the depth of the elements when we are re-directing a sub-tree to another ContentHandler.
      • namespaces

        private final java.util.ArrayList namespaces
        in-scope namespace mapping. namespaces[2n ] := prefix namespaces[2n+1] := namespace URI
      • nsEffectivePtr

        private int nsEffectivePtr
        Index on the namespaces array, which points to the top of the effective bindings. Because of the timing difference between the startPrefixMapping method and the execution of the corresponding actions, this value can be different from namespaces.size().

        For example, consider the following schema:

        
          <oneOrMore>
           <element name="foo"><empty/></element>
          </oneOrMore>
          code fragment X
          <element name="bob"/>
         
        Code fragment X is executed after we see a startElement event, but at this time the namespaces variable already include new namespace bindings declared on "bob".
      • nsEffectiveStack

        private final java.util.Stack nsEffectiveStack
        Stack to preserve old nsEffectivePtr values.
      • indent

        private int indent
      • needIndent

        private boolean needIndent
    • Constructor Detail

      • NGCCRuntime

        public NGCCRuntime()
    • Method Detail

      • setRootHandler

        public void setRootHandler​(NGCCHandler rootHandler)
        Sets the root handler, which will be used to parse the root element.

        This method can be called right after the object is created or the reset method is called. You can't replace the root handler while parsing is in progress.

        Usually a generated class that corresponds to the <start> pattern will be used as the root handler, but any NGCCHandler can be a root handler.

        Throws:
        java.lang.IllegalStateException - If this method is called but it doesn't satisfy the pre-condition stated above.
      • reset

        public void reset()
        Cleans up all the data structure so that the object can be reused later. Normally, applications do not need to call this method directly, as the runtime resets itself after the endDocument method.
      • setDocumentLocator

        public void setDocumentLocator​(org.xml.sax.Locator _loc)
        Specified by:
        setDocumentLocator in interface org.xml.sax.ContentHandler
      • getLocator

        public org.xml.sax.Locator getLocator()
        Gets the source location of the current event.

        One can call this method from RelaxNGCC handlers to access the line number information. Note that to

      • getCurrentAttributes

        public org.xml.sax.Attributes getCurrentAttributes()
        Attributes that belong to the current element.

        It's generally not recommended for applications to use this method. RelaxNGCC internally removes processed attributes, so this doesn't correctly reflect all the attributes an element carries.

      • processPendingText

        private void processPendingText​(boolean ignorable)
                                 throws org.xml.sax.SAXException
        Processes buffered text. This method will be called by the start/endElement event to process buffered text as a text event.

        Whitespace handling is a tricky business. Consider the following schema fragment:

        <element name="foo"> <choice> <element name="bar"><empty/></element> <text/> </choice> </element> Assume we hit the following instance: <foo> <bar/></foo> Then this first space needs to be ignored (for otherwise, we will end up treating this space as the match to <text/> and won't be able to process <bar>.) Now assume the following instance: <foo/> This time, we need to treat this empty string as a text, for otherwise we won't be able to accept this instance.

        This is very difficult to solve in general, but one seemingly easy solution is to use the type of next event. If a text is followed by a start tag, it follows from the constraint on RELAX NG that that text must be either whitespaces or a match to <text/>.

        On the contrary, if a text is followed by a end tag, then it cannot be whitespace unless the content model can accept empty, in which case sending a text event will be harmlessly ignored by the NGCCHandler.

        Thus this method take one parameter, which controls the behavior of this method.

        TODO: according to the constraint of RELAX NG, if characters follow an end tag, then they must be either whitespaces or must match to <text/>.

        Parameters:
        possiblyWhitespace - True if the buffered character can be ignorabale. False if it needs to be consumed.
        Throws:
        org.xml.sax.SAXException
      • processList

        public void processList​(java.lang.String str)
                         throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • startElement

        public void startElement​(java.lang.String uri,
                                 java.lang.String localname,
                                 java.lang.String qname,
                                 org.xml.sax.Attributes atts)
                          throws org.xml.sax.SAXException
        Specified by:
        startElement in interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • onEnterElementConsumed

        public void onEnterElementConsumed​(java.lang.String uri,
                                           java.lang.String localName,
                                           java.lang.String qname,
                                           org.xml.sax.Attributes atts)
                                    throws org.xml.sax.SAXException
        Called by the generated handler code when an enter element event is consumed.

        Pushes a new attribute set.

        Note that attributes are NOT pushed at the startElement method, because the processing of the enterElement event can trigger other attribute events and etc.

        This method will be called from one of handlers when it truely consumes the enterElement event.

        Throws:
        org.xml.sax.SAXException
      • onLeaveElementConsumed

        public void onLeaveElementConsumed​(java.lang.String uri,
                                           java.lang.String localName,
                                           java.lang.String qname)
                                    throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • endElement

        public void endElement​(java.lang.String uri,
                               java.lang.String localname,
                               java.lang.String qname)
                        throws org.xml.sax.SAXException
        Specified by:
        endElement in interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • characters

        public void characters​(char[] ch,
                               int start,
                               int length)
                        throws org.xml.sax.SAXException
        Specified by:
        characters in interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • ignorableWhitespace

        public void ignorableWhitespace​(char[] ch,
                                        int start,
                                        int length)
                                 throws org.xml.sax.SAXException
        Specified by:
        ignorableWhitespace in interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • getAttributeIndex

        public int getAttributeIndex​(java.lang.String uri,
                                     java.lang.String localname)
      • consumeAttribute

        public void consumeAttribute​(int index)
                              throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • startPrefixMapping

        public void startPrefixMapping​(java.lang.String prefix,
                                       java.lang.String uri)
                                throws org.xml.sax.SAXException
        Specified by:
        startPrefixMapping in interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • endPrefixMapping

        public void endPrefixMapping​(java.lang.String prefix)
                              throws org.xml.sax.SAXException
        Specified by:
        endPrefixMapping in interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • skippedEntity

        public void skippedEntity​(java.lang.String name)
                           throws org.xml.sax.SAXException
        Specified by:
        skippedEntity in interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • processingInstruction

        public void processingInstruction​(java.lang.String target,
                                          java.lang.String data)
                                   throws org.xml.sax.SAXException
        Specified by:
        processingInstruction in interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • endDocument

        public void endDocument()
                         throws org.xml.sax.SAXException
        Specified by:
        endDocument in interface org.xml.sax.ContentHandler
        Throws:
        org.xml.sax.SAXException
      • startDocument

        public void startDocument()
        Specified by:
        startDocument in interface org.xml.sax.ContentHandler
      • sendEnterAttribute

        public void sendEnterAttribute​(int threadId,
                                       java.lang.String uri,
                                       java.lang.String local,
                                       java.lang.String qname)
                                throws org.xml.sax.SAXException
        Specified by:
        sendEnterAttribute in interface NGCCEventSource
        Throws:
        org.xml.sax.SAXException
      • sendEnterElement

        public void sendEnterElement​(int threadId,
                                     java.lang.String uri,
                                     java.lang.String local,
                                     java.lang.String qname,
                                     org.xml.sax.Attributes atts)
                              throws org.xml.sax.SAXException
        Description copied from interface: NGCCEventSource
        Sends an enter element event to the specified EventReceiver thread.
        Specified by:
        sendEnterElement in interface NGCCEventSource
        Throws:
        org.xml.sax.SAXException
      • sendLeaveAttribute

        public void sendLeaveAttribute​(int threadId,
                                       java.lang.String uri,
                                       java.lang.String local,
                                       java.lang.String qname)
                                throws org.xml.sax.SAXException
        Specified by:
        sendLeaveAttribute in interface NGCCEventSource
        Throws:
        org.xml.sax.SAXException
      • sendLeaveElement

        public void sendLeaveElement​(int threadId,
                                     java.lang.String uri,
                                     java.lang.String local,
                                     java.lang.String qname)
                              throws org.xml.sax.SAXException
        Specified by:
        sendLeaveElement in interface NGCCEventSource
        Throws:
        org.xml.sax.SAXException
      • sendText

        public void sendText​(int threadId,
                             java.lang.String value)
                      throws org.xml.sax.SAXException
        Specified by:
        sendText in interface NGCCEventSource
        Throws:
        org.xml.sax.SAXException
      • redirectSubtree

        public void redirectSubtree​(org.xml.sax.ContentHandler child,
                                    java.lang.String uri,
                                    java.lang.String local,
                                    java.lang.String qname)
                             throws org.xml.sax.SAXException
        This method can be called only from the enterElement handler. The sub-tree rooted at the new element will be redirected to the specified ContentHandler.

        Currently active NGCCHandler will only receive the leaveElement event of the newly started element.

        Parameters:
        uri - ,local,qname Parameters passed to the enter element event. Used to simulate the startElement event for the new ContentHandler.
        Throws:
        org.xml.sax.SAXException
      • resolveNamespacePrefix

        public java.lang.String resolveNamespacePrefix​(java.lang.String prefix)
      • unexpectedX

        protected void unexpectedX​(java.lang.String token)
                            throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • printIndent

        private void printIndent()
      • trace

        public void trace​(java.lang.String s)
      • traceln

        public void traceln​(java.lang.String s)