Example usage for org.apache.http.message ParserCursor updatePos

List of usage examples for org.apache.http.message ParserCursor updatePos

Introduction

In this page you can find the example usage for org.apache.http.message ParserCursor updatePos.

Prototype

public void updatePos(int i) 

Source Link

Usage

From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Skip SP and HT characters in a line.   
 * /*www  . jav  a  2  s .c om*/
 * @param lnBuf Buffer containing line.
 * @param lnCursor Parser cursor for lnBuf. Adjusted to one character after and SP and HT.
 */
protected void skipSpHt(final CharArrayBuffer lnBuf, final ParserCursor lnCursor) {
    int idxTo = lnCursor.getUpperBound();
    int idxPos = lnCursor.getPos();
    while (idxPos < idxTo && (lnBuf.charAt(idxPos) == HTTP.SP || lnBuf.charAt(idxPos) == HTTP.HT)) {
        idxPos++;
    }
    lnCursor.updatePos(idxPos);
}

From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Get the next word from a buffer containing a line.
 * /*w  ww .  ja v a 2s . c  o m*/
 * @param lnBuf Buffer containing line.
 * @param lnCursor Parser cursor for lnBuf. Adjusted to one character after the word.
 * @return Next word, or null if none is found.
 */
protected String nextWord(final CharArrayBuffer lnBuf, final ParserCursor lnCursor) {
    skipSpHt(lnBuf, lnCursor);
    int idxPos = lnCursor.getPos();
    int idxLineEnd = lnBuf.indexOf(' ', idxPos, lnCursor.getUpperBound());
    if (idxLineEnd < 0) {
        if (idxPos == lnCursor.getUpperBound()) {
            return null;
        }
        idxLineEnd = lnCursor.getUpperBound();
    }
    lnCursor.updatePos(idxLineEnd);
    return lnBuf.substringTrimmed(idxPos, idxLineEnd);
}

From source file:com.ok2c.lightmtp.util.InetAddressRangeParser.java

public List<InetAddressRange> parseAll(final CharArrayBuffer buffer, final ParserCursor cursor,
        final char[] delimiters) throws ParseException, UnknownHostException {

    Args.notNull(buffer, "Char array buffer");
    Args.notNull(cursor, "Parser cursor");

    char[] delims = delimiters != null ? delimiters : COMMA;

    List<InetAddressRange> ranges = new ArrayList<InetAddressRange>();
    while (!cursor.atEnd()) {
        ranges.add(parse(buffer, cursor, delims));
        int pos = cursor.getPos();
        if (pos < cursor.getUpperBound() && isOneOf(buffer.charAt(pos), delims)) {
            cursor.updatePos(pos + 1);
        }/*w  w w . jav a 2 s.com*/
    }
    return ranges;
}

From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java

public NameValuePair[] parseParameters(final CharArrayBuffer buffer, final ParserCursor cursor) {
    AssertUtils.notNull(buffer, "Char array buffer");
    AssertUtils.notNull(cursor, "Parser cursor");
    int pos = cursor.getPos();
    final int indexTo = cursor.getUpperBound();

    while (pos < indexTo) {
        final char ch = buffer.charAt(pos);
        if (HTTP.isWhitespace(ch)) {
            pos++;//  w  w w .j  a v a 2  s . c o m
        } else {
            break;
        }
    }
    cursor.updatePos(pos);
    if (cursor.atEnd()) {
        return new NameValuePair[] {};
    }

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

    return params.toArray(new NameValuePair[params.size()]);
}

From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Get the next line of characters from a CharArrayBuffer into another CharArrayBuffer. Treats LF and CRLF as valid
 * line delimiters. Treats the entire buffer as a line if no line delimiters are found.
 * /*from  w  ww .  ja  v  a2s . c  o  m*/
 * @param src Source buffer to read line from.
 * @param srcCursor Parser cursor for src. Adjusted to discard line delimiters.
 * @param dst Destination buffer for characters from line. 
 * @return Number of characters in line minus line delimiters, or < 0 if none found.
 */
protected int readLine(final CharArrayBuffer src, final ParserCursor srcCursor, final CharArrayBuffer dst) {
    if (srcCursor.atEnd()) {
        return -1;
    }

    int idxPos = srcCursor.getPos();
    int idxLf = src.indexOf(HTTP.LF, idxPos, srcCursor.getUpperBound());
    int idxEnd;

    if (idxLf > 0) {
        if (src.charAt(idxLf - 1) == HTTP.CR) {
            idxEnd = idxLf - 1;
        } else {
            idxEnd = idxLf;
        }
    } else {
        idxEnd = srcCursor.getUpperBound();
        idxLf = idxEnd - 1;
    }

    dst.append(src, idxPos, idxEnd - idxPos);
    srcCursor.updatePos(idxLf + 1);
    return idxEnd - idxPos;
}

From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Parse HTTP headers and add them to a IHttpMessageBuilder until a non-header line is encountered.
 * /*w  w w  . j  ava  2  s  . co  m*/
 * @param parser HC line parser.
 * @param builder IHttpMessageBuilder to add parsed headers to.
 * @param buf Buffer containing header data.
 * @param bufCursor Parser cursor for buf. Adjusted to one character past the end of headers and optional CRLF line.
 */
protected void parseHeaders(final LineParser parser, final IHttpMessageBuilder builder,
        final CharArrayBuffer buf, final ParserCursor bufCursor) {
    final CharArrayBuffer lnBuf = new CharArrayBuffer(0);
    while (true) {
        lnBuf.clear();
        int idxPos = bufCursor.getPos();
        if (readLineHeader(buf, bufCursor, lnBuf) > 0) {
            try {
                // REVISIT don't want an extra step
                Header header = parser.parseHeader(lnBuf);
                builder.addHeader(header.getName(), header.getValue());
            } catch (ParseException e) {
                // for now we'll move the cursor back so the line gets treated as the start of the body
                bufCursor.updatePos(idxPos);
                return;
            }
        } else {
            break;
        }
    }
}

From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Get the next header line of characters from a CharArrayBuffer into another CharArrayBuffer. Treats LF and CRLF as
 * valid line delimiters. Treats the entire buffer as a line if no line delimiters are found. Supports folded header
 * field values as per the HTTP/1.1 specification.
 *    // w  w w .java2s  .com
 * @param src Source buffer to read line from.
 * @param srcCursor Parser cursor for src. Adjusted to discard line delimiters.
 * @param dst Destination buffer for characters from line. 
 * @return Number of characters in line minus line delimiters, or < 0 if none found.
 */
protected int readLineHeader(final CharArrayBuffer src, final ParserCursor srcCursor,
        final CharArrayBuffer dst) {
    if (srcCursor.atEnd()) {
        return -1;
    }

    int idxPos = srcCursor.getPos();
    int chCnt = 0;
    int idxLf, idxEnd;

    do {
        idxLf = src.indexOf(HTTP.LF, idxPos, srcCursor.getUpperBound());

        if (idxLf > 0) {
            if (src.charAt(idxLf - 1) == HTTP.CR) {
                idxEnd = idxLf - 1;
            } else {
                idxEnd = idxLf;
            }
        } else {
            idxEnd = srcCursor.getUpperBound();
            idxLf = idxEnd - 1;
        }

        if (chCnt != 0) {
            while (idxPos < idxEnd && (src.charAt(idxPos) == HTTP.HT || src.charAt(idxPos) == HTTP.SP)) {
                idxPos++;
            }
            if (idxPos != idxEnd) {
                dst.append(' ');
            }
        }

        dst.append(src, idxPos, idxEnd - idxPos);
        chCnt += idxEnd - idxPos;
        idxPos = idxLf + 1;
        srcCursor.updatePos(idxPos);
    } while (idxPos < srcCursor.getUpperBound()
            && (src.charAt(idxPos) == HTTP.HT || src.charAt(idxPos) == HTTP.SP));

    return chCnt;
}

From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java

public NameValuePair parseNameValuePair(final CharArrayBuffer buffer, final ParserCursor cursor,
        final char[] delimiters) {
    AssertUtils.notNull(buffer, "Char array buffer");
    AssertUtils.notNull(cursor, "Parser cursor");

    boolean terminated = false;

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

    // Find name/*  ww  w  . j a  v  a  2s  .  c o  m*/
    final String name;
    while (pos < indexTo) {
        final char ch = buffer.charAt(pos);
        if (ch == '=') {
            break;
        }
        if (isOneOf(ch, delimiters)) {
            terminated = true;
            break;
        }
        pos++;
    }

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

    if (terminated) {
        cursor.updatePos(pos);
        return createNameValuePair(name, null);
    }

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

    boolean qouted = false;
    boolean escaped = false;
    while (pos < indexTo) {
        final 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 && (HTTP.isWhitespace(buffer.charAt(i1)))) {
        i1++;
    }
    // Trim trailing white spaces
    while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) {
        i2--;
    }
    // Strip away quotes if necessary
    if (((i2 - i1) >= 2) && (buffer.charAt(i1) == '"') && (buffer.charAt(i2 - 1) == '"')) {
        i1++;
        i2--;
    }
    value = buffer.substring(i1, i2);
    if (terminated) {
        pos++;
    }
    cursor.updatePos(pos);
    return createNameValuePair(name, value);
}

From source file:com.ok2c.lightmtp.util.InetAddressRangeParser.java

public InetAddressRange parse(final CharArrayBuffer buffer, final ParserCursor cursor, final char[] delimiters)
        throws ParseException, UnknownHostException {

    Args.notNull(buffer, "Char array buffer");
    Args.notNull(cursor, "Parser cursor");

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

    while (pos < indexTo) {
        char ch = buffer.charAt(pos);
        if (ch == '/') {
            break;
        }//  w  ww  . j a  va2 s  . c  o  m
        if (isOneOf(ch, delimiters)) {
            break;
        }
        pos++;
    }

    InetAddress address = InetAddress.getByName(buffer.substringTrimmed(indexFrom, pos));
    int mask = 0;

    if (pos < indexTo && buffer.charAt(pos) == '/') {
        pos++;
        indexFrom = pos;
        while (pos < indexTo) {
            char ch = buffer.charAt(pos);
            if (isOneOf(ch, delimiters)) {
                break;
            }
            pos++;
        }
        try {
            mask = Integer.parseInt(buffer.substringTrimmed(indexFrom, pos));
            if (mask < 0) {
                throw new ParseException("Negative range mask", indexFrom);
            }
        } catch (NumberFormatException ex) {
            throw new ParseException("Invalid range mask", indexFrom);
        }
    }
    cursor.updatePos(pos);
    return new InetAddressRange(address, mask);
}