/** * Compiles the list of Bio-Formats reader classes, excluding the * {@link #DO_NOT_CONVERT} blacklist. * * @return true if the reader class list was re-generated. */ private boolean cacheReaderClasses() { final Class<? extends IFormatReader>[] defaultClasses = ImageReader.getDefaultReaderClasses().getClasses(); final int currentHash = Arrays.hashCode(defaultClasses); // If our classList is uninitialized, or the Bio-Formats classList has // changed, compute the current reader classes. if (readerClasses == null || cachedReaderHash != currentHash) { final ClassList<IFormatReader> targetClasses = new ClassList<IFormatReader>(IFormatReader.class); // add reader classes to the list, excluding the blacklist for (final Class<? extends IFormatReader> c : defaultClasses) { if (convert(c)) targetClasses.addClass(c); } readerClasses = targetClasses; cachedReaderHash = currentHash; return true; } return false; }
/** * Appends a class list which must be assignable to the base class */ public void append(ClassList<T> c) { append(Arrays.asList(c.getClasses())); }
/** Add a key/value pair to the list of options.*/ public void addOption(String key, String value) { if (!isWhitelistedKey(key)) { LOGGER.debug("{} is not a whitelisted key", key); } options.put(key, value); }
/** * Prepends a class list which must be assignable to the base class */ public void prepend(ClassList<T> c) { prepend(Arrays.asList(c.getClasses())); }
@Test public void testNullConstructor() throws IOException { c = new ClassList<Iterable>(null, Iterable.class); assertEquals(c.getClasses().length, 0); }
@Test(dataProvider = "escaped lines") public void testParseEscapedLine(String line) throws IOException { c = new ClassList<Iterable>(null, Iterable.class); c.parseLine(line); assertEquals(c.getClasses().length, 0); assertTrue(c.getOptions().isEmpty()); }
@Test public void testParseFile() throws IOException { c = new ClassList<Iterable>(null, Iterable.class); c.parseFile("iterables.txt",ClassListTest.class); assertEquals(c.getClasses().length, 2); }
@Test public void testRemoveClass() throws IOException { c = new ClassList<Iterable>("iterables.txt", Iterable.class, ClassListTest.class); c.removeClass(AbstractList.class); assertEquals(c.getClasses().length, 1); assertEquals(c.getClasses()[0], ArrayList.class); c.removeClass(ArrayList.class); assertEquals(c.getClasses().length, 0); c.removeClass(ArrayList.class); assertEquals(c.getClasses().length, 0); }
@Test public void testAppend() throws IOException { configFile = writeConfigFile("java.util.ArrayList"); c = new ClassList<Iterable>(configFile, Iterable.class, null); assertEquals(c.getClasses().length, 1); assertEquals(c.getClasses()[0], ArrayList.class); String configFile2 = writeConfigFile("java.util.AbstractList"); ClassList<Iterable> c2 = new ClassList<Iterable>(configFile2, Iterable.class, null); c.append(c2); assertEquals(c.getClasses().length, 2); assertEquals(c.getClasses()[0], ArrayList.class); assertEquals(c.getClasses()[1], AbstractList.class); }
@Test public void testPrepend() throws IOException { configFile = writeConfigFile("java.util.ArrayList"); c = new ClassList<Iterable>(configFile, Iterable.class, null); assertEquals(c.getClasses().length, 1); assertEquals(c.getClasses()[0], ArrayList.class); String configFile2 = writeConfigFile("java.util.AbstractList"); ClassList<Iterable> c2 = new ClassList<Iterable>(configFile2, Iterable.class, null); c.prepend(c2); assertEquals(c.getClasses().length, 2); assertEquals(c.getClasses()[0], AbstractList.class); assertEquals(c.getClasses()[1], ArrayList.class); }
/** * Constructs a FileStitcher with the given reader. * @param r The reader to use for reading stitched files. * @param patternIds Whether string ids given should be treated as file * patterns rather than single file paths. */ public FileStitcher(IFormatReader r, boolean patternIds) { super(r); if (r.getClass().getPackage().getName().equals("loci.formats.in")) { ClassList<IFormatReader> classes = new ClassList<IFormatReader>(IFormatReader.class); classes.addClass(r.getClass()); setReaderClassList(classes); } else { reader = DimensionSwapper.makeDimensionSwapper(r); } setUsingPatternIds(patternIds); }
public static ClassList<IFormatReader> getDefaultReaderClasses() { if (defaultClasses == null) { // load built-in reader classes from readers.txt file try { defaultClasses = new ClassList<IFormatReader>("readers.txt", IFormatReader.class); } catch (IOException exc) { defaultClasses = new ClassList<IFormatReader>(IFormatReader.class); LOGGER.info("Could not parse class list; using default classes", exc); } } return defaultClasses; }
@Test public void testSetOption() throws IOException { c = new ClassList<Iterable>(null, Iterable.class); assertEquals(Collections.emptySet(), c.getOptions().keySet()); c.addOption("a", "b"); assertEquals(c.getOptions().keySet(), Collections.singleton("a")); assertEquals(c.getOptions().get("a"), "b"); }
/** Adds the given reader class to this format's supported reader list. */ public void addReader(final Class<IFormatReader> readerClass) { readerClasses.addClass(readerClass); }
@Test(dataProvider = "option string") public void testParseOptions(String options, HashMap map) throws IOException { c = new ClassList<Iterable>(null, Iterable.class); assertEquals(map, c.parseOptions(options)); }
readers.removeClass(ZipReader.class); if (readersPath != null) return new ImageReader(new ClassList<IFormatReader>( readersPath, IFormatReader.class, k)); } catch (IOException e)
/** Constructs a new ImageWriter from the given list of writer classes. */ public ImageWriter(ClassList<IFormatWriter> classList) { // add writers to the list List<IFormatWriter> list = new ArrayList<IFormatWriter>(); Class<? extends IFormatWriter>[] c = classList.getClasses(); for (int i=0; i<c.length; i++) { IFormatWriter writer = null; try { writer = c[i].newInstance(); } catch (IllegalAccessException exc) { } catch (InstantiationException exc) { } if (writer == null) { LOGGER.error("{} cannot be instantiated.", c[i].getName()); continue; } list.add(writer); } writers = new IFormatWriter[list.size()]; list.toArray(writers); }
@Test public void testWhitelistedKeys() throws IOException { c = new ClassList<Iterable>(null, Iterable.class); assertTrue(c.isWhitelistedKey("type")); assertTrue(c.isWhitelistedKey("package.name.type")); assertTrue(c.isWhitelistedKey("type")); assertFalse(c.isWhitelistedKey("type.subtype")); assertFalse(c.isWhitelistedKey("")); assertFalse(c.isWhitelistedKey(".")); } }
ndx = line.indexOf('['); if (ndx >= 0) { o = parseOptions(line.substring(ndx + 1, line.length() - 1)); line = line.substring(0, ndx).trim(); try { Class<?> rawClass = Class.forName(line); c = cast(rawClass); for (Map.Entry<String, String> entry : o.entrySet()) addOption(line + "." + entry.getKey(), entry.getValue());
/** * Constructs a list of classes from the given configuration file. * @param file Configuration file containing the list of classes. * @param base Base class to which all classes are assignable. * @param location Class indicating which package to search for the file. * If {@code null}, 'file' is interpreted as an absolute path name. * @throws IOException if the file cannot be read. */ public ClassList(String file, Class<T> base, Class<?> location) throws IOException { this(base); if (file == null) return; parseFile(file, location); }