Top Description Fields Constructors Methods
org.jdom2.input.stax

public Class DTDParser

extends Object
Class Inheritance
Imports
java.util.HashMap, java.util.regex.Matcher, .Pattern, org.jdom2.DocType, .JDOMException, .JDOMFactory

Parses out key information from a single String representing a DOCTYPE declaration. StAX parsers supply a single string representing the DOCTYPE and this needs to be processed to get items like the SystemID, etc. Additionally it needs to be reformatted to create a standardised representation.

The assumption is that the DTD is valid.

We need to pull out 4 elements of data:

  1. The root element name
  2. The SystemID (if available)
  3. The PublicID (if available)
  4. The internal subset (if available)
The internal-subset should be re-formatted to conform to the JDOM 'standard' where each declaration starts on a new line indented with 2 spaces. This 'standard' is defined by the way that JDOM formats the DTD declarations in the SAX parse process, which fires individual events for the content in the DTD.

We can do this all with a well-structured regular expression, which is actually simpler than trying to fish out all the components ourselves....

Author
Rolf Lear

Field Summary

Modifier and TypeField and Description
private static final String
metapattern

This is the meta-pattern.

private static final Pattern
pattern

The following Pattern is the final result after parsing/tokenizing/substituting the meta-pattern.

Constructor Summary

AccessConstructor and Description
private
DTDParser()

Make instances 'impossible'.

Method Summary

Modifier and TypeMethod and Description
private static final Pattern

Returns:

The substituted pattern
buildPattern
(HashMap<String, String>
The map containing substitution tokens/patterns
map
,
String
The meta-pattern to do the substitutions on.
input
)

This method substitutes the simple tokens in the meta-pattern with the declared values in the map.

private static String

Returns:

the formatted input.
formatInternal
(String
the input DocType declaration as found in a StAX Reader.
internal
)

Reformat an internal subset....

private static final String

Returns:

The first found value.
getGroup
(final Matcher
The match that has succeeded
mat
,
final int...
The groups to check for a value.
groups
)

Looks in any number of matched groups for a value.

private static final boolean

Returns:

true if it is a space, tab, newline, or carriage-return.
isWhite
(char
The char to check
ch
)

return true if the input character is one of the types recognized in the DTD spec.

public static DocType

Returns:

The input string as a DocType.
parse
(final String
the input DOCTYPE string to parse. Must be valid.
input
,
final JDOMFactory
The JDOM factory to use to build the JDOM DocType.
factory
)

Parse out a DOCTYPE declaration as supplied by the standard StAX readers.

private static final HashMap<String, String>

Returns:

The populated map.
populatePatterns
()

This builds a substitution map containing the raw patterns for certain types of content we expect.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

metapatternback to summary
private static final String metapattern

This is the meta-pattern.

  • Where you see ' os ' there is optional space.
  • Where you see ' name ' there is the element name.
  • Where you see ' ms ' there is mandatory space.
  • Where you see ' id ' there is some quoted identifier.
  • Where you see ' internal ' there is the internal subset.
Anything else will become part of the final regex.

Space (' ') was chosen for the token delimiter because it makes the meta-pattern easy to read. There are a couple of places in this expression where there are two ' ' together, and it is critical that it does not change because there will be missed token matches then.

patternback to summary
private static final Pattern pattern

The following Pattern is the final result after parsing/tokenizing/substituting the meta-pattern.

Constructor Detail

DTDParserback to summary
private DTDParser()

Make instances 'impossible'. Everything is static.

Method Detail

buildPatternback to summary
private static final Pattern buildPattern(HashMap<String, String> map, String input)

This method substitutes the simple tokens in the meta-pattern with the declared values in the map.

Parameters
map:HashMap<String, String>

The map containing substitution tokens/patterns

input:String

The meta-pattern to do the substitutions on.

Returns:Pattern

The substituted pattern

formatInternalback to summary
private static String formatInternal(String internal)

Reformat an internal subset.... Each declaration starts on an indented newline.

Parameters
internal:String

the input DocType declaration as found in a StAX Reader.

Returns:String

the formatted input.

getGroupback to summary
private static final String getGroup(final Matcher mat, final int... groups)

Looks in any number of matched groups for a value. Returns the first set value. The assumption is that, depending on the pattern matches, the value could be in a few different locations.

Parameters
mat:Matcher

The match that has succeeded

groups:int[]

The groups to check for a value.

Returns:String

The first found value.

isWhiteback to summary
private static final boolean isWhite(char ch)

return true if the input character is one of the types recognized in the DTD spec.

Parameters
ch:char

The char to check

Returns:boolean

true if it is a space, tab, newline, or carriage-return.

parseback to summary
public static DocType parse(final String input, final JDOMFactory factory) throws JDOMException

Parse out a DOCTYPE declaration as supplied by the standard StAX readers.

Using 'XML' terminology, this method assumes that the input is both 'well-formed' and 'valid'. The assumptions that this class makes ensure that the 'right thing' is done for valid content, but invalid content may or may not fail with a JDOMException. The behaviour of this method with invalid input is 'undefined'.

Parameters
input:String

the input DOCTYPE string to parse. Must be valid.

factory:JDOMFactory

The JDOM factory to use to build the JDOM DocType.

Returns:DocType

The input string as a DocType.

Exceptions
JDOMException:
if the DocType is not generated.
populatePatternsback to summary
private static final HashMap<String, String> populatePatterns()

This builds a substitution map containing the raw patterns for certain types of content we expect.

Returns:HashMap<String, String>

The populated map.