Search in sources :

Example 1 with XmlSerializer

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

the class SimpleCommandLineValidator method setErrorHandler.

private static void setErrorHandler() {
    SourceCode sourceCode = validator.getSourceCode();
    ImageCollector imageCollector = new ImageCollector(sourceCode);
    boolean showSource = false;
    if (outputFormat == OutputFormat.TEXT) {
        errorHandler = new MessageEmitterAdapter(filterPattern, sourceCode, showSource, imageCollector, lineOffset, true, new TextMessageEmitter(out, asciiQuotes));
    } else if (outputFormat == OutputFormat.GNU) {
        errorHandler = new MessageEmitterAdapter(filterPattern, sourceCode, showSource, imageCollector, lineOffset, true, new GnuMessageEmitter(out, asciiQuotes));
    } else if (outputFormat == OutputFormat.XML) {
        errorHandler = new MessageEmitterAdapter(filterPattern, sourceCode, showSource, imageCollector, lineOffset, true, new XmlMessageEmitter(new XmlSerializer(out)));
    } else if (outputFormat == OutputFormat.JSON) {
        String callback = null;
        errorHandler = new MessageEmitterAdapter(filterPattern, sourceCode, showSource, imageCollector, lineOffset, true, new JsonMessageEmitter(new nu.validator.json.Serializer(out), callback));
    } else {
        throw new RuntimeException("Bug. Should be unreachable.");
    }
    errorHandler.setErrorsOnly(errorsOnly);
}
Also used : SourceCode(nu.validator.source.SourceCode) MessageEmitterAdapter(nu.validator.messages.MessageEmitterAdapter) TextMessageEmitter(nu.validator.messages.TextMessageEmitter) GnuMessageEmitter(nu.validator.messages.GnuMessageEmitter) ImageCollector(nu.validator.servlet.imagereview.ImageCollector) XmlMessageEmitter(nu.validator.messages.XmlMessageEmitter) JsonMessageEmitter(nu.validator.messages.JsonMessageEmitter) XmlSerializer(nu.validator.htmlparser.sax.XmlSerializer) XmlSerializer(nu.validator.htmlparser.sax.XmlSerializer)

Example 2 with XmlSerializer

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

the class ParserPerfHarness method main.

/**
 * @param args
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
    boolean html = "h".equals(args[0]);
    long duration = Long.parseLong(args[1]) * 60000L;
    String path = args[2];
    char[] testData = loadFileIntoArray(new File(path));
    XmlSerializer ch = new XmlSerializer(new NullWriter());
    XMLReader reader = null;
    if (html) {
        HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALLOW);
        parser.setContentHandler(ch);
        parser.setStreamabilityViolationPolicy(XmlViolationPolicy.FATAL);
        reader = parser;
    } else {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(false);
        reader = factory.newSAXParser().getXMLReader();
        reader.setContentHandler(ch);
        reader.setEntityResolver(new NullEntityResolver());
    }
    System.out.println("Warmup:");
    System.out.println((new ParserPerfHarness(System.currentTimeMillis() + duration, reader, testData)).runLoop());
    System.gc();
    System.out.println("Real:");
    System.out.println((new ParserPerfHarness(System.currentTimeMillis() + duration, reader, testData)).runLoop());
}
Also used : HtmlParser(nu.validator.htmlparser.sax.HtmlParser) NullEntityResolver(nu.validator.xml.NullEntityResolver) File(java.io.File) XMLReader(org.xml.sax.XMLReader) XmlSerializer(nu.validator.htmlparser.sax.XmlSerializer) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 3 with XmlSerializer

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

the class ParserPerfHarnessNew method main.

/**
 * @param args
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
    long duration = Long.parseLong(args[1]) * 60000L;
    String path = args[2];
    char[] testData = loadFileIntoArray(new File(path));
    XmlSerializer ch = new XmlSerializer(new NullWriter());
    XMLReader reader = null;
    if ("h".equals(args[0])) {
        HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALLOW);
        parser.setContentHandler(ch);
        parser.setStreamabilityViolationPolicy(XmlViolationPolicy.FATAL);
        reader = parser;
    } else if ("t".equals(args[0])) {
        Driver driver = new Driver(new TokensToSax(ch));
        driver.setContentNonXmlCharPolicy(XmlViolationPolicy.ALLOW);
        driver.setContentSpacePolicy(XmlViolationPolicy.ALLOW);
        driver.setNamePolicy(XmlViolationPolicy.ALLOW);
        driver.setXmlnsPolicy(XmlViolationPolicy.ALLOW);
        reader = new DriverWrapper(driver);
    } else if ("a".equals(args[0])) {
        reader = new SAXDriver();
        reader.setFeature("http://xml.org/sax/features/namespaces", true);
        reader.setFeature("http://xml.org/sax/features/validation", false);
        reader.setFeature("http://xml.org/sax/features/string-interning", true);
        reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
        reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        reader.setContentHandler(ch);
        reader.setEntityResolver(new NullEntityResolver());
    } else if ("n".equals(args[0])) {
        System.out.println(Version.getVersion());
        reader = new SAXParser();
        reader.setFeature("http://xml.org/sax/features/namespaces", false);
        reader.setFeature("http://xml.org/sax/features/validation", false);
        reader.setFeature("http://xml.org/sax/features/string-interning", true);
        reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
        reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        reader.setContentHandler(ch);
        reader.setEntityResolver(new NullEntityResolver());
    } else {
        System.out.println(Version.getVersion());
        reader = new SAXParser();
        reader.setFeature("http://xml.org/sax/features/namespaces", true);
        reader.setFeature("http://xml.org/sax/features/validation", false);
        reader.setFeature("http://xml.org/sax/features/string-interning", true);
        reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
        reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        reader.setContentHandler(ch);
        reader.setEntityResolver(new NullEntityResolver());
    }
    System.out.println("Warmup:");
    System.out.println((new ParserPerfHarnessNew(System.currentTimeMillis() + duration, reader, testData)).runLoop());
    System.gc();
    System.out.println("Real:");
    System.out.println((new ParserPerfHarnessNew(System.currentTimeMillis() + duration, reader, testData)).runLoop());
}
Also used : NullEntityResolver(nu.validator.xml.NullEntityResolver) SAXDriver(nu.validator.gnu.xml.aelfred2.SAXDriver) Driver(nu.validator.htmlparser.io.Driver) SAXDriver(nu.validator.gnu.xml.aelfred2.SAXDriver) HtmlParser(nu.validator.htmlparser.sax.HtmlParser) SAXParser(org.apache.xerces.parsers.SAXParser) File(java.io.File) XMLReader(org.xml.sax.XMLReader) XmlSerializer(nu.validator.htmlparser.sax.XmlSerializer)

Example 4 with XmlSerializer

use of nu.validator.htmlparser.sax.XmlSerializer 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

XmlSerializer (nu.validator.htmlparser.sax.XmlSerializer)4 File (java.io.File)2 HtmlParser (nu.validator.htmlparser.sax.HtmlParser)2 GnuMessageEmitter (nu.validator.messages.GnuMessageEmitter)2 JsonMessageEmitter (nu.validator.messages.JsonMessageEmitter)2 MessageEmitterAdapter (nu.validator.messages.MessageEmitterAdapter)2 TextMessageEmitter (nu.validator.messages.TextMessageEmitter)2 XmlMessageEmitter (nu.validator.messages.XmlMessageEmitter)2 ImageCollector (nu.validator.servlet.imagereview.ImageCollector)2 NullEntityResolver (nu.validator.xml.NullEntityResolver)2 XMLReader (org.xml.sax.XMLReader)2 IncorrectSchemaException (com.thaiopensource.validate.IncorrectSchemaException)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)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 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1