Search in sources :

Example 1 with NullEntityResolver

use of nu.validator.xml.NullEntityResolver in project validator by validator.

the class SvgAnalyzer method main.

/**
 * @param args
 * @throws Exception
 * @throws SAXException
 */
public static void main(String[] args) throws SAXException, Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    XMLReader parser = factory.newSAXParser().getXMLReader();
    SvgAnalysisHandler analysisHandler = new SvgAnalysisHandler();
    parser.setContentHandler(analysisHandler);
    parser.setDTDHandler(analysisHandler);
    parser.setProperty("http://xml.org/sax/properties/lexical-handler", analysisHandler);
    parser.setProperty("http://xml.org/sax/properties/declaration-handler", analysisHandler);
    parser.setFeature("http://xml.org/sax/features/string-interning", true);
    parser.setEntityResolver(new NullEntityResolver());
    parser.setErrorHandler(new ErrorHandler() {

        public void error(SAXParseException exception) throws SAXException {
        }

        public void fatalError(SAXParseException exception) throws SAXException {
        }

        public void warning(SAXParseException exception) throws SAXException {
        }
    });
    File dir = new File(args[0]);
    File[] list = dir.listFiles();
    int nsError = 0;
    int encodingErrors = 0;
    int otherIllFormed = 0;
    double total = (double) list.length;
    for (int i = 0; i < list.length; i++) {
        File file = list[i];
        try {
            InputSource is = new InputSource(new FileInputStream(file));
            is.setSystemId(file.toURL().toExternalForm());
            parser.parse(is);
        } catch (SAXParseException e) {
            String msg = e.getMessage();
            if (msg.startsWith("The prefix ")) {
                nsError++;
            } else if (msg.contains("Prefixed namespace bindings may not be empty.")) {
                nsError++;
            } else if (msg.startsWith("Invalid byte ")) {
                encodingErrors++;
            } else {
                otherIllFormed++;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
    System.out.print("NS errors: ");
    System.out.println(((double) nsError) / total);
    System.out.print("Encoding errors: ");
    System.out.println(((double) encodingErrors) / total);
    System.out.print("Other WF errors: ");
    System.out.println(((double) otherIllFormed) / total);
    analysisHandler.print();
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) NullEntityResolver(nu.validator.xml.NullEntityResolver) InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) File(java.io.File) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 2 with NullEntityResolver

use of nu.validator.xml.NullEntityResolver 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 NullEntityResolver

use of nu.validator.xml.NullEntityResolver 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 NullEntityResolver

use of nu.validator.xml.NullEntityResolver in project validator by validator.

the class ParseTreePrinter method service.

public void service() throws IOException {
    request.setCharacterEncoding("utf-8");
    String content = null;
    String document = scrubUrl(request.getParameter("doc"));
    document = ("".equals(document)) ? null : document;
    try (Writer writer = new OutputStreamWriter(response.getOutputStream(), "UTF-8")) {
        if (document == null && methodIsGet() && (content = request.getParameter("content")) == null) {
            response.setContentType("text/html; charset=utf-8");
            writer.write(FORM_HTML);
            writer.flush();
            return;
        }
        response.setContentType("text/plain; charset=utf-8");
        try {
            PrudentHttpEntityResolver entityResolver = new PrudentHttpEntityResolver(2048 * 1024, false, null);
            entityResolver.setAllowGenericXml(false);
            entityResolver.setAcceptAllKnownXmlTypes(false);
            entityResolver.setAllowHtml(true);
            entityResolver.setAllowXhtml(true);
            TypedInputSource documentInput;
            if (methodIsGet()) {
                if (content == null) {
                    documentInput = (TypedInputSource) entityResolver.resolveEntity(null, document);
                } else {
                    documentInput = new TypedInputSource(new StringReader(content));
                    if ("xml".equals(request.getParameter("parser"))) {
                        documentInput.setType("application/xhtml+xml");
                    } else {
                        documentInput.setType("text/html");
                    }
                }
            } else {
                // POST
                String 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;
                }
                long len = request.getContentLength();
                if (len > SIZE_LIMIT) {
                    throw new StreamBoundException("Resource size exceeds limit.");
                }
                ContentTypeParser contentTypeParser = new ContentTypeParser(null, false);
                contentTypeParser.setAllowGenericXml(false);
                contentTypeParser.setAcceptAllKnownXmlTypes(false);
                contentTypeParser.setAllowHtml(true);
                contentTypeParser.setAllowXhtml(true);
                documentInput = contentTypeParser.buildTypedInputSource(document, null, postContentType);
                documentInput.setByteStream(len < 0 ? new BoundedInputStream(request.getInputStream(), SIZE_LIMIT, document) : request.getInputStream());
                documentInput.setSystemId(request.getHeader("Content-Location"));
            }
            String type = documentInput.getType();
            XMLReader parser;
            if ("text/html".equals(type) || "text/html-sandboxed".equals(type)) {
                writer.write("HTML parser\n\n#document\n");
                parser = new nu.validator.htmlparser.sax.HtmlParser();
                parser.setProperty("http://validator.nu/properties/heuristics", Heuristics.ALL);
                parser.setProperty("http://validator.nu/properties/xml-policy", XmlViolationPolicy.ALLOW);
            } else if ("application/xhtml+xml".equals(type)) {
                writer.write("XML parser\n\n#document\n");
                parser = new SAXDriver();
                parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
                parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
                parser.setEntityResolver(new NullEntityResolver());
            } else {
                writer.write("Unsupported content type.\n");
                writer.flush();
                return;
            }
            TreeDumpContentHandler treeDumpContentHandler = new TreeDumpContentHandler(writer, false);
            ListErrorHandler listErrorHandler = new ListErrorHandler();
            parser.setContentHandler(treeDumpContentHandler);
            parser.setProperty("http://xml.org/sax/properties/lexical-handler", treeDumpContentHandler);
            parser.setErrorHandler(listErrorHandler);
            parser.parse(documentInput);
            writer.write("#errors\n");
            for (String err : listErrorHandler.getErrors()) {
                writer.write(err);
                writer.write('\n');
            }
        } catch (SAXException e) {
            writer.write("SAXException:\n");
            writer.write(e.getMessage());
            writer.write("\n");
        } catch (IOException e) {
            writer.write("IOException:\n");
            writer.write(e.getMessage());
            writer.write("\n");
        } finally {
            writer.flush();
        }
    }
}
Also used : NullEntityResolver(nu.validator.xml.NullEntityResolver) TypedInputSource(nu.validator.xml.TypedInputSource) PrudentHttpEntityResolver(nu.validator.xml.PrudentHttpEntityResolver) IOException(java.io.IOException) StreamBoundException(nu.validator.io.StreamBoundException) ContentTypeParser(nu.validator.xml.ContentTypeParser) SAXException(org.xml.sax.SAXException) SAXDriver(nu.validator.gnu.xml.aelfred2.SAXDriver) BoundedInputStream(nu.validator.io.BoundedInputStream) StringReader(java.io.StringReader) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) XMLReader(org.xml.sax.XMLReader)

Example 5 with NullEntityResolver

use of nu.validator.xml.NullEntityResolver in project validator by validator.

the class VerifierServletTransaction method setupXmlParser.

/**
 * @throws SAXNotRecognizedException
 * @throws SAXNotSupportedException
 */
protected void setupXmlParser() throws SAXNotRecognizedException, SAXNotSupportedException {
    xmlParser = new SAXDriver();
    xmlParser.setCharacterHandler(sourceCode);
    if (lexicalHandler != null) {
        xmlParser.setProperty("http://xml.org/sax/properties/lexical-handler", lexicalHandler);
    }
    reader = new IdFilter(xmlParser);
    reader.setFeature("http://xml.org/sax/features/string-interning", true);
    reader.setFeature("http://xml.org/sax/features/external-general-entities", parser == ParserMode.XML_EXTERNAL_ENTITIES_NO_VALIDATION);
    reader.setFeature("http://xml.org/sax/features/external-parameter-entities", parser == ParserMode.XML_EXTERNAL_ENTITIES_NO_VALIDATION);
    if (parser == ParserMode.XML_EXTERNAL_ENTITIES_NO_VALIDATION) {
        reader.setEntityResolver(entityResolver);
    } else {
        reader.setEntityResolver(new NullEntityResolver());
    }
    if (validator == null) {
        bufferingRootNamespaceSniffer = new BufferingRootNamespaceSniffer(this);
        reader.setContentHandler(bufferingRootNamespaceSniffer);
    } else {
        reader.setContentHandler(new RootNamespaceSniffer(this, validator.getContentHandler()));
        reader.setDTDHandler(validator.getDTDHandler());
    }
}
Also used : SAXDriver(nu.validator.gnu.xml.aelfred2.SAXDriver) IdFilter(nu.validator.xml.IdFilter) NullEntityResolver(nu.validator.xml.NullEntityResolver)

Aggregations

NullEntityResolver (nu.validator.xml.NullEntityResolver)6 SAXDriver (nu.validator.gnu.xml.aelfred2.SAXDriver)4 XMLReader (org.xml.sax.XMLReader)4 File (java.io.File)3 HtmlParser (nu.validator.htmlparser.sax.HtmlParser)3 SAXParserFactory (javax.xml.parsers.SAXParserFactory)2 XmlSerializer (nu.validator.htmlparser.sax.XmlSerializer)2 IdFilter (nu.validator.xml.IdFilter)2 SAXException (org.xml.sax.SAXException)2 CombineValidator (com.thaiopensource.relaxng.impl.CombineValidator)1 PropertyMap (com.thaiopensource.util.PropertyMap)1 PropertyMapBuilder (com.thaiopensource.util.PropertyMapBuilder)1 Validator (com.thaiopensource.validate.Validator)1 Jaxp11XMLReaderCreator (com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 ConformingButObsoleteWarner (nu.validator.checker.ConformingButObsoleteWarner)1