<manpage id="pullparser" cat="pullparser" title="pullparser">
  <namesection>
    <name>tdom::pullparser</name>
    <desc>Create an XML pull parser command</desc>
  </namesection>

  <synopsis>
    <syntax>package require tdom

    <cmd>tdom::pullparser</cmd> <m>cmdName</m> <o>-ignorewhitecdata</o>
    </syntax>
  </synopsis>

  <section>
    <title>DESCRIPTION </title>    
    
    <p>This command creates XML pull parser commands with a simple
    API, along the lines of a simple StAX parser. After creation,
    you've to set an input source, to do anything useful with the pull
    parser. For this see the methods <m>input</m>, <m>inputchannel</m>
    and <m>inputfile</m>.</p>

    <p>The parser has always a <m>state</m>. You start parsing the XML
    data until some next state, do what has to be done and skip again
    to the next state. XML well-formedness errors along the way will
    be reported as TCL_ERROR with additional info in the error
    message.</p>

    <p>The pull parsers don't follow external entities and are XML
    1.0 only, they know nothing about XML Namespaces. You get the tags
    and attribute names as in the source. You aren't noticed about
    comments, processing instructions and external entities; they are
    silently ignored for you. CDATA Sections are handled as if their
    content would have been provided without using a CDATA Section.
    </p>

    <p>On the brighter side is that character entity and attribute
    default declarations in the internal subset are respected (because
    of using expat as underlying parser). It is probably somewhat
    faster than a comperable implementation with the SAX interface.
    It's a nice programming model. It's a slim interface.
    </p>

    <p>If the option <m>-ignorewhitecdata</m> is given, the created
    XML pull parser command will ignore any white space only (' ', \t,
    \n and \r) text content between START_TAG and START_TAG / END_TAG.
    The parser won't stop at such input and will create TEXT state
    events only for not white space only text. </p>

    <p>Not all methods are valid in every state. The parser will raise
    TCL_ERROR if a method is called in a state the method isn't valid
    for. Valid methods of the created commands are:</p>
    <commandlist>
      <commanddef>
        <command><method>state</method></command>
        <desc>This method is valid in all parser states. The
        possible return values and their meanings are:
        <ul>
            <li><m>READY</m> - The parser is created or reset, but no
            input is set.</li>
            <li><m>START_DOCUMENT</m> - Input is set, parser is ready
            to start parsing.</li>
            <li><m>START_TAG</m> - Parser has stopped parsing at a
            start tag.</li>
            <li><m>END_TAG</m> - Parser has stopped parsing at an end tag</li>
            <li><m>TEXT</m> - Parser has stopped parsing to report
            text between tags.</li>
            <li><m>END_DOKUMENT</m> - Parser has finished parsing
            without error.</li>
            <li><m>PARSE_ERROR</m> - Parser stopped parsing at XML
            error in input.</li>
        </ul>
        </desc>
      </commanddef>

      <commanddef>
        <command><method>input</method> <m>data</m></command>
        <desc>This method is only valid in state <m>READY</m>. It
        prepares the parser to use <m>data</m> as XML input to parse
        and switches the parser into state START_DOCUMENT.</desc>
      </commanddef>

      <commanddef>
        <command><method>inputchannel</method> <m>channel</m></command>
        <desc>This method is only valid in state <m>READY</m>. It
        prepares the parser to read the XML input to parse out of
        <m>channel</m> and switches the parser into state START_DOCUMENT.</desc>
      </commanddef>

      <commanddef>
        <command><method>inputfile</method> <m>filename</m></command>
        <desc>This method is only valid in state <m>READY</m>. It
        open <m>filename</m> and prepares the parser to read the XML
        input to parse out of that file. The method returns TCL_ERROR,
        if the file could not be open in read mode. Otherwise it
        switches the parser into state START_DOCUMENT.</desc>
      </commanddef>

      <commanddef>
        <command><method>next</method></command>
        <desc>This method is valid in state <m>START_DOCUMENT</m>,
        <m>START_TAG</m>, <m>END_TAG</m> and <m>TEXT</m>. It continues
        parsing of the XML input until the next event, which it will
        return.</desc>
      </commanddef>

      <commanddef>
        <command><method>tag</method></command>
        <desc>This method is only valid in states <m>START_TAG</m> and
        <m>END_TAG</m>. It returns the tag name of the current start
        or end tag.</desc>
      </commanddef>

      <commanddef>
        <command><method>attributes</method></command>
        <desc>This method is only valid in state <m>START_TAG</m>. It
        returns all attributes of the element in a name value list.</desc>
      </commanddef>

      <commanddef>
        <command><method>text</method></command>
        <desc>This method is only valid in state <m>TEXT</m>. It
        returns the character data of the event. There will be always
        at most one TEXT event between START_TAG and the next
        START_TAG or END_TAG event.</desc>
      </commanddef>

      <commanddef>
        <command><method>skip</method></command>
        <desc>This method is only valid in state <m>START_TAG</m>. It
        skips to the corresponding end tag and ignores all events (but
        not XML parsing errors) on the way and returns the new state
        END_TAG.</desc>
      </commanddef>

      <commanddef>
        <command><method>find-element</method> <o>tagname | -names tagnames</o></command>
        <desc>This method is only valid in states
        <m>START_DOCUMENT</m>, <m>START_TAG</m> and <m>END_TAG</m>. It
        skips forward until the next element start tag with tag name
        <m>tagname</m> and returns the new state START_TAG. If a list
        of tagnames is provided with the <m>-names</m> option, any of
        the <m>tagnames</m> match. If there isn't such an element the
        parser stops at the end of the input and returns
        END_DOCUMENT.</desc>
      </commanddef>

      <commanddef>
        <command><method>reset</method></command>
        <desc>This method is valid in all parser states. It resets the
        parser into READY state and returns that.</desc>
      </commanddef>

      <commanddef>
        <command><method>delete</method></command>
        <desc>This method is valid in all parser states. It deletes
        the parser command.</desc>
      </commanddef>
    </commandlist>

    <p>Miscellaneous methods:</p>
    <commandlist>
      <commanddef>
        <command><method>line</method></command>
        <desc>This method is valid in all parser states except READY
        and TEXT. It returns the line number of the parsing
        position. Line counting starts with 1.</desc>
      </commanddef>

      <commanddef>
        <command><method>column</method></command>
        <desc>This method is valid in all parser states except READY
        and TEXT. It returns the offset, from the beginning of the
        current line, of the parsing position. Column counting starts
        with 0.</desc>
      </commanddef>
    </commandlist>
  </section>

  <keywords>
    <keyword>XML</keyword>
    <keyword>pull</keyword>
    <keyword>parsing</keyword>
  </keywords>
</manpage>
