The XML DTD Parser API

Version:
  $Revision: 5951 $ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
Author:
  Alexander Totok

This package consists of two parsers:

- {@link org.exolab.castor.xml.dtd.parser.DTDInitialParser Initial Parser} parses the input text, searches for parameter entity declarations (i.e. entities used only within XML DTD) and substitutes parameter entity references by corresponding replacement text. All other text is passed to the output "as is".
  The initial parser parses internal parameter entity declarations only, like:

      <!ENTITY % name "John White" >

  signaling an error if an external parameter entity declaration, like:

      <!ENTITY % ISOLat2 SYSTEM "http://www.xml.com/iso/isolat2-xml.entities" >

  is met. Future versions will be able to parse and handle external parameter entity declarations.
  The output of this parser is a document without paramater entity declarations and all parameter entity references substituted by corresponding replacement text.
- {@link org.exolab.castor.xml.dtd.parser.DTDParser Main Parser} performes the main parsing process. It is able to parse:
  - ELEMENT declarations
  - ATTRIBUTE declarations
  - GENERAL ENTITY declarations
  - NOTATION declarations
  - Comments
  The parser does not parse:
  - Conditional Sections:

        <![ INCLUDE [ ... ]]>
        <![ IGNORE  [ ... ]]>

  - Processing Instructions, like:

        <?xml version="1.0" encoding="UTF-16"?>

  The parser does not expand general entity references or character references occured within attribute and entity values.

The parser is fully compliant with the current XML specification, unless otherwise is stated, for instance it is able to parse Unicode text, provided the {@link java.io.Reader java.io.Reader} used to instantiate the parser is correctly set up.

The structure of the package:

The parser was written using JavaCC (Java Compiler Compiler) - automated tool to generate Java programming language parsers.

Package consists of the following classes and files:

- DTDInitialParser.jj - initial parser's JavaCC grammar file with the syntax specification and processing code. This file is used by JavaCC to automatically generate Java classes for the initial parser.
- {@link org.exolab.castor.xml.dtd.parser.DTDInitialParser DTDInitialParser}, {@link org.exolab.castor.xml.dtd.parser.DTDInitialParserConstants DTDInitialParserConstants}, {@link org.exolab.castor.xml.dtd.parser.DTDInitialParserTokenManager DTDInitialParserTokenManager} - classes of the initial parser automatically generated by JavaCC from the DTDInitialParser.jj file.
- DTDParser.jj - main parser's JavaCC grammar file with the syntax specification and processing code. This file is used by JavaCC to automatically generate Java classes for the main parser.
- {@link org.exolab.castor.xml.dtd.parser.DTDParser DTDParser}, {@link org.exolab.castor.xml.dtd.parser.DTDParserConstants DTDParserConstants}, {@link org.exolab.castor.xml.dtd.parser.DTDParserTokenManager DTDParserTokenManager} - classes of the main parser automatically generated by JavaCC from the DTDParser.jj file.
- {@link org.exolab.castor.xml.dtd.parser.Token Token}, {@link org.exolab.castor.xml.dtd.parser.ParseException ParseException}, {@link org.exolab.castor.xml.dtd.parser.TokenMgrError TokenMgrError}, {@link org.exolab.castor.xml.dtd.parser.CharStream CharStream} - classes used by both parsers and suitable for any grammar. JavaCC first looks for these files and generates them only if they are absent. But do not edit the first line of these files, as JavaCC will give warning message being unable to authenticate them.
  {@link org.exolab.castor.xml.dtd.parser.TokenMgrError TokenMgrError} is thrown if the Token Manager of the parser has encountered a syntax error in the text of DTD document and is unable to produce next token.
  {@link org.exolab.castor.xml.dtd.parser.ParseException ParseException} is thrown if a DTD document does not comply with DTD syntax and the parser is unable to parse the document.
- {@link org.exolab.castor.xml.dtd.parser.InputCharStream InputCharStream} - an implementation of interface {@link org.exolab.castor.xml.dtd.parser.CharStream CharStream}. Implements input character stream that maintains line and column number positions of the characters. It also has the capability to backup the stream to some extent.
  The object of this class is constructed using {@link java.io.Reader java.io.Reader} `reader` and it is left to constructor of the `reader` to set up character encoding correctly. This means that method read of the `reader` is used to get next characters, assuming it returns appropriate values. It is recommended to use class {@link java.io.InputStreamReader java.io.InputStreamReader} as a `reader`, which allows to set desired character encoding. This class is an intermediate component between input character reader and the parser.
  The code of this class is based on the class ASCII_CharStream - implementation of interface {@link org.exolab.castor.xml.dtd.parser.CharStream CharStream}, that JavaCC would have generated with the following options set in a JavaCC grammar file:

        JAVA_UNICODE_ESCAPE = false;
        UNICODE_INPUT = false;
        USER_CHAR_STREAM = false;

  Note that this class is not fully JavaCC generated.

The followinge example parses XML DTD file `dtd-document.dtd` and constructs corresponding {@link org.exolab.castor.xml.dtd.DTDdocument XML DTD document} object `dtd`.

      FileInputStream inputStream;
      InputStreamReader reader;
      InputCharStream charStream;
      DTDInitialParser initialParser;
      String intermedResult;
      StringReader strReader;
      DTDParser parser;
      DTDdocument dtd;

      // instantiate input byte stream, associated with the input file
      inputStream = new FileInputStream( "dtd-document.dtd" );

      // instantiate character reader from the input file byte stream
      reader = new InputStreamReader( inputStream, "US-ASCII" );

      // instantiate char stream for initial parser from the input reader
      charStream = new InputCharStream( reader );

      // instantiate initial parser
      initialParser = new DTDInitialParser( charStream );

      // get result of initial parsing - DTD text document with parameter
      // entity references expanded
      intermedResult = initialParser.Input();

      // construct StringReader from the intermediate parsing result
      strReader= new StringReader( intermedResult );

      // instantiate char stream for the main parser
      charStream = new InputCharStream( strReader );

      // instantiate main parser
      parser = new DTDParser( charStream );

      // parse intermediate parsing result with the main parser
      // and get corresponding DTD document oblect
      dtd = parser.Input();