Exemplo n.º 1
0
  ConfigElement processLine(String configfile, int lineno, CharBuffer buffer)
      throws ConfigParseException {
    char c;
    final StringBuilder processLineBuilder;
    final StringBuilder lineCommentBuilder;

    processLineBuilder = new StringBuilder(MAX_LINE_LENGTH);
    lineCommentBuilder = new StringBuilder(MAX_LINE_LENGTH);
    buffer.mark();

    while (buffer.hasRemaining()) {
      final int position;

      position = buffer.position();
      c = buffer.get();
      // System.out.println(position + ": " + c);

      if (c == COMMENT_META) {
        if (position >= 1 && buffer.get(position - 1) == '\\') {
          /* Escaped semicolons aren't comments. */
        } // NOPMD
        else if (buffer.remaining() >= 3
            && buffer.get(position + 1) == COMMENT_TAG
            && buffer.get(position + 2) == COMMENT_TAG
            && buffer.get(position + 3) != COMMENT_TAG) {
          /* Meta-Comment start detected ";--" */

          currentCommentLevel++;
          // System.out.println("Comment start, new level: " + currentCommentLevel);

          if (!inComment()) {
            commentBlock.append(";--");
            buffer.position(position + 3);
            buffer.mark();
            continue;
          }
        } else if (inComment()
            && position >= 2
            && buffer.get(position - 1) == COMMENT_TAG
            && buffer.get(position - 2) == COMMENT_TAG) {
          /* Meta-Comment end detected */

          currentCommentLevel--;

          if (!inComment()) {
            buffer.reset();

            // int commentLength = (position + 1) - buffer.position();

            // buffer.reset();
            // for (int i = 0; i < commentLength; i++)
            // {
            //    commentBlock.append(buffer.get());
            // }

            commentBlock.append(c);
            // System.out.println("Comment end at " + position + ": '" + commentBlock.toString() +
            // "'");

            buffer.position(position + 1);
            buffer.compact();
            buffer.flip();

            // System.out.println("Buffer compacted");
            continue;
          }
        } else {
          if (!inComment()) {
            /* If ; is found, and we are not nested in a comment, we immediately stop all comment processing */
            // System.out.println("Found ; while not in comment");
            while (buffer.hasRemaining()) {
              lineCommentBuilder.append(buffer.get());
            }
            break;
          } else {
            /* Found ';' while in comment */
          } // NOPMD
        }
      }

      if (inComment()) {
        commentBlock.append(c);
      } else {
        // System.out.println("Added '" + c + "' to processLine");
        processLineBuilder.append(c);
      }
    }

    String processLineString;
    String lineCommentString;
    ConfigElement configElement;

    processLineString = processLineBuilder.toString().trim();
    lineCommentString = lineCommentBuilder.toString().trim();

    // System.out.println("process line: '" + processLineString + "'");
    if (processLineString.length() == 0) {
      if (lineCommentString.length() != 0) {
        commentBlock.append(";");
        commentBlock.append(lineCommentString);
      }
      if (!inComment()) {
        commentBlock.append("\n");
      }
      return null;
    }

    try {
      configElement = processTextLine(configfile, lineno, processLineString);
    } catch (ConfigParseException e) {
      // some parsing exceptions are treated as warnings by Asterisk, we mirror this behavior.
      if (WARNING_CLASSES.contains(e.getClass())) {
        warnings.add(e);
        return null;
      } else {
        throw e;
      }
    }

    if (lineCommentString.length() != 0) {
      configElement.setComment(lineCommentString);
    }

    if (commentBlock.length() != 0) {
      configElement.setPreComment(commentBlock.toString());
      commentBlock.delete(0, commentBlock.length());
    }

    return configElement;
  }