lcsim/sandbox
diff -N AidaCursor.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ AidaCursor.java 30 Mar 2009 23:17:54 -0000 1.1
@@ -0,0 +1,257 @@
+package org.lcsim.util.aida;
+
+import hep.aida.IAnalysisFactory;
+import hep.aida.IManagedObject;
+import hep.aida.ITree;
+import hep.aida.ITreeFactory;
+
+import java.io.IOException;
+
+/**
+ * Utility class for accessing an AIDA
+ * file's contents sequentially.
+ *
+ * @author jeremym
+ * @version $Id: AidaCursor.java,v 1.1 2009/03/30 23:17:54 jeremy Exp $
+ */
+public class AidaCursor
+{
+ private String filepath;
+ private int currentIndex;
+ private ITree tree;
+ private String[] objectNames;
+ private String[] objectTypes;
+ private int nnames;
+
+ /**
+ * Create a cursor from a filepath.
+ * @param filepath
+ */
+ public AidaCursor(String filepath)
+ {
+ this.filepath=filepath;
+ try {
+ ITree tree = open(this.filepath);
+ setup(tree);
+ }
+ catch ( Throwable x )
+ {
+ throw new RuntimeException(x);
+ }
+ }
+
+ /**
+ * Create a cursor from an ITree.
+ * @param tree
+ */
+ public AidaCursor(ITree tree)
+ {
+ setup(tree);
+ }
+
+ /**
+ * Open the AIDA file at filepath.
+ * @param filepath Path to AIDA file.
+ * @throws IOException Problem reading AIDA file.
+ */
+ private ITree open(String filepath) throws IOException
+ {
+ ITreeFactory tf = IAnalysisFactory.create().createTreeFactory();
+ return tf.create(filepath);
+ }
+
+ /**
+ * Setup this cursor from an open ITree.
+ * @param tree An AIDA ITree.
+ */
+ private void setup(ITree tree)
+ {
+ this.tree = tree;
+ objectNames = this.tree.listObjectNames("/",true);
+ nnames=objectNames.length;
+ objectTypes = this.tree.listObjectTypes("/",true);
+ reset();
+ }
+
+ /**
+ * File path.
+ * @return
+ */
+ public String filepath()
+ {
+ return filepath;
+ }
+
+ /**
+ * ITree associated with cursor.
+ * @return
+ */
+ public ITree tree()
+ {
+ return tree;
+ }
+
+ /**
+ * Current AIDA object at position().
+ * @return The IManagedObject at the current position.
+ */
+ public IManagedObject object()
+ {
+ return tree.find(objectNames[position()]);
+ }
+
+ /**
+ * Class of AIDA object at position().
+ * @return Type of AIDA object.
+ */
+ public Class objectClass()
+ {
+ return object().getClass();
+ }
+
+ /**
+ * Type of AIDA object at position().
+ * @return The type string of the AIDA object at current position.
+ */
+ public String type()
+ {
+ return objectTypes[position()];
+ }
+
+ /**
+ * Full name of current object.
+ * @return Name of current object.
+ */
+ public String name()
+ {
+ return objectNames[position()];
+ }
+
+ /**
+ * Increment the position by 1, skipping directories.
+ * @return The next position that is an AIDA object or end.
+ */
+ public int next()
+ {
+ while (currentIndex < end())
+ {
+ currentIndex += 1;
+
+ if (position() == end() || !isdir())
+ {
+ break;
+ }
+ }
+ return position();
+ }
+
+ /**
+ * Decrement the position by 1, skipping directories.
+ * @return The first previous position that is an AIDA object or begin.
+ */
+ public int prev()
+ {
+ while (currentIndex > begin() )
+ {
+ currentIndex -= 1;
+
+ if (position() == end() || !isdir())
+ {
+ break;
+ }
+ }
+ return position();
+ }
+
+ /**
+ * Return the begin position, or 0.
+ * Does not set position.
+ * @return The begin position.
+ */
+ public int begin()
+ {
+ return 0;
+ }
+
+ /**
+ * Return the end position, or size of names array.
+ * Does not set position.
+ * @return The end position.
+ */
+ public int end()
+ {
+ return nnames;
+ }
+
+ /**
+ * The current position.
+ * @return The current position.
+ */
+ public int position()
+ {
+ return currentIndex;
+ }
+
+ /**
+ * Go to first object at position or greater.
+ * @param idx The new index.
+ * @return The new position.
+ */
+ public int seek(int idx)
+ {
+ if (idx < end())
+ {
+ currentIndex=idx;
+ }
+ else {
+ currentIndex=end();
+ }
+ return position();
+ }
+
+ /**
+ * Reset cursor to begginning.
+ * @return The new position.
+ */
+ public int reset()
+ {
+ seek(begin());
+ if (isdir())
+ {
+ next();
+ }
+ return position();
+ }
+
+ /**
+ * Check if the current position is a directory.
+ * @return The object is a directory or not.
+ */
+ public boolean isdir()
+ {
+ boolean isdir=false;
+ if (position() != end())
+ {
+ isdir = (objectTypes[position()].compareTo("dir") == 0);
+ }
+ return isdir;
+ }
+
+ /**
+ * Find an object by name and set to its position.
+ * @param name Name of object to be found.
+ * @return New position if found or end() if not found.
+ */
+ public int find(String name)
+ {
+ for ( int i=seek(begin()); i<end(); i++ )
+ {
+ if ( name().compareTo(name) == 0 )
+ {
+ break;
+ }
+ next();
+ }
+ return position();
+ }
+}
\ No newline at end of file