예제 #1
0
  private static String readSectionName(final StringReader in) throws ConfigInvalidException {
    final StringBuilder name = new StringBuilder();
    for (; ; ) {
      int c = in.read();
      if (c < 0) throw new ConfigInvalidException(JGitText.get().unexpectedEndOfConfigFile);

      if (']' == c) {
        in.reset();
        break;
      }

      if (' ' == c || '\t' == c) {
        for (; ; ) {
          c = in.read();
          if (c < 0) throw new ConfigInvalidException(JGitText.get().unexpectedEndOfConfigFile);

          if ('"' == c) {
            in.reset();
            break;
          }

          if (' ' == c || '\t' == c) continue; // Skipped...
          throw new ConfigInvalidException(
              MessageFormat.format(JGitText.get().badSectionEntry, name));
        }
        break;
      }

      if (Character.isLetterOrDigit((char) c) || '.' == c || '-' == c) name.append((char) c);
      else
        throw new ConfigInvalidException(
            MessageFormat.format(JGitText.get().badSectionEntry, name));
    }
    return name.toString();
  }
예제 #2
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));
  }
예제 #3
0
 @Override
 protected void readSingleChildElement(
     XMLStreamReader reader,
     ParsingInfo parsingInfo,
     RawRoot root,
     Map<String, Object> objectMap,
     MdmiBusinessElementRule object)
     throws XMLStreamException {
   // Read rule element
   if (s_ruleReader.canRead(reader)) {
     object.setRule(s_ruleReader.readAndBuild(reader, parsingInfo, root, objectMap));
   }
 }
예제 #4
0
  private static String readKeyName(final StringReader in) throws ConfigInvalidException {
    final StringBuilder name = new StringBuilder();
    for (; ; ) {
      int c = in.read();
      if (c < 0) throw new ConfigInvalidException(JGitText.get().unexpectedEndOfConfigFile);

      if ('=' == c) break;

      if (' ' == c || '\t' == c) {
        for (; ; ) {
          c = in.read();
          if (c < 0) throw new ConfigInvalidException(JGitText.get().unexpectedEndOfConfigFile);

          if ('=' == c) break;

          if (';' == c || '#' == c || '\n' == c) {
            in.reset();
            break;
          }

          if (' ' == c || '\t' == c) continue; // Skipped...
          throw new ConfigInvalidException(JGitText.get().badEntryDelimiter);
        }
        break;
      }

      if (Character.isLetterOrDigit((char) c) || c == '-') {
        // From the git-config man page:
        // The variable names are case-insensitive and only
        // alphanumeric characters and - are allowed.
        name.append((char) c);
      } else if ('\n' == c) {
        in.reset();
        name.append((char) c);
        break;
      } else
        throw new ConfigInvalidException(MessageFormat.format(JGitText.get().badEntryName, name));
    }
    return name.toString();
  }
예제 #5
0
  private static String readValue(final StringReader in, boolean quote, final int eol)
      throws ConfigInvalidException {
    final StringBuilder value = new StringBuilder();
    boolean space = false;
    for (; ; ) {
      int c = in.read();
      if (c < 0) {
        if (value.length() == 0)
          throw new ConfigInvalidException(JGitText.get().unexpectedEndOfConfigFile);
        break;
      }

      if ('\n' == c) {
        if (quote) throw new ConfigInvalidException(JGitText.get().newlineInQuotesNotAllowed);
        in.reset();
        break;
      }

      if (eol == c) break;

      if (!quote) {
        if (Character.isWhitespace((char) c)) {
          space = true;
          continue;
        }
        if (';' == c || '#' == c) {
          in.reset();
          break;
        }
      }

      if (space) {
        if (value.length() > 0) value.append(' ');
        space = false;
      }

      if ('\\' == c) {
        c = in.read();
        switch (c) {
          case -1:
            throw new ConfigInvalidException(JGitText.get().endOfFileInEscape);
          case '\n':
            continue;
          case 't':
            value.append('\t');
            continue;
          case 'b':
            value.append('\b');
            continue;
          case 'n':
            value.append('\n');
            continue;
          case '\\':
            value.append('\\');
            continue;
          case '"':
            value.append('"');
            continue;
          default:
            throw new ConfigInvalidException(
                MessageFormat.format(JGitText.get().badEscape, ((char) c)));
        }
      }

      if ('"' == c) {
        quote = !quote;
        continue;
      }

      value.append((char) c);
    }
    return value.length() > 0 ? value.toString() : null;
  }