home *** CD-ROM | disk | FTP | other *** search
- package dhClasses.html;
-
- import java.io.IOException;
- import java.io.Reader;
- import java.util.Vector;
-
- public class HtmlParser$Table {
- // $FF: synthetic field
- private final HtmlParser this$0;
- private HtmlParser m_tok;
- private boolean m_pushback;
- private int m_tokenType;
- private StringBuffer m_stringValue;
- private StringBuffer m_whiteSpace;
- private HtmlParser.TableCell m_cell;
- private Vector m_row;
- private Vector m_rows;
- private HtmlParser.TableCell[][] m_elements;
- private HtmlTag m_tableTag;
- private Vector m_rowTags;
-
- public HtmlParser$Table(HtmlParser this$0) {
- this.this$0 = this$0;
- this.m_tok = null;
- this.m_pushback = false;
- this.m_tokenType = 0;
- this.m_stringValue = null;
- this.m_whiteSpace = null;
- this.m_cell = null;
- this.m_row = null;
- this.m_rows = new Vector();
- this.m_elements = null;
- this.m_tableTag = null;
- this.m_rowTags = new Vector();
- }
-
- private void addToCell(Object o) {
- this.m_cell.addElement(o);
- }
-
- private void beginCell(HtmlTag tag) {
- int rowspan = 1;
-
- try {
- rowspan = tag.getIntParam(HtmlTag.P_ROWSPAN);
- if (rowspan <= 0) {
- rowspan = 1;
- }
- } catch (NumberFormatException var6) {
- }
-
- int colspan = 1;
-
- try {
- colspan = tag.getIntParam(HtmlTag.P_COLSPAN);
- if (colspan <= 0) {
- colspan = 1;
- }
- } catch (NumberFormatException var5) {
- }
-
- HtmlParser.TableCell cell = new HtmlParser.TableCell(this.this$0, rowspan, colspan, new HtmlTag(tag));
- this.m_cell = cell;
- this.m_row.addElement(cell);
- }
-
- public HtmlParser.TableCell elementAt(int row, int col) {
- return this.m_elements[row][col];
- }
-
- private void endCell() {
- this.m_cell = null;
- }
-
- public int getColumns() {
- return this.m_elements[0].length;
- }
-
- public HtmlTag getRowTag(int row) {
- return row < this.m_rowTags.size() ? (HtmlTag)this.m_rowTags.elementAt(row) : null;
- }
-
- public int getRows() {
- return this.m_elements.length;
- }
-
- private StringBuffer getStringValue() {
- return this.m_stringValue;
- }
-
- public HtmlTag getTableTag() {
- return this.m_tableTag;
- }
-
- private int getTokenType() {
- return this.m_tokenType;
- }
-
- private StringBuffer getWhiteSpace() {
- return this.m_whiteSpace;
- }
-
- private void newRow() {
- Vector row = new Vector();
- this.m_row = row;
- this.m_rows.addElement(row);
- }
-
- private int nextToken() throws IOException {
- if (this.m_pushback) {
- this.m_pushback = false;
- } else {
- this.m_tokenType = this.m_tok.nextToken();
- this.m_stringValue = this.m_tok.getStringValue();
- this.m_whiteSpace = this.m_tok.getWhiteSpace();
- }
-
- return this.m_tokenType;
- }
-
- private void organizeRowCol() throws HtmlException {
- int maxcol = 0;
- Vector rows = this.m_rows;
- int sizey = rows.size();
-
- for(int y = 0; y < sizey; ++y) {
- int col = 0;
- Vector row = (Vector)rows.elementAt(y);
- int sizex = row.size();
-
- for(int x = 0; x < sizex; ++x) {
- HtmlParser.TableCell cell = (HtmlParser.TableCell)row.elementAt(x);
- col += cell.getColSpan();
- if (col > maxcol) {
- maxcol = col;
- }
- }
- }
-
- if (maxcol == 0) {
- throw new HtmlException("zero columns");
- } else {
- int[] rownum = new int[maxcol];
- int maxrow = 0;
-
- for(int y = 0; y < sizey; ++y) {
- int col = 0;
- Vector row = (Vector)rows.elementAt(y);
- int sizex = row.size();
-
- for(int x = 0; x < sizex; ++x) {
- while(y < rownum[col]) {
- ++col;
- }
-
- HtmlParser.TableCell cell = (HtmlParser.TableCell)row.elementAt(x);
- int colspan = cell.getColSpan();
-
- for(int i = 0; i < colspan; ++i) {
- int colnum = col + i;
- rownum[colnum] += cell.getRowSpan();
- if (rownum[colnum] > maxrow) {
- maxrow = rownum[colnum];
- }
- }
-
- col += colspan;
- }
- }
-
- if (maxrow == 0) {
- throw new HtmlException("zero rows");
- } else {
- for(int i = 0; i < maxcol; ++i) {
- rownum[i] = 0;
- }
-
- HtmlParser.TableCell[][] elements = new HtmlParser.TableCell[maxrow][maxcol];
-
- for(int y = 0; y < sizey; ++y) {
- int col = 0;
- Vector row = (Vector)rows.elementAt(y);
- int sizex = row.size();
-
- for(int x = 0; x < sizex; ++x) {
- while(y < rownum[col]) {
- ++col;
- }
-
- HtmlParser.TableCell cell = (HtmlParser.TableCell)row.elementAt(x);
- int r = rownum[col];
- int c = col;
- elements[r][col] = cell;
- int colspan = cell.getColSpan();
-
- for(int i = 0; i < colspan; ++i) {
- int colnum = col + i;
- int rowspan = cell.getRowSpan();
-
- for(int j = 0; j < rowspan; ++j) {
- if (i > 0 || j > 0) {
- elements[rownum[colnum] + j][colnum] = new HtmlParser.TableCell(this.this$0, r, c);
- }
- }
-
- rownum[colnum] += rowspan;
- }
-
- col += colspan;
- }
-
- row.removeAllElements();
- }
-
- rows.removeAllElements();
- this.m_elements = elements;
- }
- }
- }
-
- private boolean parseCol() throws IOException {
- boolean continueParsing = false;
- HtmlTag tag = new HtmlTag();
-
- while(this.nextToken() != -1) {
- int ttype = this.getTokenType();
- if (ttype == -3) {
- try {
- HtmlParser.parseTag(this.getStringValue(), tag);
- int tagtype = tag.getTagType();
- boolean isEndTag = tag.isEndTag();
- if (tagtype == 98) {
- if (!isEndTag) {
- this.pushBackToken();
- }
-
- continueParsing = true;
- break;
- }
-
- if (tagtype == 92 || tagtype == 95) {
- if (!isEndTag) {
- this.pushBackToken();
- }
-
- continueParsing = true;
- break;
- }
-
- if (tagtype == 90) {
- if (isEndTag) {
- continueParsing = false;
- break;
- }
-
- HtmlParser$Table table = new HtmlParser$Table(this.this$0);
- table.parseTable(this.m_tok, new HtmlTag(tag));
- this.addToCell(table);
- } else {
- this.addToCell(new HtmlTag(tag));
- }
- } catch (HtmlException var7) {
- this.addToCell("<" + this.getStringValue().toString() + ">");
- }
- } else if (ttype == -2) {
- String obj = this.getWhiteSpace().toString();
- obj = obj + this.getStringValue().toString();
- this.addToCell(obj);
- }
- }
-
- return continueParsing;
- }
-
- private boolean parseRow() throws IOException {
- boolean continueParsing = false;
- HtmlTag tag = new HtmlTag();
-
- while(this.nextToken() != -1) {
- int ttype = this.getTokenType();
- if (ttype == -3) {
- try {
- HtmlParser.parseTag(this.getStringValue(), tag);
- int tagtype = tag.getTagType();
- boolean isEndTag = tag.isEndTag();
- if (tagtype == 98) {
- if (!isEndTag) {
- this.pushBackToken();
- }
-
- continueParsing = true;
- break;
- }
-
- if ((tagtype == 92 || tagtype == 95) && !isEndTag) {
- this.beginCell(tag);
- if (!this.parseCol()) {
- this.endCell();
- continueParsing = false;
- break;
- }
-
- this.endCell();
- }
- } catch (HtmlException var6) {
- }
- }
- }
-
- return continueParsing;
- }
-
- public void parseTable(HtmlParser tokenizer, HtmlTag tableTag) throws HtmlException, IOException {
- this.m_tok = tokenizer;
- this.m_tableTag = tableTag;
- HtmlTag tag = new HtmlTag();
-
- while(this.nextToken() != -1) {
- int ttype = this.getTokenType();
- if (ttype == -3) {
- try {
- HtmlParser.parseTag(this.getStringValue(), tag);
- int tagtype = tag.getTagType();
- boolean isEndTag = tag.isEndTag();
- if (tagtype == 98 && !isEndTag) {
- this.m_rowTags.addElement(new HtmlTag(tag));
- this.newRow();
- if (!this.parseRow()) {
- break;
- }
- } else if (tagtype == 90 && isEndTag) {
- break;
- }
- } catch (HtmlException var7) {
- }
- }
- }
-
- this.m_tok = null;
- this.m_pushback = false;
- this.m_tokenType = 0;
- this.m_stringValue = null;
- this.m_whiteSpace = null;
- this.m_cell = null;
- this.m_row = null;
- this.organizeRowCol();
- }
-
- public void parseTable(Reader data) throws HtmlException, IOException {
- HtmlParser tok = new HtmlParser(data);
- HtmlTag tag = new HtmlTag();
-
- while(tok.nextToken() != -1) {
- int ttype = tok.getTokenType();
- if (ttype == -3) {
- HtmlParser.parseTag(tok.getStringValue(), tag);
- if (tag.getTagType() == 90 && !tag.isEndTag()) {
- this.parseTable(tok, new HtmlTag(tag));
- }
- }
- }
-
- }
-
- private void pushBackToken() throws IOException {
- if (this.m_pushback) {
- throw new IOException("only one token pushback supported");
- } else {
- this.m_tokenType = this.m_tok.getTokenType();
- this.m_stringValue = this.m_tok.getStringValue();
- this.m_whiteSpace = this.m_tok.getWhiteSpace();
- this.m_pushback = true;
- }
- }
- }
-