/** * Validate XML data from reader using specified grammar with Jing. * * @param stream XML input document. * @param grammarUrl User supplied path to grammar. * @return Validation report containing all validation info. */ public ValidationReport validateJing(InputStream stream, String grammarUrl) { ValidationReport report = new ValidationReport(); try { report.start(); // Setup validation properties. see Jing interface PropertyMapBuilder properties = new PropertyMapBuilder(); ValidateProperty.ERROR_HANDLER.put(properties, report); // Copied from Jing code ; the Compact syntax seem to have a different // Schema reader. To be investigated. // http://www.thaiopensource.com/relaxng/api/jing/index.html SchemaReader schemaReader = grammarUrl.endsWith(".rnc") ? CompactSchemaReader.getInstance() : null; // Setup driver ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader); // Load schema driver.loadSchema(new InputSource(grammarUrl)); // Validate XML instance driver.validate(new InputSource(stream)); } catch (ExistIOException ex) { logger.error(ex.getCause()); report.setThrowable(ex.getCause()); } catch (Exception ex) { logger.debug(ex); report.setThrowable(ex); } finally { report.stop(); } return report; }
/** * Validate XML data from reader using specified grammar. * * @param grammarUrl User supplied path to grammar. * @param stream XML input. * @return Validation report containing all validation info. */ public ValidationReport validateParse(InputStream stream, String grammarUrl) { logger.debug("Start validation."); ValidationReport report = new ValidationReport(); ValidationContentHandler contenthandler = new ValidationContentHandler(); try { XMLReader xmlReader = getXMLReader(contenthandler, report); if (grammarUrl == null) { // Scenario 1 : no params - use system catalog logger.debug("Validation using system catalog."); xmlReader.setProperty( XMLReaderObjectFactory.APACHE_PROPERTIES_ENTITYRESOLVER, systemCatalogResolver); } else if (grammarUrl.endsWith(".xml")) { // Scenario 2 : path to catalog (xml) logger.debug("Validation using user specified catalog '" + grammarUrl + "'."); eXistXMLCatalogResolver resolver = new eXistXMLCatalogResolver(); resolver.setCatalogList(new String[] {grammarUrl}); xmlReader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_ENTITYRESOLVER, resolver); } else if (grammarUrl.endsWith("/")) { // Scenario 3 : path to collection ("/"): search. logger.debug("Validation using searched grammar, start from '" + grammarUrl + "'."); SearchResourceResolver resolver = new SearchResourceResolver(grammarUrl, brokerPool); xmlReader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_ENTITYRESOLVER, resolver); } else { // Scenario 4 : path to grammar (xsd, dtd) specified. logger.debug("Validation using specified grammar '" + grammarUrl + "'."); AnyUriResolver resolver = new AnyUriResolver(grammarUrl); xmlReader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_ENTITYRESOLVER, resolver); } logger.debug("Validation started."); report.start(); InputSource source = new InputSource(stream); xmlReader.parse(source); logger.debug("Validation stopped."); report.stop(); report.setNamespaceUri(contenthandler.getNamespaceUri()); if (!report.isValid()) { logger.debug("Document is not valid."); } } catch (ExistIOException ex) { logger.error(ex.getCause()); report.setThrowable(ex.getCause()); } catch (Exception ex) { logger.error(ex); report.setThrowable(ex); } finally { report.stop(); logger.debug("Validation performed in " + report.getValidationDuration() + " msec."); } return report; }