Changeset 3108


Ignore:
Timestamp:
08/27/07 21:35:03 (12 years ago)
Author:
curtis
Message:
  1. Cleaned up MetadataStore and MetadataRetrieve interface usage:
    • Renamed OMEXMLMetadataStore to OMEXMLMetadata.
    • Created MetadataTools utility class with conversion methods between OMEXMLMetadata, OME-XML string, and other ("foreign") MetadataStore and MetadataRetrieve object types.
    • Migrated existing convertMetadata(MetadataRetrieve, MetadataStore) method from FormatTools to MetadataTools.
    • Also included useful reflection methods for working with OMEXMLMetadata objects without depending on loci.formats.ome package at compile time.
    • Reworked places that uses OMEXMLMetadata objects via reflection to use new MetadataTools utility methods instead.
    • Reworked OMETiffReader and OMETiffWriter to be more flexible about their assigned MetadataStore and MetadataRetrieve objects, respectively; using the MetadataTools convertMetadata functionality, even foreign metadata object implementations are usable.
  2. Eliminated a few catch-all exception handling blocks, but still some left.
  3. Some miscellaneous cleanup and tweaks.
  4. Given the scope of the changes, I probably broke something. If anything stops working for you, please let me know.
Location:
trunk/loci/formats
Files:
1 added
21 edited
1 moved

Legend:

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

    r3079 r3108  
    2828import java.awt.image.BufferedImage; 
    2929import java.io.IOException; 
    30 import java.lang.reflect.Method; 
    3130import java.util.Arrays; 
    3231import java.util.Hashtable; 
    33 import org.xml.sax.ErrorHandler; 
    34 import org.xml.sax.SAXParseException; 
    3532 
    3633/** 
     
    156153    if (omexml) { 
    157154      reader.setOriginalMetadataPopulated(true); 
    158       try { 
    159         // NB: avoid dependencies on optional loci.formats.ome package 
    160         Class c = Class.forName("loci.formats.ome.OMEXMLMetadataStore"); 
    161         MetadataStore ms = (MetadataStore) c.newInstance(); 
    162         reader.setMetadataStore(ms); 
    163       } 
    164       catch (Throwable t) { 
    165         // NB: error messages for missing OME-Java are printed later 
    166       } 
     155      MetadataStore store = MetadataTools.createOMEXMLMetadata(); 
     156      if (store != null) reader.setMetadataStore(store); 
    167157    } 
    168158 
     
    533523      LogTools.println("Generating OME-XML"); 
    534524      MetadataStore ms = reader.getMetadataStore(); 
    535  
    536       // NB: avoid dependencies on optional loci.formats.ome package 
    537       if (ms.getClass().getName().equals( 
    538         "loci.formats.ome.OMEXMLMetadataStore")) 
    539       { 
    540         // output OME-XML 
    541         String xml = null; 
    542         try { 
    543           Method m = ms.getClass().getMethod("dumpXML", (Class[]) null); 
    544           xml = (String) m.invoke(ms, (Object[]) null); 
    545           LogTools.println(FormatTools.indentXML(xml)); 
    546         } 
    547         catch (Throwable t) { 
    548           LogTools.println("Error generating OME-XML:"); 
    549           LogTools.trace(t); 
    550         } 
    551  
    552         // check Java version (XML validation only works in Java 1.5+) 
    553         String version = System.getProperty("java.version"); 
    554         int dot = version.indexOf("."); 
    555         if (dot >= 0) dot = version.indexOf(".", dot + 1); 
    556         float ver = Float.NaN; 
    557         if (dot >= 0) { 
    558           try { 
    559             ver = Float.parseFloat(version.substring(0, dot)); 
    560           } 
    561           catch (NumberFormatException exc) { } 
    562         } 
    563         if (ver != ver) { 
    564           LogTools.println("Warning: cannot determine if Java version\"" + 
    565             version + "\" supports Java v1.5. OME-XML validation may fail."); 
    566         } 
    567  
    568         if (ver != ver || ver >= 1.5f) { 
    569           // validate OME-XML (Java 1.5+ only) 
    570           LogTools.println("Validating OME-XML"); 
    571  
    572           // use reflection to avoid dependency on optional 
    573           // org.openmicroscopy.xml or javax.xml.validation packages 
    574           ReflectedUniverse r = new ReflectedUniverse(); 
    575  
    576           try { 
    577             // look up a factory for the W3C XML Schema language 
    578             r.setVar("schemaPath", "http://www.w3.org/2001/XMLSchema"); 
    579             r.exec("import javax.xml.validation.SchemaFactory"); 
    580             r.exec("factory = SchemaFactory.newInstance(schemaPath)"); 
    581  
    582             // compile the schema 
    583             r.exec("import java.net.URL"); 
    584             r.setVar("omePath", 
    585               "http://www.openmicroscopy.org/XMLschemas/OME/FC/ome.xsd"); 
    586             r.exec("schemaLocation = new URL(omePath)"); 
    587             r.exec("schema = factory.newSchema(schemaLocation)"); 
    588  
    589             // HACK - workaround for weird Linux bug preventing use of 
    590             // schema.newValidator() method even though it is "public final" 
    591             r.setAccessibilityIgnored(true); 
    592  
    593             // get a validator from the schema 
    594             r.exec("validator = schema.newValidator()"); 
    595  
    596             // prepare the XML source 
    597             r.setVar("ms", ms); 
    598             r.exec("root = ms.getRoot()"); 
    599             r.exec("import java.io.StringReader"); 
    600             r.setVar("xml", xml); 
    601             r.exec("reader = new StringReader(xml)"); 
    602             r.exec("import org.xml.sax.InputSource"); 
    603             r.exec("is = new InputSource(reader)"); 
    604             r.exec("import javax.xml.transform.sax.SAXSource"); 
    605             r.exec("source = new SAXSource(is)"); 
    606  
    607             // validate the OME-XML 
    608             ValidationHandler handler = new ValidationHandler(); 
    609             r.setVar("handler", handler); 
    610             r.exec("validator.setErrorHandler(handler)"); 
    611             r.exec("validator.validate(source)"); 
    612             if (handler.ok()) LogTools.println("No validation errors found."); 
    613           } 
    614           catch (ReflectException exc) { 
    615             LogTools.println("Error validating OME-XML:"); 
    616             LogTools.trace(exc); 
    617           } 
    618         } 
     525      if (MetadataTools.isOMEXMLMetadata(ms)) { 
     526        String xml = MetadataTools.getOMEXML((MetadataRetrieve) ms); 
     527        LogTools.println(MetadataTools.indentXML(xml)); 
     528        MetadataTools.validateOMEXML(xml); 
    619529      } 
    620530      else { 
     
    659569    ImageReader reader = new ImageReader(); 
    660570    reader.setOriginalMetadataPopulated(true); 
    661  
    662     try { 
    663       Class c = Class.forName("loci.formats.ome.OMEXMLMetadataStore"); 
    664       MetadataStore ms = (MetadataStore) c.newInstance(); 
    665       reader.setMetadataStore(ms); 
    666     } 
    667     catch (Throwable t) { 
    668       LogTools.println("OME-Java library not found."); 
    669     } 
     571    MetadataStore store = MetadataTools.createOMEXMLMetadata(); 
     572    if (store == null) LogTools.println("OME-Java library not found."); 
     573    else reader.setMetadataStore(store); 
    670574 
    671575    reader.setId(in); 
    672576    LogTools.print("[" + reader.getFormat() + "] -> " + out + " "); 
    673577 
    674     MetadataStore store = reader.getMetadataStore(); 
     578    store = reader.getMetadataStore(); 
    675579    if (store instanceof MetadataRetrieve) { 
    676580      writer.setMetadataRetrieve((MetadataRetrieve) store); 
     
    707611  } 
    708612 
    709   // -- Helper classes -- 
    710  
    711613  /** Used by testRead to echo status messages to the console. */ 
    712614  private static class StatusEchoer implements StatusListener { 
     
    726628  } 
    727629 
    728   /** Used by testRead to handle XML validation errors. */ 
    729   private static class ValidationHandler implements ErrorHandler { 
    730     private boolean ok = true; 
    731     public boolean ok() { return ok; } 
    732     public void error(SAXParseException e) { 
    733       LogTools.println("error: " + e.getMessage()); 
    734       ok = false; 
    735     } 
    736     public void fatalError(SAXParseException e) { 
    737       LogTools.println("fatal error: " + e.getMessage()); 
    738       ok = false; 
    739     } 
    740     public void warning(SAXParseException e) { 
    741       LogTools.println("warning: " + e.getMessage()); 
    742       ok = false; 
    743     } 
    744   } 
    745  
    746630} 
  • trunk/loci/formats/FormatReader.java

    r3104 r3108  
    2727import java.awt.image.BufferedImage; 
    2828import java.io.IOException; 
    29 import java.lang.reflect.Method; 
    3029import java.util.*; 
    3130 
     
    173172 
    174173    if (saveOriginalMetadata) { 
    175       try { 
    176         MetadataStore store = getMetadataStore(); 
    177         if (store.getClass().getName().equals( 
    178           "loci.formats.ome.OMEXMLMetadataStore")); 
    179         { 
    180           Method m = store.getClass().getMethod("populateOriginalMetadata", 
    181             new Class[] {String.class, String.class}); 
    182           m.invoke(store, new Object[] {key, value.toString()}); 
    183         } 
    184       } 
    185       catch (Throwable t) { 
    186         debug("Error populating OME-XML"); 
    187         trace(t); 
     174      MetadataStore store = getMetadataStore(); 
     175      if (MetadataTools.isOMEXMLMetadata(store)) { 
     176        MetadataTools.populateOriginalMetadata(store, key, value.toString()); 
    188177      } 
    189178    } 
  • trunk/loci/formats/FormatTools.java

    r3104 r3108  
    2424 
    2525package loci.formats; 
    26  
    27 import java.util.StringTokenizer; 
    2826 
    2927/** 
     
    351349   */ 
    352350  public static String getPixelTypeString(int pixelType) { 
    353     return pixelType < 0 || pixelType >= pixelTypes.length ? 
    354       "unknown (" + pixelType + ")" : pixelTypes[pixelType]; 
     351    if (pixelType < 0 || pixelType >= pixelTypes.length) { 
     352      throw new IllegalArgumentException("Unknown pixel type: " + pixelType); 
     353    } 
     354    return pixelTypes[pixelType]; 
    355355  } 
    356356 
    357357  /** 
    358358   * Retrieves how many bytes per pixel the current plane or section has. 
    359    * @param type the pixel type as retrieved from 
    360    *   {@link IFormatReader#getPixelType(String)}. 
     359   * @param pixelType the pixel type as retrieved from 
     360   *   {@link IFormatReader#getPixelType()}. 
    361361   * @return the number of bytes per pixel. 
    362362   * @see IFormatReader#getPixelType(String) 
    363363   */ 
    364   public static int getBytesPerPixel(int type) { 
    365     switch (type) { 
     364  public static int getBytesPerPixel(int pixelType) { 
     365    switch (pixelType) { 
    366366      case INT8: 
    367367      case UINT8: 
     
    377377        return 8; 
    378378    } 
    379     throw new RuntimeException("Unknown type with id: '" + type + "'"); 
    380   } 
    381  
    382   // -- Utility methods - XML -- 
    383  
    384   /** Indents XML to be more readable. */ 
    385   public static String indentXML(String xml) { return indentXML(xml, 3); } 
    386  
    387   /** Indents XML by the given spacing to be more readable. */ 
    388   public static String indentXML(String xml, int spacing) { 
    389     int indent = 0; 
    390     StringBuffer sb = new StringBuffer(); 
    391     StringTokenizer st = new StringTokenizer(xml, "<>", true); 
    392     boolean element = false; 
    393     while (st.hasMoreTokens()) { 
    394       String token = st.nextToken().trim(); 
    395       if (token.equals("")) continue; 
    396       if (token.equals("<")) { 
    397         element = true; 
    398         continue; 
    399       } 
    400       if (element && token.equals(">")) { 
    401         element = false; 
    402         continue; 
    403       } 
    404       if (element && token.startsWith("/")) indent -= spacing; 
    405       for (int j=0; j<indent; j++) sb.append(" "); 
    406       if (element) sb.append("<"); 
    407       sb.append(token); 
    408       if (element) sb.append(">"); 
    409       sb.append("\n"); 
    410       if (element && !token.startsWith("?") && 
    411         !token.startsWith("/") && !token.endsWith("/")) 
    412       { 
    413         indent += spacing; 
    414       } 
    415     } 
    416     return sb.toString(); 
     379    throw new IllegalArgumentException("Unknown pixel type: " + pixelType); 
    417380  } 
    418381 
     
    452415  } 
    453416 
    454   // -- Utility methods -- metadata conversion -- 
    455  
    456   /** 
    457    * Copies information from a metadata retrieval object 
    458    * (source) into a metadata store (destination). 
    459    */ 
    460   public static void convertMetadata(MetadataRetrieve source, 
    461     MetadataStore dest) 
    462   { 
    463     Integer ii = null; 
    464     int globalPixCount = 0; 
    465  
    466     for (int i=0; i<source.getImageCount(); i++) { 
    467       ii = new Integer(i); 
    468       dest.setImage(source.getImageName(ii), source.getCreationDate(ii), 
    469         source.getDescription(ii), ii); 
    470  
    471       dest.setDimensions(source.getPixelSizeX(ii), 
    472         source.getPixelSizeY(ii), source.getPixelSizeZ(ii), 
    473         source.getPixelSizeC(ii), source.getPixelSizeT(ii), ii); 
    474  
    475       for (int j=0; j<source.getPixelsCount(ii); j++) { 
    476         Integer p = new Integer(j); 
    477         dest.setPixels(source.getSizeX(ii), source.getSizeY(ii), 
    478           source.getSizeZ(ii), source.getSizeC(ii), 
    479           source.getSizeT(ii), 
    480           new Integer(pixelTypeFromString(source.getPixelType(ii))), 
    481           source.getBigEndian(ii), source.getDimensionOrder(ii), ii, p); 
    482  
    483         dest.setDisplayOptions(source.getZoom(ii), 
    484           source.isRedChannelOn(ii), source.isGreenChannelOn(ii), 
    485           source.isBlueChannelOn(ii), source.isDisplayRGB(ii), 
    486           source.getColorMap(ii), source.getZStart(ii), 
    487           source.getZStop(ii), source.getTStart(ii), 
    488           source.getTStop(ii), ii, p, new Integer(0), new Integer(1), 
    489             new Integer(2), new Integer(0)); 
    490  
    491         Integer globalPix = new Integer(globalPixCount); 
    492         for (int ch=0; ch<source.getChannelCount(globalPix); ch++) { 
    493           Integer c = new Integer(ch); 
    494           dest.setLogicalChannel(ch, source.getChannelName(globalPix, c), 
    495             null, null, null, null, null, null, null, null, null, null, null, 
    496             source.getPhotometricInterpretation(globalPix, c), 
    497             source.getMode(globalPix, c), null, null, null, null, null, 
    498             source.getEmWave(globalPix, c), source.getExWave(globalPix, c), 
    499             null, source.getChannelNDFilter(globalPix, c), globalPix); 
    500  
    501           dest.setChannelGlobalMinMax(ch, source.getGlobalMin(globalPix, c), 
    502             source.getGlobalMax(globalPix, c), globalPix); 
    503  
    504           dest.setDisplayChannel(c, source.getBlackLevel(globalPix, c), 
    505             source.getWhiteLevel(globalPix, c), source.getGamma(globalPix, c), 
    506             globalPix); 
    507         } 
    508  
    509         globalPixCount++; 
    510       } 
    511  
    512       dest.setImagingEnvironment(source.getTemperature(ii), 
    513         source.getAirPressure(ii), source.getHumidity(ii), 
    514         source.getCO2Percent(ii), ii); 
    515     } 
    516  
    517     for (int i=0; i<source.getExperimenterCount(); i++) { 
    518       ii = new Integer(i); 
    519       dest.setExperimenter(source.getFirstName(ii), 
    520         source.getLastName(ii), source.getEmail(ii), 
    521         source.getInstitution(ii), source.getDataDirectory(ii), 
    522         source.getGroup(ii), ii); 
    523     } 
    524  
    525     for (int i=0; i<source.getGroupCount(); i++) { 
    526       ii = new Integer(i); 
    527       dest.setGroup(source.getGroupName(ii), source.getLeader(ii), 
    528         source.getContact(ii), ii); 
    529     } 
    530  
    531     for (int i=0; i<source.getInstrumentCount(); i++) { 
    532       ii = new Integer(i); 
    533       dest.setInstrument(source.getManufacturer(ii), 
    534         source.getModel(ii), source.getSerialNumber(ii), 
    535         source.getType(ii), ii); 
    536     } 
    537  
    538     for (int i=0; i<source.getDisplayROICount(); i++) { 
    539       ii = new Integer(i); 
    540       dest.setDisplayROI(source.getX0(ii), source.getY0(ii), 
    541         source.getZ0(ii), source.getX1(ii), source.getY1(ii), 
    542         source.getZ1(ii), source.getT0(ii), source.getT1(ii), 
    543         source.getDisplayOptions(ii), ii); 
    544     } 
    545  
    546     for (int i=0; i<source.getStageLabelCount(); i++) { 
    547       ii = new Integer(i); 
    548       dest.setStageLabel(source.getStageName(ii), source.getStageX(ii), 
    549         source.getStageY(ii), source.getStageZ(ii), ii); 
    550     } 
    551  
    552     ii = null; 
    553  
    554     dest.setPlaneInfo(0, 0, 0, source.getTimestamp(ii, ii, ii, ii), 
    555       source.getExposureTime(ii, ii, ii, ii), ii); 
    556  
    557     dest.setLightSource(source.getLightManufacturer(ii), 
    558       source.getLightModel(ii), source.getLightSerial(ii), ii, ii); 
    559  
    560     dest.setLaser(source.getLaserType(ii), source.getLaserMedium(ii), 
    561       source.getLaserWavelength(ii), source.isFrequencyDoubled(ii), 
    562       source.isTunable(ii), source.getPulse(ii), 
    563       source.getPower(ii), ii, ii, ii, ii); 
    564  
    565     dest.setFilament(source.getFilamentType(ii), 
    566       source.getFilamentPower(ii), ii, ii); 
    567  
    568     dest.setArc(source.getArcType(ii), source.getArcPower(ii), ii, ii); 
    569  
    570     dest.setDetector(source.getDetectorManufacturer(ii), 
    571       source.getDetectorModel(ii), source.getDetectorSerial(ii), 
    572       source.getDetectorType(ii), source.getDetectorGain(ii), 
    573       source.getDetectorVoltage(ii), 
    574       source.getDetectorOffset(ii), ii, ii); 
    575  
    576     dest.setObjective(source.getObjectiveManufacturer(ii), 
    577       source.getObjectiveModel(ii), source.getObjectiveSerial(ii), 
    578       source.getLensNA(ii), 
    579       source.getObjectiveMagnification(ii), ii, ii); 
    580  
    581     dest.setExcitationFilter(source.getExcitationManufacturer(ii), 
    582       source.getExcitationModel(ii), source.getExcitationLotNumber(ii), 
    583       source.getExcitationType(ii), ii); 
    584  
    585     dest.setDichroic(source.getDichroicManufacturer(ii), 
    586       source.getDichroicModel(ii), source.getDichroicLotNumber(ii), ii); 
    587  
    588     dest.setEmissionFilter(source.getEmissionManufacturer(ii), 
    589       source.getEmissionModel(ii), source.getEmissionLotNumber(ii), 
    590       source.getEmissionType(ii), ii); 
    591  
    592     dest.setFilterSet(source.getFilterSetManufacturer(ii), 
    593       source.getFilterSetModel(ii), 
    594       source.getFilterSetLotNumber(ii), ii, ii); 
    595  
    596     dest.setOTF(source.getOTFSizeX(ii), source.getOTFSizeY(ii), 
    597       source.getOTFPixelType(ii), source.getOTFPath(ii), 
    598       source.getOTFOpticalAxisAverage(ii), ii, ii, ii, ii); 
    599   } 
    600  
    601417} 
  • trunk/loci/formats/FormatWriter.java

    r3015 r3108  
    5757  protected boolean initialized; 
    5858 
    59   protected MetadataRetrieve retrieve; 
     59  protected MetadataRetrieve metadataRetrieve; 
    6060 
    6161  // -- Constructors -- 
     
    9696 
    9797  /* @see IFormatWriter#setMetadataRetrieve(MetadataRetrieve) */ 
    98   public void setMetadataRetrieve(MetadataRetrieve r) { 
     98  public void setMetadataRetrieve(MetadataRetrieve retrieve) { 
    9999    FormatTools.assertId(currentId, false, 1); 
    100     retrieve = r; 
     100    metadataRetrieve = retrieve; 
     101  } 
     102 
     103  /* @see IFormatWriter#getMetadataRetrieve() */ 
     104  public MetadataRetrieve getMetadataRetrieve() { 
     105    return metadataRetrieve; 
    101106  } 
    102107 
  • trunk/loci/formats/IFormatReader.java

    r3083 r3108  
    291291   * assured that this method will <b>never</b> return a <code>null</code> 
    292292   * metadata store. 
    293    * @return a metadata store implementation. 
     293   * @return A metadata store implementation. 
    294294   */ 
    295295  MetadataStore getMetadataStore(); 
     
    299299   * all file parsing has been performed by the reader prior to retrieval. 
    300300   * Requests for a full populated root object should be made using this method. 
    301    * @return current metadata store's root object fully populated. 
     301   * @return Current metadata store's root object fully populated. 
    302302   */ 
    303303  Object getMetadataStoreRoot(); 
  • trunk/loci/formats/IFormatWriter.java

    r3015 r3108  
    7171  boolean canDoStacks(); 
    7272 
    73   /** Sets the retrievable metadata. */ 
     73  /** 
     74   * Sets the metadata retrieval object from 
     75   * which to retrieve standardized metadata. 
     76   */ 
    7477  void setMetadataRetrieve(MetadataRetrieve r); 
     78 
     79  /** 
     80   * Retrieves the current metadata retrieval object for this writer. You can 
     81   * be assured that this method will <b>never</b> return a <code>null</code> 
     82   * metadata retrieval object. 
     83   * @return A metadata retrieval object. 
     84   */ 
     85  MetadataRetrieve getMetadataRetrieve(); 
    7586 
    7687  /** Sets the color model. */ 
  • trunk/loci/formats/ImageReader.java

    r3015 r3108  
    371371  } 
    372372 
    373   /* @see IFormatReader#getMetadataStore() */ 
    374   public MetadataStore getMetadataStore() { 
    375     FormatTools.assertId(currentId, true, 2); 
    376     return getReader().getMetadataStore(); 
    377   } 
    378  
    379   /* @see IFormatReader#getMetadataStoreRoot() */ 
    380   public Object getMetadataStoreRoot() { 
    381     FormatTools.assertId(currentId, true, 2); 
    382     return getReader().getMetadataStoreRoot(); 
    383   } 
    384  
    385373  /* @see IFormatReader#close(boolean) */ 
    386374  public void close(boolean fileOnly) throws IOException { 
     
    468456    FormatTools.assertId(currentId, false, 2); 
    469457    for (int i=0; i<readers.length; i++) readers[i].setMetadataStore(store); 
     458  } 
     459 
     460  /* @see IFormatReader#getMetadataStore() */ 
     461  public MetadataStore getMetadataStore() { 
     462    FormatTools.assertId(currentId, true, 2); 
     463    return getReader().getMetadataStore(); 
     464  } 
     465 
     466  /* @see IFormatReader#getMetadataStoreRoot() */ 
     467  public Object getMetadataStoreRoot() { 
     468    FormatTools.assertId(currentId, true, 2); 
     469    return getReader().getMetadataStoreRoot(); 
    470470  } 
    471471 
  • trunk/loci/formats/ImageWriter.java

    r3015 r3108  
    205205  } 
    206206 
     207  /* @see IFormatReader#getMetadataStore() */ 
     208  public MetadataRetrieve getMetadataRetrieve() { 
     209    return getWriter().getMetadataRetrieve(); 
     210  } 
     211 
    207212  /* @see IFormatWriter#setColorModel(ColorModel) */ 
    208213  public void setColorModel(ColorModel cm) { 
  • trunk/loci/formats/LegacyQTTools.java

    r3104 r3108  
    146146      r.exec("import quicktime.util.QTHandle"); 
    147147      r.exec("import quicktime.util.RawEncodedImage"); 
    148       r.exec("import quicktime.util.EndianOrder");  
     148      r.exec("import quicktime.util.EndianOrder"); 
    149149    } 
    150150    catch (ExceptionInInitializerError err) { 
     
    158158    catch (Throwable t) { 
    159159      noQT = true; 
     160      if (FormatHandler.debug) LogTools.trace(t); 
    160161    } 
    161162    finally { 
    162163      if (needClose) { 
    163164        try { r.exec("QTSession.close()"); } 
    164         catch (Throwable t) { } 
     165        catch (Throwable t) { 
     166          if (FormatHandler.debug) LogTools.trace(t); 
     167        } 
    165168      } 
    166169      initialized = true; 
  • trunk/loci/formats/gui/DataConverter.java

    r3105 r3108  
    3131import java.awt.image.*; 
    3232import java.io.*; 
    33 //import java.util.Vector; 
    3433import javax.swing.*; 
    3534import javax.swing.border.EmptyBorder; 
    3635import javax.swing.event.*; 
    3736import loci.formats.*; 
    38 //import loci.formats.ome.OMEXMLMetadataStore; 
    39 //import loci.formats.out.TiffWriter; 
    40 //import org.openmicroscopy.xml.*; 
    41 //import org.openmicroscopy.xml.st.*; 
    4237 
    4338/** 
     
    473468 
    474469      swap.swapDimensions(order); 
    475  
    476       // CTR TODO this code is probably no longer necessary 
    477 //      OMEXMLMetadataStore store = new OMEXMLMetadataStore(); 
    478 //      store.createRoot(); 
    479 //      swap.close(); 
    480 //      swap.setMetadataStore(store); 
    481470 
    482471      // determine internal and external dimensions for each axis 
     
    621610              } 
    622611            } 
    623  
    624             // if we're writing a TIFF file, insert an OME-XML block 
    625             // CTR TODO fix this code; it is wrong 
    626             // First of all, it writes OMECA-XML instead of OME-XML. 
    627             // Secondly, this logic should not be in the DataConverter itself; 
    628             // DataConverter should just use OMETiffWriter, which handles it. 
    629 //            if (writer.getWriter(outFile) instanceof TiffWriter) { 
    630 //              RandomAccessFile raf = new RandomAccessFile(outFile, "rw"); 
    631 // 
    632 //              OMENode root = (OMENode) 
    633 //                ((OMEXMLMetadataStore) swap.getMetadataStore()).getRoot(); 
    634 // 
    635 //              // add TiffData element here 
    636 //              Vector images = root.getChildNodes("Image"); 
    637 //              for (int p=0; p<images.size(); p++) { 
    638 //                PixelsNode pix = 
    639 //                  (PixelsNode) ((ImageNode) images.get(p)).getDefaultPixels(); 
    640 //                DOMUtil.createChild(pix.getDOMElement(), "TiffData"); 
    641 //              } 
    642 // 
    643 //              TiffTools.overwriteIFDValue(raf, 0, TiffTools.IMAGE_DESCRIPTION, 
    644 //                root.writeOME(true)); 
    645 //              raf.close(); 
    646 //            } 
    647612          } 
    648613        } 
  • trunk/loci/formats/gui/ImageViewer.java

    r3104 r3108  
    174174      if (c != null) canDoNotes = true; 
    175175    } 
    176     catch (Throwable t) { } 
     176    catch (Throwable t) { 
     177      if (FormatHandler.debug) LogTools.trace(t); 
     178    } 
    177179    if (canDoNotes) { 
    178180      JMenuItem fileView = new JMenuItem("View Metadata..."); 
  • trunk/loci/formats/in/MDBParser.java

    r3015 r3108  
    6868    catch (Throwable t) { 
    6969      noMDB = true; 
     70      if (FormatHandler.debug) LogTools.trace(t); 
    7071    } 
    7172    return r; 
  • trunk/loci/formats/in/ND2Reader.java

    r3107 r3108  
    180180      throw new FormatException("Buffer too small."); 
    181181    } 
    182    
     182 
    183183    in.seek(offsets[no]); 
    184184 
  • trunk/loci/formats/in/OMETiffReader.java

    r3106 r3108  
    445445    String comment = (String) getMeta("Comment"); 
    446446    metadata.remove("Comment"); 
    447  
    448     boolean isOMEXML; 
    449     try { 
    450       Class omexmlMeta = Class.forName("loci.formats.ome.OMEXMLMetadataStore"); 
    451       isOMEXML = omexmlMeta.isAssignableFrom(metadataStore.getClass()); 
    452     } 
    453     catch (Throwable t) { 
    454       isOMEXML = false; 
    455     } 
    456  
    457     if (isOMEXML) { 
    458       ReflectedUniverse r = new ReflectedUniverse(); 
    459       try { 
    460         r.exec("import loci.formats.ome.OMEXMLMetadataStore"); 
    461         r.setVar("xmlStore", metadataStore); 
    462         r.setVar("comment", comment); 
    463         r.exec("xmlStore.createRoot(comment)"); 
    464         return; 
    465       } 
    466       catch (ReflectException exc) { 
    467         // OME Java probably not available; ignore this error 
    468       } 
    469     } 
     447    MetadataStore store = getMetadataStore(); 
     448    MetadataTools.convertMetadata(comment, store); 
    470449  } 
    471450 
  • trunk/loci/formats/in/OMEXMLReader.java

    r3104 r3108  
    171171    ReflectedUniverse r = new ReflectedUniverse(); 
    172172    try { 
    173       r.exec("import loci.formats.ome.OMEXMLMetadataStore"); 
     173      r.exec("import loci.formats.ome.OMEXMLMetadata"); 
    174174      r.exec("import org.openmicroscopy.xml.OMENode"); 
    175       r.exec("omexml = new OMEXMLMetadataStore()"); 
     175      r.exec("omexmlMeta = new OMEXMLMetadata()"); 
    176176    } 
    177177    catch (ReflectException exc) { 
     
    203203    } 
    204204    try { 
    205       r.exec("omexml.setRoot(ome)"); 
     205      r.exec("omexmlMeta.setRoot(ome)"); 
    206206    } 
    207207    catch (ReflectException exc) { 
     
    344344 
    345345    try { 
    346       r.exec("omexml.setRoot(ome)"); 
     346      r.exec("omexmlMeta.setRoot(ome)"); 
    347347    } 
    348348    catch (ReflectException exc) { 
     
    358358      try { 
    359359        r.setVar("ndx", i); 
    360         w = (Integer) r.exec("omexml.getSizeX(ndx)"); 
    361         h = (Integer) r.exec("omexml.getSizeY(ndx)"); 
    362         t = (Integer) r.exec("omexml.getSizeT(ndx)"); 
    363         z = (Integer) r.exec("omexml.getSizeZ(ndx)"); 
    364         c = (Integer) r.exec("omexml.getSizeC(ndx)"); 
    365         pixType = (String) r.exec("omexml.getPixelType(ndx)"); 
     360        w = (Integer) r.exec("omexmlMeta.getSizeX(ndx)"); 
     361        h = (Integer) r.exec("omexmlMeta.getSizeY(ndx)"); 
     362        t = (Integer) r.exec("omexmlMeta.getSizeT(ndx)"); 
     363        z = (Integer) r.exec("omexmlMeta.getSizeZ(ndx)"); 
     364        c = (Integer) r.exec("omexmlMeta.getSizeC(ndx)"); 
     365        pixType = (String) r.exec("omexmlMeta.getPixelType(ndx)"); 
    366366        core.currentOrder[i] = 
    367           (String) r.exec("omexml.getDimensionOrder(ndx)"); 
     367          (String) r.exec("omexmlMeta.getDimensionOrder(ndx)"); 
    368368      } 
    369369      catch (ReflectException exc) { 
     
    431431    Arrays.fill(core.orderCertain, true); 
    432432 
     433    // populate assigned metadata store with the 
     434    // contents of the internal OME-XML metadata object 
    433435    MetadataStore store = getMetadataStore(); 
    434436 
    435     try { 
    436       if (store.getClass().getName().equals( 
    437         "loci.formats.ome.OMEXMLMetadataStore")) 
    438       { 
    439         MetadataStore m = (MetadataStore) r.getVar("omexml"); 
    440         store.setRoot(m.getRoot()); 
    441       } 
    442       else { 
    443         FormatTools.convertMetadata((MetadataRetrieve) 
    444           r.getVar("omexml"), store); 
    445       } 
     437    MetadataRetrieve omexmlMeta = null; 
     438    try { 
     439      omexmlMeta = (MetadataRetrieve) r.getVar("omexmlMeta"); 
    446440    } 
    447441    catch (ReflectException e) { 
     
    449443    } 
    450444 
    451     System.gc(); 
     445    String xml = MetadataTools.getOMEXML(omexmlMeta); 
     446    MetadataTools.convertMetadata(xml, store); 
    452447  } 
    453448 
  • trunk/loci/formats/in/PerkinElmerReader.java

    r3095 r3108  
    2727import java.awt.image.BufferedImage; 
    2828import java.io.*; 
     29import java.lang.reflect.InvocationTargetException; 
    2930import java.lang.reflect.Method; 
    3031import java.text.*; 
     
    469470      Class c = String.class; 
    470471      String[] tokens = new String[0]; 
     472      Throwable th = null; 
    471473      try { 
    472474        Method split = c.getMethod("split", new Class[] {c}); 
     
    474476          new Object[] {regex}); 
    475477      } 
    476       catch (Throwable th) { 
    477         // CTR TODO - eliminate catch-all exception handling 
    478         if (debug) trace(th); 
    479       } 
     478      catch (NoSuchMethodException exc) { if (debug) trace(exc); } 
     479      catch (IllegalAccessException exc) { if (debug) trace(exc); } 
     480      catch (InvocationTargetException exc) { if (debug) trace(exc); } 
    480481 
    481482      for (int j=0; j<tokens.length; j++) { 
  • trunk/loci/formats/in/ZeissZVIReader.java

    r3104 r3108  
    6363      r.exec("import java.util.Iterator"); 
    6464    } 
    65     catch (Throwable t) { 
     65    catch (ReflectException exc) { 
    6666      noPOI = true; 
    67       if (debug) LogTools.trace(t); 
     67      if (debug) LogTools.trace(exc); 
    6868    } 
    6969    return r; 
  • trunk/loci/formats/ome/OMEWriter.java

    r3104 r3108  
    8787  // -- OMEWriter API methods -- 
    8888 
    89   public void setMetadataStore(MetadataRetrieve store) { 
    90     metadata = store; 
     89  public void setMetadata(MetadataRetrieve meta) { 
     90    metadata = meta; 
    9191  } 
    9292 
     
    442442 
    443443    FileStitcher reader = new FileStitcher(); 
    444     reader.setMetadataStore(new OMEXMLMetadataStore()); 
     444    reader.setMetadataStore(new OMEXMLMetadata()); 
    445445    reader.setId(id); 
    446     uploader.setMetadataStore((MetadataRetrieve) reader.getMetadataStore()); 
     446    uploader.setMetadata((MetadataRetrieve) reader.getMetadataStore()); 
    447447    for (int i=0; i<reader.getImageCount(); i++) { 
    448448      uploader.saveImage(reader.openImage(i), i == reader.getImageCount() - 1); 
  • trunk/loci/formats/ome/OMEXMLMetadata.java

    r3104 r3108  
    11// 
    2 // OMEXMLMetadataStore.java 
     2// OMEXMLMetadata.java 
    33// 
    44 
     
    4242 * 
    4343 * <dl><dt><b>Source code:</b></dt> 
    44  * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/loci/formats/ome/OMEXMLMetadataStore.java">Trac</a>, 
    45  * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/loci/formats/ome/OMEXMLMetadataStore.java">SVN</a></dd></dl> 
     44 * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/loci/formats/ome/OMEXMLMetadata.java">Trac</a>, 
     45 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/loci/formats/ome/OMEXMLMetadata.java">SVN</a></dd></dl> 
    4646 * 
    4747 * @author Curtis Rueden ctrueden at wisc.edu 
    4848 * @author Melissa Linkert linkert at wisc.edu 
    4949 */ 
    50 public class OMEXMLMetadataStore implements MetadataStore, MetadataRetrieve { 
    51  
    52   // -- Static fields -- 
    53  
    54   /** Logger for this class. */ 
    55   //private static Log log = LogFactory.getLog(OMEXMLMetadataStore.class); 
     50public class OMEXMLMetadata implements MetadataStore, MetadataRetrieve { 
    5651 
    5752  // -- Fields -- 
     
    6964  private Element firstImageCA; 
    7065 
    71   // -- Constructor -- 
    72  
    73   /** Creates a new instance. */ 
    74   public OMEXMLMetadataStore() { createRoot(); } 
    75  
    76   // -- OMEXMLMetadataStore API methods -- 
     66  // -- Constructors -- 
     67 
     68  /** Creates a new OME-XML metadata object. */ 
     69  public OMEXMLMetadata() { this(null); } 
     70 
     71  /** Creates a new OME-XML metadata object around the given OME-XML string. */ 
     72  public OMEXMLMetadata(String xml) { createRoot(xml); } 
     73 
     74  // -- OMEXMLMetadata API methods -- 
    7775 
    7876  /** Constructs a new OME-XML root node with the given XML block. */ 
     
    9896  } 
    9997 
    100   /** Add the key/value pair as a new OriginalMetadata node. */ 
     98  /** Adds the key/value pair as a new OriginalMetadata node. */ 
    10199  public void populateOriginalMetadata(String key, String value) { 
    102100    if (firstImageCA == null) { 
     
    11271125    pixels.setSizeC(sizeC); 
    11281126    pixels.setSizeT(sizeT); 
    1129     pixels.setPixelType(pixelTypeAsString(pixelType)); 
     1127    pixels.setPixelType(FormatTools.getPixelTypeString(pixelType.intValue())); 
    11301128    pixels.setBigEndian(bigEndian); 
    11311129    pixels.setDimensionOrder(dimensionOrder); 
     
    16851683 
    16861684  // -- Helper methods -- 
    1687  
    1688   /** 
    1689    * Gets the OME pixel type string from the Bio-Formats enumeration. 
    1690    * @param pixelType the <i>pixel type</i> as an enumeration. 
    1691    * @return the <i>pixel type</i> as a string. 
    1692    */ 
    1693   private String pixelTypeAsString(Integer pixelType) { 
    1694     if (pixelType == null) return null; 
    1695  
    1696     switch (pixelType.intValue()) { 
    1697       case FormatTools.INT8: 
    1698         return "int8"; 
    1699       case FormatTools.UINT8: 
    1700         return "Uint8"; 
    1701       case FormatTools.INT16: 
    1702         return "int16"; 
    1703       case FormatTools.UINT16: 
    1704         return "Uint16"; 
    1705       case FormatTools.INT32: 
    1706         return "int32"; 
    1707       case FormatTools.UINT32: 
    1708         return "Uint32"; 
    1709       case FormatTools.FLOAT: 
    1710         return "float"; 
    1711       case FormatTools.DOUBLE: 
    1712         return "double"; 
    1713     } 
    1714     throw new RuntimeException("Unknown pixel type: " + pixelType); 
    1715   } 
    17161685 
    17171686  /** 
  • trunk/loci/formats/ome/OmeisImporter.java

    r3104 r3108  
    6767  private FileStitcher reader; 
    6868 
    69   /** Metadata store, for gathering OME-XML metadata. */ 
    70   private OMEXMLMetadataStore store; 
     69  /** Metadata object, for gathering OME-XML metadata. */ 
     70  private OMEXMLMetadata omexmlMeta; 
    7171 
    7272  // -- Constructor -- 
     
    7474  public OmeisImporter() { 
    7575    reader = new FileStitcher(new ChannelSeparator()); 
    76     store = new OMEXMLMetadataStore(); 
     76    omexmlMeta = new OMEXMLMetadata(); 
    7777    reader.setOriginalMetadataPopulated(true); 
    78     reader.setMetadataStore(store); 
     78    reader.setMetadataStore(omexmlMeta); 
    7979  } 
    8080 
     
    190190 
    191191    // get DOM and Pixels elements for the file's OME-XML metadata 
    192     OMENode ome = (OMENode) store.getRoot(); 
     192    OMENode ome = (OMENode) omexmlMeta.getRoot(); 
    193193    Document omeDoc = null; 
    194194    try { 
  • trunk/loci/formats/out/OMETiffWriter.java

    r3015 r3108  
    2626 
    2727import java.io.IOException; 
    28 import java.lang.reflect.Method; 
    2928import loci.formats.*; 
    3029 
     
    5049    if (out != null) out.close(); 
    5150    out = null; 
    52     if (currentId != null && retrieve != null) { 
     51    if (currentId != null) { 
     52      // extract OME-XML string from metadata object 
     53      MetadataRetrieve retrieve = getMetadataRetrieve(); 
     54      String xml = MetadataTools.getOMEXML(retrieve); 
     55 
     56      // insert TiffData element 
     57      int pix = xml.indexOf("<Pixels "); 
     58      int end = xml.indexOf("/>", pix); 
     59      xml = xml.substring(0, end) + "><TiffData/></Pixels>" + 
     60        xml.substring(end + 2); 
     61 
    5362      // write OME-XML to the first IFD's comment 
    54  
    55       MetadataStore omexml = null; 
    56  
    5763      try { 
    58         Class c = Class.forName("loci.formats.ome.OMEXMLMetadataStore"); 
    59         omexml = (MetadataStore) c.newInstance(); 
    60  
    61         if (c.isInstance(retrieve)) omexml = (MetadataStore) retrieve; 
    62         else FormatTools.convertMetadata(retrieve, omexml); 
    63  
    64         Method m = omexml.getClass().getMethod("dumpXML", (Class[]) null); 
    65         String xml = (String) m.invoke(omexml, (Object[]) null); 
    66  
    67         // insert TiffData element 
    68         int pix = xml.indexOf("<Pixels "); 
    69         int end = xml.indexOf("/>", pix); 
    70         xml = xml.substring(0, end) + "><TiffData/></Pixels>" + 
    71           xml.substring(end + 2); 
    72  
    7364        TiffTools.overwriteComment(currentId, xml); 
    7465      } 
    75       catch (Throwable t) { 
    76         LogTools.trace(t); 
    77       } 
     66      catch (FormatException exc) { LogTools.trace(exc); } 
     67      catch (IOException exc) { LogTools.trace(exc); } 
    7868    } 
    7969    super.close(); 
  • trunk/loci/formats/test/ReaderTest.java

    r3105 r3108  
    3131import java.util.*; 
    3232import loci.formats.*; 
    33 // NB: OME Java is required to run the test suite. 
    34 import loci.formats.ome.OMEXMLMetadataStore; 
    3533 
    3634/** 
    3735 * TestNG tester for Bio-Formats file format readers. 
    3836 * Details on failed tests are written to a log file, for easier processing. 
     37 * 
     38 * NB: {@link loci.formats.ome} and ome-java.jar 
     39 * are required for some of the tests. 
    3940 * 
    4041 * To run tests: 
     
    300301  public void testOMEXML(String file) { 
    301302    try { 
    302       OMEXMLMetadataStore store = new OMEXMLMetadataStore(); 
    303       store.createRoot(); 
    304       FileStitcher reader = new FileStitcher(); 
    305       reader.setMetadataStore(store); 
    306       reader.setId(file); 
    307  
    308       store = (OMEXMLMetadataStore) reader.getMetadataStore(); 
     303      MetadataStore omexmlMeta = MetadataTools.createOMEXMLMetadata(); 
     304      FileStitcher reader = new FileStitcher(); 
     305      reader.setMetadataStore(omexmlMeta); 
     306      reader.setId(file); 
     307 
     308      MetadataRetrieve retrieve = (MetadataRetrieve) reader.getMetadataStore(); 
    309309 
    310310      boolean success = true; 
     
    316316        String type = FormatTools.getPixelTypeString(reader.getPixelType()); 
    317317 
    318         boolean failX = reader.getSizeX() != store.getSizeX(ii).intValue(); 
    319         boolean failY = reader.getSizeY() != store.getSizeY(ii).intValue(); 
    320         boolean failZ = reader.getSizeZ() != store.getSizeZ(ii).intValue(); 
    321         boolean failC = reader.getSizeC() != store.getSizeC(ii).intValue(); 
    322         boolean failT = reader.getSizeT() != store.getSizeT(ii).intValue(); 
     318        boolean failX = reader.getSizeX() != retrieve.getSizeX(ii).intValue(); 
     319        boolean failY = reader.getSizeY() != retrieve.getSizeY(ii).intValue(); 
     320        boolean failZ = reader.getSizeZ() != retrieve.getSizeZ(ii).intValue(); 
     321        boolean failC = reader.getSizeC() != retrieve.getSizeC(ii).intValue(); 
     322        boolean failT = reader.getSizeT() != retrieve.getSizeT(ii).intValue(); 
    323323        boolean failBE = reader.isLittleEndian() == 
    324           store.getBigEndian(ii).booleanValue(); 
     324          retrieve.getBigEndian(ii).booleanValue(); 
    325325        boolean failDE = 
    326           !reader.getDimensionOrder().equals(store.getDimensionOrder(ii)); 
    327         boolean failType = !type.equalsIgnoreCase(store.getPixelType(ii)); 
     326          !reader.getDimensionOrder().equals(retrieve.getDimensionOrder(ii)); 
     327        boolean failType = !type.equalsIgnoreCase(retrieve.getPixelType(ii)); 
    328328 
    329329        if (success) { 
     
    545545  public void testValidXML(String file) { 
    546546    try { 
    547       OMEXMLMetadataStore store = new OMEXMLMetadataStore(); 
    548       store.createRoot(); 
    549       FileStitcher reader = new FileStitcher(); 
    550       reader.setMetadataStore(store); 
    551       reader.setId(file); 
    552  
    553       String xml = ((OMEXMLMetadataStore) reader.getMetadataStore()).dumpXML(); 
     547      MetadataStore omexmlMeta = MetadataTools.createOMEXMLMetadata(); 
     548      FileStitcher reader = new FileStitcher(); 
     549      reader.setMetadataStore(omexmlMeta); 
     550      reader.setId(file); 
     551 
     552      MetadataRetrieve retrieve = 
     553        (MetadataRetrieve) reader.getMetadataStore(); 
     554 
     555      String xml = MetadataTools.getOMEXML(retrieve); 
    554556      if (xml == null) writeLog(file + " failed OME-XML validation"); 
    555557      reader.close(); 
Note: See TracChangeset for help on using the changeset viewer.