Search in sources :

Example 1 with TypedInputSource

use of nu.validator.xml.TypedInputSource 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 2 with TypedInputSource

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

the class VerifierServletTransaction method schemaByUrl.

/**
 * @param url
 * @return
 * @throws SAXException
 * @throws IOException
 * @throws IncorrectSchemaException
 */
private static Schema schemaByUrl(String url, EntityResolver resolver, PropertyMap pMap) throws SAXException, IOException, IncorrectSchemaException {
    log4j.debug("Will load schema: " + url);
    TypedInputSource schemaInput;
    try {
        schemaInput = (TypedInputSource) resolver.resolveEntity(null, url);
    } catch (ClassCastException e) {
        log4j.fatal(url, e);
        throw e;
    }
    SchemaReader sr = null;
    if ("application/relax-ng-compact-syntax".equals(schemaInput.getType())) {
        sr = CompactSchemaReader.getInstance();
    } else {
        sr = new AutoSchemaReader();
    }
    Schema sch = sr.createSchema(schemaInput, pMap);
    return sch;
}
Also used : CompactSchemaReader(com.thaiopensource.validate.rng.CompactSchemaReader) AutoSchemaReader(com.thaiopensource.validate.auto.AutoSchemaReader) SchemaReader(com.thaiopensource.validate.SchemaReader) TypedInputSource(nu.validator.xml.TypedInputSource) AutoSchemaReader(com.thaiopensource.validate.auto.AutoSchemaReader) Schema(com.thaiopensource.validate.Schema) CheckerSchema(nu.validator.checker.jing.CheckerSchema)

Example 3 with TypedInputSource

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

the class VerifierServletTransaction method resolveSchema.

@Override
public Schema resolveSchema(String url, PropertyMap options) throws SAXException, IOException, IncorrectSchemaException {
    int i = Arrays.binarySearch(preloadedSchemaUrls, url);
    if (i > -1) {
        Schema rv = preloadedSchemas[i];
        if (options.contains(WrapProperty.ATTRIBUTE_OWNER)) {
            if (rv instanceof CheckerSchema) {
                errorHandler.error(new SAXParseException("A non-schema checker cannot be used as an attribute schema.", null, url, -1, -1));
                throw new IncorrectSchemaException();
            } else {
            // ugly fall through
            }
        } else {
            return rv;
        }
    }
    externalSchema = true;
    TypedInputSource schemaInput = (TypedInputSource) entityResolver.resolveEntity(null, url);
    SchemaReader sr = null;
    if ("application/relax-ng-compact-syntax".equals(schemaInput.getType())) {
        sr = CompactSchemaReader.getInstance();
    } else {
        sr = new AutoSchemaReader();
    }
    Schema sch = sr.createSchema(schemaInput, options);
    if (Statistics.STATISTICS != null && "com.thaiopensource.validate.schematron.SchemaImpl".equals(sch.getClass().getName())) {
        externalSchematron = true;
    }
    return sch;
}
Also used : CheckerSchema(nu.validator.checker.jing.CheckerSchema) CompactSchemaReader(com.thaiopensource.validate.rng.CompactSchemaReader) AutoSchemaReader(com.thaiopensource.validate.auto.AutoSchemaReader) SchemaReader(com.thaiopensource.validate.SchemaReader) TypedInputSource(nu.validator.xml.TypedInputSource) AutoSchemaReader(com.thaiopensource.validate.auto.AutoSchemaReader) SAXParseException(org.xml.sax.SAXParseException) Schema(com.thaiopensource.validate.Schema) CheckerSchema(nu.validator.checker.jing.CheckerSchema) IncorrectSchemaException(com.thaiopensource.validate.IncorrectSchemaException)

Example 4 with TypedInputSource

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

the class SimpleDocumentValidator method schemaByUrl.

private Schema schemaByUrl(String schemaUrl, ErrorHandler errorHandler) throws Exception, SchemaReadException {
    PropertyMapBuilder pmb = new PropertyMapBuilder();
    pmb.put(ValidateProperty.ERROR_HANDLER, errorHandler);
    pmb.put(ValidateProperty.ENTITY_RESOLVER, entityResolver);
    pmb.put(ValidateProperty.XML_READER_CREATOR, new Jaxp11XMLReaderCreator());
    RngProperty.CHECK_ID_IDREF.add(pmb);
    PropertyMap jingPropertyMap = pmb.toPropertyMap();
    try {
        TypedInputSource schemaInput = (TypedInputSource) entityResolver.resolveEntity(null, schemaUrl);
        SchemaReader sr;
        if ("application/relax-ng-compact-syntax".equals(schemaInput.getType())) {
            sr = CompactSchemaReader.getInstance();
        } else {
            sr = new AutoSchemaReader();
        }
        return sr.createSchema(schemaInput, jingPropertyMap);
    } catch (ClassCastException e) {
        throw new SchemaReadException(String.format("Failed to resolve schema URL \"%s\".", schemaUrl));
    }
}
Also used : CompactSchemaReader(com.thaiopensource.validate.rng.CompactSchemaReader) AutoSchemaReader(com.thaiopensource.validate.auto.AutoSchemaReader) SchemaReader(com.thaiopensource.validate.SchemaReader) Jaxp11XMLReaderCreator(com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator) PropertyMap(com.thaiopensource.util.PropertyMap) TypedInputSource(nu.validator.xml.TypedInputSource) AutoSchemaReader(com.thaiopensource.validate.auto.AutoSchemaReader) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder)

Example 5 with TypedInputSource

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

the class LocalCacheEntityResolver method resolveEntity.

/**
 * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
 *      java.lang.String)
 */
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
    String path = PATH_MAP.get(systemId);
    if (path != null) {
        @SuppressWarnings("resource") InputStream stream = LOADER.getResourceAsStream(path);
        if (stream != null) {
            TypedInputSource is = new TypedInputSource();
            if (systemId.endsWith(".rnc")) {
                is.setType("application/relax-ng-compact-syntax");
                if (!allowRnc) {
                    stream.close();
                    throw new IOException("Not an XML resource: " + systemId);
                }
            } else if (systemId.endsWith(".dtd")) {
                is.setType("application/xml-dtd");
            } else if (systemId.endsWith(".ent")) {
                is.setType("application/xml-external-parsed-entity");
            } else {
                is.setType("application/xml");
            }
            is.setByteStream(stream);
            is.setSystemId(systemId);
            is.setPublicId(publicId);
            return is;
        }
    }
    return delegate.resolveEntity(publicId, systemId);
}
Also used : TypedInputSource(nu.validator.xml.TypedInputSource) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

TypedInputSource (nu.validator.xml.TypedInputSource)6 SchemaReader (com.thaiopensource.validate.SchemaReader)3 AutoSchemaReader (com.thaiopensource.validate.auto.AutoSchemaReader)3 CompactSchemaReader (com.thaiopensource.validate.rng.CompactSchemaReader)3 Schema (com.thaiopensource.validate.Schema)2 IOException (java.io.IOException)2 CheckerSchema (nu.validator.checker.jing.CheckerSchema)2 PropertyMap (com.thaiopensource.util.PropertyMap)1 PropertyMapBuilder (com.thaiopensource.util.PropertyMapBuilder)1 IncorrectSchemaException (com.thaiopensource.validate.IncorrectSchemaException)1 Jaxp11XMLReaderCreator (com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator)1 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 SAXDriver (nu.validator.gnu.xml.aelfred2.SAXDriver)1 BoundedInputStream (nu.validator.io.BoundedInputStream)1 StreamBoundException (nu.validator.io.StreamBoundException)1 ContentTypeParser (nu.validator.xml.ContentTypeParser)1 NullEntityResolver (nu.validator.xml.NullEntityResolver)1