Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

73
LINES

< > BotCompany Repo | #1000903 // SelectFinder (find "select" tags in HTML, todo)

JavaX fragment (include)

1  
static class SelectFinder {
2  
  L<S> tok; // list of tokens in HTML document
3  
  L<S> select; // list of tokens in select
4  
  L<L<S>> rows; // for every row, list of tokens
5  
  L<S[]> options; // for every options, value plus content
6  
7  
  void go(S html) {
8  
    tok = htmlcoarsetok(html);
9  
    findSelect();
10  
  }
11  
12  
  void findSelect() {
13  
    print("Finding select.");
14  
    for (int i = 1; i < tok.size(); i += 2)
15  
      if (isTag(tok.get(i), "select"))
16  
        for (int j = i+2; j < tok.size(); j += 2)
17  
          if (isTag(tok.get(j), "/select")) {
18  
            print("Select found!");
19  
            select = tok.subList(i-1, j+2);
20  
            findRows();
21  
            return;
22  
          }
23  
  }
24  
  
25  
  void findRows() {
26  
    L<S> tok = select;
27  
    rows = new ArrayList<List<S>>();
28  
    options = new ArrayList<S[]>();
29  
    int rowStart = 0;
30  
    
31  
    for (int i = 1; i < select.size(); i += 2) {
32  
      //print(tok.get(i));
33  
      if (isTag(tok.get(i), "option")) {
34  
        if (rowStart != 0)
35  
          rows.add(select.subList(rowStart-1, i));
36  
        rowStart = i;
37  
      } else if (isTag(tok.get(i), "/option") && rowStart != 0) {
38  
        rows.add(select.subList(rowStart-1, i+2));
39  
        rowStart = 0;
40  
      }
41  
    }
42  
    /*if (rowStart != 0)
43  
      rows.add(select.subList(rowStart-1, i+2));*/ // TODO (unclosed option at the end)
44  
      
45  
    for (L<S> row : rows) options.add(getData(row));
46  
    
47  
    print(rows.size() + " row(s)");
48  
  }
49  
  
50  
  boolean isTag(S token, S tag) {
51  
    return token.regionMatches(true, 0, "<" + tag + " ", 0, tag.length()+2)
52  
      || token.regionMatches(true, 0, "<" + tag + ">", 0, tag.length()+2);
53  
  }
54  
  
55  
  S[] getData(L<S> row) {
56  
    int colStart = 0, colEnd = 0;
57  
58  
    for (int i = 1; i < row.size(); i += 2) {
59  
      S t = row.get(i);
60  
      if (isTag(t, "option"))
61  
        colStart = i;
62  
      else if (isTag(t, "/option"))
63  
        colEnd = i;
64  
    }
65  
    
66  
    Map<S, S> map = htmlgetparams(row.get(colStart));
67  
    S value = map.get("value");
68  
    if (colEnd == 0) colEnd = row.size();
69  
    S data = join(row.subList(colStart+1, colEnd));
70  
    if (value == null) value = data;
71  
    return new S[] {value, data};
72  
  }
73  
}

Author comment

Began life as a copy of #1000850

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000903
Snippet name: SelectFinder (find "select" tags in HTML, todo)
Eternal ID of this version: #1000903/1
Text MD5: 48273cc4348adb5cd2dd4919482cd46b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-07 16:16:51
Source code size: 2181 bytes / 73 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 637 / 847
Referenced in: [show references]