Esempio n. 1
0
  public static String createPath(
      ConfigurationSection section, String key, ConfigurationSection relativeTo) {
    Configuration root = section.getRoot();
    if (root == null) {
      throw new IllegalStateException("Cannot create path without a root");
    }
    char separator = root.options().pathSeparator();

    StringBuilder builder = new StringBuilder();
    if (section != null) {
      for (ConfigurationSection parent = section;
          (parent != null) && (parent != relativeTo);
          parent = parent.getParent()) {
        if (builder.length() > 0) {
          builder.insert(0, separator);
        }

        builder.insert(0, parent.getName());
      }
    }

    if ((key != null) && (key.length() > 0)) {
      if (builder.length() > 0) {
        builder.append(separator);
      }

      builder.append(key);
    }

    return builder.toString();
  }
Esempio n. 2
0
  public ConfigurationSection createSection(String path) {
    Configuration root = getRoot();
    if (root == null) {
      throw new IllegalStateException("Cannot create section without a root");
    }

    final char separator = root.options().pathSeparator();
    // i1 is the leading (higher) index
    // i2 is the trailing (lower) index
    int i1 = -1, i2;
    ConfigurationSection section = this;
    while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) {
      String node = path.substring(i2, i1);
      ConfigurationSection subSection = section.getConfigurationSection(node);
      if (subSection == null) {
        section = section.createSection(node);
      } else {
        section = subSection;
      }
    }

    String key = path.substring(i2);
    if (section == this) {
      ConfigurationSection result = new MemorySection(this, key);
      map.put(key, result);
      return result;
    }
    return section.createSection(key);
  }
Esempio n. 3
0
  public Object get(String path, Object def) {

    if (path.length() == 0) {
      return this;
    }

    Configuration root = getRoot();
    if (root == null) {
      throw new IllegalStateException("Cannot access section without a root");
    }

    final char separator = root.options().pathSeparator();
    // i1 is the leading (higher) index
    // i2 is the trailing (lower) index
    int i1 = -1, i2;
    ConfigurationSection section = this;
    while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) {
      section = section.getConfigurationSection(path.substring(i2, i1));
      if (section == null) {
        return def;
      }
    }

    String key = path.substring(i2);
    if (section == this) {
      Object result = map.get(key);
      return (result == null) ? def : result;
    }
    return section.get(key, def);
  }
Esempio n. 4
0
  public void set(String path, Object value) {

    Configuration root = getRoot();
    if (root == null) {
      throw new IllegalStateException("Cannot use section without a root");
    }

    final char separator = root.options().pathSeparator();
    // i1 is the leading (higher) index
    // i2 is the trailing (lower) index
    int i1 = -1, i2;
    ConfigurationSection section = this;
    while ((i1 = path.indexOf(separator, i2 = i1 + 1)) != -1) {
      String node = path.substring(i2, i1);
      ConfigurationSection subSection = section.getConfigurationSection(node);
      if (subSection == null) {
        section = section.createSection(node);
      } else {
        section = subSection;
      }
    }

    String key = path.substring(i2);
    if (section == this) {
      if (value == null) {
        map.remove(key);
      } else {
        map.put(key, value);
      }
    } else {
      section.set(key, value);
    }
  }
Esempio n. 5
0
 public boolean isSet(String path) {
   Configuration root = getRoot();
   if (root == null) {
     return false;
   }
   if (root.options().copyDefaults()) {
     return contains(path);
   }
   return get(path, null) != null;
 }
Esempio n. 6
0
  public Map<String, Object> getValues(boolean deep) {
    Map<String, Object> result = new LinkedHashMap<String, Object>();

    Configuration root = getRoot();
    if (root != null && root.options().copyDefaults()) {
      ConfigurationSection defaults = getDefaultSection();

      if (defaults != null) {
        result.putAll(defaults.getValues(deep));
      }
    }

    mapChildrenValues(result, this, deep);

    return result;
  }
Esempio n. 7
0
  public Set<String> getKeys(boolean deep) {
    Set<String> result = new LinkedHashSet<String>();

    Configuration root = getRoot();
    if (root != null && root.options().copyDefaults()) {
      ConfigurationSection defaults = getDefaultSection();

      if (defaults != null) {
        result.addAll(defaults.getKeys(deep));
      }
    }

    mapChildrenKeys(result, this, deep);

    return result;
  }