Search in sources :

Example 1 with HtmlSerializer

use of nu.validator.htmlparser.sax.HtmlSerializer in project validator by validator.

the class Statistics method writeToResponse.

public void writeToResponse(HttpServletResponse response) throws IOException {
    try {
        long totalCopy;
        long[] countersCopy = new long[counters.length];
        synchronized (this) {
            totalCopy = total;
            System.arraycopy(counters, 0, countersCopy, 0, counters.length);
        }
        double totalDouble = totalCopy;
        double uptimeMillis = System.currentTimeMillis() - startTime;
        response.setContentType("text/html; charset=utf-8");
        ContentHandler ch = new HtmlSerializer(response.getOutputStream());
        try {
            ch.startDocument();
            startElement(ch, "html");
            startElement(ch, "head");
            startElement(ch, "title");
            characters(ch, VALIDATOR_STATISTICS);
            endElement(ch, "title");
            startElement(ch, "style");
            characters(ch, STYLESHEET);
            endElement(ch, "style");
            endElement(ch, "head");
            startElement(ch, "body");
            startElement(ch, "h1");
            characters(ch, VALIDATOR_STATISTICS);
            endElement(ch, "h1");
            startElement(ch, "dl");
            startElement(ch, "dt");
            characters(ch, TOTAL_VALIDATIONS);
            endElement(ch, "dt");
            startElement(ch, "dd");
            characters(ch, totalCopy);
            endElement(ch, "dd");
            startElement(ch, "dt");
            characters(ch, UPTIME_DAYS);
            endElement(ch, "dt");
            startElement(ch, "dd");
            characters(ch, uptimeMillis / (1000 * 60 * 60 * 24));
            endElement(ch, "dd");
            startElement(ch, "dt");
            characters(ch, VALIDATIONS_PER_SECOND);
            endElement(ch, "dt");
            startElement(ch, "dd");
            characters(ch, totalDouble / (uptimeMillis / 1000.0));
            endElement(ch, "dd");
            endElement(ch, "dl");
            startElement(ch, "table");
            startElement(ch, "thead");
            startElement(ch, "tr");
            startElement(ch, "th");
            characters(ch, COUNTER_NAME);
            endElement(ch, "th");
            startElement(ch, "th");
            characters(ch, COUNTER_VALUE);
            endElement(ch, "th");
            startElement(ch, "th");
            characters(ch, COUNTER_PROPORTION);
            endElement(ch, "th");
            endElement(ch, "tr");
            endElement(ch, "thead");
            startElement(ch, "tbody");
            for (int i = 0; i < countersCopy.length; i++) {
                long count = countersCopy[i];
                startElement(ch, "tr");
                startElement(ch, "td");
                characters(ch, Field.values()[i].toString());
                endElement(ch, "td");
                startElement(ch, "td");
                characters(ch, count);
                endElement(ch, "td");
                startElement(ch, "td");
                characters(ch, count / totalDouble);
                endElement(ch, "td");
                endElement(ch, "tr");
            }
            endElement(ch, "tbody");
            endElement(ch, "table");
            startElement(ch, "script");
            characters(ch, SORT_LANGS_SCRIPT);
            endElement(ch, "script");
            endElement(ch, "body");
            endElement(ch, "html");
        } finally {
            ch.endDocument();
        }
    } catch (SAXException e) {
        throw new IOException(e);
    }
}
Also used : HtmlSerializer(nu.validator.htmlparser.sax.HtmlSerializer) IOException(java.io.IOException) ContentHandler(org.xml.sax.ContentHandler) SAXException(org.xml.sax.SAXException)

Example 2 with HtmlSerializer

use of nu.validator.htmlparser.sax.HtmlSerializer in project whole by wholeplatform.

the class HtmlPrettyPrinterVisitor method visit.

public void visit(IEntity entity) {
    IEntity xhtmlDocument = BehaviorUtils.apply("whole:org.whole.lang.html:HTML5Semantics#toXHtml", entity);
    HtmlSerializer serializer = new HtmlSerializer(out.asWriter());
    SaxHandlerBuilderOperation sbop = new SaxHandlerBuilderOperation(serializer, serializer, "http://www.w3.org/1999/xhtml");
    if (xhtmlDocument != null)
        new ModelTemplate(xhtmlDocument).apply(sbop);
}
Also used : ModelTemplate(org.whole.lang.templates.ModelTemplate) IEntity(org.whole.lang.model.IEntity) HtmlSerializer(nu.validator.htmlparser.sax.HtmlSerializer) SaxHandlerBuilderOperation(org.whole.lang.xml.util.SaxHandlerBuilderOperation)

Example 3 with HtmlSerializer

use of nu.validator.htmlparser.sax.HtmlSerializer in project validator by validator.

the class VerifierServletTransaction method service.

void service() throws ServletException, IOException {
    this.methodIsGet = "GET".equals(request.getMethod()) || "HEAD".equals(request.getMethod());
    this.out = response.getOutputStream();
    try {
        request.setCharacterEncoding("utf-8");
    } catch (NoSuchMethodError e) {
        log4j.debug("Vintage Servlet API doesn't support setCharacterEncoding().", e);
    }
    if (!methodIsGet) {
        postContentType = request.getContentType();
        if (postContentType == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Type missing");
            return;
        } else if (postContentType.trim().toLowerCase().startsWith("application/x-www-form-urlencoded")) {
            response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "application/x-www-form-urlencoded not supported. Please use multipart/form-data.");
            return;
        }
    }
    String outFormat = request.getParameter("out");
    if (outFormat == null) {
        outputFormat = OutputFormat.HTML;
    } else {
        if ("html".equals(outFormat)) {
            outputFormat = OutputFormat.HTML;
        } else if ("xhtml".equals(outFormat)) {
            outputFormat = OutputFormat.XHTML;
        } else if ("text".equals(outFormat)) {
            outputFormat = OutputFormat.TEXT;
        } else if ("gnu".equals(outFormat)) {
            outputFormat = OutputFormat.GNU;
        } else if ("xml".equals(outFormat)) {
            outputFormat = OutputFormat.XML;
        } else if ("json".equals(outFormat)) {
            outputFormat = OutputFormat.JSON;
        } else {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unsupported output format");
            return;
        }
    }
    if (!methodIsGet) {
        document = request.getHeader("Content-Location");
    }
    if (document == null) {
        document = request.getParameter("doc");
    }
    if (document == null) {
        document = request.getParameter("file");
    }
    document = ("".equals(document)) ? null : document;
    if (document != null) {
        for (String domain : DENY_LIST) {
            if (!"".equals(domain) && document.contains(domain)) {
                response.sendError(429, "Too many requests");
                return;
            }
        }
    }
    String callback = null;
    if (outputFormat == OutputFormat.JSON) {
        callback = request.getParameter("callback");
        if (callback != null) {
            Matcher m = JS_IDENTIFIER.matcher(callback);
            if (m.matches()) {
                if (Arrays.binarySearch(JS_RESERVED_WORDS, callback) >= 0) {
                    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Callback is a reserved word.");
                    return;
                }
            } else {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Callback is not a valid ECMA 262 IdentifierName.");
                return;
            }
        }
    }
    if (willValidate()) {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
    } else if (outputFormat == OutputFormat.HTML || outputFormat == OutputFormat.XHTML) {
        response.setDateHeader("Last-Modified", lastModified);
    } else {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No input document");
        return;
    }
    setup();
    String filterString = systemFilterString;
    String filterPatternParam = request.getParameter("filterpattern");
    if (filterPatternParam != null && !"".equals(filterPatternParam)) {
        if ("".equals(filterString)) {
            filterString = scrub(filterPatternParam);
        } else {
            filterString += "|" + scrub(filterPatternParam);
        }
    }
    String filterUrl = request.getParameter("filterurl");
    if (filterUrl != null && !"".equals(filterUrl)) {
        try {
            // 
            InputSource filterFile = // 
            (new PrudentHttpEntityResolver(-1, true, null)).resolveEntity(null, filterUrl);
            StringBuilder sb = new StringBuilder();
            // 
            BufferedReader reader = new BufferedReader(new InputStreamReader(filterFile.getByteStream()));
            String line;
            String pipe = "";
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("#")) {
                    continue;
                }
                sb.append(pipe);
                sb.append(line);
                pipe = "|";
            }
            if (sb.length() != 0) {
                if (!"".equals(filterString)) {
                    filterString = scrub(sb.toString());
                } else {
                    filterString += "|" + scrub(sb.toString());
                }
            }
        } catch (Exception e) {
            response.sendError(500, e.getMessage());
        }
    }
    Pattern filterPattern = null;
    if (!"".equals(filterString)) {
        filterPattern = Pattern.compile(filterString);
    }
    if (request.getParameter("useragent") != null) {
        userAgent = scrub(request.getParameter("useragent"));
    } else {
        userAgent = USER_AGENT;
    }
    if (request.getParameter("acceptlanguage") != null) {
        request.setAttribute("http://validator.nu/properties/accept-language", scrub(request.getParameter("acceptlanguage")));
    }
    Object inputType = request.getAttribute("nu.validator.servlet.MultipartFormDataFilter.type");
    showSource = (request.getParameter("showsource") != null);
    showSource = (showSource || "textarea".equals(inputType));
    showOutline = (request.getParameter("showoutline") != null);
    if (request.getParameter("checkerrorpages") != null) {
        request.setAttribute("http://validator.nu/properties/ignore-response-status", true);
    }
    if (request.getParameter("showimagereport") != null) {
        imageCollector = new ImageCollector(sourceCode);
    }
    String charset = request.getParameter("charset");
    if (charset != null) {
        charset = scrub(charset.trim());
        if (!"".equals(charset)) {
            charsetOverride = charset;
        }
    }
    String nsfilter = request.getParameter("nsfilter");
    if (nsfilter != null) {
        for (String ns : SPACE.split(nsfilter)) {
            if (ns.length() > 0) {
                filteredNamespaces.add(ns);
            }
        }
    }
    boolean errorsOnly = ("error".equals(request.getParameter("level")));
    boolean asciiQuotes = (request.getParameter("asciiquotes") != null);
    int lineOffset = 0;
    String lineOffsetStr = request.getParameter("lineoffset");
    if (lineOffsetStr != null) {
        try {
            lineOffset = Integer.parseInt(lineOffsetStr);
        } catch (NumberFormatException e) {
        }
    }
    try {
        if (outputFormat == OutputFormat.HTML || outputFormat == OutputFormat.XHTML) {
            if (outputFormat == OutputFormat.HTML) {
                response.setContentType("text/html; charset=utf-8");
                contentHandler = new HtmlSerializer(out);
            } else {
                response.setContentType("application/xhtml+xml");
                contentHandler = new XmlSerializer(out);
            }
            emitter = new XhtmlSaxEmitter(contentHandler);
            errorHandler = new MessageEmitterAdapter(filterPattern, sourceCode, showSource, imageCollector, lineOffset, false, new XhtmlMessageEmitter(contentHandler));
            PageEmitter.emit(contentHandler, this);
        } else {
            if (outputFormat == OutputFormat.TEXT) {
                response.setContentType("text/plain; charset=utf-8");
                errorHandler = new MessageEmitterAdapter(filterPattern, sourceCode, showSource, null, lineOffset, false, new TextMessageEmitter(out, asciiQuotes));
            } else if (outputFormat == OutputFormat.GNU) {
                response.setContentType("text/plain; charset=utf-8");
                errorHandler = new MessageEmitterAdapter(filterPattern, sourceCode, showSource, null, lineOffset, false, new GnuMessageEmitter(out, asciiQuotes));
            } else if (outputFormat == OutputFormat.XML) {
                response.setContentType("application/xml");
                errorHandler = new MessageEmitterAdapter(filterPattern, sourceCode, showSource, null, lineOffset, false, new XmlMessageEmitter(new XmlSerializer(out)));
            } else if (outputFormat == OutputFormat.JSON) {
                if (callback == null) {
                    response.setContentType("application/json; charset=utf-8");
                } else {
                    response.setContentType("application/javascript; charset=utf-8");
                }
                errorHandler = new MessageEmitterAdapter(filterPattern, sourceCode, showSource, null, lineOffset, false, new JsonMessageEmitter(new nu.validator.json.Serializer(out), callback));
            } else {
                throw new RuntimeException("Unreachable.");
            }
            errorHandler.setErrorsOnly(errorsOnly);
            validate();
        }
    } catch (SAXException e) {
        log4j.debug("SAXException: " + e.getMessage());
    }
}
Also used : Matcher(java.util.regex.Matcher) MessageEmitterAdapter(nu.validator.messages.MessageEmitterAdapter) PrudentHttpEntityResolver(nu.validator.xml.PrudentHttpEntityResolver) TextMessageEmitter(nu.validator.messages.TextMessageEmitter) FatalSAXException(nu.validator.gnu.xml.aelfred2.FatalSAXException) SAXException(org.xml.sax.SAXException) GnuMessageEmitter(nu.validator.messages.GnuMessageEmitter) ImageCollector(nu.validator.servlet.imagereview.ImageCollector) XhtmlSaxEmitter(nu.validator.xml.XhtmlSaxEmitter) XmlSerializer(nu.validator.htmlparser.sax.XmlSerializer) HtmlSerializer(nu.validator.htmlparser.sax.HtmlSerializer) XhtmlMessageEmitter(nu.validator.messages.XhtmlMessageEmitter) Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) StreamBoundException(nu.validator.io.StreamBoundException) TooManyErrorsException(nu.validator.messages.TooManyErrorsException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) NonXmlContentTypeException(nu.validator.xml.ContentTypeParser.NonXmlContentTypeException) IOException(java.io.IOException) FatalSAXException(nu.validator.gnu.xml.aelfred2.FatalSAXException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) ServletException(javax.servlet.ServletException) SAXException(org.xml.sax.SAXException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IncorrectSchemaException(com.thaiopensource.validate.IncorrectSchemaException) ResourceNotRetrievableException(nu.validator.xml.PrudentHttpEntityResolver.ResourceNotRetrievableException) SAXParseException(org.xml.sax.SAXParseException) HtmlSerializer(nu.validator.htmlparser.sax.HtmlSerializer) BufferedReader(java.io.BufferedReader) XmlMessageEmitter(nu.validator.messages.XmlMessageEmitter) JsonMessageEmitter(nu.validator.messages.JsonMessageEmitter) XmlSerializer(nu.validator.htmlparser.sax.XmlSerializer)

Aggregations

HtmlSerializer (nu.validator.htmlparser.sax.HtmlSerializer)3 IOException (java.io.IOException)2 SAXException (org.xml.sax.SAXException)2 IncorrectSchemaException (com.thaiopensource.validate.IncorrectSchemaException)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 ServletException (javax.servlet.ServletException)1 FatalSAXException (nu.validator.gnu.xml.aelfred2.FatalSAXException)1 XmlSerializer (nu.validator.htmlparser.sax.XmlSerializer)1 StreamBoundException (nu.validator.io.StreamBoundException)1 GnuMessageEmitter (nu.validator.messages.GnuMessageEmitter)1 JsonMessageEmitter (nu.validator.messages.JsonMessageEmitter)1 MessageEmitterAdapter (nu.validator.messages.MessageEmitterAdapter)1 TextMessageEmitter (nu.validator.messages.TextMessageEmitter)1 TooManyErrorsException (nu.validator.messages.TooManyErrorsException)1 XhtmlMessageEmitter (nu.validator.messages.XhtmlMessageEmitter)1 XmlMessageEmitter (nu.validator.messages.XmlMessageEmitter)1