List of usage examples for javax.xml.validation Validator setErrorHandler
public abstract void setErrorHandler(ErrorHandler errorHandler);
From source file:org.kuali.kfs.sys.batch.XmlBatchInputFileTypeBase.java
/** * Validates the xml contents against the batch input type schema using the java 1.5 validation package. * //from www .j a v a 2 s . c o m * @param schemaLocation - location of the schema file * @param fileContents - xml contents to validate against the schema */ protected void validateContentsAgainstSchema(String schemaLocation, InputStream fileContents) throws ParseException { // create a SchemaFactory capable of understanding WXS schemas SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // get schemaFile Resource schemaResource = SpringContext.getResource(schemaLocation); // load a WXS schema, represented by a Schema instance Source schemaSource = null; try { schemaSource = new StreamSource(schemaResource.getInputStream()); } catch (IOException e2) { LOG.error("error getting schema stream from url: " + e2.getMessage()); throw new RuntimeException("error getting schema stream from url: " + e2.getMessage(), e2); } Schema schema = null; try { schema = factory.newSchema(schemaSource); } catch (SAXException e) { LOG.error("error occured while setting schema file: " + e.getMessage()); throw new RuntimeException("error occured while setting schema file: " + e.getMessage(), e); } // create a Validator instance, which can be used to validate an instance document Validator validator = schema.newValidator(); validator.setErrorHandler(new XmlErrorHandler()); // validate try { validator.validate(new StreamSource(fileContents)); } catch (SAXException e) { LOG.error("error encountered while parsing xml " + e.getMessage()); throw new ParseException("Schema validation error occured while processing file: " + e.getMessage(), e); } catch (IOException e1) { LOG.error("error occured while validating file contents: " + e1.getMessage()); throw new RuntimeException("error occured while validating file contents: " + e1.getMessage(), e1); } }
From source file:org.kuali.ole.ingest.KrmsXMLSchemaValidator.java
/** * This method return True/False./*w ww. j a v a 2s.co m*/ * This method checks fileContent schema with W3 Xml schema standards,If it matches it return True else return False. * @param inputStream * @return boolean * @throws org.kuali.ole.exception.ParseException * @throws java.io.IOException * @throws org.xml.sax.SAXException */ public boolean validateContentsAgainstSchema(InputStream inputStream) throws ParseException, IOException, SAXException { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source schemaSource = null; schemaSource = new StreamSource(getFileContents()); Schema schema = null; schema = factory.newSchema(schemaSource); Validator validator = schema.newValidator(); validator.setErrorHandler(new XmlErrorHandler()); validator.validate(new StreamSource(inputStream)); return true; } catch (Exception ex) { //TODO: logging required } return false; }
From source file:org.kuali.ole.sys.batch.XmlBatchInputFileTypeBase.java
/** * Validates the xml contents against the batch input type schema using the java 1.5 validation package. * //w w w .ja v a 2s . co m * @param schemaLocation - location of the schema file * @param fileContents - xml contents to validate against the schema */ protected void validateContentsAgainstSchema(String schemaLocation, InputStream fileContents) throws ParseException { // create a SchemaFactory capable of understanding WXS schemas SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // get schemaFile UrlResource schemaResource = null; try { schemaResource = new UrlResource(schemaLocation); } catch (MalformedURLException e2) { LOG.error("error getting schema url: " + e2.getMessage()); throw new RuntimeException("error getting schema url: " + e2.getMessage(), e2); } // load a WXS schema, represented by a Schema instance Source schemaSource = null; try { schemaSource = new StreamSource(schemaResource.getInputStream()); } catch (IOException e2) { LOG.error("error getting schema stream from url: " + e2.getMessage()); throw new RuntimeException("error getting schema stream from url: " + e2.getMessage(), e2); } Schema schema = null; try { schema = factory.newSchema(schemaSource); } catch (SAXException e) { LOG.error("error occured while setting schema file: " + e.getMessage()); throw new RuntimeException("error occured while setting schema file: " + e.getMessage(), e); } // create a Validator instance, which can be used to validate an instance document Validator validator = schema.newValidator(); validator.setErrorHandler(new XmlErrorHandler()); // validate try { validator.validate(new StreamSource(fileContents)); } catch (SAXException e) { LOG.error("error encountered while parsing xml " + e.getMessage()); throw new ParseException("Schema validation error occured while processing file: " + e.getMessage(), e); } catch (IOException e1) { LOG.error("error occured while validating file contents: " + e1.getMessage()); throw new RuntimeException("error occured while validating file contents: " + e1.getMessage(), e1); } }
From source file:org.lilyproject.indexer.model.indexerconf.LilyIndexerConfBuilder.java
public static void validate(InputStream is) throws IndexerConfException { MyErrorHandler errorHandler = new MyErrorHandler(); try {//from ww w . ja v a 2s .co m SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL url = LilyIndexerConfBuilder.class.getClassLoader() .getResource("org/lilyproject/indexer/model/indexerconf/indexerconf.xsd"); Schema schema = factory.newSchema(url); Validator validator = schema.newValidator(); validator.setErrorHandler(errorHandler); validator.validate(new StreamSource(is)); } catch (Exception e) { if (!errorHandler.hasErrors()) { throw new IndexerConfException("Error validating indexer configuration.", e); } // else it will be reported below } if (errorHandler.hasErrors()) { throw new IndexerConfException("The following errors occurred validating the indexer configuration:\n" + errorHandler.getMessage()); } }
From source file:org.modeldriven.fuml.bind.ValidatingUnmarshaler.java
/** * Creates an unmarshaler using the given factories and URL. Loads only the * given (subclass) schema as this is the "root" schema and it should * include any other schema resources it needs, and so on. Note all included * schemas MUST be found at the same class level as the root schema. * /* ww w . j a v a2s. c om*/ * @param url * the Schema URL * @param context * the SAXB context * @param handler * the SAX handler * @param resolver * the SAX resolver * @return the unmarshaler * @throws JAXBException * @throws SAXException */ private Unmarshaller createUnmarshaler(URL url, JAXBContext context, Handler handler, Resolver resolver) throws JAXBException, SAXException { Unmarshaller u = context.createUnmarshaller(); // adds a custom object factory // u.setProperty("com.sun.xml.bind.ObjectFactory",new // ObjectFactoryEx()); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schemaGrammar = schemaFactory.newSchema(url); u.setSchema(schemaGrammar); Validator schemaValidator = schemaGrammar.newValidator(); schemaValidator.setResourceResolver(resolver); schemaValidator.setErrorHandler(handler); return u; }
From source file:org.modeldriven.fuml.bind.ValidatingUnmarshaler.java
/** * Creates an unmarshaler using the given factories and sream. Loads only * the given (subclass) schema as this is the "root" schema and it should * include any other schema resources it needs, and so on. Note all included * schemas MUST be found at the same class level as the root schema. * /*from w w w .j av a 2 s .c o m*/ * @param stream * the Schema stream * @param context * the SAXB context * @param handler * the SAX handler * @param resolver * the SAX resolver * @return the unmarshaler * @throws JAXBException * @throws SAXException */ private Unmarshaller createUnmarshaler(InputStream stream, JAXBContext context, Handler handler, Resolver resolver) throws JAXBException, SAXException { Unmarshaller u = context.createUnmarshaller(); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schemaGrammar = schemaFactory.newSchema(new StreamSource(stream)); u.setSchema(schemaGrammar); Validator schemaValidator = schemaGrammar.newValidator(); schemaValidator.setResourceResolver(resolver); schemaValidator.setErrorHandler(handler); return u; }
From source file:org.n52.ifgicopter.spf.xml.XMLTools.java
/** * @param f the xml file/*ww w . j a v a 2s.co m*/ * @return the parsed {@link Document} */ public static Document parseDocument(File f) { DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = fac.newDocumentBuilder(); } catch (ParserConfigurationException e) { log.warn(null, e); return null; } //get the config as Document Document doc = null; try { doc = builder.parse(f); } catch (SAXException e) { log.warn(null, e); return null; } catch (IOException e) { log.warn(null, e); return null; } /* * do we validate? */ if (!Boolean.parseBoolean(SPFRegistry.getInstance().getConfigProperty(SPFRegistry.VALIDATE_XML_PROP))) { return doc; } SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = null; try { schema = factory.newSchema(); } catch (SAXException e) { log.warn(null, e); return null; } Validator val = schema.newValidator(); val.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { log.warn(null, exception); } @Override public void fatalError(SAXParseException exception) throws SAXException { log.warn(null, exception); } @Override public void error(SAXParseException exception) throws SAXException { warning(exception); } }); /* * do the validation */ try { val.validate(new SAXSource(new InputSource(new FileInputStream(f)))); } catch (FileNotFoundException e) { log.warn(null, e); return null; } catch (SAXException e) { log.warn(null, e); return null; } catch (IOException e) { log.warn(null, e); return null; } return doc; }
From source file:org.openmrs.module.radiology.report.template.XsdMrrtReportTemplateValidator.java
/** * @see MrrtReportTemplateValidator#validate(String) *//*w w w . j ava2 s . co m*/ @Override public void validate(String mrrtTemplate) throws IOException { final Document document = Jsoup.parse(mrrtTemplate, ""); final Elements metatags = document.getElementsByTag("meta"); ValidationResult validationResult = metaTagsValidationEngine.run(metatags); final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema; final Validator validator; try (InputStream in = IOUtils.toInputStream(mrrtTemplate)) { schema = factory.newSchema(getSchemaFile()); validator = schema.newValidator(); validator.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { log.debug(exception.getMessage(), exception); validationResult.addError(exception.getMessage(), ""); } @Override public void error(SAXParseException exception) throws SAXException { log.debug(exception.getMessage(), exception); validationResult.addError(exception.getMessage(), ""); } @Override public void fatalError(SAXParseException exception) throws SAXException { log.debug(exception.getMessage(), exception); validationResult.addError(exception.getMessage(), ""); } }); validator.validate(new StreamSource(in)); validationResult.assertOk(); } catch (SAXException e) { log.error(e.getMessage(), e); throw new APIException("radiology.report.template.validation.error", null, e); } }
From source file:org.plasma.common.bind.ValidatingUnmarshaler.java
/** * Creates an unmarshaler using the given factories and sream. Loads only * the given (subclass) schema as this is the "root" schema and it should * include any other schema resources it needs, and so on. Note all included * schemas MUST be found at the same class level as the root schema. * /* w ww.j a v a2s . c o m*/ * @param stream * the Schema stream * @param context * the SAXB context * @param handler * the SAX handler * @param resolver * the SAX resolver * @return the unmarshaler * @throws JAXBException * @throws SAXException */ private Unmarshaller createUnmarshaler(InputStream[] streams, JAXBContext context, Handler handler, Resolver resolver) throws JAXBException, SAXException { Unmarshaller u = context.createUnmarshaller(); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); StreamSource[] sources = new StreamSource[streams.length]; for (int i = 0; i < streams.length; i++) { sources[i] = new StreamSource(streams[i]); //FIXME: will not resolve relative URI's (included schemas) without this //sources[i].setSystemId(systemId) } Schema schemaGrammar = schemaFactory.newSchema(sources); u.setSchema(schemaGrammar); Validator schemaValidator = schemaGrammar.newValidator(); schemaValidator.setResourceResolver(resolver); schemaValidator.setErrorHandler(handler); return u; }
From source file:org.rimudb.configuration.AbstractXmlLoader.java
/** * @param document/*from w ww .j a v a 2 s. c o m*/ * @param compoundDbSchemaUrl * @return SAXParseException * @throws Exception */ protected SAXParseException validate(Document document, String compoundDbSchemaUrl) throws Exception { // Compile a schema for the XSD SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // Look up the schema URL and find it's local resource file URL schemaURL = null; String schemaPath = RimuDBNamespace.lookupInternalSchema(compoundDbSchemaUrl); if (schemaPath != null) { schemaURL = getClass().getClassLoader().getResource(schemaPath); } else { schemaURL = new URL(compoundDbSchemaUrl); } Schema schema = schemaFactory.newSchema(schemaURL); // Validate the document against the schema Validator validator = schema.newValidator(); validator.setErrorHandler(new StrictErrorHandler()); try { validator.validate(new DOMSource(document), new DOMResult()); } catch (SAXParseException e) { return e; } return null; }