/** * Check a file using the specific format's check criteria. * * @param pFile file to check * @return a list of {@link uk.bl.dpt.qa.flint.checks.CheckResult} */ public List<CheckResult> check(File pFile) { boolean checked = false; String mimetype = getMimetype(pFile); List<CheckResult> results = new ArrayList<CheckResult>(); gLogger.info("Starting to check file {}..", pFile.getName()); for (Format format : formats) { if (format.canCheck(pFile, mimetype)) { gLogger.info("Validating {} with {} checker", pFile.getName(), format.getFormatName()); CheckResult checkResult = format.validationResult(pFile); gLogger.info("check-result: {}", checkResult); results.add(checkResult); checked = true; } } if (!checked) { gLogger.error("Unable to check: {}, mimetype: {}", pFile, mimetype); } return results; }
/** * Gets the accepted mimetypes for each available format. * * @return a Collection of Strings representing these mimetypes * @throws InstantiationException * @throws IllegalAccessException */ public static Collection<String> getAcceptedMimetypes() throws InstantiationException, IllegalAccessException { Collection<String> mTypes = new HashSet<String>(); for (Format f : getAvailableFormats().values()) { mTypes.addAll(f.acceptedMimeTypes()); } return mTypes; }
/** * Create a new FLint object, adding an instance of all formats to the format list for use by * check() * * <p>This constructor can overwrite default policy properties for each available format if the * supplied map contains a key with its name. * * @param policyMap the map to use for overriding defaults * @throws InstantiationException * @throws IllegalAccessException */ public Flint(Map<String, Set<String>> policyMap) throws InstantiationException, IllegalAccessException { formats = getAvailableFormats().values(); for (Format f : formats) { if (f instanceof PolicyAware) { ((PolicyAware) f).setPatternFilter(policyMap.get(f.getFormatName())); } } }
/** * @return a list of available formats, gathered via *reflection* * @throws IllegalAccessException * @throws InstantiationException */ public static Map<String, Format> getAvailableFormats() throws IllegalAccessException, InstantiationException { Map<String, Format> fs = new LinkedHashMap<String, Format>(); Set<Class<? extends Format>> reflections = new Reflections("uk.bl.dpt.qa.flint.formats").getSubTypesOf(Format.class); for (Class<? extends Format> fClass : reflections) { Format f = fClass.newInstance(); gLogger.info("available format {}, as in {}", fClass, fClass); fs.put(f.getFormatName(), f); } return fs; }
/** * Create a new FLint object, adding an instance of all formats to the format list for use by * check() * * <p>This constructor can overwrite default policy properties if there are property files in the * specified `policyDir` that match the formats' file type. * * @param policyDir directory containing policy files ("formatName-policy.properties") * @throws IllegalAccessException * @throws InstantiationException * @throws IOException */ public Flint(File policyDir) throws IllegalAccessException, InstantiationException, IOException { formats = getAvailableFormats().values(); for (Format f : formats) { if (f instanceof PolicyAware) { final String formatName = f.getFormatName(); File[] propsFiles = policyDir.listFiles( new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.equals(formatName + "-policy.properties"); } }); if (propsFiles.length == 1) { ((PolicyAware) f).setPatternFilter(propsFiles[0].getAbsolutePath()); gLogger.info("found and set a custom policy for {}", f); gLogger.debug("the policy should be: {}", propsFiles[0].getAbsolutePath()); } } } }
/** * Gets the Format instance for a given format name * * @param format the format name * @return the Format instance */ public Format getFormat(String format) { for (Format f : this.formats) { if (f.getFormatName().equals(format)) return f; } return null; }