001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.util.xml;
018:
019: import java.io.BufferedReader;
020: import java.io.CharConversionException;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024:
025: import org.springframework.util.StringUtils;
026:
027: /**
028: * Detects whether an XML stream is using DTD- or XSD-based validation.
029: *
030: * @author Rob Harrop
031: * @author Juergen Hoeller
032: * @since 2.0
033: */
034: public class XmlValidationModeDetector {
035:
036: /**
037: * Indicates that the validation mode should be auto-guessed, since we cannot find
038: * a clear indication (probably choked on some special characters, or the like).
039: */
040: public static final int VALIDATION_AUTO = 1;
041:
042: /**
043: * Indicates that DTD validation should be used (we found a "DOCTYPE" declaration).
044: */
045: public static final int VALIDATION_DTD = 2;
046:
047: /**
048: * Indicates that XSD validation should be used (found no "DOCTYPE" declaration).
049: */
050: public static final int VALIDATION_XSD = 3;
051:
052: /**
053: * The token in a XML document that declares the DTD to use for validation
054: * and thus that DTD validation is being used.
055: */
056: private static final String DOCTYPE = "DOCTYPE";
057:
058: /**
059: * The token that indicates the start of an XML comment.
060: */
061: private static final String START_COMMENT = "<!--";
062:
063: /**
064: * The token that indicates the end of an XML comment.
065: */
066: private static final String END_COMMENT = "-->";
067:
068: /**
069: * Indicates whether or not the current parse position is inside an XML comment.
070: */
071: private boolean inComment;
072:
073: /**
074: * Detect the validation mode for the XML document in the supplied {@link InputStream}.
075: * Note that the supplied {@link InputStream} is closed by this method before returning.
076: * @param inputStream the InputStream to parse
077: * @throws IOException in case of I/O failure
078: * @see #VALIDATION_DTD
079: * @see #VALIDATION_XSD
080: */
081: public int detectValidationMode(InputStream inputStream)
082: throws IOException {
083: // Peek into the file to look for DOCTYPE.
084: BufferedReader reader = new BufferedReader(
085: new InputStreamReader(inputStream));
086: try {
087: boolean isDtdValidated = false;
088: String content;
089: while ((content = reader.readLine()) != null) {
090: content = consumeCommentTokens(content);
091: if (this .inComment || !StringUtils.hasText(content)) {
092: continue;
093: }
094: if (hasDoctype(content)) {
095: isDtdValidated = true;
096: break;
097: }
098: if (hasOpeningTag(content)) {
099: // End of meaningful data...
100: break;
101: }
102: }
103: return (isDtdValidated ? VALIDATION_DTD : VALIDATION_XSD);
104: } catch (CharConversionException ex) {
105: // Choked on some character encoding...
106: // Leave the decision up to the caller.
107: return VALIDATION_AUTO;
108: } finally {
109: reader.close();
110: }
111: }
112:
113: /**
114: * Does the content contain the the DTD DOCTYPE declaration?
115: */
116: private boolean hasDoctype(String content) {
117: return (content.indexOf(DOCTYPE) > -1);
118: }
119:
120: /**
121: * Does the supplied content contain an XML opening tag. If the parse state is currently
122: * in an XML comment then this method always returns false. It is expected that all comment
123: * tokens will have consumed for the supplied content before passing the remainder to this method.
124: */
125: private boolean hasOpeningTag(String content) {
126: if (this .inComment) {
127: return false;
128: }
129: int openTagIndex = content.indexOf('<');
130: return (openTagIndex > -1 && content.length() > openTagIndex && Character
131: .isLetter(content.charAt(openTagIndex + 1)));
132: }
133:
134: /**
135: * Consumes all the leading comment data in the given String and returns the remaining content, which
136: * may be empty since the supplied content might be all comment data. For our purposes it is only important
137: * to strip leading comment content on a line since the first piece of non comment content will be either
138: * the DOCTYPE declaration or the root element of the document.
139: */
140: private String consumeCommentTokens(String line) {
141: if (line.indexOf(START_COMMENT) == -1
142: && line.indexOf(END_COMMENT) == -1) {
143: return line;
144: }
145: while ((line = consume(line)) != null) {
146: if (!this .inComment
147: && !line.trim().startsWith(START_COMMENT)) {
148: return line;
149: }
150: }
151: return line;
152: }
153:
154: /**
155: * Consume the next comment token, update the "inComment" flag
156: * and return the remaining content.
157: */
158: private String consume(String line) {
159: int index = (this .inComment ? endComment(line)
160: : startComment(line));
161: return (index == -1 ? null : line.substring(index));
162: }
163:
164: /**
165: * Try to consume the {@link #START_COMMENT} token.
166: * @see #commentToken(String, String, boolean)
167: */
168: private int startComment(String line) {
169: return commentToken(line, START_COMMENT, true);
170: }
171:
172: private int endComment(String line) {
173: return commentToken(line, END_COMMENT, false);
174: }
175:
176: /**
177: * Try to consume the supplied token against the supplied content and update the
178: * in comment parse state to the supplied value. Returns the index into the content
179: * which is after the token or -1 if the token is not found.
180: */
181: private int commentToken(String line, String token,
182: boolean inCommentIfPresent) {
183: int index = line.indexOf(token);
184: if (index > -1) {
185: this .inComment = inCommentIfPresent;
186: }
187: return (index == -1 ? index : index + token.length());
188: }
189:
190: }
|