BasicHeaderValueParser implements HeaderValueParser { @Override public NameValuePair parseNameValuePair(final CharSequence buffer, final ParserCursor cursor) { Args.notNull(buffer, "Char sequence"); Args.notNull(cursor, "Parser cursor"); final String name = tokenizer.parseToken(buffer, cursor, TOKEN_DELIMS); if (cursor.atEnd()) { return new BasicNameValuePair(name, null); } final int delim = buffer.charAt(cursor.getPos()); cursor.updatePos(cursor.getPos() + 1); if (delim != '=') { return new BasicNameValuePair(name, null); } final String value = tokenizer.parseValue(buffer, cursor, VALUE_DELIMS); if (!cursor.atEnd()) { cursor.updatePos(cursor.getPos() + 1); } return new BasicNameValuePair(name, value); } BasicHeaderValueParser(); @Override HeaderElement[] parseElements(final CharSequence buffer, final ParserCursor cursor); @Override HeaderElement parseHeaderElement(final CharSequence buffer, final ParserCursor cursor); @Override NameValuePair[] parseParameters(final CharSequence buffer, final ParserCursor cursor); @Override NameValuePair parseNameValuePair(final CharSequence buffer, final ParserCursor cursor); final static BasicHeaderValueParser INSTANCE; }
@Test public void testNVParse() { String s = "test"; CharArrayBuffer buffer = new CharArrayBuffer(64); buffer.append(s); ParserCursor cursor = new ParserCursor(0, s.length()); NameValuePair param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("test", param.getName()); Assert.assertEquals(null, param.getValue()); Assert.assertEquals(s.length(), cursor.getPos()); Assert.assertTrue(cursor.atEnd()); s = "test;"; buffer = new CharArrayBuffer(16); buffer.append(s); cursor = new ParserCursor(0, s.length()); param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("test", param.getName()); Assert.assertEquals(null, param.getValue()); Assert.assertEquals(s.length(), cursor.getPos()); Assert.assertTrue(cursor.atEnd()); s = "test ,12"; buffer = new CharArrayBuffer(16); buffer.append(s); cursor = new ParserCursor(0, s.length()); param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("test", param.getName()); Assert.assertEquals(null, param.getValue()); Assert.assertEquals(s.length() - 2, cursor.getPos()); Assert.assertFalse(cursor.atEnd()); s = "test=stuff"; buffer = new CharArrayBuffer(16); buffer.append(s); cursor = new ParserCursor(0, s.length()); param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("test", param.getName()); Assert.assertEquals("stuff", param.getValue()); Assert.assertEquals(s.length(), cursor.getPos()); Assert.assertTrue(cursor.atEnd()); s = " test = stuff "; buffer = new CharArrayBuffer(16); buffer.append(s); cursor = new ParserCursor(0, s.length()); param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("test", param.getName()); Assert.assertEquals("stuff", param.getValue()); Assert.assertEquals(s.length(), cursor.getPos()); Assert.assertTrue(cursor.atEnd()); s = " test = stuff ;1234"; buffer = new CharArrayBuffer(16); buffer.append(s); cursor = new ParserCursor(0, s.length()); param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("test", param.getName()); Assert.assertEquals("stuff", param.getValue()); Assert.assertEquals(s.length() - 4, cursor.getPos()); Assert.assertFalse(cursor.atEnd()); s = "test = \"stuff\""; buffer = new CharArrayBuffer(16); buffer.append(s); cursor = new ParserCursor(0, s.length()); param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("test", param.getName()); Assert.assertEquals("stuff", param.getValue()); s = "test = \" stuff\\\"\""; buffer = new CharArrayBuffer(16); buffer.append(s); cursor = new ParserCursor(0, s.length()); param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("test", param.getName()); Assert.assertEquals(" stuff\"", param.getValue()); s = " test"; buffer = new CharArrayBuffer(16); buffer.append(s); cursor = new ParserCursor(0, s.length()); param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("test", param.getName()); Assert.assertEquals(null, param.getValue()); s = " "; buffer = new CharArrayBuffer(16); buffer.append(s); cursor = new ParserCursor(0, s.length()); param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("", param.getName()); Assert.assertEquals(null, param.getValue()); s = " = stuff "; buffer = new CharArrayBuffer(16); buffer.append(s); cursor = new ParserCursor(0, s.length()); param = this.parser.parseNameValuePair(buffer, cursor); Assert.assertEquals("", param.getName()); Assert.assertEquals("stuff", param.getValue()); }