org.springframework.util.xml
Class XmlValidationModeDetector

java.lang.Object
  extended by org.springframework.util.xml.XmlValidationModeDetector

public class XmlValidationModeDetector
extends java.lang.Object

Detects whether an XML stream is using DTD- or XSD-based validation.

Since:
2.0
Author:
Rob Harrop, Juergen Hoeller

Field Summary
private static java.lang.String DOCTYPE
          The token in a XML document that declares the DTD to use for validation and thus that DTD validation is being used.
private static java.lang.String END_COMMENT
          The token that indicates the end of an XML comment.
private  boolean inComment
          Indicates whether or not the current parse position is inside an XML comment.
private static java.lang.String START_COMMENT
          The token that indicates the start of an XML comment.
static 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).
static int VALIDATION_DTD
          Indicates that DTD validation should be used (we found a "DOCTYPE" declaration).
static int VALIDATION_NONE
          Indicates that the validation should be disabled.
static int VALIDATION_XSD
          Indicates that XSD validation should be used (found no "DOCTYPE" declaration).
 
Constructor Summary
XmlValidationModeDetector()
           
 
Method Summary
private  int commentToken(java.lang.String line, java.lang.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  java.lang.String consume(java.lang.String line)
          Consume the next comment token, update the "inComment" flag and return the remaining content.
private  java.lang.String consumeCommentTokens(java.lang.String line)
          Consumes all the leading comment data in the given String and returns the remaining content, which may be empty since the supplied content might be all comment data.
 int detectValidationMode(java.io.InputStream inputStream)
          Detect the validation mode for the XML document in the supplied InputStream.
private  int endComment(java.lang.String line)
           
private  boolean hasDoctype(java.lang.String content)
          Does the content contain the the DTD DOCTYPE declaration?
private  boolean hasOpeningTag(java.lang.String content)
          Does the supplied content contain an XML opening tag.
private  int startComment(java.lang.String line)
          Try to consume the START_COMMENT token.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VALIDATION_NONE

public static final int VALIDATION_NONE
Indicates that the validation should be disabled.

See Also:
Constant Field Values

VALIDATION_AUTO

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).

See Also:
Constant Field Values

VALIDATION_DTD

public static final int VALIDATION_DTD
Indicates that DTD validation should be used (we found a "DOCTYPE" declaration).

See Also:
Constant Field Values

VALIDATION_XSD

public static final int VALIDATION_XSD
Indicates that XSD validation should be used (found no "DOCTYPE" declaration).

See Also:
Constant Field Values

DOCTYPE

private static final java.lang.String DOCTYPE
The token in a XML document that declares the DTD to use for validation and thus that DTD validation is being used.

See Also:
Constant Field Values

START_COMMENT

private static final java.lang.String START_COMMENT
The token that indicates the start of an XML comment.

See Also:
Constant Field Values

END_COMMENT

private static final java.lang.String END_COMMENT
The token that indicates the end of an XML comment.

See Also:
Constant Field Values

inComment

private boolean inComment
Indicates whether or not the current parse position is inside an XML comment.

Constructor Detail

XmlValidationModeDetector

public XmlValidationModeDetector()
Method Detail

detectValidationMode

public int detectValidationMode(java.io.InputStream inputStream)
                         throws java.io.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 - the InputStream to parse
Throws:
java.io.IOException - in case of I/O failure
See Also:
VALIDATION_DTD, VALIDATION_XSD

hasDoctype

private boolean hasDoctype(java.lang.String content)
Does the content contain the the DTD DOCTYPE declaration?


hasOpeningTag

private boolean hasOpeningTag(java.lang.String content)
Does the supplied content contain an XML opening tag. If the parse state is currently in an XML comment then this method always returns false. It is expected that all comment tokens will have consumed for the supplied content before passing the remainder to this method.


consumeCommentTokens

private java.lang.String consumeCommentTokens(java.lang.String line)
Consumes all the leading comment data in the given String and returns the remaining content, which may be empty since the supplied content might be all comment data. For our purposes it is only important to strip leading comment content on a line since the first piece of non comment content will be either the DOCTYPE declaration or the root element of the document.


consume

private java.lang.String consume(java.lang.String line)
Consume the next comment token, update the "inComment" flag and return the remaining content.


startComment

private int startComment(java.lang.String line)
Try to consume the START_COMMENT token.

See Also:
commentToken(String, String, boolean)

endComment

private int endComment(java.lang.String line)

commentToken

private int commentToken(java.lang.String line,
                         java.lang.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.