Changeset 5073


Ignore:
Timestamp:
04/25/14 14:38:07 (10 years ago)
Author:
Oliver Schonefeld
Message:
  • allow user to supply custom Schematron schema file
  • rename option for setting custom schema cache directory
Location:
CMDIValidator/trunk/src/main/java/eu/clarin/cmdi/validator
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • CMDIValidator/trunk/src/main/java/eu/clarin/cmdi/validator/CMDIValidatorFactory.java

    r5056 r5073  
    22
    33import java.io.File;
     4import java.net.MalformedURLException;
    45import java.net.URL;
    56
     
    3637
    3738
    38     private CMDIValidatorFactory(File cacheDirectory, boolean disableSchematron)
     39    private CMDIValidatorFactory(File cacheDirectory,
     40            File schematronSchemaFile, boolean disableSchematron)
    3941            throws CMDIValidatorInitException {
    4042        /*
     
    9395        if (!disableSchematron) {
    9496            logger.debug("initializing Schematron validator ...");
    95             URL schema = this.getClass().getResource(DEFAULT_SCHEMATRON_SCHEMA);
    96             if (schema == null) {
    97                 throw new CMDIValidatorInitException(
    98                         "cannot locate schematron schema");
     97
     98            URL schema = null;
     99            if (schematronSchemaFile != null) {
     100                if (!schematronSchemaFile.exists()) {
     101                    throw new CMDIValidatorInitException("file '" +
     102                            schematronSchemaFile.getAbsolutePath() +
     103                            "' does not exist");
     104                }
     105                if (!schematronSchemaFile.isFile()) {
     106                    throw new CMDIValidatorInitException("file '" +
     107                            schematronSchemaFile.getAbsolutePath() +
     108                            "' is not a regular file");
     109                }
     110                if (!schematronSchemaFile.canRead()) {
     111                    throw new CMDIValidatorInitException("file '" +
     112                            schematronSchemaFile.getAbsolutePath() +
     113                            "' cannot be read");
     114                }
     115                try {
     116                    schema = schematronSchemaFile.toURI().toURL();
     117                } catch (MalformedURLException e) {
     118                    throw new CMDIValidatorInitException("internal error", e);
     119                }
     120            } else {
     121                schema = this.getClass().getResource(DEFAULT_SCHEMATRON_SCHEMA);
     122                if (schema == null) {
     123                    throw new CMDIValidatorInitException(
     124                            "cannot locate bundled Schematron schema: " +
     125                                    DEFAULT_SCHEMATRON_SCHEMA);
     126                }
    99127            }
    100128            final XsltCompiler compiler = processor.newXsltCompiler();
     
    132160
    133161    public static CMDIValidatorFactory newInstance(File cacheDircetory,
    134             boolean disableSchematron) throws CMDIValidatorInitException {
    135         return new CMDIValidatorFactory(cacheDircetory, disableSchematron);
     162            File schematronSchemaFile, boolean disableSchematron)
     163            throws CMDIValidatorInitException {
     164        return new CMDIValidatorFactory(cacheDircetory, schematronSchemaFile,
     165                disableSchematron);
    136166    }
    137167
     
    139169    public static CMDIValidatorFactory newInstance()
    140170            throws CMDIValidatorInitException {
    141         return new CMDIValidatorFactory(null, false);
     171        return new CMDIValidatorFactory(null, null, false);
    142172    }
    143173
  • CMDIValidator/trunk/src/main/java/eu/clarin/cmdi/validator/tool/CMDIValidatorTool.java

    r5056 r5073  
    4545    private static final char OPT_NO_THREADS            = 'T';
    4646    private static final char OPT_NO_ESTIMATE           = 'E';
    47     private static final char OPT_SCHEMA_CACHE_DIR      = 'C';
     47    private static final char OPT_SCHEMA_CACHE_DIR      = 'c';
    4848    private static final char OPT_NO_SCHEMATRON         = 'S';
    4949    private static final char OPT_SCHEMATRON_FILE       = 's';
     
    6565        File schemaCacheDir       = null;
    6666        boolean disableSchematron = false;
     67        File schematronFile       = null;
    6768
    6869        /*
     
    120121                String dir = line.getOptionValue(OPT_SCHEMA_CACHE_DIR);
    121122                if ((dir == null) || dir.isEmpty()) {
    122                     throw new ParseException("invalid argument for -S");
     123                    throw new ParseException("invalid argument for -" +
     124                            OPT_SCHEMA_CACHE_DIR);
    123125                }
    124126                schemaCacheDir = new File(dir);
     
    126128            if (line.hasOption(OPT_NO_SCHEMATRON)) {
    127129                disableSchematron = true;
     130            }
     131            if (line.hasOption(OPT_SCHEMATRON_FILE)) {
     132                String name = line.getOptionValue(OPT_SCHEMATRON_FILE);
     133                if ((name == null) || name.isEmpty()) {
     134                    throw new ParseException("invalid argument for -" +
     135                            OPT_SCHEMATRON_FILE);
     136                }
     137                schematronFile = new File(name);
    128138            }
    129139
     
    157167                    logger.info("using schema cache directory: {}", schemaCacheDir);
    158168                }
     169                if (schematronFile != null) {
     170                    logger.info("using Schematron schema from file: {}", schematronFile);
     171                }
    159172                final CMDIValidatorFactory factory =
    160173                        CMDIValidatorFactory.newInstance(schemaCacheDir,
    161                                 disableSchematron);
     174                                schematronFile, disableSchematron);
    162175
    163176                /*
     
    340353                .withLongOpt("no-schematron")
    341354                .create(OPT_NO_SCHEMATRON));
    342 //        g3.addOption(OptionBuilder
    343 //                .withDescription("load Schematron validator rules from file")
    344 //                .hasArg()
    345 //                .withArgName("FILE")
    346 //                .withLongOpt("schematron-file")
    347 //                .create(OPT_SCHEMATRON_FILE));
     355        g3.addOption(OptionBuilder
     356                .withDescription("load Schematron schema from file")
     357                .hasArg()
     358                .withArgName("FILE")
     359                .withLongOpt("schematron-file")
     360                .create(OPT_SCHEMATRON_FILE));
    348361        options.addOptionGroup(g3);
    349362        return options;
Note: See TracChangeset for help on using the changeset viewer.