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)

static class SelectFinder {
  L<S> tok; // list of tokens in HTML document
  L<S> select; // list of tokens in select
  L<L<S>> rows; // for every row, list of tokens
  L<S[]> 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<S> tok = select;
    rows = new ArrayList<List<S>>();
    options = new ArrayList<S[]>();
    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<S> 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<S> 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<S, S> 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};
  }
}

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: 638 / 847
Referenced in: #1000902 - Testing SelectFinder (find snippet types)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)