Source Code Cross Referenced for XmlValidationModeDetector.java in  » J2EE » spring-framework-2.0.6 » org » springframework » util » xml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.0.6 » org.springframework.util.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.