001 /*
002 * Copyright (c) 2005, 2006 Henri Sivonen
003 * Copyright (c) 2007 Mozilla Foundation
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a
006 * copy of this software and associated documentation files (the "Software"),
007 * to deal in the Software without restriction, including without limitation
008 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
009 * and/or sell copies of the Software, and to permit persons to whom the
010 * Software is furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
018 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
020 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
021 * DEALINGS IN THE SOFTWARE.
022 */
023
024 package nu.validator.servlet;
025
026 import java.io.IOException;
027
028 import javax.servlet.ServletException;
029 import javax.servlet.http.HttpServletRequest;
030 import javax.servlet.http.HttpServletResponse;
031
032 import nu.validator.htmlparser.common.DoctypeExpectation;
033 import nu.validator.htmlparser.common.XmlViolationPolicy;
034 import nu.validator.htmlparser.sax.HtmlParser;
035
036 import org.xml.sax.SAXException;
037 import org.xml.sax.SAXNotRecognizedException;
038 import org.xml.sax.SAXNotSupportedException;
039 import org.xml.sax.SAXParseException;
040
041 import com.thaiopensource.validate.IncorrectSchemaException;
042
043
044 public class Html5ConformanceCheckerTransaction extends
045 VerifierServletTransaction {
046
047 private static final char[] SERVICE_TITLE = "(X)HTML5 Conformance Checking Service ".toCharArray();
048
049 private static final char[] TECHNOLOGY_PREVIEW = "Technology Preview".toCharArray();
050
051 private static final char[] RESULTS_TITLE = "(X)HTML5 conformance checking results".toCharArray();
052
053 private static final char[] FOR = " for ".toCharArray();
054
055 private static final String SUCCESS_HTML = "The document conforms to the machine-checkable conformance requirements for HTML5 (subject to the utter previewness of this service).";
056
057 private static final String SUCCESS_XHTML = "The document conforms to the machine-checkable conformance requirements for XHTML5 (subject to the utter previewness of this service).";
058
059 private static final String FAILURE_HTML = "There were errors. (Tried in the text/html mode.)";
060
061 private static final String FAILURE_XHTML = "There were errors. (Tried in the XHTML mode.)";
062
063 private boolean usingHtml = false;
064
065 public Html5ConformanceCheckerTransaction(HttpServletRequest request,
066 HttpServletResponse response) {
067 super(request, response);
068 }
069
070 /**
071 * @see nu.validator.servlet.VerifierServletTransaction#successMessage()
072 */
073 protected String successMessage() throws SAXException {
074 if (usingHtml) {
075 return SUCCESS_HTML;
076 } else {
077 return SUCCESS_XHTML;
078 }
079 }
080
081 /**
082 * @see nu.validator.servlet.VerifierServletTransaction#loadDocAndSetupParser()
083 */
084 protected void loadDocAndSetupParser() throws SAXException, IOException, IncorrectSchemaException, SAXNotRecognizedException, SAXNotSupportedException {
085 setAllowGenericXml(false);
086 setAcceptAllKnownXmlTypes(false);
087 setAllowHtml(true);
088 setAllowXhtml(true);
089 loadDocumentInput();
090 String type = documentInput.getType();
091 if ("text/html".equals(type)) {
092 validator = validatorByDoctype(HTML5_SCHEMA);
093 usingHtml = true;
094 newHtmlParser();
095 htmlParser.setDoctypeExpectation(DoctypeExpectation.HTML);
096 htmlParser.setDocumentModeHandler(this);
097 htmlParser.setContentHandler(validator.getContentHandler());
098 reader = htmlParser;
099 } else {
100 validator = validatorByDoctype(XHTML5_SCHEMA);
101 setupXmlParser();
102 if (!("application/xhtml+xml".equals(type) || "application/xml".equals(type))) {
103 String message = "The preferred Content-Type for XHTML5 is application/xhtml+xml. The Content-Type was " + type + ".";
104 SAXParseException spe = new SAXParseException(message, null, documentInput.getSystemId(), -1, -1);
105 errorHandler.warning(spe);
106 }
107 }
108
109 }
110
111 /**
112 * @see nu.validator.servlet.VerifierServletTransaction#setupAndStartEmission()
113 */
114 protected void setup() throws ServletException {
115 // No-op
116 }
117
118 /**
119 * @see nu.validator.servlet.VerifierServletTransaction#emitTitle()
120 */
121 void emitTitle(boolean markupAllowed) throws SAXException {
122 if (willValidate()) {
123 emitter.characters(RESULTS_TITLE);
124 if (document != null) {
125 emitter.characters(FOR);
126 emitter.characters(scrub(document));
127 }
128 } else {
129 emitter.characters(SERVICE_TITLE);
130 if (markupAllowed) {
131 emitter.startElement("span");
132 emitter.characters(TECHNOLOGY_PREVIEW);
133 emitter.endElement("span");
134 }
135 }
136 }
137
138 /**
139 * @see nu.validator.servlet.VerifierServletTransaction#tryToSetupValidator()
140 */
141 protected void tryToSetupValidator() throws SAXException, IOException, IncorrectSchemaException {
142 // No-op
143 }
144
145 /**
146 * @see nu.validator.servlet.VerifierServletTransaction#failureMessage()
147 */
148 protected String failureMessage() throws SAXException {
149 if (usingHtml) {
150 return FAILURE_HTML;
151 } else {
152 return FAILURE_XHTML;
153 }
154 }
155
156 /**
157 * @see nu.validator.servlet.VerifierServletTransaction#emitFormContent()
158 */
159 protected void emitFormContent() throws SAXException {
160 Html5FormEmitter.emit(contentHandler, this);
161 }
162
163 /**
164 * @see nu.validator.servlet.VerifierServletTransaction#doctype(int)
165 */
166 public void doctype(int doctype) throws SAXException {
167 // No-op
168 }
169
170 }