ParameterParser { public Map<String, String> parse(final String str, char[] separators) { if (separators == null || separators.length == 0) { return new HashMap<String, String>(); } char separator = separators[0]; if (str != null) { int idx = str.length(); for (char separator2 : separators) { int tmp = str.indexOf(separator2); if (tmp != -1 && tmp < idx) { idx = tmp; separator = separator2; } } } return parse(str, separator); } ParameterParser(); boolean isLowerCaseNames(); void setLowerCaseNames(boolean b); Map<String, String> parse(final String str, char[] separators); Map<String, String> parse(final String str, char separator); Map<String, String> parse(final char[] charArray, char separator); Map<String, String> parse(
        final char[] charArray,
        int offset,
        int length,
        char separator);  }
@Test public void testParsing() { String s = "test; test1 = stuff ; test2 = \"stuff; stuff\"; test3=\"stuff"; ParameterParser parser = new ParameterParser(); Map<String, String> params = parser.parse(s, ';'); assertEquals(null, params.get("test")); assertEquals("stuff", params.get("test1")); assertEquals("stuff; stuff", params.get("test2")); assertEquals("\"stuff", params.get("test3")); params = parser.parse(s, new char[] { ',', ';' }); assertEquals(null, params.get("test")); assertEquals("stuff", params.get("test1")); assertEquals("stuff; stuff", params.get("test2")); assertEquals("\"stuff", params.get("test3")); s = " test , test1=stuff , , test2=, test3, "; params = parser.parse(s, ','); assertEquals(null, params.get("test")); assertEquals("stuff", params.get("test1")); assertEquals(null, params.get("test2")); assertEquals(null, params.get("test3")); s = " test"; params = parser.parse(s, ';'); assertEquals(null, params.get("test")); s = " "; params = parser.parse(s, ';'); assertEquals(0, params.size()); s = " = stuff "; params = parser.parse(s, ';'); assertEquals(0, params.size()); }
@Test public void testParsingEscapedChars() { String s = "param = \"stuff\\\"; more stuff\""; ParameterParser parser = new ParameterParser(); Map<String, String> params = parser.parse(s, ';'); assertEquals(1, params.size()); assertEquals("stuff\\\"; more stuff", params.get("param")); s = "param = \"stuff\\\\\"; anotherparam"; params = parser.parse(s, ';'); assertEquals(2, params.size()); assertEquals("stuff\\\\", params.get("param")); assertNull(params.get("anotherparam")); }
@Test public void testFileUpload139() { ParameterParser parser = new ParameterParser(); String s = "Content-type: multipart/form-data , boundary=AaB03x"; Map<String, String> params = parser.parse(s, new char[] { ',', ';' }); assertEquals("AaB03x", params.get("boundary")); s = "Content-type: multipart/form-data, boundary=AaB03x"; params = parser.parse(s, new char[] { ';', ',' }); assertEquals("AaB03x", params.get("boundary")); s = "Content-type: multipart/mixed, boundary=BbC04y"; params = parser.parse(s, new char[] { ',', ';' }); assertEquals("BbC04y", params.get("boundary")); }
@Test public void fileUpload199() { ParameterParser parser = new ParameterParser(); String s = "Content-Disposition: form-data; name=\"file\"; filename=\"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n"; Map<String, String> params = parser.parse(s, new char[] { ',', ';' }); assertEquals("If you can read this you understand the example.", params.get("filename")); }