Top Description Fields Constructors Methods
org.springframework.util.xml

public Class XmlValidationModeDetector

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .CharConversionException, .IOException, .InputStream, .InputStreamReader, org.springframework.lang.Nullable, org.springframework.util.StringUtils

Detects whether an XML stream is using DTD- or XSD-based validation.
Authors
Rob Harrop, Juergen Hoeller, Sam Brannen
Since
2.0

Field Summary

Modifier and TypeField and Description
private static final String
DOCTYPE

The token in an XML document that declares the DTD to use for validation and thus that DTD validation is being used.

private static final String
END_COMMENT

The token that indicates the end of an XML comment.

private boolean
inComment

Indicates whether the current parse position is inside an XML comment.

private static final String
START_COMMENT

The token that indicates the start of an XML comment.

public static final int
VALIDATION_AUTO

Indicates that the validation mode should be auto-guessed, since we cannot find a clear indication (probably choked on some special characters, or the like).

public static final int
VALIDATION_DTD

Indicates that DTD validation should be used (we found a "DOCTYPE" declaration).

public static final int
VALIDATION_NONE

Indicates that the validation should be disabled.

public static final int
VALIDATION_XSD

Indicates that XSD validation should be used (found no "DOCTYPE" declaration).

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private int
commentToken(String line, String token, boolean inCommentIfPresent)

Try to consume the supplied token against the supplied content and update the "in comment" parse state to the supplied value.

private String
consume(String line)

Consume the next comment token, update the "inComment" flag, and return the remaining content.

private String
consumeCommentTokens(String line)

Consume all comments in the given String and return the remaining content, which may be empty since the supplied content might be all comment data.

public int
detectValidationMode(InputStream
the InputStream to parse
inputStream
)

Detect the validation mode for the XML document in the supplied InputStream.

private int
endComment(String line)

Try to consume the END_COMMENT token.

private boolean
hasDoctype(String content)

Does the content contain the DTD DOCTYPE declaration?

private boolean
hasOpeningTag(String content)

Determine if the supplied content contains an XML opening tag.

private int
startComment(String line)

Try to consume the START_COMMENT token.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DOCTYPEback to summary
private static final String DOCTYPE

The token in an XML document that declares the DTD to use for validation and thus that DTD validation is being used.

END_COMMENTback to summary
private static final String END_COMMENT

The token that indicates the end of an XML comment.

inCommentback to summary
private boolean inComment

Indicates whether the current parse position is inside an XML comment.

START_COMMENTback to summary
private static final String START_COMMENT

The token that indicates the start of an XML comment.

VALIDATION_AUTOback to summary
public static final int VALIDATION_AUTO

Indicates that the validation mode should be auto-guessed, since we cannot find a clear indication (probably choked on some special characters, or the like).

VALIDATION_DTDback to summary
public static final int VALIDATION_DTD

Indicates that DTD validation should be used (we found a "DOCTYPE" declaration).

VALIDATION_NONEback to summary
public static final int VALIDATION_NONE

Indicates that the validation should be disabled.

VALIDATION_XSDback to summary
public static final int VALIDATION_XSD

Indicates that XSD validation should be used (found no "DOCTYPE" declaration).

Constructor Detail

XmlValidationModeDetectorback to summary
public XmlValidationModeDetector()

Method Detail

commentTokenback to summary
private int commentToken(String line, String token, boolean inCommentIfPresent)

Try to consume the supplied token against the supplied content and update the "in comment" parse state to the supplied value.

Returns the index into the content which is after the token or -1 if the token is not found.

consumeback to summary
private String consume(String line)

Consume the next comment token, update the "inComment" flag, and return the remaining content.

Annotations
@Nullable
consumeCommentTokensback to summary
private String consumeCommentTokens(String line)

Consume all comments in the given String and return the remaining content, which may be empty since the supplied content might be all comment data.

This method takes the current "in comment" parsing state into account.

detectValidationModeback to summary
public int detectValidationMode(InputStream inputStream) throws IOException

Detect the validation mode for the XML document in the supplied InputStream.

Note that the supplied InputStream is closed by this method before returning.

Parameters
inputStream:InputStream

the InputStream to parse

Exceptions
IOException:
in case of I/O failure
See Also
VALIDATION_DTD, VALIDATION_XSD
endCommentback to summary
private int endComment(String line)

Try to consume the END_COMMENT token.

See Also
commentToken(String, String, boolean)
hasDoctypeback to summary
private boolean hasDoctype(String content)

Does the content contain the DTD DOCTYPE declaration?

hasOpeningTagback to summary
private boolean hasOpeningTag(String content)

Determine if the supplied content contains an XML opening tag.

It is expected that all comment tokens will have been consumed for the supplied content before passing the remainder to this method. However, as a sanity check, if the parse state is currently in an XML comment this method always returns false.

startCommentback to summary
private int startComment(String line)

Try to consume the START_COMMENT token.

See Also
commentToken(String, String, boolean)