001    package fi.iki.hsivonen.verifierservlet;
002    
003    import java.io.IOException;
004    
005    import javax.servlet.ServletException;
006    import javax.servlet.http.HttpServletRequest;
007    import javax.servlet.http.HttpServletResponse;
008    
009    import org.xml.sax.SAXException;
010    import org.xml.sax.SAXNotRecognizedException;
011    import org.xml.sax.SAXNotSupportedException;
012    import org.xml.sax.SAXParseException;
013    
014    import com.thaiopensource.validate.IncorrectSchemaException;
015    
016    import fi.iki.hsivonen.htmlparser.DoctypeHandler;
017    import fi.iki.hsivonen.htmlparser.HtmlParser;
018    import fi.iki.hsivonen.xml.TypedInputSource;
019    
020    public class Html5ConformanceCheckerTransaction extends
021            VerifierServletTransaction {
022    
023        private static final char[] SERVICE_TITLE = "(X)HTML5 Conformance Checking Service ".toCharArray();
024    
025        private static final char[] TECHNOLOGY_PREVIEW = "Technology Preview".toCharArray();
026    
027        private static final char[] RESULTS_TITLE = "(X)HTML5 conformance checking results for ".toCharArray();
028    
029        private static final char[] SUCCESS_HTML = "The document conforms to the machine-checkable conformance requirements for HTML5 (subject to the utter previewness of this service).".toCharArray();
030    
031        private static final char[] SUCCESS_XHTML = "The document conforms to the machine-checkable conformance requirements for XHTML5 (subject to the utter previewness of this service).".toCharArray();
032    
033        private static final char[] FAILURE_HTML = "There were errors. (Tried in the text/html mode.)".toCharArray();
034    
035        private static final char[] FAILURE_XHTML = "There were errors. (Tried in the XHTML mode.)".toCharArray();
036    
037        private boolean usingHtml = false;
038        
039        public Html5ConformanceCheckerTransaction(HttpServletRequest request,
040                HttpServletResponse response) {
041            super(request, response);
042        }
043    
044        /**
045         * @see fi.iki.hsivonen.verifierservlet.VerifierServletTransaction#emitSuccess()
046         */
047        protected void emitSuccess() throws SAXException {
048            if (usingHtml) {
049                emitter.characters(SUCCESS_HTML);
050            } else {
051                emitter.characters(SUCCESS_XHTML);
052            }
053        }
054    
055        /**
056         * @see fi.iki.hsivonen.verifierservlet.VerifierServletTransaction#loadDocAndSetupParser()
057         */
058        protected void loadDocAndSetupParser() throws SAXException, IOException, IncorrectSchemaException, SAXNotRecognizedException, SAXNotSupportedException {
059            httpRes.setAllowGenericXml(false);
060            httpRes.setAcceptAllKnownXmlTypes(false);
061            httpRes.setAllowHtml(true);
062            httpRes.setAllowXhtml(true);
063            documentInput = (TypedInputSource) entityResolver.resolveEntity(
064                    null, document);
065            String type = documentInput.getType();
066            if ("text/html".equals(type)) {
067                validator = validatorByDoctype(DoctypeHandler.DOCTYPE_HTML5);
068                usingHtml = true;
069                htmlParser = new HtmlParser();
070                htmlParser.setDoctypeMode(DoctypeHandler.DOCTYPE_HTML5);
071                htmlParser.setDoctypeHandler(this);
072                htmlParser.setContentHandler(validator.getContentHandler());
073                reader = htmlParser;
074            } else {
075                validator = validatorByDoctype(XHTML5_SCHEMA);
076                reader = setupXmlParser();
077                if (!("application/xhtml+xml".equals(type) || "application/xml".equals(type))) {
078                    String message = "The preferred Content-Type for XHTML5 is application/xhtml+xml. The Content-Type was " + type + ".";
079                    SAXParseException spe = new SAXParseException(message, null, documentInput.getSystemId(), -1, -1);
080                    errorHandler.warning(spe);
081                }
082            }
083    
084        }
085    
086        /**
087         * @see fi.iki.hsivonen.verifierservlet.VerifierServletTransaction#setupAndStartEmission()
088         */
089        protected void setup() throws ServletException {
090            // No-op
091        }
092    
093        /**
094         * @see fi.iki.hsivonen.verifierservlet.VerifierServletTransaction#emitTitle()
095         */
096        void emitTitle(boolean markupAllowed) throws SAXException {
097            if (willValidate()) {
098                emitter.characters(RESULTS_TITLE);
099                emitter.characters(scrub(document));
100            } else {
101                emitter.characters(SERVICE_TITLE);
102                if (markupAllowed) {
103                    emitter.startElement("span");
104                    emitter.characters(TECHNOLOGY_PREVIEW);
105                    emitter.endElement("span");
106                }
107            }
108        }
109    
110        /**
111         * @see fi.iki.hsivonen.verifierservlet.VerifierServletTransaction#tryToSetupValidator()
112         */
113        protected void tryToSetupValidator() throws SAXException, IOException, IncorrectSchemaException {
114            // No-op
115        }
116    
117        /**
118         * @see fi.iki.hsivonen.verifierservlet.VerifierServletTransaction#emitFailure()
119         */
120        protected void emitFailure() throws SAXException {
121            if (usingHtml) {
122                emitter.characters(FAILURE_HTML);
123            } else {
124                emitter.characters(FAILURE_XHTML);
125            }
126        }
127    
128        /**
129         * @see fi.iki.hsivonen.verifierservlet.VerifierServletTransaction#emitFormContent()
130         */
131        protected void emitFormContent() throws SAXException {
132            Html5FormEmitter.emit(contentHandler, this);
133        }
134    
135        /**
136         * @see fi.iki.hsivonen.verifierservlet.VerifierServletTransaction#doctype(int)
137         */
138        public void doctype(int doctype) throws SAXException {
139            // No-op
140        }
141    
142    }