Changeset 1967


Ignore:
Timestamp:
12/22/06 13:43:15 (13 years ago)
Author:
curtis
Message:

Add simple API for adding and removing reader and writer classes from their
respective lists in ImageReader and ImageWriter. Use new API in the ImageJ
importer plugin to avoid an infinite loop when ImageJ tries to hand off a FITS
file to Bio-Formats, which attempts to hand it off to ImageJ, which hands it
off to Bio-Formats...

Location:
trunk/loci
Files:
3 edited

Legend:

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

    r1926 r1967  
    4646 
    4747  /** List of reader classes. */ 
    48   private static Class[] readerClasses; 
     48  private static Vector readerClasses; 
    4949 
    5050  // -- Static initializer -- 
     
    5454    BufferedReader in = new BufferedReader(new InputStreamReader( 
    5555      ImageReader.class.getResourceAsStream("readers.txt"))); 
    56     Vector v = new Vector(); 
     56    readerClasses = new Vector(); 
    5757    while (true) { 
    5858      String line = null; 
     
    7676        continue; 
    7777      } 
    78       v.add(c); 
     78      readerClasses.add(c); 
    7979    } 
    8080    try { in.close(); } 
    8181    catch (IOException exc) { exc.printStackTrace(); } 
    82     readerClasses = new Class[v.size()]; 
    83     v.copyInto(readerClasses); 
    8482  } 
    8583 
     
    126124    Vector v = new Vector(); 
    127125    Hashtable map = null; 
    128     for (int i=0; i<readerClasses.length; i++) { 
     126    for (int i=0; i<readerClasses.size(); i++) { 
     127      Class readerClass = (Class) readerClasses.elementAt(i); 
    129128      IFormatReader reader = null; 
    130129      try { 
    131         reader = (IFormatReader) readerClasses[i].newInstance(); 
     130        reader = (IFormatReader) readerClass.newInstance(); 
    132131        // NB: ensure all readers share the same ID map 
    133132        if (i == 0) map = reader.getIdMap(); 
     
    137136      catch (InstantiationException exc) { } 
    138137      if (reader == null) { 
    139         System.err.println("Error: " + readerClasses[i].getName() + 
     138        System.err.println("Error: " + readerClass.getName() + 
    140139          " cannot be instantiated."); 
    141140        continue; 
     
    525524  } 
    526525 
     526  // -- Static ImageReader API methods -- 
     527 
     528  /** 
     529   * Adds the given class, which must implement IFormatReader, 
     530   * to the reader list. 
     531   * 
     532   * @throws FormatException if the class does not implement 
     533   *   the IFormatReader interface. 
     534   */ 
     535  public static void addReaderType(Class c) throws FormatException { 
     536    if (!IFormatReader.class.isAssignableFrom(c)) { 
     537      throw new FormatException( 
     538        "Reader class must implement IFormatReader interface"); 
     539    } 
     540    readerClasses.add(c); 
     541  } 
     542 
     543  /** Removes the given class from the reader list. */ 
     544  public static void removeReaderType(Class c) { 
     545    readerClasses.remove(c); 
     546  } 
     547 
    527548  // -- Main method -- 
    528549 
  • trunk/loci/formats/ImageWriter.java

    r1810 r1967  
    4646 
    4747  /** List of writer classes. */ 
    48   protected static Class[] writerClasses; 
     48  protected static Vector writerClasses; 
    4949 
    5050  // -- Static initializer -- 
     
    5454    BufferedReader in = new BufferedReader(new InputStreamReader( 
    5555      ImageWriter.class.getResourceAsStream("writers.txt"))); 
    56     Vector v = new Vector(); 
     56    writerClasses = new Vector(); 
    5757    while (true) { 
    5858      String line = null; 
     
    7676        continue; 
    7777      } 
    78       v.add(c); 
     78      writerClasses.add(c); 
    7979    } 
    8080    try { in.close(); } 
    8181    catch (IOException exc) { exc.printStackTrace(); } 
    82     writerClasses = new Class[v.size()]; 
    83     v.copyInto(writerClasses); 
    8482  } 
    8583 
     
    126124    Vector v = new Vector(); 
    127125    Hashtable map = null; 
    128     for (int i=0; i<writerClasses.length; i++) { 
     126    for (int i=0; i<writerClasses.size(); i++) { 
     127      Class writerClass = (Class) writerClasses.elementAt(i); 
    129128      FormatWriter writer = null; 
    130129      try { 
    131         writer = (FormatWriter) writerClasses[i].newInstance(); 
     130        writer = (FormatWriter) writerClass.newInstance(); 
    132131        // NB: ensure all writers share the same ID map 
    133132        if (i == 0) map = writer.getIdMap(); 
     
    137136      catch (InstantiationException exc) { } 
    138137      if (writer == null) { 
    139         System.err.println("Error: " + writerClasses[i].getName() + 
     138        System.err.println("Error: " + writerClass.getName() + 
    140139          " cannot be instantiated."); 
    141140        continue; 
     
    337336  } 
    338337 
     338  // -- Static ImageWriter API methods -- 
     339 
     340  /** 
     341   * Adds the given class, which must implement IFormatWriter, 
     342   * to the writer list. 
     343   * 
     344   * @throws FormatException if the class does not implement 
     345   *   the IFormatWriter interface. 
     346   */ 
     347  public static void addWriterType(Class c) throws FormatException { 
     348    if (!IFormatWriter.class.isAssignableFrom(c)) { 
     349      throw new FormatException( 
     350        "Writer class must implement IFormatWriter interface"); 
     351    } 
     352    writerClasses.add(c); 
     353  } 
     354 
     355  /** Removes the given class from the writer list. */ 
     356  public static void removeWriterType(Class c) { 
     357    writerClasses.remove(c); 
     358  } 
     359 
    339360  // -- Main method -- 
    340361 
  • trunk/loci/plugins/Importer.java

    r1965 r1967  
    4040import javax.swing.*; 
    4141import loci.formats.*; 
     42import loci.formats.in.ImageJReader; 
    4243 
    4344/** 
     
    5152  // -- Constants -- 
    5253 
    53   private static final String VIEW_STANDARD= "Standard ImageJ"; 
     54  private static final String VIEW_STANDARD = "Standard ImageJ"; 
    5455  private static final String VIEW_BROWSER = "4D Data Browser"; 
    5556  private static final String VIEW_IMAGE_5D = "Image5D"; 
     
    6364  // -- Constructor -- 
    6465 
    65   public Importer(LociImporter plugin) { this.plugin = plugin; } 
     66  public Importer(LociImporter plugin) { 
     67    this.plugin = plugin; 
     68    ImageReader.removeReaderType(ImageJReader.class); 
     69  } 
    6670 
    6771  // -- Importer API methods -- 
Note: See TracChangeset for help on using the changeset viewer.