private Validator getValidator(URL xmlSchema) throws SAXException { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(xmlSchema); Validator validator = schema.newValidator(); validator.setErrorHandler(new ErrorHandler()); return validator; }
Validator v = schema.newValidator(); final SAXException[] error = {null}; v.setErrorHandler(new ErrorHandler() { public @Override void warning(SAXParseException x) throws SAXException {} public @Override void error(SAXParseException x) throws SAXException {
private void validate(Document document) { // todo : add ability to disable validation... final String version = document.getDocumentElement().getAttribute( "version" ); final Validator validator = new ConfigXsdSupport().jpaXsd( version ).getSchema().newValidator(); List<SAXException> errors = new ArrayList<>(); validator.setErrorHandler( new ErrorHandlerImpl( errors ) ); try { validator.validate( new DOMSource( document ) ); } catch (SAXException e) { errors.add( e ); } catch (IOException e) { throw new PersistenceException( "Unable to validate persistence.xml", e ); } if ( errors.size() != 0 ) { //report all errors in the exception StringBuilder errorMessage = new StringBuilder( ); for ( SAXException error : errors ) { errorMessage.append( extractInfo( error ) ).append( '\n' ); } throw new PersistenceException( "Invalid persistence.xml.\n" + errorMessage.toString() ); } }
public static List validate( Source xml, URL schemaURL, boolean skipTargetNamespaceException, EntityResolver entityResolver) { try { Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(schemaURL); Validator v = schema.newValidator(); if (entityResolver != null) { v.setResourceResolver( new EntityResolverToLSResourceResolver( v.getResourceResolver(), entityResolver)); } Handler handler = new Handler(skipTargetNamespaceException, entityResolver); v.setErrorHandler(handler); v.validate(xml); return handler.errors; } catch (SAXException e) { return exception(e); } catch (IOException e) { return exception(e); } }
final Validator validator = schema.newValidator(); final List<Exception> validationErrors = new ArrayList<Exception>(); validator.setErrorHandler( new ErrorHandler() {
final Validator validator = schema.newValidator(); final List<Exception> validationErrors = new ArrayList<Exception>(); validator.setErrorHandler( new ErrorHandler() {
schemaValidator.setErrorHandler(new ErrorLogger("XML InputStream", validationErrors)); schemaValidator.validate(new DOMSource(xmlRootNode));
void validate(byte[] data, Configuration configuration) throws Exception { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema s = sf.newSchema(new URI(configuration.getSchemaFileURL()).toURL()); Validator v = s.newValidator(); final ArrayList errors = new ArrayList(); DefaultHandler handler = new DefaultHandler() { public void error(SAXParseException e) throws SAXException { // System.out.println(e.getMessage()); errors.add(e); } public void fatalError(SAXParseException e) throws SAXException { // System.out.println(e.getMessage()); errors.add(e); } }; v.setErrorHandler(handler); v.validate(new StreamSource(new ByteArrayInputStream(data))); assertTrue(errors.isEmpty()); } }
Validator validator = schema.newValidator(); MyErrorHandler handler = new MyErrorHandler(theContext); validator.setErrorHandler(handler); String encodedResource; if (theContext.getResourceAsStringEncoding() == EncodingEnum.XML) {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("customer.xsd")); Validator validator = schema.newValidator(); validator.setErrorHandler(new MyErrorHandler()); validator.validate(source);
public static ValidationResult validate(Document doc, Source... schemas) throws IOException, SAXException { XMLErrorHandler errorHandler = new XMLErrorHandler(); Validator validator = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemas).newValidator(); try { validator.setErrorHandler(errorHandler); validator.validate(new DOMSource(doc)); return errorHandler; } finally { validator.reset(); } }
protected void validateSchema(final ValidationMethod method, final Resolver resolver, final byte[] schemaData, byte[] xmlData) throws SAXException, IOException { final SchemaFactory schemaFactory = SchemaFactory.newInstance(method.schemaLanguage); schemaFactory.setResourceResolver(resolver); final Schema schema = schemaFactory.newSchema(new StreamSource(new ByteArrayInputStream(schemaData))); final Validator validator = schema.newValidator(); validator.setErrorHandler(new ErrorHandler()); validator.setResourceResolver(resolver); validator.validate(new StreamSource(new ByteArrayInputStream(xmlData))); }
public void validate(final byte[] xmlBytes) { final javax.xml.validation.Validator validator = schema.newValidator(); final ValidationExceptionBuilder errorHandler = new ValidationExceptionBuilder(); validator.setErrorHandler(errorHandler); try { validator.validate(new StreamSource(new ByteArrayInputStream(xmlBytes))); if (!errorHandler.errors.isEmpty()) throw errorHandler.build(); } catch (SAXException | IOException e) { throw new ValidationException(e); } }
public void validate(Source source) throws ParserException { try { Validator validator = getSchema().newValidator(); validator.setErrorHandler(this); validator.validate(source); } catch (Exception ex) { throw unwrapException(ex); } }
public Validator getValidator() { if (null == validator) { Schema s = getSchema(); validator = s.newValidator(); validator.setErrorHandler(new TypeSchemaErrorHandler()); } return validator; }
private void validateNode(org.w3c.dom.Node node) { if (getSchema() != null) { Validator validator = getSchema().newValidator(); validator.setErrorHandler(getErrorHandler()); try { validator.validate(new DOMSource(node)); } catch (Exception e) { throw XMLMarshalException.validateException(e); } } }
private void validateNode(org.w3c.dom.Node node) { if (getSchema() != null) { Validator validator = getSchema().newValidator(); validator.setErrorHandler(getErrorHandler()); try { validator.validate(new DOMSource(node)); } catch (Exception e) { throw XMLMarshalException.validateException(e); } } }
public void validate(Document doc) throws IOException, SAXException { Validator validator = camelSchemas().newValidator(); validator.setErrorHandler(this); validate(validator, doc.getRootElement()); }
public static Validator validator() throws SAXException, IOException { SystemPropertiesUtil.ensure(); if (validator == null) { Schema schema = getSchema(); if (schema == null) throw logger.nullValueError("schema"); validator = schema.newValidator(); validator.setErrorHandler(new CustomErrorHandler()); } return validator; }
public static Validator validator() throws SAXException, IOException { SystemPropertiesUtil.ensure(); if (validator == null) { Schema schema = getSchema(); if (schema == null) throw logger.nullValueError("schema"); validator = schema.newValidator(); validator.setErrorHandler(new CustomErrorHandler()); } return validator; }