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 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();
  }
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 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);
    }
  }
Exemplo n.º 6
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);
  }
Exemplo n.º 7
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);
  }
Exemplo n.º 8
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.º 9
0
 public String getValue(String sectionName, String entryKey) {
   if (!StringUtils.isNullOrEmpty(sectionName) && !StringUtils.isNullOrEmpty(entryKey)) {
     for (ConfigurationSection section : this.sections) {
       if (StringUtils.equals(section.getName(), sectionName, false)) {
         ConfigurationSectionEntry entry = section.getEntry(entryKey);
         return (entry == null) ? null : entry.getValue();
       }
     }
   }
   return null;
 }
Exemplo n.º 10
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.º 11
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.º 12
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.º 13
0
 public boolean setEntry(String sectionName, String entryKey, String entryValue) {
   if (StringUtils.isNullOrEmpty(sectionName) || StringUtils.isNullOrEmpty(entryKey)) {
     return false;
   }
   ConfigurationSection section = new ConfigurationSection(sectionName);
   int index = this.sections.indexOf(section);
   if (index == -1) {
     section.addEntry(new ConfigurationSectionEntry(entryKey, entryValue));
     return this.sections.add(section);
   } else {
     section = this.sections.get(index);
     section.addEntry(new ConfigurationSectionEntry(entryKey, entryValue));
     // this.sections.set(index, section);
     return true;
   }
 }
Exemplo n.º 14
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) {

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

    this.fullPath = createPath(parent, path);
  }
Exemplo n.º 15
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.º 16
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.º 17
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());
      }
    }
  }
 /**
  * Returns the new value for the modified variable cast as a {@link ValueList}.
  *
  * <p>If the variable has been deleted, this method will return null.
  *
  * @param separator string used to tokenise the variable's value.
  * @return the new value for the modified variable.
  */
 public ValueList getListValue(String separator) {
   return ConfigurationSection.getListValue(value, separator);
 }
Exemplo n.º 19
0
 public static String createPath(ConfigurationSection section, String key) {
   return createPath(section, key, (section == null) ? null : section.getRoot());
 }
 /**
  * Returns the new value for the modified variable cast as an integer.
  *
  * <p>If the variable has been deleted, this method will return 0.
  *
  * @return the new value for the modified variable.
  * @throws NumberFormatException if {@link #getValue()} cannot be cast as an integer.
  */
 public int getIntegerValue() throws NumberFormatException {
   return ConfigurationSection.getIntegerValue(value);
 }
 /**
  * Returns the new value for the modified variable cast as a float.
  *
  * <p>If the variable has been deleted, this method will return 0.
  *
  * @return the new value for the modified variable.
  * @throws NumberFormatException if {@link #getValue()} cannot be cast as a float
  */
 public float getFloatValue() throws NumberFormatException {
   return ConfigurationSection.getFloatValue(value);
 }
 /**
  * Returns the new value for the modified variable cast as a boolean.
  *
  * <p>If the variable has been deleted, this method will return <code>false</code>.
  *
  * @return the new value for the modified variable.
  */
 public boolean getBooleanValue() {
   return ConfigurationSection.getBooleanValue(value);
 }
 /**
  * Returns the new value for the modified variable as a long.
  *
  * <p>If the variable has been deleted, this method will return 0.
  *
  * @return the new value for the modified variable.
  * @throws NumberFormatException if {@link #getValue()} cannot be cast as a long.
  */
 public long getLongValue() throws NumberFormatException {
   return ConfigurationSection.getLongValue(value);
 }
 /**
  * Returns the new value for the modified variable cast as a double.
  *
  * <p>If the variable has been deleted, this method will return 0.
  *
  * @return the new value for the modified variable.
  * @throws NumberFormatException if {@link #getValue()} cannot be cast as a double.
  */
 public double getDoubleValue() {
   return ConfigurationSection.getDoubleValue(value);
 }