Exemplo n.º 1
0
  /**
   * Clear this configuration and reset to the contents of the parsed string.
   *
   * @param text Git style text file listing configuration properties.
   * @throws ConfigInvalidException the text supplied is not formatted correctly. No changes were
   *     made to {@code this}.
   */
  public void fromText(final String text) throws ConfigInvalidException {
    final List<Entry> newEntries = new ArrayList<Entry>();
    final StringReader in = new StringReader(text);
    Entry last = null;
    Entry e = new Entry();
    for (; ; ) {
      int input = in.read();
      if (-1 == input) break;

      final char c = (char) input;
      if ('\n' == c) {
        // End of this entry.
        newEntries.add(e);
        if (e.section != null) last = e;
        e = new Entry();

      } else if (e.suffix != null) {
        // Everything up until the end-of-line is in the suffix.
        e.suffix += c;

      } else if (';' == c || '#' == c) {
        // The rest of this line is a comment; put into suffix.
        e.suffix = String.valueOf(c);

      } else if (e.section == null && Character.isWhitespace(c)) {
        // Save the leading whitespace (if any).
        if (e.prefix == null) e.prefix = "";
        e.prefix += c;

      } else if ('[' == c) {
        // This is a section header.
        e.section = readSectionName(in);
        input = in.read();
        if ('"' == input) {
          e.subsection = readValue(in, true, '"');
          input = in.read();
        }
        if (']' != input) throw new ConfigInvalidException(JGitText.get().badGroupHeader);
        e.suffix = "";

      } else if (last != null) {
        // Read a value.
        e.section = last.section;
        e.subsection = last.subsection;
        in.reset();
        e.name = readKeyName(in);
        if (e.name.endsWith("\n")) {
          e.name = e.name.substring(0, e.name.length() - 1);
          e.value = MAGIC_EMPTY_VALUE;
        } else e.value = readValue(in, false, -1);

      } else throw new ConfigInvalidException(JGitText.get().invalidLineInConfigFile);
    }

    state.set(newState(newEntries));
  }
Exemplo n.º 2
0
 Entry forValue(final String newValue) {
   final Entry e = new Entry();
   e.prefix = prefix;
   e.section = section;
   e.subsection = subsection;
   e.name = name;
   e.value = newValue;
   e.suffix = suffix;
   return e;
 }
Exemplo n.º 3
0
    private void add(Entry entry) {
      entries.add(entry);
      entry.section = this;
      // totalYield += entry.Yield;
      // size += 1;
      // if (entry.MN != null) {
      //    MNSize += 1;
      //    totalMN += entry.MN;

      // }

    }
Exemplo n.º 4
0
  private State replaceStringList(
      final State srcState,
      final String section,
      final String subsection,
      final String name,
      final List<String> values) {
    final List<Entry> entries = copy(srcState, values);
    int entryIndex = 0;
    int valueIndex = 0;
    int insertPosition = -1;

    // Reset the first n Entry objects that match this input name.
    //
    while (entryIndex < entries.size() && valueIndex < values.size()) {
      final Entry e = entries.get(entryIndex);
      if (e.match(section, subsection, name)) {
        entries.set(entryIndex, e.forValue(values.get(valueIndex++)));
        insertPosition = entryIndex + 1;
      }
      entryIndex++;
    }

    // Remove any extra Entry objects that we no longer need.
    //
    if (valueIndex == values.size() && entryIndex < entries.size()) {
      while (entryIndex < entries.size()) {
        final Entry e = entries.get(entryIndex++);
        if (e.match(section, subsection, name)) entries.remove(--entryIndex);
      }
    }

    // Insert new Entry objects for additional/new values.
    //
    if (valueIndex < values.size() && entryIndex == entries.size()) {
      if (insertPosition < 0) {
        // We didn't find a matching key above, but maybe there
        // is already a section available that matches. Insert
        // after the last key of that section.
        //
        insertPosition = findSectionEnd(entries, section, subsection);
      }
      if (insertPosition < 0) {
        // We didn't find any matching section header for this key,
        // so we must create a new section header at the end.
        //
        final Entry e = new Entry();
        e.section = section;
        e.subsection = subsection;
        entries.add(e);
        insertPosition = entries.size();
      }
      while (valueIndex < values.size()) {
        final Entry e = new Entry();
        e.section = section;
        e.subsection = subsection;
        e.name = name;
        e.value = values.get(valueIndex++);
        entries.add(insertPosition++, e);
      }
    }

    return newState(entries);
  }