2
|
1 package edu.unc.genomics;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.nio.file.Files;
|
|
5 import java.nio.file.Path;
|
|
6 import java.nio.file.Paths;
|
|
7
|
|
8 import javax.swing.tree.DefaultMutableTreeNode;
|
|
9 import javax.swing.tree.DefaultTreeModel;
|
|
10 import javax.xml.parsers.DocumentBuilder;
|
|
11 import javax.xml.parsers.DocumentBuilderFactory;
|
|
12 import javax.xml.parsers.ParserConfigurationException;
|
|
13
|
|
14 import org.apache.log4j.Logger;
|
|
15 import org.w3c.dom.Document;
|
|
16 import org.w3c.dom.Node;
|
|
17 import org.w3c.dom.NodeList;
|
|
18 import org.xml.sax.SAXException;
|
|
19
|
|
20 /**
|
|
21 * Model for the ToolsTree
|
|
22 * Essentially just a DefaultTreeModel, but provides methods for
|
|
23 * loading the available tools from a configuration file
|
|
24 *
|
|
25 * @author timpalpant
|
|
26 *
|
|
27 */
|
|
28 public class ToolsTreeModel extends DefaultTreeModel {
|
|
29
|
|
30 public static final Path DEFAULT_CONFIGURATION_FILE = Paths.get("toolConf.xml");
|
|
31
|
|
32 private static final Logger log = Logger.getLogger(ToolsTreeModel.class);
|
|
33
|
|
34 private static final long serialVersionUID = -6587614270922489960L;
|
|
35
|
|
36 public ToolsTreeModel() {
|
|
37 super(new DefaultMutableTreeNode("Tools"));
|
|
38 }
|
|
39
|
|
40 public static ToolsTreeModel loadDefaultConfig() throws ClassNotFoundException, ParserConfigurationException, SAXException, IOException {
|
|
41 return loadConfig(ResourceManager.getResourceDirectory().resolve(DEFAULT_CONFIGURATION_FILE));
|
|
42 }
|
|
43
|
|
44 public static ToolsTreeModel loadConfig(Path p) throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException {
|
|
45 ToolsTreeModel model = new ToolsTreeModel();
|
|
46 DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
|
|
47
|
|
48 // Populate the TreeModel with the tools in the default configuration file
|
|
49 log.debug("Loading tools from: " + DEFAULT_CONFIGURATION_FILE.toAbsolutePath());
|
|
50 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
|
51 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
|
52 Document doc = dBuilder.parse(p.toFile());
|
|
53
|
|
54 // Iterate over the sections
|
|
55 NodeList sections = doc.getElementsByTagName("section");
|
|
56 log.debug("Found "+sections.getLength()+" sections");
|
|
57 for (int i = 0; i < sections.getLength(); i++) {
|
|
58 Node section = sections.item(i);
|
|
59 String sectionName = section.getAttributes().getNamedItem("name").getNodeValue();
|
|
60 log.debug("Loading section: " + sectionName);
|
|
61 DefaultMutableTreeNode sectionNode = new DefaultMutableTreeNode(sectionName);
|
|
62 root.add(sectionNode);
|
|
63 NodeList tools = section.getChildNodes();
|
|
64
|
|
65 // Iterate over the tools in each section
|
|
66 for (int j = 0; j < tools.getLength(); j++) {
|
|
67 Node tool = tools.item(j);
|
|
68 if (tool.getNodeType() == Node.ELEMENT_NODE && tool.getNodeName().equalsIgnoreCase("tool")) {
|
|
69 String toolName = tool.getAttributes().getNamedItem("name").getNodeValue();
|
|
70 log.debug("Loading tool: " + toolName);
|
|
71 String toolClassName = tool.getAttributes().getNamedItem("class").getNodeValue();
|
|
72 Class<? extends CommandLineTool> toolClass = (Class<? extends CommandLineTool>) Class.forName(toolClassName);
|
|
73 ToolsTreeNode toolNode = new ToolsTreeNode(toolName, toolClass);
|
|
74 sectionNode.add(toolNode);
|
|
75 }
|
|
76 }
|
|
77 }
|
|
78
|
|
79 return model;
|
|
80 }
|
|
81
|
|
82 }
|