Commit in lcsim/src/org/lcsim/util/aida on MAIN
AidaCursor.java+209added 1.1


lcsim/src/org/lcsim/util/aida
AidaCursor.java added at 1.1
diff -N AidaCursor.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ AidaCursor.java	9 Feb 2007 22:35:58 -0000	1.1
@@ -0,0 +1,209 @@
+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 2007/02/09 22:35:58 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.
+     * Sets the tree variable.
+     * @param filepath
+     */
+    public AidaCursor(String filepath)
+    {    
+        this.filepath=filepath;
+        try {
+            setup(open(this.filepath));
+        }
+        catch ( Throwable x )
+        {
+            throw new RuntimeException(x);
+        }
+    }
+
+    /**
+     * Create a cursor from an ITree.
+     * Sets the tree variable.
+     * @param tree
+     */
+    public AidaCursor(ITree tree)
+    {
+        setup(this.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 the cursor from an 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);
+        seek(0);
+    }
+
+    /**
+     * 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()];
+    }
+
+    /**
+     * Increment the position by 1.
+     * @return The new position.
+     */
+    public int next()
+    {
+        if (currentIndex < nnames)
+        {
+            currentIndex += 1;
+        }
+        return position();
+    }
+
+    /**
+     * Decrement the position by 1.
+     * @return The next position.
+     */
+    public int prev()
+    {
+        if (currentIndex > begin())
+        {
+            currentIndex -= 1;
+        }       
+        return position();
+    }
+
+    /**
+     * Return the begin position.  Does not set position.
+     * @return The begin position.
+     */
+    public int begin()
+    {
+        return 0;
+    }
+
+    /**
+     * Return the end position.  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 position.
+     * @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() 
+    {
+        return seek(begin());
+    }
+
+    /**
+     * Check if the current position is a directory. 
+     * @return The object is a directory or not.
+     */
+    public boolean isdir()
+    {
+        return position() != end() && object().getClass() != IManagedObject.class;
+    }       
+}
\ No newline at end of file
CVSspam 0.2.8