示例#1
0
 private Directive parseDirective(final StringBuilder buffer, final ParserCursor cursor) {
   Parameter parameter = parseParameter(buffer, cursor, ALL_DELIMITERS);
   List<Parameter> params = Collections.emptyList();
   if (!cursor.atEnd()) {
     char ch = buffer.charAt(cursor.getPos() - 1);
     if (ch != ELEM_DELIMITER) {
       params = parseParameters(buffer, cursor);
     }
   }
   if (parameter instanceof QuotedParameter) {
     return createDirective(
         parameter.getName(), ((QuotedParameter) parameter).getQuotedValue(), params);
   }
   return createDirective(parameter.getName(), parameter.getValue(), params);
 }
示例#2
0
 private List<Directive> parseDirectives(final StringBuilder buffer, final ParserCursor cursor) {
   List<Directive> elements = new ArrayList<Directive>();
   while (!cursor.atEnd()) {
     Directive element = parseDirective(buffer, cursor);
     if (!(element.getName().length() == 0 && element.getValue() == null)) {
       elements.add(element);
     }
   }
   return elements;
 }
示例#3
0
  private List<Parameter> parseParameters(final StringBuilder buffer, final ParserCursor cursor) {

    if (buffer == null) {
      throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
      throw new IllegalArgumentException("Parser cursor may not be null");
    }

    int pos = cursor.getPos();
    int indexTo = cursor.getUpperBound();

    while (pos < indexTo) {
      char ch = buffer.charAt(pos);
      if (isWhitespace(ch)) {
        pos++;
      } else {
        break;
      }
    }
    cursor.updatePos(pos);
    if (cursor.atEnd()) {
      return Collections.emptyList();
    }

    List<Parameter> params = new ArrayList<Parameter>();
    while (!cursor.atEnd()) {
      params.add(parseParameter(buffer, cursor, ALL_DELIMITERS));
      char ch = buffer.charAt(cursor.getPos() - 1);
      if (ch == ELEM_DELIMITER) {
        break;
      }
    }

    return params;
  }
示例#4
0
  private Parameter parseParameter(
      final StringBuilder buffer, final ParserCursor cursor, final char[] delimiters) {

    if (buffer == null) {
      throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
      throw new IllegalArgumentException("Parser cursor may not be null");
    }

    boolean terminated = false;

    int pos = cursor.getPos();
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();

    // Find name
    String name = null;
    while (pos < indexTo) {
      char ch = buffer.charAt(pos);
      if (ch == '=') {
        break;
      }
      if (isOneOf(ch, delimiters)) {
        terminated = true;
        break;
      }
      pos++;
    }

    if (pos == indexTo) {
      terminated = true;
      name = buffer.substring(indexFrom, indexTo).trim();
    } else {
      name = buffer.substring(indexFrom, pos).trim();
      pos++;
    }

    if (terminated) {
      cursor.updatePos(pos);
      int i = name.indexOf('<');
      int j = name.indexOf('>');
      if (i != -1 && j != -1) { // this is a Link header
        return createParameter(HeaderConstants.LINK_HEADER, name.substring(i + 1, j));
      }

      return createParameter(name, null);
    }

    // Find value
    String value = null;
    int i1 = pos;

    boolean qouted = false;
    boolean escaped = false;
    while (pos < indexTo) {
      char ch = buffer.charAt(pos);
      if (ch == '"' && !escaped) {
        qouted = !qouted;
      }
      if (!qouted && !escaped && isOneOf(ch, delimiters)) {
        terminated = true;
        break;
      }
      if (escaped) {
        escaped = false;
      } else {
        escaped = qouted && ch == '\\';
      }
      pos++;
    }

    int i2 = pos;
    // Trim leading white spaces
    while (i1 < i2 && (isWhitespace(buffer.charAt(i1)))) {
      i1++;
    }
    // Trim trailing white spaces
    while ((i2 > i1) && (isWhitespace(buffer.charAt(i2 - 1)))) {
      i2--;
    }
    value = buffer.substring(i1, i2);
    if (terminated) {
      pos++;
    }
    cursor.updatePos(pos);
    return createParameter(name, value);
  }