package test;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gumtree.data.common.DataContainer;
import org.gumtree.data.common.DataModelException;
import org.gumtree.data.common.DictionaryPathResolver;
import org.gumtree.data.common.IDataContainer;
import org.gumtree.data.common.IPathResolver;
import org.gumtree.data.gdm.core.DataItem;
import org.gumtree.data.gdm.core.Dataset;
import org.gumtree.data.gdm.core.Factory;
import org.gumtree.data.gdm.core.Group;
import org.gumtree.data.gdm.core.exception.FileAccessException;
import ucar.nc2.Attribute;
public class ExampleFileParser {
/**
* createDictionary
* Generate a map associating a unique key (arbitrarily choose as a concatenation of node's name to
* open to reach the dataset) to path targeting a dataset (NeXus meaning)
*
* @param curGroup node from which to scan all following nodes
*
* @return the dictionary of curGroup descending nodes
*/
public static Map<String, String> createDictionary(final Group curGroup)
{
HashMap<String, String> dico = new HashMap<String, String>();
DataItem item;
List<?> dataItems;
List<?> groups;
Group group;
// Get all data nodes (means NeXus dataset) sons of currently opened node
dataItems = curGroup.getDataItems();
// Add each node to the dictionary
String key, path;
for (Object name : dataItems) {
item = (DataItem) name;
// Fill the map: "unique_node_name" => "path/to/reach/node"
key = item.getName().replace("/", "_");
path = "/" + item.getName();
dico.put(key, path);
}
// Get all groups directly belonging to the current node
groups = curGroup.getGroups();
// Recursive call on each group node that were found
for (Object name : groups) {
group = (Group) name;
// Recursive call
dico.putAll(createDictionary(group));
}
return dico;
}
/**
* Main process
* @param args path to reach file we want to parse
* @throws FileAccessException
*/
public static void main(String[] args) throws FileAccessException {
final long time = System.currentTimeMillis();
final String filename = args[0];
// Instantiate Dataset (Gumtree meaning) on file to be read
Dataset dataset = Factory.getDataset(new File(filename).toURI());
// Get the file root
Group group = dataset.getRootGroup();
// generate the file's dictionary starting from the file's root
Map<String, String> dictionary = createDictionary(group);
// Instantiate the dictionary
IPathResolver pathResolver = new DictionaryPathResolver(dictionary);
// Instantiation the "container" that links the dataset and the dictionary
IDataContainer container = new DataContainer(dataset);
// Set dictionary on the container
container.setPathSolver(pathResolver);
// Parse the file (using the dictionary) to display data
for (String key : dictionary.keySet())
{
String value;
try
{
// get the value of the dataset corresponding to the current key
value = container.get(key).toString();
System.out.println(key + ": " + (value.length() < 1000 ? value : "Data to long to be displayed using eclipse!") );
}
catch(DataModelException n)
{
System.out.println("ERROR on key: " + key);
n.printStackTrace();
}
// Get the group node corresponding to the current key
group = dataset.getRootGroup();
DataItem toto = (DataItem) group.getObjectByPath(container.getPathResolver()
.resolvePath(key));
group = toto.getParentGroup();
// Scan node's attribute
List<?> list_attr = group.getAttributes();
for (Object name : list_attr) {
Attribute attr = (Attribute) name;
System.out.println(">> attribut : " + attr.toString());
}
}
System.out.println("Total execution time: " + (System.currentTimeMillis() - time) / 1000. + "s");
}
}