com.sun.xml.dtdparser
Class DTDParser

java.lang.Object
  extended by com.sun.xml.dtdparser.DTDParser

public class DTDParser
extends java.lang.Object

This implements parsing of XML 1.0 DTDs.

This conforms to the portion of the XML 1.0 specification related to the external DTD subset.

For multi-language applications (such as web servers using XML processing to create dynamic content), a method supports choosing a locale for parser diagnostics which is both understood by the message recipient and supported by the parser.

This parser produces a stream of parse events. It supports some features (exposing comments, CDATA sections, and entity references) which are not required to be reported by conformant XML processors.

Version:
$Id: DTDParser.java,v 1.1 2005/05/31 22:28:54 kohsuke Exp $
Author:
David Brownell, Janet Koenig, Kohsuke KAWAGUCHI

Nested Class Summary
(package private) static class DTDParser.Catalog
           
(package private) static class DTDParser.NameCache
           
(package private) static class DTDParser.NameCacheEntry
           
 
Field Summary
private  char[] charTmp
           
protected  java.util.Set declaredElements
           
private  boolean doLexicalPE
           
private  DTDEventListener dtdHandler
           
(package private)  SimpleHashtable entities
           
private  SimpleHashtable ids
           
private  InputEntity in
           
private  java.util.Locale locale
           
(package private) static DTDParser.Catalog messages
           
private  DTDParser.NameCache nameCache
           
private  char[] nameTmp
           
(package private)  java.util.Hashtable notations
           
private  SimpleHashtable params
           
private  org.xml.sax.EntityResolver resolver
           
(package private) static java.lang.String strANY
           
(package private) static java.lang.String strEMPTY
           
private  java.lang.StringBuffer strTmp
           
static java.lang.String TYPE_CDATA
           
static java.lang.String TYPE_ENTITIES
           
static java.lang.String TYPE_ENTITY
           
static java.lang.String TYPE_ENUMERATION
           
static java.lang.String TYPE_ID
           
static java.lang.String TYPE_IDREF
           
static java.lang.String TYPE_IDREFS
           
static java.lang.String TYPE_NMTOKEN
           
static java.lang.String TYPE_NMTOKENS
           
static java.lang.String TYPE_NOTATION
           
private static java.lang.String XmlLang
           
 
Constructor Summary
DTDParser()
           
 
Method Summary
(package private)  void afterRoot()
           
private  void builtin(java.lang.String entityName, java.lang.String entityValue)
           
 java.util.Locale chooseLocale(java.lang.String[] languages)
          Chooses a client locale to use for diagnostics, using the first language specified in the list that is supported by this parser.
(package private)  void error(java.lang.String messageId, java.lang.Object[] parameters)
           
private  void expandEntityInLiteral(java.lang.String name, SimpleHashtable table, boolean isEntityValue)
           
private  void externalParameterEntity(ExternalEntity next)
           
private  void fatal(java.lang.String messageId)
           
private  void fatal(java.lang.String messageId, java.lang.Object[] parameters)
           
private  char getc()
           
 int getColumnNumber()
           
private  void getcps(java.lang.String elementName, InputEntity start)
           
 DTDEventListener getDtdHandler()
          Returns the handler used to for DTD parsing events.
 org.xml.sax.EntityResolver getEntityResolver()
          Returns the object used to resolve entities
private  short getFrequency()
           
 int getLineNumber()
           
 java.util.Locale getLocale()
          Returns the diagnostic locale.
private  java.lang.String getMarkupDeclname(java.lang.String roleId, boolean qname)
           
private  void getMixed(java.lang.String elementName, InputEntity start)
           
private  short getMixedOrChildren(java.lang.String elementName)
          returns content model type.
private  java.lang.String getNmtoken()
           
 java.lang.String getPublicId()
           
private  java.lang.String getQuotedString(java.lang.String type, java.lang.String extra)
           
 java.lang.String getSystemId()
           
private  void init()
           
private  boolean isXmlLang(java.lang.String value)
           
private  boolean maybeAttlistDecl()
           
private  boolean maybeComment(boolean skipStart)
           
private  boolean maybeConditionalSect()
           
private  boolean maybeElementDecl()
          To validate, subclassers should at this time make sure that values are of the declared types: ID and IDREF(S) values are Names NMTOKEN(S) are Nmtokens ENUMERATION values match one of the tokens NOTATION values match a notation name ENTITIY(IES) values match an unparsed external entity

private  boolean maybeEntityDecl()
           
private  ExternalEntity maybeExternalID()
           
private  java.lang.String maybeGetName()
           
private  DTDParser.NameCacheEntry maybeGetNameCacheEntry()
           
private  boolean maybeMarkupDecl()
           
private  boolean maybeNotationDecl()
           
private  boolean maybePEReference()
           
private  boolean maybePI(boolean skipStart)
           
private  java.lang.String maybeReadAttribute(java.lang.String name, boolean must)
           
private  void maybeTextDecl()
           
private  boolean maybeWhitespace()
           
private  DTDParser.NameCacheEntry nameCharString(char c)
           
private  void nextChar(char c, java.lang.String location, java.lang.String near)
           
private  java.lang.String normalize(boolean invalidIfNeeded)
           
 void parse(org.xml.sax.InputSource in)
          Parse a DTD.
 void parse(java.lang.String uri)
          Parse a DTD.
private  int parseCharNumber()
           
private  void parseInternal(org.xml.sax.InputSource input)
           
private  void parseLiteral(boolean isEntityValue)
           
private  java.lang.String parsePublicId()
           
private  java.lang.String parseSystemId()
           
private  boolean peek(java.lang.String s)
           
private  InputEntity peekDeclaration(java.lang.String s)
           
private  void pushReader(char[] buf, java.lang.String name, boolean isGeneral)
           
private  boolean pushReader(ExternalEntity next)
           
private  void readEncoding(boolean must)
           
private  void readVersion(boolean must, java.lang.String versionNum)
           
private  void reportConnector(char type)
           
 void setDtdHandler(DTDEventListener handler)
          Used by applications to set handling of DTD parsing events.
 void setEntityResolver(org.xml.sax.EntityResolver r)
          Lets applications control entity resolution.
 void setLocale(java.util.Locale l)
          Used by applications to request locale for diagnostics.
private  int surrogatesToCharTmp(int ucs4)
           
private  void ungetc()
           
private  void warning(java.lang.String messageId, java.lang.Object[] parameters)
           
private  void whitespace(java.lang.String roleId)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TYPE_CDATA

public static final java.lang.String TYPE_CDATA
See Also:
Constant Field Values

TYPE_ID

public static final java.lang.String TYPE_ID
See Also:
Constant Field Values

TYPE_IDREF

public static final java.lang.String TYPE_IDREF
See Also:
Constant Field Values

TYPE_IDREFS

public static final java.lang.String TYPE_IDREFS
See Also:
Constant Field Values

TYPE_ENTITY

public static final java.lang.String TYPE_ENTITY
See Also:
Constant Field Values

TYPE_ENTITIES

public static final java.lang.String TYPE_ENTITIES
See Also:
Constant Field Values

TYPE_NMTOKEN

public static final java.lang.String TYPE_NMTOKEN
See Also:
Constant Field Values

TYPE_NMTOKENS

public static final java.lang.String TYPE_NMTOKENS
See Also:
Constant Field Values

TYPE_NOTATION

public static final java.lang.String TYPE_NOTATION
See Also:
Constant Field Values

TYPE_ENUMERATION

public static final java.lang.String TYPE_ENUMERATION
See Also:
Constant Field Values

in

private InputEntity in

strTmp

private java.lang.StringBuffer strTmp

nameTmp

private char[] nameTmp

nameCache

private DTDParser.NameCache nameCache

charTmp

private char[] charTmp

doLexicalPE

private boolean doLexicalPE

declaredElements

protected final java.util.Set declaredElements

params

private SimpleHashtable params

notations

java.util.Hashtable notations

entities

SimpleHashtable entities

ids

private SimpleHashtable ids

dtdHandler

private DTDEventListener dtdHandler

resolver

private org.xml.sax.EntityResolver resolver

locale

private java.util.Locale locale

strANY

static final java.lang.String strANY
See Also:
Constant Field Values

strEMPTY

static final java.lang.String strEMPTY
See Also:
Constant Field Values

XmlLang

private static final java.lang.String XmlLang
See Also:
Constant Field Values

messages

static final DTDParser.Catalog messages
Constructor Detail

DTDParser

public DTDParser()
Method Detail

setLocale

public void setLocale(java.util.Locale l)
               throws org.xml.sax.SAXException
Used by applications to request locale for diagnostics.

Parameters:
l - The locale to use, or null to use system defaults (which may include only message IDs).
Throws:
org.xml.sax.SAXException

getLocale

public java.util.Locale getLocale()
Returns the diagnostic locale.


chooseLocale

public java.util.Locale chooseLocale(java.lang.String[] languages)
                              throws org.xml.sax.SAXException
Chooses a client locale to use for diagnostics, using the first language specified in the list that is supported by this parser. That locale is then set using setLocale(). Such a list could be provided by a variety of user preference mechanisms, including the HTTP Accept-Language header field.

Parameters:
languages - Array of language specifiers, ordered with the most preferable one at the front. For example, "en-ca" then "fr-ca", followed by "zh_CN". Both RFC 1766 and Java styles are supported.
Returns:
The chosen locale, or null.
Throws:
org.xml.sax.SAXException
See Also:
MessageCatalog

setEntityResolver

public void setEntityResolver(org.xml.sax.EntityResolver r)
Lets applications control entity resolution.


getEntityResolver

public org.xml.sax.EntityResolver getEntityResolver()
Returns the object used to resolve entities


setDtdHandler

public void setDtdHandler(DTDEventListener handler)
Used by applications to set handling of DTD parsing events.


getDtdHandler

public DTDEventListener getDtdHandler()
Returns the handler used to for DTD parsing events.


parse

public void parse(org.xml.sax.InputSource in)
           throws java.io.IOException,
                  org.xml.sax.SAXException
Parse a DTD.

Throws:
java.io.IOException
org.xml.sax.SAXException

parse

public void parse(java.lang.String uri)
           throws java.io.IOException,
                  org.xml.sax.SAXException
Parse a DTD.

Throws:
java.io.IOException
org.xml.sax.SAXException

init

private void init()

builtin

private void builtin(java.lang.String entityName,
                     java.lang.String entityValue)

parseInternal

private void parseInternal(org.xml.sax.InputSource input)
                    throws java.io.IOException,
                           org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

afterRoot

void afterRoot()
         throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

whitespace

private void whitespace(java.lang.String roleId)
                 throws java.io.IOException,
                        org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeWhitespace

private boolean maybeWhitespace()
                         throws java.io.IOException,
                                org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeGetName

private java.lang.String maybeGetName()
                               throws java.io.IOException,
                                      org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeGetNameCacheEntry

private DTDParser.NameCacheEntry maybeGetNameCacheEntry()
                                                 throws java.io.IOException,
                                                        org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

getNmtoken

private java.lang.String getNmtoken()
                             throws java.io.IOException,
                                    org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

nameCharString

private DTDParser.NameCacheEntry nameCharString(char c)
                                         throws java.io.IOException,
                                                org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

parseLiteral

private void parseLiteral(boolean isEntityValue)
                   throws java.io.IOException,
                          org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

expandEntityInLiteral

private void expandEntityInLiteral(java.lang.String name,
                                   SimpleHashtable table,
                                   boolean isEntityValue)
                            throws java.io.IOException,
                                   org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

getQuotedString

private java.lang.String getQuotedString(java.lang.String type,
                                         java.lang.String extra)
                                  throws java.io.IOException,
                                         org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

parsePublicId

private java.lang.String parsePublicId()
                                throws java.io.IOException,
                                       org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeComment

private boolean maybeComment(boolean skipStart)
                      throws java.io.IOException,
                             org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybePI

private boolean maybePI(boolean skipStart)
                 throws java.io.IOException,
                        org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeReadAttribute

private java.lang.String maybeReadAttribute(java.lang.String name,
                                            boolean must)
                                     throws java.io.IOException,
                                            org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

readVersion

private void readVersion(boolean must,
                         java.lang.String versionNum)
                  throws java.io.IOException,
                         org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

getMarkupDeclname

private java.lang.String getMarkupDeclname(java.lang.String roleId,
                                           boolean qname)
                                    throws java.io.IOException,
                                           org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeMarkupDecl

private boolean maybeMarkupDecl()
                         throws java.io.IOException,
                                org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

isXmlLang

private boolean isXmlLang(java.lang.String value)

maybeElementDecl

private boolean maybeElementDecl()
                          throws java.io.IOException,
                                 org.xml.sax.SAXException
To validate, subclassers should at this time make sure that values are of the declared types:

Separately, make sure IDREF values match some ID provided in the document (in the afterRoot method).

Throws:
java.io.IOException
org.xml.sax.SAXException

getMixedOrChildren

private short getMixedOrChildren(java.lang.String elementName)
                          throws java.io.IOException,
                                 org.xml.sax.SAXException
returns content model type.

Throws:
java.io.IOException
org.xml.sax.SAXException

getcps

private void getcps(java.lang.String elementName,
                    InputEntity start)
             throws java.io.IOException,
                    org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

reportConnector

private void reportConnector(char type)
                      throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

getFrequency

private short getFrequency()
                    throws java.io.IOException,
                           org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

getMixed

private void getMixed(java.lang.String elementName,
                      InputEntity start)
               throws java.io.IOException,
                      org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeAttlistDecl

private boolean maybeAttlistDecl()
                          throws java.io.IOException,
                                 org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

normalize

private java.lang.String normalize(boolean invalidIfNeeded)

maybeConditionalSect

private boolean maybeConditionalSect()
                              throws java.io.IOException,
                                     org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

parseCharNumber

private int parseCharNumber()
                     throws java.io.IOException,
                            org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

surrogatesToCharTmp

private int surrogatesToCharTmp(int ucs4)
                         throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

maybePEReference

private boolean maybePEReference()
                          throws java.io.IOException,
                                 org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeEntityDecl

private boolean maybeEntityDecl()
                         throws java.io.IOException,
                                org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeExternalID

private ExternalEntity maybeExternalID()
                                throws java.io.IOException,
                                       org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

parseSystemId

private java.lang.String parseSystemId()
                                throws java.io.IOException,
                                       org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeTextDecl

private void maybeTextDecl()
                    throws java.io.IOException,
                           org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

externalParameterEntity

private void externalParameterEntity(ExternalEntity next)
                              throws java.io.IOException,
                                     org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

readEncoding

private void readEncoding(boolean must)
                   throws java.io.IOException,
                          org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

maybeNotationDecl

private boolean maybeNotationDecl()
                           throws java.io.IOException,
                                  org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

getc

private char getc()
           throws java.io.IOException,
                  org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

ungetc

private void ungetc()

peek

private boolean peek(java.lang.String s)
              throws java.io.IOException,
                     org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

peekDeclaration

private InputEntity peekDeclaration(java.lang.String s)
                             throws java.io.IOException,
                                    org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

nextChar

private void nextChar(char c,
                      java.lang.String location,
                      java.lang.String near)
               throws java.io.IOException,
                      org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

pushReader

private void pushReader(char[] buf,
                        java.lang.String name,
                        boolean isGeneral)
                 throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

pushReader

private boolean pushReader(ExternalEntity next)
                    throws java.io.IOException,
                           org.xml.sax.SAXException
Throws:
java.io.IOException
org.xml.sax.SAXException

getPublicId

public java.lang.String getPublicId()

getSystemId

public java.lang.String getSystemId()

getLineNumber

public int getLineNumber()

getColumnNumber

public int getColumnNumber()

warning

private void warning(java.lang.String messageId,
                     java.lang.Object[] parameters)
              throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

error

void error(java.lang.String messageId,
           java.lang.Object[] parameters)
     throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

fatal

private void fatal(java.lang.String messageId)
            throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

fatal

private void fatal(java.lang.String messageId,
                   java.lang.Object[] parameters)
            throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException