Search in sources :

Example 1 with Schema

use of com.thaiopensource.validate.Schema in project intellij-community by JetBrains.

the class XmlInstanceValidator method doValidation.

public static void doValidation(@NotNull final XmlDocument doc, final Validator.ValidationHost host, final XmlFile descriptorFile) {
    try {
        final Schema schema = RngParser.getCachedSchema(descriptorFile);
        if (schema == null) {
            // did not manage to get a compiled schema. no validation...
            return;
        }
        final ErrorHandler eh = MyErrorHandler.create(doc, host);
        if (eh == null) {
            return;
        }
        final PropertyMapBuilder builder = new PropertyMapBuilder();
        builder.put(ValidateProperty.ERROR_HANDLER, eh);
        final ContentHandler handler = schema.createValidator(builder.toPropertyMap()).getContentHandler();
        doc.accept(new Psi2SaxAdapter(handler));
    } catch (ProcessCanceledException e) {
        throw e;
    } catch (RuntimeException e) {
        LOG.error(e);
    } catch (Exception e) {
        LOG.info(e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) Schema(com.thaiopensource.validate.Schema) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) ContentHandler(org.xml.sax.ContentHandler) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 2 with Schema

use of com.thaiopensource.validate.Schema in project intellij-community by JetBrains.

the class RngParser method getCachedSchema.

public static Schema getCachedSchema(final XmlFile descriptorFile) {
    CachedValue<Schema> value = descriptorFile.getUserData(SCHEMA_KEY);
    if (value == null) {
        final CachedValueProvider<Schema> provider = () -> {
            final InputSource inputSource = makeInputSource(descriptorFile);
            try {
                final Schema schema = new MySchemaReader(descriptorFile).createSchema(inputSource, EMPTY_PROPS);
                final PsiElementProcessor.CollectElements<XmlFile> processor = new PsiElementProcessor.CollectElements<>();
                RelaxIncludeIndex.processForwardDependencies(descriptorFile, processor);
                if (processor.getCollection().size() > 0) {
                    return CachedValueProvider.Result.create(schema, processor.toArray(), descriptorFile);
                } else {
                    return CachedValueProvider.Result.createSingleDependency(schema, descriptorFile);
                }
            } catch (Exception e) {
                LOG.info(e);
                return CachedValueProvider.Result.createSingleDependency(null, descriptorFile);
            }
        };
        final CachedValuesManager mgr = CachedValuesManager.getManager(descriptorFile.getProject());
        value = mgr.createCachedValue(provider, false);
        descriptorFile.putUserData(SCHEMA_KEY, value);
    }
    return value.getValue();
}
Also used : InputSource(org.xml.sax.InputSource) CachedValuesManager(com.intellij.psi.util.CachedValuesManager) Schema(com.thaiopensource.validate.Schema) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) SAXException(org.xml.sax.SAXException) IllegalSchemaException(org.kohsuke.rngom.parse.IllegalSchemaException) SAXParseException(org.xml.sax.SAXParseException) BuildException(org.kohsuke.rngom.ast.builder.BuildException)

Example 3 with Schema

use of com.thaiopensource.validate.Schema in project validator by validator.

the class ValidationWorker method main.

public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "utf-8"));
    PrintWriter out = new PrintWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(args[1])), "utf-8"), true);
    File rootDir = new File(args[2]);
    Set<Schema> schemas = new HashSet<Schema>();
    schemas.add(CheckerSchema.ASSERTION_SCH);
    schemas.add(CheckerSchema.NORMALIZATION_CHECKER);
    schemas.add(CheckerSchema.TABLE_CHECKER);
    schemas.add(CheckerSchema.TEXT_CONTENT_CHECKER);
    schemas.add(CheckerSchema.USEMAP_CHECKER);
    InputSource is = new InputSource((new File(args[3])).toURL().toExternalForm());
    SchemaReader sr = CompactSchemaReader.getInstance();
    schemas.add(new XmlLangAttributeDroppingSchemaWrapper(new DataAttributeDroppingSchemaWrapper(sr.createSchema(is, PropertyMap.EMPTY))));
    CountingReadLine countingReadLine = new CountingReadLine(in);
    for (int i = 0; i < 4; i++) {
        (new Thread(new ValidationWorker(countingReadLine, out, rootDir, schemas))).start();
    }
}
Also used : InputSource(org.xml.sax.InputSource) CompactSchemaReader(com.thaiopensource.validate.rng.CompactSchemaReader) SchemaReader(com.thaiopensource.validate.SchemaReader) InputStreamReader(java.io.InputStreamReader) Schema(com.thaiopensource.validate.Schema) CheckerSchema(org.whattf.checker.jing.CheckerSchema) XmlLangAttributeDroppingSchemaWrapper(nu.validator.xml.langattributes.XmlLangAttributeDroppingSchemaWrapper) DataAttributeDroppingSchemaWrapper(nu.validator.xml.dataattributes.DataAttributeDroppingSchemaWrapper) FileInputStream(java.io.FileInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Example 4 with Schema

use of com.thaiopensource.validate.Schema in project validator by validator.

the class ValidationWorker method setupValidator.

private Validator setupValidator(Set<Schema> schemas) {
    PropertyMapBuilder builder = new PropertyMapBuilder();
    builder.put(ValidateProperty.ERROR_HANDLER, new ErrorHandler() {

        public void error(SAXParseException exception) throws SAXException {
            validationErrors.add(replaceSpecificValues(exception.getMessage()));
        }

        public void fatalError(SAXParseException exception) throws SAXException {
            // should not happen
            validationErrors.add(replaceSpecificValues(exception.getMessage()));
        }

        public void warning(SAXParseException exception) throws SAXException {
        }
    });
    PropertyMap map = builder.toPropertyMap();
    Validator rv = null;
    for (Schema schema : schemas) {
        Validator v = schema.createValidator(map);
        if (rv == null) {
            rv = v;
        } else {
            rv = new CombineValidator(rv, v);
        }
    }
    return rv;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) PropertyMap(com.thaiopensource.util.PropertyMap) SAXParseException(org.xml.sax.SAXParseException) Schema(com.thaiopensource.validate.Schema) CheckerSchema(org.whattf.checker.jing.CheckerSchema) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) CombineValidator(com.thaiopensource.relaxng.impl.CombineValidator) Validator(com.thaiopensource.validate.Validator) CombineValidator(com.thaiopensource.relaxng.impl.CombineValidator) SAXException(org.xml.sax.SAXException)

Example 5 with Schema

use of com.thaiopensource.validate.Schema in project validator by validator.

the class VerifierServletTransaction method validatorByUrl.

/**
 * @param url
 * @return
 * @throws SAXException
 * @throws IOException
 * @throws IncorrectSchemaException
 */
private Validator validatorByUrl(String url) throws SAXException, IOException, IncorrectSchemaException {
    if (loadedValidatorUrls.contains(url)) {
        return null;
    }
    loadedValidatorUrls.add(url);
    if ("http://s.validator.nu/xhtml5.rnc".equals(url) || "http://s.validator.nu/html5.rnc".equals(url) || "http://s.validator.nu/html5-all.rnc".equals(url) || "http://s.validator.nu/xhtml5-all.rnc".equals(url) || "http://s.validator.nu/html5-its.rnc".equals(url) || "http://s.validator.nu/xhtml5-rdfalite.rnc".equals(url) || "http://s.validator.nu/html5-rdfalite.rnc".equals(url)) {
        errorHandler.setSpec(html5spec);
    }
    Schema sch = resolveSchema(url, jingPropertyMap);
    Validator validator = sch.createValidator(jingPropertyMap);
    ContentHandler validatorContentHandler = validator.getContentHandler();
    if (validatorContentHandler instanceof XmlPiChecker) {
        lexicalHandler = (LexicalHandler) validatorContentHandler;
    }
    if (validatorContentHandler instanceof Assertions) {
        Assertions assertions = (Assertions) validatorContentHandler;
        assertions.setRequest(request);
        assertions.setSourceIsCss(sourceCode.getIsCss());
    }
    if (validatorContentHandler instanceof LanguageDetectingChecker) {
        // 
        LanguageDetectingChecker langdetect = (LanguageDetectingChecker) validatorContentHandler;
        langdetect.setRequest(request);
        langdetect.setHttpContentLanguageHeader(request.getHeader("Content-Language"));
    }
    return validator;
}
Also used : LanguageDetectingChecker(nu.validator.checker.LanguageDetectingChecker) Schema(com.thaiopensource.validate.Schema) CheckerSchema(nu.validator.checker.jing.CheckerSchema) XmlPiChecker(nu.validator.checker.XmlPiChecker) Assertions(nu.validator.checker.schematronequiv.Assertions) Validator(com.thaiopensource.validate.Validator) CombineValidator(com.thaiopensource.relaxng.impl.CombineValidator) CombineContentHandler(nu.validator.xml.CombineContentHandler) ContentHandler(org.xml.sax.ContentHandler)

Aggregations

Schema (com.thaiopensource.validate.Schema)8 SAXParseException (org.xml.sax.SAXParseException)5 CheckerSchema (nu.validator.checker.jing.CheckerSchema)4 SAXException (org.xml.sax.SAXException)4 SchemaReader (com.thaiopensource.validate.SchemaReader)3 CompactSchemaReader (com.thaiopensource.validate.rng.CompactSchemaReader)3 CombineValidator (com.thaiopensource.relaxng.impl.CombineValidator)2 PropertyMapBuilder (com.thaiopensource.util.PropertyMapBuilder)2 Validator (com.thaiopensource.validate.Validator)2 AutoSchemaReader (com.thaiopensource.validate.auto.AutoSchemaReader)2 TypedInputSource (nu.validator.xml.TypedInputSource)2 DataAttributeDroppingSchemaWrapper (nu.validator.xml.dataattributes.DataAttributeDroppingSchemaWrapper)2 XmlLangAttributeDroppingSchemaWrapper (nu.validator.xml.langattributes.XmlLangAttributeDroppingSchemaWrapper)2 CheckerSchema (org.whattf.checker.jing.CheckerSchema)2 InputSource (org.xml.sax.InputSource)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)1 CachedValuesManager (com.intellij.psi.util.CachedValuesManager)1 PropertyMap (com.thaiopensource.util.PropertyMap)1 IncorrectSchemaException (com.thaiopensource.validate.IncorrectSchemaException)1