static class SelectFinder { L tok; // list of tokens in HTML document L select; // list of tokens in select L> rows; // for every row, list of tokens L options; // for every options, value plus content void go(S html) { tok = htmlcoarsetok(html); findSelect(); } void findSelect() { print("Finding select."); for (int i = 1; i < tok.size(); i += 2) if (isTag(tok.get(i), "select")) for (int j = i+2; j < tok.size(); j += 2) if (isTag(tok.get(j), "/select")) { print("Select found!"); select = tok.subList(i-1, j+2); findRows(); return; } } void findRows() { L tok = select; rows = new ArrayList>(); options = new ArrayList(); int rowStart = 0; for (int i = 1; i < select.size(); i += 2) { //print(tok.get(i)); if (isTag(tok.get(i), "option")) { if (rowStart != 0) rows.add(select.subList(rowStart-1, i)); rowStart = i; } else if (isTag(tok.get(i), "/option") && rowStart != 0) { rows.add(select.subList(rowStart-1, i+2)); rowStart = 0; } } /*if (rowStart != 0) rows.add(select.subList(rowStart-1, i+2));*/ // TODO (unclosed option at the end) for (L row : rows) options.add(getData(row)); print(rows.size() + " row(s)"); } boolean isTag(S token, S tag) { return token.regionMatches(true, 0, "<" + tag + " ", 0, tag.length()+2) || token.regionMatches(true, 0, "<" + tag + ">", 0, tag.length()+2); } S[] getData(L row) { int colStart = 0, colEnd = 0; for (int i = 1; i < row.size(); i += 2) { S t = row.get(i); if (isTag(t, "option")) colStart = i; else if (isTag(t, "/option")) colEnd = i; } Map map = htmlgetparams(row.get(colStart)); S value = map.get("value"); if (colEnd == 0) colEnd = row.size(); S data = join(row.subList(colStart+1, colEnd)); if (value == null) value = data; return new S[] {value, data}; } }