Search in sources :

Example 1 with Jaxp11XMLReaderCreator

use of com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator in project camel by apache.

the class JingValidator method process.

public void process(Exchange exchange) throws Exception {
    Jaxp11XMLReaderCreator xmlCreator = new Jaxp11XMLReaderCreator();
    DefaultValidationErrorHandler errorHandler = new DefaultValidationErrorHandler();
    PropertyMapBuilder mapBuilder = new PropertyMapBuilder();
    mapBuilder.put(ValidateProperty.XML_READER_CREATOR, xmlCreator);
    mapBuilder.put(ValidateProperty.ERROR_HANDLER, errorHandler);
    PropertyMap propertyMap = mapBuilder.toPropertyMap();
    Validator validator = getSchema().createValidator(propertyMap);
    Message in = exchange.getIn();
    SAXSource saxSource = in.getBody(SAXSource.class);
    if (saxSource == null) {
        Source source = exchange.getIn().getMandatoryBody(Source.class);
        saxSource = ExchangeHelper.convertToMandatoryType(exchange, SAXSource.class, source);
    }
    InputSource bodyInput = saxSource.getInputSource();
    // now lets parse the body using the validator
    XMLReader reader = xmlCreator.createXMLReader();
    reader.setContentHandler(validator.getContentHandler());
    reader.setDTDHandler(validator.getDTDHandler());
    reader.setErrorHandler(errorHandler);
    reader.parse(bodyInput);
    errorHandler.handleErrors(exchange, schema);
}
Also used : InputSource(org.xml.sax.InputSource) Jaxp11XMLReaderCreator(com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator) PropertyMap(com.thaiopensource.util.PropertyMap) SAXSource(javax.xml.transform.sax.SAXSource) Message(org.apache.camel.Message) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) DefaultValidationErrorHandler(org.apache.camel.processor.validation.DefaultValidationErrorHandler) Validator(com.thaiopensource.validate.Validator) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader)

Example 2 with Jaxp11XMLReaderCreator

use of com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator in project camel by apache.

the class JingEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    if (inputSource == null) {
        ObjectHelper.notEmpty(resourceUri, "resourceUri", this);
        InputStream inputStream = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), resourceUri);
        inputSource = new InputSource(inputStream);
    }
    if (schemaFactory == null) {
        schemaFactory = new SchemaFactory();
        schemaFactory.setCompactSyntax(compactSyntax);
        schemaFactory.setXMLReaderCreator(new Jaxp11XMLReaderCreator());
    }
    if (schema == null) {
        schema = schemaFactory.createSchema(inputSource);
    }
}
Also used : SchemaFactory(com.thaiopensource.relaxng.SchemaFactory) InputSource(org.xml.sax.InputSource) Jaxp11XMLReaderCreator(com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator) InputStream(java.io.InputStream)

Example 3 with Jaxp11XMLReaderCreator

use of com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator in project validator by validator.

the class SimpleDocumentValidator method setUpValidatorAndParsers.

/* *
     * Prepares a Validator instance along with HTML and XML parsers, and then
     * attaches the Validator instance and supplied ErrorHandler instance to the
     * parsers so that the ErrorHandler is used for processing of all document-
     * validation problems reported.
     * 
     * @param docValidationErrHandler error handler for doc-validation reporting
     * 
     * @param loadExternalEnts whether XML parser should load remote DTDs, etc.
     * 
     * @param noStream whether HTML parser should buffer instead of streaming
     */
public void setUpValidatorAndParsers(ErrorHandler docValidationErrHandler, boolean noStream, boolean loadExternalEnts) throws SAXException {
    PropertyMapBuilder pmb = new PropertyMapBuilder();
    pmb.put(ValidateProperty.ERROR_HANDLER, docValidationErrHandler);
    pmb.put(ValidateProperty.XML_READER_CREATOR, new Jaxp11XMLReaderCreator());
    RngProperty.CHECK_ID_IDREF.add(pmb);
    PropertyMap jingPropertyMap = pmb.toPropertyMap();
    validator = this.mainSchema.createValidator(jingPropertyMap);
    if (this.hasHtml5Schema) {
        Validator assertionValidator = assertionSchema.createValidator(jingPropertyMap);
        validator = new CombineValidator(validator, assertionValidator);
        validator = new CombineValidator(validator, new CheckerValidator(new TableChecker(), jingPropertyMap));
        validator = new CombineValidator(validator, new CheckerValidator(new ConformingButObsoleteWarner(), jingPropertyMap));
        validator = new CombineValidator(validator, new CheckerValidator(new MicrodataChecker(), jingPropertyMap));
        validator = new CombineValidator(validator, new CheckerValidator(new NormalizationChecker(), jingPropertyMap));
        validator = new CombineValidator(validator, new CheckerValidator(new TextContentChecker(), jingPropertyMap));
        validator = new CombineValidator(validator, new CheckerValidator(new UncheckedSubtreeWarner(), jingPropertyMap));
        validator = new CombineValidator(validator, new CheckerValidator(new UnsupportedFeatureChecker(), jingPropertyMap));
        validator = new CombineValidator(validator, new CheckerValidator(new UsemapChecker(), jingPropertyMap));
        validator = new CombineValidator(validator, new CheckerValidator(new XmlPiChecker(), jingPropertyMap));
    }
    HtmlParser htmlParser = new HtmlParser();
    htmlParser.addCharacterHandler(sourceCode);
    htmlParser.setCommentPolicy(XmlViolationPolicy.ALLOW);
    htmlParser.setContentNonXmlCharPolicy(XmlViolationPolicy.ALLOW);
    htmlParser.setContentSpacePolicy(XmlViolationPolicy.ALTER_INFOSET);
    htmlParser.setNamePolicy(XmlViolationPolicy.ALLOW);
    htmlParser.setXmlnsPolicy(XmlViolationPolicy.ALTER_INFOSET);
    htmlParser.setMappingLangToXmlLang(true);
    htmlParser.setHeuristics(Heuristics.ALL);
    htmlParser.setContentHandler(validator.getContentHandler());
    htmlParser.setErrorHandler(docValidationErrHandler);
    htmlParser.setNamePolicy(XmlViolationPolicy.ALLOW);
    htmlParser.setMappingLangToXmlLang(true);
    htmlParser.setFeature("http://xml.org/sax/features/unicode-normalization-checking", true);
    if (!noStream) {
        htmlParser.setStreamabilityViolationPolicy(XmlViolationPolicy.FATAL);
    }
    htmlReader = getWiretap(htmlParser);
    xmlParser = new SAXDriver();
    xmlParser.setContentHandler(validator.getContentHandler());
    if (lexicalHandler != null) {
        xmlParser.setProperty("http://xml.org/sax/properties/lexical-handler", lexicalHandler);
    }
    xmlReader = new IdFilter(xmlParser);
    xmlReader.setFeature("http://xml.org/sax/features/string-interning", true);
    xmlReader.setContentHandler(validator.getContentHandler());
    xmlReader.setFeature("http://xml.org/sax/features/unicode-normalization-checking", true);
    if (loadExternalEnts) {
        xmlReader.setEntityResolver(entityResolver);
    } else {
        xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        xmlReader.setEntityResolver(new NullEntityResolver());
    }
    xmlReader = getWiretap(xmlParser);
    xmlParser.setErrorHandler(docValidationErrHandler);
    xmlParser.lockErrorHandler();
}
Also used : IdFilter(nu.validator.xml.IdFilter) NullEntityResolver(nu.validator.xml.NullEntityResolver) Jaxp11XMLReaderCreator(com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator) ConformingButObsoleteWarner(nu.validator.checker.ConformingButObsoleteWarner) XmlPiChecker(nu.validator.checker.XmlPiChecker) TextContentChecker(nu.validator.checker.TextContentChecker) CheckerValidator(nu.validator.checker.jing.CheckerValidator) SAXDriver(nu.validator.gnu.xml.aelfred2.SAXDriver) UnsupportedFeatureChecker(nu.validator.checker.UnsupportedFeatureChecker) HtmlParser(nu.validator.htmlparser.sax.HtmlParser) PropertyMap(com.thaiopensource.util.PropertyMap) NormalizationChecker(nu.validator.checker.NormalizationChecker) UsemapChecker(nu.validator.checker.UsemapChecker) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) TableChecker(nu.validator.checker.table.TableChecker) UncheckedSubtreeWarner(nu.validator.checker.UncheckedSubtreeWarner) CombineValidator(com.thaiopensource.relaxng.impl.CombineValidator) CheckerValidator(nu.validator.checker.jing.CheckerValidator) Validator(com.thaiopensource.validate.Validator) CombineValidator(com.thaiopensource.relaxng.impl.CombineValidator) MicrodataChecker(nu.validator.checker.MicrodataChecker)

Example 4 with Jaxp11XMLReaderCreator

use of com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator 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)

Aggregations

Jaxp11XMLReaderCreator (com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator)4 PropertyMap (com.thaiopensource.util.PropertyMap)3 PropertyMapBuilder (com.thaiopensource.util.PropertyMapBuilder)3 Validator (com.thaiopensource.validate.Validator)2 InputSource (org.xml.sax.InputSource)2 SchemaFactory (com.thaiopensource.relaxng.SchemaFactory)1 CombineValidator (com.thaiopensource.relaxng.impl.CombineValidator)1 SchemaReader (com.thaiopensource.validate.SchemaReader)1 AutoSchemaReader (com.thaiopensource.validate.auto.AutoSchemaReader)1 CompactSchemaReader (com.thaiopensource.validate.rng.CompactSchemaReader)1 InputStream (java.io.InputStream)1 Source (javax.xml.transform.Source)1 SAXSource (javax.xml.transform.sax.SAXSource)1 ConformingButObsoleteWarner (nu.validator.checker.ConformingButObsoleteWarner)1 MicrodataChecker (nu.validator.checker.MicrodataChecker)1 NormalizationChecker (nu.validator.checker.NormalizationChecker)1 TextContentChecker (nu.validator.checker.TextContentChecker)1 UncheckedSubtreeWarner (nu.validator.checker.UncheckedSubtreeWarner)1 UnsupportedFeatureChecker (nu.validator.checker.UnsupportedFeatureChecker)1 UsemapChecker (nu.validator.checker.UsemapChecker)1