Search in sources :

Example 1 with BaciPropertyLocator

use of alma.acs.cdbChecker.BaciSchemaChecker.BaciPropertyLocator in project ACS by ACS-Community.

the class CDBChecker method validateSchemas.

/**
	 * This method validates the XSD files.
	 * 
	 * @param xsdFilenames names with absolute path of the XSD file to validate.
	 */
protected void validateSchemas(Vector<String> xsdFilenames) {
    System.out.println("*** Will verify XSD files in: " + this.XSDPath);
    // We share the resolver, to benefit more from its cache.
    CDBSchemasResolver resolver = new CDBSchemasResolver(this, schemaFolder + File.pathSeparator + XSDPath);
    ErrorHandler errorHandler = new CDBErrorHandler(this);
    for (String xsdFilename : xsdFilenames) {
        final File xsdFile = new File(xsdFilename);
        if (xsdFile.length() != 0) {
            if (verbose) {
                System.out.print("    " + xsdFilename);
            }
            try {
                validateFileEncoding(xsdFilename);
                SP.reset();
                // not sure why, but this is the legacy behavior
                resolver.setResolveOnlyHttp(true);
                SP.setEntityResolver(resolver);
                SP.setFeature("http://xml.org/sax/features/validation", true);
                SP.setFeature("http://apache.org/xml/features/validation/schema", true);
                SP.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
                SP.setFeature("http://xml.org/sax/features/namespaces", true);
                SP.setErrorHandler(errorHandler);
                SP.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", "http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema.xsd");
                FileInputStream fis = new FileInputStream(xsdFile);
                InputSource inputSource = new InputSource(fis);
                inputSource.setSystemId("file:///" + xsdFile.getAbsolutePath());
                SP.parse(inputSource);
                fis.close();
                try {
                    // Now we know that the schema is technically valid (well, it does not seem to check xsd imports...)
                    // Still we have to check special requirements for CharacteristicComponent XSDs.
                    // This second check probably includes the first check's functionality, so that the
                    // first check could be removed once we have verified XSOM's xsd error reporting
                    // and hooked up the shared error handler in BaciSchemaChecker.
                    // here we want to resolve all schemas
                    resolver.setResolveOnlyHttp(false);
                    BaciSchemaChecker baciChecker = new BaciSchemaChecker(xsdFile, resolver, errorHandler, logger);
                    List<BaciPropertyLocator> badProps = baciChecker.findBaciPropsOutsideCharacteristicComp();
                    if (!badProps.isEmpty()) {
                        // Reduce the available error output to show only xsd element(s), not the individual baci properties
                        Set<String> badXsdElementNames = new HashSet<String>();
                        for (BaciPropertyLocator badProp : badProps) {
                            badXsdElementNames.add(badProp.elementName);
                        }
                        System.out.println(xsdFilename + ": illegal use of baci properties in xsd elements that are not derived from baci:CharacteristicComponent. " + "Offending element(s): " + StringUtils.join(badXsdElementNames, ' '));
                        errorFlag = true;
                        globalErrorFlag = true;
                    }
                } catch (SAXException ex) {
                // ignore SAXException coming from BaciSchemaChecker, because we don't want to report errors
                // twice when the xsd file is really messed up. 
                // There are cases where xerces reports a normal error but XSOM reports a fatal error and throws this exception.
                }
                if (verbose && !errorFlag) {
                    System.out.println("[OK]");
                }
            } catch (SAXException e) {
                System.out.println("Caught a SAXException in validateSchemas: ");
                e.printStackTrace(System.out);
            } catch (IOException e) {
                System.out.println("[IOException] Probably " + xsdFilename + " doesn't exists.");
            }
        } else {
            System.out.print(xsdFilename + ": [Warning] file is empty.\n");
        }
    }
    // back to legacy mode again... yuck, our resolver still sticks to the "SP" field and will be used elsewhere!
    resolver.setResolveOnlyHttp(true);
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) BaciPropertyLocator(alma.acs.cdbChecker.BaciSchemaChecker.BaciPropertyLocator) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) BaciSchemaChecker(alma.acs.cdbChecker.BaciSchemaChecker) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

BaciSchemaChecker (alma.acs.cdbChecker.BaciSchemaChecker)1 BaciPropertyLocator (alma.acs.cdbChecker.BaciSchemaChecker.BaciPropertyLocator)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ErrorHandler (org.xml.sax.ErrorHandler)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1