home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2003 June / PCpro_2003_06.ISO / offline / picgrabber / pg-setup.exe / CLASSES / CLASSESA.jar / dhClasses / html / HtmlParser$Table.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-09-24  |  7.1 KB  |  376 lines

  1. package dhClasses.html;
  2.  
  3. import java.io.IOException;
  4. import java.io.Reader;
  5. import java.util.Vector;
  6.  
  7. public class HtmlParser$Table {
  8.    // $FF: synthetic field
  9.    private final HtmlParser this$0;
  10.    private HtmlParser m_tok;
  11.    private boolean m_pushback;
  12.    private int m_tokenType;
  13.    private StringBuffer m_stringValue;
  14.    private StringBuffer m_whiteSpace;
  15.    private HtmlParser.TableCell m_cell;
  16.    private Vector m_row;
  17.    private Vector m_rows;
  18.    private HtmlParser.TableCell[][] m_elements;
  19.    private HtmlTag m_tableTag;
  20.    private Vector m_rowTags;
  21.  
  22.    public HtmlParser$Table(HtmlParser this$0) {
  23.       this.this$0 = this$0;
  24.       this.m_tok = null;
  25.       this.m_pushback = false;
  26.       this.m_tokenType = 0;
  27.       this.m_stringValue = null;
  28.       this.m_whiteSpace = null;
  29.       this.m_cell = null;
  30.       this.m_row = null;
  31.       this.m_rows = new Vector();
  32.       this.m_elements = null;
  33.       this.m_tableTag = null;
  34.       this.m_rowTags = new Vector();
  35.    }
  36.  
  37.    private void addToCell(Object o) {
  38.       this.m_cell.addElement(o);
  39.    }
  40.  
  41.    private void beginCell(HtmlTag tag) {
  42.       int rowspan = 1;
  43.  
  44.       try {
  45.          rowspan = tag.getIntParam(HtmlTag.P_ROWSPAN);
  46.          if (rowspan <= 0) {
  47.             rowspan = 1;
  48.          }
  49.       } catch (NumberFormatException var6) {
  50.       }
  51.  
  52.       int colspan = 1;
  53.  
  54.       try {
  55.          colspan = tag.getIntParam(HtmlTag.P_COLSPAN);
  56.          if (colspan <= 0) {
  57.             colspan = 1;
  58.          }
  59.       } catch (NumberFormatException var5) {
  60.       }
  61.  
  62.       HtmlParser.TableCell cell = new HtmlParser.TableCell(this.this$0, rowspan, colspan, new HtmlTag(tag));
  63.       this.m_cell = cell;
  64.       this.m_row.addElement(cell);
  65.    }
  66.  
  67.    public HtmlParser.TableCell elementAt(int row, int col) {
  68.       return this.m_elements[row][col];
  69.    }
  70.  
  71.    private void endCell() {
  72.       this.m_cell = null;
  73.    }
  74.  
  75.    public int getColumns() {
  76.       return this.m_elements[0].length;
  77.    }
  78.  
  79.    public HtmlTag getRowTag(int row) {
  80.       return row < this.m_rowTags.size() ? (HtmlTag)this.m_rowTags.elementAt(row) : null;
  81.    }
  82.  
  83.    public int getRows() {
  84.       return this.m_elements.length;
  85.    }
  86.  
  87.    private StringBuffer getStringValue() {
  88.       return this.m_stringValue;
  89.    }
  90.  
  91.    public HtmlTag getTableTag() {
  92.       return this.m_tableTag;
  93.    }
  94.  
  95.    private int getTokenType() {
  96.       return this.m_tokenType;
  97.    }
  98.  
  99.    private StringBuffer getWhiteSpace() {
  100.       return this.m_whiteSpace;
  101.    }
  102.  
  103.    private void newRow() {
  104.       Vector row = new Vector();
  105.       this.m_row = row;
  106.       this.m_rows.addElement(row);
  107.    }
  108.  
  109.    private int nextToken() throws IOException {
  110.       if (this.m_pushback) {
  111.          this.m_pushback = false;
  112.       } else {
  113.          this.m_tokenType = this.m_tok.nextToken();
  114.          this.m_stringValue = this.m_tok.getStringValue();
  115.          this.m_whiteSpace = this.m_tok.getWhiteSpace();
  116.       }
  117.  
  118.       return this.m_tokenType;
  119.    }
  120.  
  121.    private void organizeRowCol() throws HtmlException {
  122.       int maxcol = 0;
  123.       Vector rows = this.m_rows;
  124.       int sizey = rows.size();
  125.  
  126.       for(int y = 0; y < sizey; ++y) {
  127.          int col = 0;
  128.          Vector row = (Vector)rows.elementAt(y);
  129.          int sizex = row.size();
  130.  
  131.          for(int x = 0; x < sizex; ++x) {
  132.             HtmlParser.TableCell cell = (HtmlParser.TableCell)row.elementAt(x);
  133.             col += cell.getColSpan();
  134.             if (col > maxcol) {
  135.                maxcol = col;
  136.             }
  137.          }
  138.       }
  139.  
  140.       if (maxcol == 0) {
  141.          throw new HtmlException("zero columns");
  142.       } else {
  143.          int[] rownum = new int[maxcol];
  144.          int maxrow = 0;
  145.  
  146.          for(int y = 0; y < sizey; ++y) {
  147.             int col = 0;
  148.             Vector row = (Vector)rows.elementAt(y);
  149.             int sizex = row.size();
  150.  
  151.             for(int x = 0; x < sizex; ++x) {
  152.                while(y < rownum[col]) {
  153.                   ++col;
  154.                }
  155.  
  156.                HtmlParser.TableCell cell = (HtmlParser.TableCell)row.elementAt(x);
  157.                int colspan = cell.getColSpan();
  158.  
  159.                for(int i = 0; i < colspan; ++i) {
  160.                   int colnum = col + i;
  161.                   rownum[colnum] += cell.getRowSpan();
  162.                   if (rownum[colnum] > maxrow) {
  163.                      maxrow = rownum[colnum];
  164.                   }
  165.                }
  166.  
  167.                col += colspan;
  168.             }
  169.          }
  170.  
  171.          if (maxrow == 0) {
  172.             throw new HtmlException("zero rows");
  173.          } else {
  174.             for(int i = 0; i < maxcol; ++i) {
  175.                rownum[i] = 0;
  176.             }
  177.  
  178.             HtmlParser.TableCell[][] elements = new HtmlParser.TableCell[maxrow][maxcol];
  179.  
  180.             for(int y = 0; y < sizey; ++y) {
  181.                int col = 0;
  182.                Vector row = (Vector)rows.elementAt(y);
  183.                int sizex = row.size();
  184.  
  185.                for(int x = 0; x < sizex; ++x) {
  186.                   while(y < rownum[col]) {
  187.                      ++col;
  188.                   }
  189.  
  190.                   HtmlParser.TableCell cell = (HtmlParser.TableCell)row.elementAt(x);
  191.                   int r = rownum[col];
  192.                   int c = col;
  193.                   elements[r][col] = cell;
  194.                   int colspan = cell.getColSpan();
  195.  
  196.                   for(int i = 0; i < colspan; ++i) {
  197.                      int colnum = col + i;
  198.                      int rowspan = cell.getRowSpan();
  199.  
  200.                      for(int j = 0; j < rowspan; ++j) {
  201.                         if (i > 0 || j > 0) {
  202.                            elements[rownum[colnum] + j][colnum] = new HtmlParser.TableCell(this.this$0, r, c);
  203.                         }
  204.                      }
  205.  
  206.                      rownum[colnum] += rowspan;
  207.                   }
  208.  
  209.                   col += colspan;
  210.                }
  211.  
  212.                row.removeAllElements();
  213.             }
  214.  
  215.             rows.removeAllElements();
  216.             this.m_elements = elements;
  217.          }
  218.       }
  219.    }
  220.  
  221.    private boolean parseCol() throws IOException {
  222.       boolean continueParsing = false;
  223.       HtmlTag tag = new HtmlTag();
  224.  
  225.       while(this.nextToken() != -1) {
  226.          int ttype = this.getTokenType();
  227.          if (ttype == -3) {
  228.             try {
  229.                HtmlParser.parseTag(this.getStringValue(), tag);
  230.                int tagtype = tag.getTagType();
  231.                boolean isEndTag = tag.isEndTag();
  232.                if (tagtype == 98) {
  233.                   if (!isEndTag) {
  234.                      this.pushBackToken();
  235.                   }
  236.  
  237.                   continueParsing = true;
  238.                   break;
  239.                }
  240.  
  241.                if (tagtype == 92 || tagtype == 95) {
  242.                   if (!isEndTag) {
  243.                      this.pushBackToken();
  244.                   }
  245.  
  246.                   continueParsing = true;
  247.                   break;
  248.                }
  249.  
  250.                if (tagtype == 90) {
  251.                   if (isEndTag) {
  252.                      continueParsing = false;
  253.                      break;
  254.                   }
  255.  
  256.                   HtmlParser$Table table = new HtmlParser$Table(this.this$0);
  257.                   table.parseTable(this.m_tok, new HtmlTag(tag));
  258.                   this.addToCell(table);
  259.                } else {
  260.                   this.addToCell(new HtmlTag(tag));
  261.                }
  262.             } catch (HtmlException var7) {
  263.                this.addToCell("<" + this.getStringValue().toString() + ">");
  264.             }
  265.          } else if (ttype == -2) {
  266.             String obj = this.getWhiteSpace().toString();
  267.             obj = obj + this.getStringValue().toString();
  268.             this.addToCell(obj);
  269.          }
  270.       }
  271.  
  272.       return continueParsing;
  273.    }
  274.  
  275.    private boolean parseRow() throws IOException {
  276.       boolean continueParsing = false;
  277.       HtmlTag tag = new HtmlTag();
  278.  
  279.       while(this.nextToken() != -1) {
  280.          int ttype = this.getTokenType();
  281.          if (ttype == -3) {
  282.             try {
  283.                HtmlParser.parseTag(this.getStringValue(), tag);
  284.                int tagtype = tag.getTagType();
  285.                boolean isEndTag = tag.isEndTag();
  286.                if (tagtype == 98) {
  287.                   if (!isEndTag) {
  288.                      this.pushBackToken();
  289.                   }
  290.  
  291.                   continueParsing = true;
  292.                   break;
  293.                }
  294.  
  295.                if ((tagtype == 92 || tagtype == 95) && !isEndTag) {
  296.                   this.beginCell(tag);
  297.                   if (!this.parseCol()) {
  298.                      this.endCell();
  299.                      continueParsing = false;
  300.                      break;
  301.                   }
  302.  
  303.                   this.endCell();
  304.                }
  305.             } catch (HtmlException var6) {
  306.             }
  307.          }
  308.       }
  309.  
  310.       return continueParsing;
  311.    }
  312.  
  313.    public void parseTable(HtmlParser tokenizer, HtmlTag tableTag) throws HtmlException, IOException {
  314.       this.m_tok = tokenizer;
  315.       this.m_tableTag = tableTag;
  316.       HtmlTag tag = new HtmlTag();
  317.  
  318.       while(this.nextToken() != -1) {
  319.          int ttype = this.getTokenType();
  320.          if (ttype == -3) {
  321.             try {
  322.                HtmlParser.parseTag(this.getStringValue(), tag);
  323.                int tagtype = tag.getTagType();
  324.                boolean isEndTag = tag.isEndTag();
  325.                if (tagtype == 98 && !isEndTag) {
  326.                   this.m_rowTags.addElement(new HtmlTag(tag));
  327.                   this.newRow();
  328.                   if (!this.parseRow()) {
  329.                      break;
  330.                   }
  331.                } else if (tagtype == 90 && isEndTag) {
  332.                   break;
  333.                }
  334.             } catch (HtmlException var7) {
  335.             }
  336.          }
  337.       }
  338.  
  339.       this.m_tok = null;
  340.       this.m_pushback = false;
  341.       this.m_tokenType = 0;
  342.       this.m_stringValue = null;
  343.       this.m_whiteSpace = null;
  344.       this.m_cell = null;
  345.       this.m_row = null;
  346.       this.organizeRowCol();
  347.    }
  348.  
  349.    public void parseTable(Reader data) throws HtmlException, IOException {
  350.       HtmlParser tok = new HtmlParser(data);
  351.       HtmlTag tag = new HtmlTag();
  352.  
  353.       while(tok.nextToken() != -1) {
  354.          int ttype = tok.getTokenType();
  355.          if (ttype == -3) {
  356.             HtmlParser.parseTag(tok.getStringValue(), tag);
  357.             if (tag.getTagType() == 90 && !tag.isEndTag()) {
  358.                this.parseTable(tok, new HtmlTag(tag));
  359.             }
  360.          }
  361.       }
  362.  
  363.    }
  364.  
  365.    private void pushBackToken() throws IOException {
  366.       if (this.m_pushback) {
  367.          throw new IOException("only one token pushback supported");
  368.       } else {
  369.          this.m_tokenType = this.m_tok.getTokenType();
  370.          this.m_stringValue = this.m_tok.getStringValue();
  371.          this.m_whiteSpace = this.m_tok.getWhiteSpace();
  372.          this.m_pushback = true;
  373.       }
  374.    }
  375. }
  376.