Example #1
0
  /*
   * add a new resource to the node. E.g. the resource temperature or
   * humidity. If the path is /readings/temp, temp will be a subResource
   * of readings, which is a subResource of the node.
   */
  public ResourceBase addNodeResource(String path) {
    Scanner scanner = new Scanner(path);
    scanner.useDelimiter("/");
    String next = "";
    boolean resourceExist = false;
    Resource resource = this; // It's the resource that represents the endpoint

    ResourceBase subResource = null;
    while (scanner.hasNext()) {
      resourceExist = false;
      next = scanner.next();
      for (Resource res : resource.getChildren()) {
        if (res.getName().equals(next)) {
          subResource = (ResourceBase) res;
          resourceExist = true;
        }
      }
      if (!resourceExist) {
        subResource = new RDTagResource(next, true, this);
        resource.add(subResource);
      }
      resource = subResource;
    }
    subResource.setPath(resource.getPath());
    subResource.setName(next);
    scanner.close();
    return subResource;
  }