Search in sources :

Example 1 with PropertyMapBuilder

use of com.thaiopensource.util.PropertyMapBuilder 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 PropertyMapBuilder

use of com.thaiopensource.util.PropertyMapBuilder in project intellij-community by JetBrains.

the class ValidateAction method doValidation.

@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
private static void doValidation(VirtualFile instanceFile, VirtualFile schemaFile, org.xml.sax.ErrorHandler eh) {
    final SchemaReader sr = schemaFile.getFileType() == RncFileType.getInstance() ? CompactSchemaReader.getInstance() : new AutoSchemaReader();
    final PropertyMapBuilder properties = new PropertyMapBuilder();
    ValidateProperty.ERROR_HANDLER.put(properties, eh);
    // TODO: should some options dialog displayed before validating?
    RngProperty.CHECK_ID_IDREF.add(properties);
    try {
        final String schemaPath = VfsUtilCore.fixIDEAUrl(schemaFile.getUrl());
        try {
            final ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), sr);
            final InputSource in = ValidationDriver.uriOrFileInputSource(schemaPath);
            in.setEncoding(schemaFile.getCharset().name());
            if (driver.loadSchema(in)) {
                final String path = VfsUtilCore.fixIDEAUrl(instanceFile.getUrl());
                try {
                    driver.validate(ValidationDriver.uriOrFileInputSource(path));
                } catch (IOException e1) {
                    eh.fatalError(new SAXParseException(e1.getMessage(), null, UriOrFile.fileToUri(path), -1, -1, e1));
                }
            }
        } catch (SAXParseException e1) {
            eh.fatalError(e1);
        } catch (IOException e1) {
            eh.fatalError(new SAXParseException(e1.getMessage(), null, UriOrFile.fileToUri(schemaPath), -1, -1, e1));
        }
    } catch (SAXException | MalformedURLException e1) {
        // huh?
        Logger.getInstance(ValidateAction.class.getName()).error(e1);
    }
}
Also used : CompactSchemaReader(com.thaiopensource.validate.rng.CompactSchemaReader) AutoSchemaReader(com.thaiopensource.validate.auto.AutoSchemaReader) SchemaReader(com.thaiopensource.validate.SchemaReader) InputSource(org.xml.sax.InputSource) MalformedURLException(java.net.MalformedURLException) AutoSchemaReader(com.thaiopensource.validate.auto.AutoSchemaReader) SAXParseException(org.xml.sax.SAXParseException) ValidationDriver(com.thaiopensource.validate.ValidationDriver) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 3 with PropertyMapBuilder

use of com.thaiopensource.util.PropertyMapBuilder 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)

Aggregations

PropertyMapBuilder (com.thaiopensource.util.PropertyMapBuilder)3 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 SAXParseException (org.xml.sax.SAXParseException)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 PropertyMap (com.thaiopensource.util.PropertyMap)1 Schema (com.thaiopensource.validate.Schema)1 SchemaReader (com.thaiopensource.validate.SchemaReader)1 ValidationDriver (com.thaiopensource.validate.ValidationDriver)1 Validator (com.thaiopensource.validate.Validator)1 AutoSchemaReader (com.thaiopensource.validate.auto.AutoSchemaReader)1 CompactSchemaReader (com.thaiopensource.validate.rng.CompactSchemaReader)1 Jaxp11XMLReaderCreator (com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 Source (javax.xml.transform.Source)1 SAXSource (javax.xml.transform.sax.SAXSource)1 Message (org.apache.camel.Message)1 DefaultValidationErrorHandler (org.apache.camel.processor.validation.DefaultValidationErrorHandler)1 ContentHandler (org.xml.sax.ContentHandler)1