Changeset 2550


Ignore:
Timestamp:
04/04/07 04:58:48 (13 years ago)
Author:
curtis
Message:

Move configuration file logic out of ImageReader and ImageWriter into
new ClassList class. Add new constructors to ImageReader and ImageWriter to
specify a ClassList other than the default readers.txt/writers.txt.

Location:
trunk/loci/formats
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/ImageReader.java

    r2538 r2550  
    2626 
    2727import java.awt.image.BufferedImage; 
    28 import java.io.BufferedReader; 
    2928import java.io.IOException; 
    30 import java.io.InputStreamReader; 
    3129import java.util.*; 
    3230 
    3331/** 
    3432 * ImageReader is the master file format reader for all supported formats. 
    35  * It uses one instance of each reader subclass (specified in readers.txt) 
    36  * to identify file formats and read data. 
     33 * It uses one instance of each reader subclass (specified in readers.txt, 
     34 * or other class list source) to identify file formats and read data. 
    3735 * 
    3836 * @author Curtis Rueden ctrueden at wisc.edu 
     
    4341 
    4442  /** List of reader classes. */ 
    45   private static Vector readerClasses; 
     43  private static ClassList readerClasses; 
    4644 
    4745  // -- Static initializer -- 
    4846 
    4947  static { 
    50     // read built-in reader classes from readers.txt file 
    51     BufferedReader in = new BufferedReader(new InputStreamReader( 
    52       ImageReader.class.getResourceAsStream("readers.txt"))); 
    53     readerClasses = new Vector(); 
    54     while (true) { 
    55       String line = null; 
    56       try { line = in.readLine(); } 
    57       catch (IOException exc) { exc.printStackTrace(); } 
    58       if (line == null) break; 
    59  
    60       // ignore characters following # sign (comments) 
    61       int ndx = line.indexOf("#"); 
    62       if (ndx >= 0) line = line.substring(0, ndx); 
    63       line = line.trim(); 
    64       if (line.equals("")) continue; 
    65  
    66       // load reader class 
    67       Class c = null; 
    68       try { c = Class.forName(line); } 
    69       catch (ClassNotFoundException exc) { 
    70         if (FormatReader.debug) exc.printStackTrace(); 
    71       } 
    72       catch (NoClassDefFoundError err) { 
    73         if (FormatReader.debug) err.printStackTrace(); 
    74       } 
    75       catch (ExceptionInInitializerError err) { 
    76         if (FormatReader.debug) err.printStackTrace(); 
    77       } 
    78       if (c == null || !IFormatReader.class.isAssignableFrom(c)) { 
    79         System.err.println("Error: \"" + line + 
    80           "\" is not a valid format reader."); 
    81         continue; 
    82       } 
    83       readerClasses.add(c); 
    84     } 
    85     try { in.close(); } 
    86     catch (IOException exc) { exc.printStackTrace(); } 
     48    // load built-in reader classes from readers.txt file 
     49    try { 
     50      readerClasses = new ClassList("readers.txt", IFormatReader.class); 
     51    } 
     52    catch (IOException exc) { 
     53      exc.printStackTrace(); 
     54      readerClasses = new ClassList(IFormatReader.class); 
     55    } 
    8756  } 
    8857 
     
    10675  // -- Constructors -- 
    10776 
    108   /** Constructs a new ImageReader. */ 
    109   public ImageReader() { this(null); } 
    110  
    11177  /** 
    112    * Constructs a new ImageReader with a MetadataStore. 
    113    * @param store the default metadata store. 
     78   * Constructs a new ImageReader with the default 
     79   * list of reader classes from readers.txt. 
    11480   */ 
    115   public ImageReader(MetadataStore store) { 
    116     // add built-in readers to the list 
     81  public ImageReader() { 
     82    this(readerClasses); 
     83  } 
     84 
     85  /** Constructs a new ImageReader from the given list of reader classes. */ 
     86  public ImageReader(ClassList classList) { 
     87    // add readers to the list 
    11788    Vector v = new Vector(); 
    118     for (int i=0; i<readerClasses.size(); i++) { 
    119       Class readerClass = (Class) readerClasses.elementAt(i); 
     89    Class[] c = classList.getClasses(); 
     90    for (int i=0; i<c.length; i++) { 
    12091      IFormatReader reader = null; 
    12192      try { 
    122         reader = (IFormatReader) readerClass.newInstance(); 
     93        reader = (IFormatReader) c[i].newInstance(); 
    12394      } 
    12495      catch (IllegalAccessException exc) { } 
    12596      catch (InstantiationException exc) { } 
    12697      if (reader == null) { 
    127         System.err.println("Error: " + readerClass.getName() + 
     98        System.err.println("Error: " + c[i].getName() + 
    12899          " cannot be instantiated."); 
    129100        continue; 
     
    133104    readers = new IFormatReader[v.size()]; 
    134105    v.copyInto(readers); 
    135  
    136     if (store != null) setMetadataStore(store); 
    137106  } 
    138107 
     
    515484  } 
    516485 
    517   // -- Static ImageReader API methods -- 
    518  
    519   /** 
    520    * Adds the given class, which must implement IFormatReader, 
    521    * to the reader list. 
    522    * 
    523    * @throws FormatException if the class does not implement 
    524    *   the IFormatReader interface. 
    525    */ 
    526   public static void addReaderType(Class c) throws FormatException { 
    527     if (!IFormatReader.class.isAssignableFrom(c)) { 
    528       throw new FormatException( 
    529         "Reader class must implement IFormatReader interface"); 
    530     } 
    531     readerClasses.add(c); 
    532   } 
    533  
    534   /** Removes the given class from the reader list. */ 
    535   public static void removeReaderType(Class c) { 
    536     readerClasses.remove(c); 
    537   } 
    538  
    539486  // -- Main method -- 
    540487 
  • trunk/loci/formats/ImageWriter.java

    r2538 r2550  
    2727import java.awt.Image; 
    2828import java.awt.image.ColorModel; 
    29 import java.io.BufferedReader; 
    3029import java.io.IOException; 
    31 import java.io.InputStreamReader; 
    3230import java.util.*; 
    3331 
    3432/** 
    3533 * ImageWriter is the master file format writer for all supported formats. 
    36  * It uses one instance of each writer subclass (specified in writers.txt) 
    37  * to identify file formats based on extension and write data. 
     34 * It uses one instance of each writer subclass (specified in writers.txt, 
     35 * or other class list source) to identify file formats and write data. 
    3836 * 
    3937 * @author Curtis Rueden ctrueden at wisc.edu 
     
    4442 
    4543  /** List of writer classes. */ 
    46   protected static Vector writerClasses; 
     44  private static ClassList writerClasses; 
    4745 
    4846  // -- Static initializer -- 
    4947 
    5048  static { 
    51     // read built-in writer classes from writers.txt file 
    52     BufferedReader in = new BufferedReader(new InputStreamReader( 
    53       ImageWriter.class.getResourceAsStream("writers.txt"))); 
    54     writerClasses = new Vector(); 
    55     while (true) { 
    56       String line = null; 
    57       try { line = in.readLine(); } 
    58       catch (IOException exc) { exc.printStackTrace(); } 
    59       if (line == null) break; 
    60  
    61       // ignore characters following # sign (comments) 
    62       int ndx = line.indexOf("#"); 
    63       if (ndx >= 0) line = line.substring(0, ndx); 
    64       line = line.trim(); 
    65       if (line.equals("")) continue; 
    66  
    67       // load writer class 
    68       Class c = null; 
    69       try { c = Class.forName(line); } 
    70       catch (ClassNotFoundException exc) { 
    71         if (FormatWriter.debug) exc.printStackTrace(); 
    72       } 
    73       catch (NoClassDefFoundError err) { 
    74         if (FormatWriter.debug) err.printStackTrace(); 
    75       } 
    76       catch (ExceptionInInitializerError err) { 
    77         if (FormatWriter.debug) err.printStackTrace(); 
    78       } 
    79       if (c == null || !IFormatWriter.class.isAssignableFrom(c)) { 
    80         System.err.println("Error: \"" + line + 
    81           "\" is not a valid format writer."); 
    82         continue; 
    83       } 
    84       writerClasses.add(c); 
    85     } 
    86     try { in.close(); } 
    87     catch (IOException exc) { exc.printStackTrace(); } 
     49    // load built-in writer classes from writers.txt file 
     50    try { 
     51      writerClasses = new ClassList("writers.txt", IFormatWriter.class); 
     52    } 
     53    catch (IOException exc) { 
     54      exc.printStackTrace(); 
     55      writerClasses = new ClassList(IFormatWriter.class); 
     56    } 
    8857  } 
    8958 
     
    9160 
    9261  /** List of supported file format writers. */ 
    93   protected FormatWriter[] writers; 
     62  protected IFormatWriter[] writers; 
    9463 
    9564  /** 
     
    11382  // -- Constructor -- 
    11483 
    115   /** Constructs a new ImageWriter. */ 
     84  /** 
     85   * Constructs a new ImageWriter with the default 
     86   * list of writer classes from writers.txt. 
     87   */ 
    11688  public ImageWriter() { 
    117     // add built-in writers to the list 
     89    this(writerClasses); 
     90  } 
     91 
     92  /** Constructs a new ImageWriter from the given list of writer classes. */ 
     93  public ImageWriter(ClassList classList) { 
     94    // add writers to the list 
    11895    Vector v = new Vector(); 
    119     for (int i=0; i<writerClasses.size(); i++) { 
    120       Class writerClass = (Class) writerClasses.elementAt(i); 
    121       FormatWriter writer = null; 
     96    Class[] c = classList.getClasses(); 
     97    for (int i=0; i<c.length; i++) { 
     98      IFormatWriter writer = null; 
    12299      try { 
    123         writer = (FormatWriter) writerClass.newInstance(); 
     100        writer = (IFormatWriter) c[i].newInstance(); 
    124101      } 
    125102      catch (IllegalAccessException exc) { } 
    126103      catch (InstantiationException exc) { } 
    127104      if (writer == null) { 
    128         System.err.println("Error: " + writerClass.getName() + 
     105        System.err.println("Error: " + c[i].getName() + 
    129106          " cannot be instantiated."); 
    130107        continue; 
     
    132109      v.add(writer); 
    133110    } 
    134     writers = new FormatWriter[v.size()]; 
     111    writers = new IFormatWriter[v.size()]; 
    135112    v.copyInto(writers); 
    136113  } 
     
    144121 
    145122  /** Gets the writer used to save the given file. */ 
    146   public FormatWriter getWriter(String id) throws FormatException { 
     123  public IFormatWriter getWriter(String id) throws FormatException { 
    147124    if (!id.equals(currentId)) { 
    148125      // initialize file 
     
    162139 
    163140  /** Gets the file format writer instance matching the given class. */ 
    164   public FormatWriter getWriter(Class c) { 
     141  public IFormatWriter getWriter(Class c) { 
    165142    for (int i=0; i<writers.length; i++) { 
    166143      if (writers[i].getClass().equals(c)) return writers[i]; 
     
    328305  } 
    329306 
    330   // -- Static ImageWriter API methods -- 
    331  
    332   /** 
    333    * Adds the given class, which must implement IFormatWriter, 
    334    * to the writer list. 
    335    * 
    336    * @throws FormatException if the class does not implement 
    337    *   the IFormatWriter interface. 
    338    */ 
    339   public static void addWriterType(Class c) throws FormatException { 
    340     if (!IFormatWriter.class.isAssignableFrom(c)) { 
    341       throw new FormatException( 
    342         "Writer class must implement IFormatWriter interface"); 
    343     } 
    344     writerClasses.add(c); 
    345   } 
    346  
    347   /** Removes the given class from the writer list. */ 
    348   public static void removeWriterType(Class c) { 
    349     writerClasses.remove(c); 
    350   } 
    351  
    352307  // -- Main method -- 
    353308 
Note: See TracChangeset for help on using the changeset viewer.