All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ome.metakit.MetakitReader Maven / Gradle / Ivy

There is a newer version: 5.3.7
Show newest version
/*
 * #%L
 * OME Metakit package for reading Metakit database files.
 * %%
 * Copyright (C) 2011 - 2016 Open Microscopy Environment:
 *   - Board of Regents of the University of Wisconsin-Madison
 *   - Glencoe Software, Inc.
 *   - University of Dundee
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */

package ome.metakit;

import java.io.IOException;

import loci.common.DataTools;
import loci.common.RandomAccessInputStream;

/**
 * Top-level reader for Metakit database files.
 * See http://equi4.com/metakit/metakit-ff.html for basic documentation on the
 * Metakit file format.
 *
 * @author Melissa Linkert melissa at glencoesoftware.com
 */
public class MetakitReader {

  // -- Fields --

  private RandomAccessInputStream stream;

  private String[] tableNames;
  private Column[][] columns;
  private int[] rowCount;

  private Object[][][] data;

  private boolean littleEndian = false;

  // -- Constructors --

  public MetakitReader(String file) throws IOException, MetakitException {
    this(new RandomAccessInputStream(file));
  }

  public MetakitReader(RandomAccessInputStream stream) throws MetakitException {
    this.stream = stream;
    try {
      initialize();
    }
    catch (IOException e) {
      throw new MetakitException(e);
    }
  }

  // -- MetakitReader API methods --

  /**
   * Close the reader and release any resources in use.
   */
  public void close() {
    try {
      if (stream != null) {
        stream.close();
      }
      stream = null;
      tableNames = null;
      columns = null;
      rowCount = null;
      data = null;
      littleEndian = false;
    }
    catch (IOException e) { }
  }

  /**
   * Retrieve the number of tables in this database file.
   */
  public int getTableCount() {
    return tableNames.length;
  }

  /**
   * Retrieve the name of every table in this database file.
   * The length of the returned array is equivalent to
   * {@link #getTableCount()}.
   */
  public String[] getTableNames() {
    return tableNames;
  }

  /**
   * Retrieve the name of every column in the table with the given index.
   * Tables are indexed from 0 to {@link #getTableCount()} - 1.
   */
  public String[] getColumnNames(int tableIndex) {
    String[] columnNames = new String[columns[tableIndex].length];
    for (int i=0; i{@link #getTableCount()} - 1.
   *
   * Every Object in the arrays returned by {@link #getTableData(int)} and
   * {@link #getTableData(String)} will be an instance of the corresponding
   * Class in the Class[] returned by this method.
   */
  public Class[] getColumnTypes(int tableIndex) {
    Class[] types = new Class[columns[tableIndex].length];
    for (int i=0; i{@link #getTableCount()} - 1.
   */
  public int getRowCount(int tableIndex) {
    return rowCount[tableIndex];
  }

  /**
   * Retrieve the number of rows in the named table.
   */
  public int getRowCount(String tableName) {
    return getRowCount(DataTools.indexOf(tableNames, tableName));
  }

  /**
   * Retrieve all of the tabular data for the table with the given index.
   * Tables are indexed from 0 to {@link #getTableCount()} - 1.
   *
   * @see #getColumnTypes(int)
   */
  public Object[][] getTableData(int tableIndex) {
    Object[][] table = data[tableIndex];
    if (table == null) return null;

    // table is stored in [column][row] order; reverse it for convenience
    int rowCount = table[0] == null ? 0 : table[0].length;
    Object[][] newTable = new Object[rowCount][table.length];

    for (int row=0; row{@link #getTableCount()} - 1.
   *
   * @see #getColumnTypes(int)
   */
  public Object[] getRowData(int rowIndex, int tableIndex) {
    Object[] row = new Object[data[tableIndex].length];
    for (int col=0; col= 0;

      columnList = columnList.substring(openBracket + 1);
      String[] cols = columnList.split(",");
      columns[i] = new Column[cols.length];

      for (int col=0; col 1) {
            rowCount[table] += count;
            for (int col=0; col 0) {
          for (int col=0; col




© 2015 - 2024 Weber Informatics LLC | Privacy Policy