private void setJsonTree(List<String> path, JSONArray parent, GadgetSpec gadgetSpec)
     throws JSONException {
   String tempId = "root"; // Temp variable to keep track of parent ID.
   for (int i = 0; i < path.size(); i++) {
     JSONObject newNode = new JSONObject();
     if (!this.isNodeExisting(path.get(i), parent)) {
       // If node is a GadgetSpec (last in path)
       if (i == path.size() - 1) {
         newNode.put("name", StringUtils.capitalize(gadgetSpec.getTitle()));
         newNode.put("hasChildren", false);
         newNode.put("isDefault", gadgetSpec.isDefault());
         newNode.put("id", gadgetSpec.getId());
       }
       // Id node is a unique folder
       else {
         newNode.put("name", StringUtils.capitalize(path.get(i)));
         newNode.put("hasChildren", true);
         newNode.put("isDefault", false);
         newNode.put("id", getFolderId(path.get(i)));
       }
       newNode.put("parent", tempId);
       parent.put(newNode);
     }
     tempId = getFolderId(path.get(i));
   }
 }
 private void addToTree(GadgetSpec gadgetSpec, JSONArray tree) throws JSONException {
   String specPath = gadgetSpec.getPathToSpec();
   specPath = specPath.replace("/spec.json", "");
   String[] nodes = specPath.split("/");
   List<String> nodesArray = new ArrayList<String>();
   Collections.addAll(nodesArray, nodes);
   setJsonTree(nodesArray, tree, gadgetSpec);
 }
 private void loadSpecs() {
   final String method = "loadSpecs";
   try {
     // FIXME: If there's only one spec, we should force it to be the default. This might be easier
     // to do client-side
     // TODO: What if two specs want to be the default? Last one wins? This also might be easier to
     // do client-side
     for (String specPath : getSpecRegistryContents()) {
       GadgetSpec gadgetSpec = specFactory.create(specPath);
       if (gadgetSpec == null) {
         LOG.logp(Level.WARNING, CLASS, method, "Unable to load gadget at path {0}", specPath);
         continue;
       }
       specs.put(gadgetSpec.getId(), gadgetSpec);
       if (gadgetSpec.isDefault()) {
         defaultSpec = gadgetSpec;
       }
       addToTree(gadgetSpec, specTree);
     }
   } catch (Exception e) {
     LOG.logp(Level.SEVERE, CLASS, method, e.getMessage(), e);
   }
 }