Exemplo n.º 1
1
  /**
   * Creates a relative path to the given {@link ConfigurationSection} from the given relative
   * section.
   *
   * <p>You may use this method for any given {@link ConfigurationSection}, not only {@link
   * MemorySection}.
   *
   * @param section Section to create a path for.
   * @param key Name of the specified section.
   * @param relativeTo Section to create the path relative to.
   * @return Full path of the section from its root.
   */
  public static String createPath(
      ConfigurationSection section, String key, ConfigurationSection relativeTo) {
    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, section.getRoot().options().pathSeparator());
        }

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

    if ((key != null) && (key.length() > 0)) {
      if (builder.length() > 0) {
        builder.append(section.getRoot().options().pathSeparator());
      }

      builder.append(key);
    }

    return builder.toString();
  }
Exemplo n.º 2
0
  public ConfigurationSection createSection(String path) {
    if (path == null) {
      throw new IllegalArgumentException("Path cannot be null");
    } else if (path.length() == 0) {
      throw new IllegalArgumentException("Cannot create section at empty path");
    }

    String[] split =
        path.split(Pattern.quote(Character.toString(getRoot().options().pathSeparator())));
    ConfigurationSection section = this;

    for (int i = 0; i < split.length - 1; i++) {
      ConfigurationSection last = section;

      section = last.getConfigurationSection(split[i]);

      if (section == null) {
        section = last.createSection(split[i]);
      }
    }

    String key = split[split.length - 1];

    if (section == this) {
      ConfigurationSection result = new MemorySection(this, key);
      map.put(key, result);
      return result;
    } else {
      return section.createSection(key);
    }
  }
Exemplo n.º 3
0
  public Object get(String path, Object def) {
    if (path == null) {
      throw new IllegalArgumentException("Path cannot be null");
    } else if (path.length() == 0) {
      return this;
    }

    Object result = null;
    String[] split =
        path.split(Pattern.quote(Character.toString(getRoot().options().pathSeparator())));
    ConfigurationSection section = this;

    for (int i = 0; i < split.length - 1; i++) {
      section = section.getConfigurationSection(split[i]);

      if (section == null) {
        return def;
      }
    }

    String key = split[split.length - 1];

    if (section == this) {
      result = map.get(key);
      return (result == null) ? def : result;
    }
    return section.get(key, def);
  }
Exemplo n.º 4
0
  public void set(String path, Object value) {
    String[] split =
        path.split(Pattern.quote(Character.toString(getRoot().options().pathSeparator())));
    ConfigurationSection section = this;

    if (path.length() == 0) {
      throw new IllegalArgumentException("Cannot set to an empty path");
    }

    for (int i = 0; i < split.length - 1; i++) {
      ConfigurationSection last = section;

      section = last.getConfigurationSection(split[i]);

      if (section == null) {
        section = last.createSection(split[i]);
      }
    }

    String key = split[split.length - 1];

    if (section == this) {
      if (value == null) {
        map.remove(key);
      } else {
        map.put(key, value);
      }
    } else {
      section.set(key, value);
    }
  }
Exemplo n.º 5
0
  public ConfigurationSection createSection(String path, Map<?, ?> map) {
    ConfigurationSection section = createSection(path);

    for (Map.Entry<?, ?> entry : map.entrySet()) {
      if (entry.getValue() instanceof Map) {
        section.createSection(entry.getKey().toString(), (Map<?, ?>) entry.getValue());
      } else {
        section.set(entry.getKey().toString(), entry.getValue());
      }
    }

    return section;
  }
Exemplo n.º 6
0
  public Map<String, Object> getValues(boolean deep) {
    Map<String, Object> result = new LinkedHashMap<String, Object>();

    if (getRoot().options().copyDefaults()) {
      ConfigurationSection defaults = getDefaultSection();

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

    mapChildrenValues(result, this, deep);

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

    if (getRoot().options().copyDefaults()) {
      ConfigurationSection defaults = getDefaultSection();

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

    mapChildrenKeys(result, this, deep);

    return result;
  }
Exemplo n.º 8
0
  /**
   * Creates an empty MemorySection with the specified parent and path.
   *
   * @param parent Parent section that contains this own section.
   * @param path Path that you may access this section from via the root {@link Configuration}.
   * @throws IllegalArgumentException Thrown is parent or path is null, or if parent contains no
   *     root Configuration.
   */
  protected MemorySection(ConfigurationSection parent, String path) {
    if (parent == null) {
      throw new IllegalArgumentException("Parent cannot be null");
    }
    if (path == null) {
      throw new IllegalArgumentException("Path cannot be null");
    }

    this.path = path;
    this.parent = parent;
    this.root = parent.getRoot();

    if (root == null) {
      throw new IllegalArgumentException("Path cannot be orphaned");
    }

    this.fullPath = createPath(parent, path);
  }
Exemplo n.º 9
0
  protected void mapChildrenKeys(Set<String> output, ConfigurationSection section, boolean deep) {
    if (section instanceof MemorySection) {
      MemorySection sec = (MemorySection) section;

      for (Map.Entry<String, Object> entry : sec.map.entrySet()) {
        output.add(createPath(section, entry.getKey(), this));

        if ((deep) && (entry.getValue() instanceof ConfigurationSection)) {
          ConfigurationSection subsection = (ConfigurationSection) entry.getValue();
          mapChildrenKeys(output, subsection, deep);
        }
      }
    } else {
      Set<String> keys = section.getKeys(deep);

      for (String key : keys) {
        output.add(createPath(section, key, this));
      }
    }
  }
Exemplo n.º 10
0
  protected void mapChildrenValues(
      Map<String, Object> output, ConfigurationSection section, boolean deep) {
    if (section instanceof MemorySection) {
      MemorySection sec = (MemorySection) section;

      for (Map.Entry<String, Object> entry : sec.map.entrySet()) {
        output.put(createPath(section, entry.getKey(), this), entry.getValue());

        if (entry.getValue() instanceof ConfigurationSection) {
          if (deep) {
            mapChildrenValues(output, (ConfigurationSection) entry.getValue(), deep);
          }
        }
      }
    } else {
      Map<String, Object> values = section.getValues(deep);

      for (Map.Entry<String, Object> entry : values.entrySet()) {
        output.put(createPath(section, entry.getKey(), this), entry.getValue());
      }
    }
  }
Exemplo n.º 11
0
 /**
  * Creates a full path to the given {@link ConfigurationSection} from its root {@link
  * Configuration}.
  *
  * <p>You may use this method for any given {@link ConfigurationSection}, not only {@link
  * MemorySection}.
  *
  * @param section Section to create a path for.
  * @param key Name of the specified section.
  * @return Full path of the section from its root.
  */
 public static String createPath(ConfigurationSection section, String key) {
   return createPath(section, key, (section == null) ? null : section.getRoot());
 }