/** * Creates a selector with a default {@link XmlValidator}. The validator will be initialized with * the provided 'schema' location {@link Resource} and 'schemaType'. The valid options for schema * type are {@link XmlValidatorFactory#SCHEMA_W3C_XML} or {@link XmlValidatorFactory#SCHEMA_RELAX_NG}. * If no 'schemaType' is provided it will default to {@link XmlValidatorFactory#SCHEMA_W3C_XML}; * @param schema The schema. * @param schemaType The schema type. * @throws IOException if the XmlValidatorFactory fails to create a validator */ public XmlValidatingMessageSelector(Resource schema, SchemaType schemaType) throws IOException { this(XmlValidatorFactory.createValidator(schema, schemaType == null ? SchemaType.XML_SCHEMA.getUrl() : schemaType.getUrl())); }
@Override public boolean accept(Message<?> message) { SAXParseException[] validationExceptions = null; try { validationExceptions = this.xmlValidator.validate(this.converter.convertToSource(message.getPayload())); } catch (Exception e) { throw new MessageHandlingException(message, e); } boolean validationSuccess = ObjectUtils.isEmpty(validationExceptions); if (!validationSuccess) { if (this.throwExceptionOnRejection) { throw new MessageRejectedException(message, "Message was rejected due to XML Validation errors", new AggregatedXmlMessageValidationException(Arrays.asList(validationExceptions))); } else if (this.logger.isInfoEnabled()) { this.logger.info("Message was rejected due to XML Validation errors", new AggregatedXmlMessageValidationException(Arrays.asList(validationExceptions))); } } return validationSuccess; }
@Override public SAXParseException[] validate(Source source) throws IOException { return validate(source, null); }
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException { try { Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage); return new Jaxp13Validator(schema); } catch (SAXException ex) { throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex); } }
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException { try { Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage); return new Jaxp15Validator(schema); } catch (SAXException ex) { throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex); } }
@Override public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException { if (errorHandler == null) { errorHandler = new DefaultValidationErrorHandler(); } Validator validator = schema.newValidator(); validator.setErrorHandler(errorHandler); try { validator.validate(source); return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } } }
/** * Load schema from the given resource. * * @param resource the resource to load from * @param schemaLanguage the language of the schema. Can be <code>XMLConstants.W3C_XML_SCHEMA_NS_URI</code> or * <code>XMLConstants.RELAXNG_NS_URI</code>. * @throws IOException if loading failed * @throws SAXException if loading failed * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI * @see javax.xml.XMLConstants#RELAXNG_NS_URI */ public static Schema loadSchema(Resource resource, String schemaLanguage) throws IOException, SAXException { return loadSchema(new Resource[]{resource}, schemaLanguage); }
@Override public SAXParseException[] validate(Source source) throws IOException { return validate(source, null); }
static XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException { InputSource[] inputSources = new InputSource[schemaResources.length]; for (int i = 0; i < schemaResources.length; i++) { inputSources[i] = SaxUtils.createInputSource(schemaResources[i]); } return new Jaxp10Validator(inputSources, schemaLanguage); }
public XmlValidator createValidator() throws IOException { return XmlValidatorFactory.createValidator(xsdResource, XmlValidatorFactory.SCHEMA_W3C_XML); }
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException { try { Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage); return new Jaxp13Validator(schema); } catch (SAXException ex) { throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex); } }
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException { try { Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage); return new Jaxp15Validator(schema); } catch (SAXException ex) { throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex); } }
/** * Load schema from the given resource. * * @param resource the resource to load from * @param schemaLanguage the language of the schema. Can be {@code XMLConstants.W3C_XML_SCHEMA_NS_URI} or * {@code XMLConstants.RELAXNG_NS_URI}. * @throws IOException if loading failed * @throws SAXException if loading failed * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI * @see javax.xml.XMLConstants#RELAXNG_NS_URI */ public static Schema loadSchema(Resource resource, String schemaLanguage) throws IOException, SAXException { return loadSchema(new Resource[]{resource}, schemaLanguage); }
@Override public SAXParseException[] validate(Source source) throws IOException { return validate(source, null); }
@Override public SAXParseException[] validate(Source source) throws IOException { return validate(source, null); }
/** * Create a {@link XmlValidator} with the given schema resource and schema language type. The schema language must * be one of the {@code SCHEMA_XXX} constants. * * @param schemaResource a resource that locates the schema to validate against * @param schemaLanguage the language of the schema * @return a validator * @throws IOException if the schema resource cannot be read * @throws IllegalArgumentException if the schema language is not supported * @throws IllegalStateException if JAXP 1.0 cannot be located * @throws XmlValidationException if a {@code XmlValidator} cannot be created * @see #SCHEMA_RELAX_NG * @see #SCHEMA_W3C_XML */ public static XmlValidator createValidator(Resource schemaResource, String schemaLanguage) throws IOException { return createValidator(new Resource[]{schemaResource}, schemaLanguage); }
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException { try { Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage); return new Jaxp13Validator(schema); } catch (SAXException ex) { throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex); } }
/** * Load schema from the given resource. * * @param resource the resource to load from * @param schemaLanguage the language of the schema. Can be {@code XMLConstants.W3C_XML_SCHEMA_NS_URI} or * {@code XMLConstants.RELAXNG_NS_URI}. * @throws IOException if loading failed * @throws SAXException if loading failed * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI * @see javax.xml.XMLConstants#RELAXNG_NS_URI */ public static Schema loadSchema(Resource resource, String schemaLanguage) throws IOException, SAXException { return loadSchema(new Resource[]{resource}, schemaLanguage); }
/** * Create a {@link XmlValidator} with the given schema resource and schema language type. The schema language must * be one of the <code>SCHEMA_XXX</code> constants. * * @param schemaResource a resource that locates the schema to validate against * @param schemaLanguage the language of the schema * @return a validator * @throws IOException if the schema resource cannot be read * @throws IllegalArgumentException if the schema language is not supported * @throws IllegalStateException if JAXP 1.0 cannot be located * @throws XmlValidationException if a <code>XmlValidator</code> cannot be created * @see #SCHEMA_RELAX_NG * @see #SCHEMA_W3C_XML */ public static XmlValidator createValidator(Resource schemaResource, String schemaLanguage) throws IOException { return createValidator(new Resource[]{schemaResource}, schemaLanguage); }
/** * Create a {@link XmlValidator} with the given schema resource and schema language type. The schema language must * be one of the {@code SCHEMA_XXX} constants. * * @param schemaResource a resource that locates the schema to validate against * @param schemaLanguage the language of the schema * @return a validator * @throws IOException if the schema resource cannot be read * @throws IllegalArgumentException if the schema language is not supported * @throws IllegalStateException if JAXP 1.0 cannot be located * @throws XmlValidationException if a {@code XmlValidator} cannot be created * @see #SCHEMA_RELAX_NG * @see #SCHEMA_W3C_XML */ public static XmlValidator createValidator(Resource schemaResource, String schemaLanguage) throws IOException { return createValidator(new Resource[]{schemaResource}, schemaLanguage); }