예제 #1
0
  /**
   * Builds a HashMap containing all of the key,value pairs stored in this PropertyTree
   *
   * @return
   */
  private Map collectEntries(String prefix, boolean flattenEntries) {
    LinkedHashMap entryMap = new LinkedHashMap();

    for (Iterator i = this.children.entrySet().iterator(); i.hasNext(); ) {
      Map.Entry e = (Map.Entry) i.next();
      PropertyTree child = (PropertyTree) e.getValue();
      String childKey = (String) e.getKey();

      // handle children with values
      if (child.hasDirectValue()) {
        String entryKey = (prefix == null) ? childKey : prefix + "." + childKey;
        String entryValue = child.getDirectValue();

        entryMap.put(entryKey, entryValue);
      }

      // handle children with children
      if (!flattenEntries && child.hasChildren()) {
        String childPrefix = (prefix == null) ? childKey : prefix + "." + childKey;

        entryMap.putAll(child.collectEntries(childPrefix, flattenEntries));
      }
    }

    return entryMap;
  }
예제 #2
0
  /**
   * @param key
   * @return the directValue of the PropertyTree associated with the given key, or null if there is
   *     none
   */
  public String getProperty(String key) {
    String propertyValue = null;

    PropertyTree subtree = getSubtree(key);
    if (subtree != null) {
      propertyValue = subtree.getDirectValue();
    }

    return propertyValue;
  }
예제 #3
0
  /**
   * Returns the PropertyTree object with the given key, or null if there is none.
   *
   * @param key
   * @return
   * @throws IllegalArgumentException if the key is null
   */
  private PropertyTree getSubtree(String key) {
    validateKey(key);

    PropertyTree returnValue = null;
    if (StringUtils.contains(key, '.')) {
      String prefix = StringUtils.substringBefore(key, ".");
      String suffix = StringUtils.substringAfter(key, ".");

      PropertyTree child = (PropertyTree) this.children.get(prefix);
      if (child != null) {
        returnValue = child.getSubtree(suffix);
      }
    } else {
      returnValue = (PropertyTree) this.children.get(key);
    }

    return returnValue;
  }
예제 #4
0
  /**
   * Associates the given key with the given value. If the given key has multiple levels (consists
   * of multiple strings separated by '.'), the property value is stored such that it can be
   * retrieved either directly, by calling get() and passing the entire key; or indirectly, by
   * decomposing the key into its separate levels and calling get() successively on the result of
   * the previous level's get. <br>
   * For example, given <br>
   * <code>
   * PropertyTree tree = new PropertyTree();
   * tree.set( "a.b.c", "something" );
   * </code> the following statements are equivalent ways to retrieve the value: <br>
   * <code>
   * Object one = tree.get( "a.b.c" );
   * </code> <code>
   * Object two = tree.get( "a" ).get( "b" ).get( "c" );
   * </code><br>
   * Note: since I can't have the get method return both a PropertyTree and a String, getting an
   * actual String requires calling toString on the PropertyTree returned by get.
   *
   * @param key
   * @param value
   * @throws IllegalArgumentException if the key is null
   * @throws IllegalArgumentException if the value is null
   */
  public void setProperty(String key, String value) {
    validateKey(key);
    validateValue(value);

    if (parent == null) {
      LOG.debug("setting (k,v) (" + key + "," + value + ")");
    }

    if (StringUtils.contains(key, '.')) {
      String prefix = StringUtils.substringBefore(key, ".");
      String suffix = StringUtils.substringAfter(key, ".");

      PropertyTree node = getChild(prefix);
      node.setProperty(suffix, value);
    } else {
      PropertyTree node = getChild(key);
      node.setDirectValue(value);
    }
  }