Source code: com/arthurdo/parser/TableCell.java
1 /*
2 * Copyright (c) 1996, 2001 by Arthur Do <arthur@cs.stanford.edu>.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package com.arthurdo.parser;
21
22 import java.util.*;
23
24 /**
25 * <p>TableCell repesents a table cell. Normally its elements are
26 * a collection of markup and text data. However, if <i>isSpanned()</i>
27 * is true, then it is a pseudo cell, a place holder cell that
28 * contains the location of the actual cell. Pseudo cells
29 * have no elements, i.e. size() returns zero. An example
30 * of a pseudo cell would be:
31 *
32 * <p><blockquote>
33 * <table><br>
34 * <tr><td colspan=2>abc<br>
35 * </table>
36 * </blockquote>
37 *
38 * <p>In this case, the cell at coordinate [0,0] is a real cell
39 * while [0,1] is a pseudo cell.
40 *
41 * <p><ul>
42 * <li> 02/09/98 Dr. Jaron Collis, added getCharacterData() which
43 * returns only the cell's character data, without any markup.
44 * </ul>
45 *
46 * @version 0.9 01/32/98
47 * @author Arthur Do <arthur@cs.stanford.edu>
48 * @see com.arthurdo.parser.Table
49 */
50 public class TableCell
51 {
52 public TableCell(int rowspan, int colspan, HtmlTag cellTag)
53 {
54 m_rowspan = rowspan;
55 m_colspan = colspan;
56 m_cellTag = cellTag;
57 }
58
59 /**
60 * Use this constructor to create a pseudo cell. This is a
61 * placeholder for a cell that has been spanned.
62 * Call <i>getRowSpan()</i> and <i>getColumnSpan()</i> to locate
63 * the coordinate of the actual cell.
64 */
65 public TableCell(int row, int col)
66 {
67 m_spanned = true;
68 m_rowspan = row;
69 m_colspan = col;
70 }
71
72 /**
73 * @return whether this is a spanned (pseudo) cell.
74 */
75 public boolean isSpanned()
76 {
77 return m_spanned;
78 }
79
80 /**
81 * @return if <i>isSpanned()</i> is true then function
82 * returns the row of the actual cell that
83 * spans this pseudo cell otherwise it returns
84 * the number of rows this cell spans.
85 */
86 public int getRowSpan()
87 {
88 return m_rowspan;
89 }
90
91 /**
92 * @return if <i>isSpanned()</i> is true then function
93 * returns the column of the actual cell that
94 * spans this pseudo cell otherwise it returns
95 * the number of columns this cell spans.
96 */
97 public int getColSpan()
98 {
99 return m_colspan;
100 }
101
102 /**
103 * @return the original <TD> tag for this cell.
104 */
105 public HtmlTag getCellTag()
106 {
107 return m_cellTag;
108 }
109
110 /**
111 * @return the number of elements.
112 */
113 public int size()
114 {
115 return m_content.size();
116 }
117
118 /**
119 * @param obj element to add
120 */
121 public void addElement(Object obj)
122 {
123 m_content.addElement(obj);
124 }
125
126 /**
127 * @param index index of element
128 * @return a cell element, normally this will be either
129 * a String, HtmlTag, or HtmlTable object (comments are thrown
130 * away) but new object types could be added in the
131 * future. Use <i>instanceof</i>
132 * to determine the particular object type.
133 */
134 public Object elementAt(int index)
135 {
136 return m_content.elementAt(index);
137 }
138
139 /**
140 * @return only the character data, i.e. no markup
141 */
142 public String getCharacterData()
143 {
144 String content = "";
145 for (int i=0; i < m_content.size(); i++)
146 {
147 Object obj = m_content.elementAt(i);
148 if (obj instanceof String)
149 content += obj;
150 }
151 return content;
152 }
153
154 private int m_rowspan = 0;
155 private int m_colspan = 0;
156 private Vector m_content = new Vector();
157 private HtmlTag m_cellTag = null;
158 private boolean m_spanned = false;
159 }