2 * $Id: TableElementModel.java,v 1.2 2006-10-21 23:18:43 pombredanne Exp $
3 * Copyright Narushima Hironori. All rights reserved.
5 package net.sourceforge.phpeclipse.wizards.html;
7 import java.io.IOException;
8 import java.io.StringReader;
9 import java.util.ArrayList;
10 import java.util.Arrays;
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.FactoryConfigurationError;
15 import javax.xml.parsers.ParserConfigurationException;
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.Node;
20 import org.w3c.dom.NodeList;
21 import org.xml.sax.InputSource;
22 import org.xml.sax.SAXException;
27 public class TableElementModel {
29 final static char[] CHAR_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
31 StringDivider stringDivider = new StringDivider();
35 DocumentBuilder docBuilder;
41 String[] columnProperties;
43 public TableElementModel(String content, boolean parse)
44 throws FactoryConfigurationError, ParserConfigurationException,
45 SAXException, IOException {
46 docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
52 columnProperties = createColumnProperties();
54 // create elementWriter
55 writer = new ElementWriter(0, null);
56 writer.setExpandOption("caption", ElementWriter.END_CHANGELINE);
57 writer.setExpandOption("table", ElementWriter.BEGIN_CHANGELINE
58 | ElementWriter.END_CHANGELINE);
59 writer.setExpandOption("thead", ElementWriter.BEGIN_CHANGELINE
60 | ElementWriter.END_CHANGELINE);
61 writer.setExpandOption("tfoot", ElementWriter.BEGIN_CHANGELINE
62 | ElementWriter.END_CHANGELINE);
63 writer.setExpandOption("tbody", ElementWriter.BEGIN_CHANGELINE
64 | ElementWriter.END_CHANGELINE);
65 writer.setExpandOption("tr", ElementWriter.END_CHANGELINE);
68 void initModel(String content) throws ParserConfigurationException,
69 SAXException, IOException {
70 StringReader strReader = new StringReader(content);
71 InputSource inputSrc = new InputSource(strReader);
73 document = docBuilder.parse(inputSrc);
74 tableElement = document.getDocumentElement();
76 Element[] rows = getRows();
77 for (int i = 0; i < rows.length; i++) {
78 Element[] cells = chooseCellElements(rows[i]);
79 for (int j = 0; j < cells.length; j++) {
80 Element cell = cells[j];
81 if (!cell.hasChildNodes()) {
82 cell.appendChild(document.createTextNode(""));
88 public void initAsParse(String content)
89 throws ParserConfigurationException, FactoryConfigurationError {
90 // create new table model.
91 document = docBuilder.newDocument();
92 tableElement = document.createElement("table");
94 String[][] cells = stringDivider.divide(content);
95 if (cells.length > 0) {
96 for (int i = 0; i < cells.length; i++) {
97 String[] rows = cells[i];
98 Element tr = document.createElement("tr");
99 for (int j = 0; j < rows.length; j++) {
100 Element e = document.createElement("td");
101 e.appendChild(document.createTextNode(rows[j]));
104 tableElement.appendChild(tr);
107 setColumnCount(cells[0].length);
109 Element tr = document.createElement("tr");
110 Element td = document.createElement("td");
111 td.appendChild(document.createTextNode(""));
113 tableElement.appendChild(tr);
119 String[] createColumnProperties() {
120 int len = getColumnCount();
121 String[] props = new String[len];
122 for (int i = 0; i < len; i++) {
123 props[i] = toColumnName(i);
128 public void setRowCount(int rowCount) {
129 Element[] rows = getRows();
130 if (rowCount > rows.length) {
131 for (int i = rows.length; i < rowCount; i++) {
132 tableElement.appendChild(createRowElement());
134 } else if (rowCount < rows.length) {
135 for (int i = rowCount; i < rows.length; i++) {
136 tableElement.removeChild(rows[i]);
141 public Element[] getRows() {
142 ArrayList rows = new ArrayList();
143 NodeList nodes = tableElement.getElementsByTagName("tr");
144 for (int i = 0; i < nodes.getLength(); i++) {
145 rows.add(nodes.item(i));
147 return (Element[]) rows.toArray(new Element[rows.size()]);
150 public int getRowCount() {
151 return getRows().length;
154 Element createRowElement() {
155 Element tr = document.createElement("tr");
156 for (int i = 0, columnCount = getColumnCount(); i < columnCount; i++) {
157 Element td = document.createElement("td");
158 td.appendChild(document.createTextNode(""));
164 public void setColumnCount(int newLength) {
165 NodeList trs = tableElement.getElementsByTagName("tr");
166 for (int i = 0; i < trs.getLength(); i++) {
167 Element tr = (Element) trs.item(i);
168 Element[] cells = chooseCellElements(tr);
169 int colLen = cells.length;
171 if (newLength > colLen) {
172 for (int j = 0, len = newLength - colLen; j < len; j++) {
173 Element cell = document.createElement("td");
174 cell.appendChild(document.createTextNode(""));
175 tr.appendChild(cell);
177 } else if (newLength < colLen) {
178 for (int j = newLength; j < colLen; j++) {
179 tr.removeChild(cells[j]);
183 columnProperties = createColumnProperties();
186 public int getColumnCount() {
187 NodeList trs = tableElement.getElementsByTagName("tr");
188 if (trs.getLength() > 0) {
189 Element tr = (Element) trs.item(0);
190 return chooseCellElements(tr).length;
196 public static Element[] chooseCellElements(Element tr) {
197 NodeList nodeList = tr.getChildNodes();
199 ArrayList result = new ArrayList();
200 for (int i = 0; i < nodeList.getLength(); i++) {
201 Node node = nodeList.item(i);
202 if (node instanceof Element) {
203 String nodeName = node.getNodeName();
204 if (nodeName.equals("td") || nodeName.equals("th")) {
210 return (Element[]) result.toArray(new Element[result.size()]);
213 public String expandCodes() {
214 return writer.expandTag(tableElement);
217 public static String toColumnName(int i) {
218 StringBuffer buff = new StringBuffer();
219 int u = i / CHAR_TABLE.length;
221 buff.append(CHAR_TABLE[u - 1]);
223 buff.append(CHAR_TABLE[i % CHAR_TABLE.length]);
224 return buff.toString();
228 * Return index of char map. If can not parse values return -1.
230 public static int toNumeric(String code) {
232 for (int i = 0; i < code.length(); i++) {
233 char c = code.charAt(i);
234 int match = Arrays.binarySearch(CHAR_TABLE, c);
240 int u = code.length() - 1 - i;
242 v = CHAR_TABLE.length * u * (v + 1);
250 public void move(Element tr, int moveCount) {
251 Element[] rows = getRows();
253 for (int i = 0; i < rows.length; i++) {
254 if (tr.equals(rows[i])) {
259 throw new IllegalArgumentException(
260 "Invalid row node (not countained in this table):" + tr);
264 for (int i = index; i < moveCount + index && i < rows.length - 1; i++) {
265 tableElement.insertBefore(rows[i + 1], rows[i]);
267 } else if (moveCount < 0) {
269 for (int i = index; i >= moveCount + index + 1 && i >= 1; i--) {
270 tableElement.insertBefore(rows[index], rows[i - 1]);
277 public void insertNewRowBefore(Element tr) {
278 Element newRow = createRowElement();
280 tableElement.appendChild(newRow);
282 tableElement.insertBefore(newRow, tr);
286 public void removeRow(Element tr) {
287 tableElement.removeChild(tr);
290 public String[] getColumnProperties() {
291 return (String[]) columnProperties.clone();