//
// ClassList.java
//

/*
LOCI Bio-Formats package for reading and converting biological file formats.
Copyright (C) 2005-@year@ Melissa Linkert, Curtis Rueden, Chris Allan,
Eric Kjellman and Brian Loranger.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.formats;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

/**
 * ClassList is a list of classes for use with ImageReader or ImageWriter,
 * parsed from a configuration file such as readers.txt or writers.txt.
 *
 * @author Curtis Rueden ctrueden at wisc.edu
 */
public class ClassList {

  // -- Fields --

  /** Base class from which all classes are assignable. */
  private Class base;

  /** List of classes. */
  private Vector classes;

  // -- Constructor --

  /**
   * Constructs a list of classes, initially empty.
   * @param base Base class from which all classes are assignable.
   */
  public ClassList(Class base) {
    this.base = base;
    classes = new Vector();
  }

  /**
   * Constructs a list of classes from the given configuration file.
   * @param file Configuration file containing the list of classes.
   * @param base Base class from which all classes are assignable.
   * @throws IOException if the file cannot be read.
   */
  public ClassList(String file, Class base) throws IOException {
    this.base = base;
    classes = new Vector();
    if (file == null) return;

    // read classes from file
    BufferedReader in = new BufferedReader(new InputStreamReader(
      getClass().getResourceAsStream(file)));
    while (true) {
      String line = null;
      line = in.readLine();
      if (line == null) break;

      // ignore characters following # sign (comments)
      int ndx = line.indexOf("#");
      if (ndx >= 0) line = line.substring(0, ndx);
      line = line.trim();
      if (line.equals("")) continue;

      // load reader class
      Class c = null;
      try { c = Class.forName(line); }
      catch (ClassNotFoundException exc) {
        if (FormatReader.debug) exc.printStackTrace();
      }
      catch (NoClassDefFoundError err) {
        if (FormatReader.debug) err.printStackTrace();
      }
      catch (ExceptionInInitializerError err) {
        if (FormatReader.debug) err.printStackTrace();
      }
      if (c == null || (base != null && !base.isAssignableFrom(c))) {
        System.err.println("Error: \"" + line + "\" is not valid.");
        continue;
      }
      classes.add(c);
    }
    in.close();
  }

  // -- ClassList API methods --

  /**
   * Adds the given class, which must be assignable
   * from the base class, to the list.
   *
   * @throws FormatException if the class is not assignable from the base class.
   */
  public void addClass(Class c) throws FormatException {
    if (base != null && !base.isAssignableFrom(c)) {
      throw new FormatException(
        "Class is not assignable from the base class");
    }
    classes.add(c);
  }

  /** Removes the given class from the list. */
  public void removeClass(Class c) {
    classes.remove(c);
  }

  /** Gets the list of classes as an array. */
  public Class[] getClasses() {
    Class[] c = new Class[classes.size()];
    classes.copyInto(c);
    return c;
  }

}

@@ -1308,36 +1308,34 @@
  /** Base class 
-from
+to
  which all class
@@ -1534,36 +1534,34 @@
 base Base class 
-from
+to
  which all class
@@ -1844,20 +1844,18 @@
 e class 
-from
+to
  which a
@@ -3258,20 +3258,18 @@
 le%0A   * 
-from
+to
  the bas
@@ -3347,36 +3347,34 @@
  not assignable 
-from
+to
  the base class.
@@ -3556,20 +3556,18 @@
 ignable 
-from
+to
  the bas

Tweak documentation.