Take a look at the TableUtils script include.



There is a method or two in there that may help. Particularly, look at getAllExtensions(), and getHierarchy()