2  * $Id: TableElementModel.java,v 1.1 2004-10-05 20:51:57 jsurfer 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;
 
  38         String[] columnProperties;
 
  40         public TableElementModel(String content, boolean parse) throws FactoryConfigurationError, ParserConfigurationException, SAXException, IOException{
 
  41                 docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 
  47                 columnProperties = createColumnProperties();
 
  49                 // create elementWriter
 
  50                 writer = new ElementWriter(0, null);
 
  51                 writer.setExpandOption("caption", ElementWriter.END_CHANGELINE );
 
  52                 writer.setExpandOption("table", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE);
 
  53                 writer.setExpandOption("thead", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE);
 
  54                 writer.setExpandOption("tfoot", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE);
 
  55                 writer.setExpandOption("tbody", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE);
 
  56                 writer.setExpandOption("tr", ElementWriter.END_CHANGELINE);
 
  59         void initModel(String content) throws ParserConfigurationException, SAXException, IOException {
 
  60                 StringReader strReader = new StringReader(content);
 
  61                 InputSource inputSrc = new InputSource(strReader);
 
  63                 document = docBuilder.parse(inputSrc);
 
  64                 tableElement = document.getDocumentElement();
 
  66                 Element[] rows = getRows();
 
  67                 for (int i = 0; i < rows.length; i++) {
 
  68                         Element[] cells = chooseCellElements(rows[i]);
 
  69                         for (int j = 0; j < cells.length; j++) {
 
  70                                 Element cell = cells[j];
 
  71                                 if( !cell.hasChildNodes() ){
 
  72                                         cell.appendChild(document.createTextNode(""));
 
  78         public void initAsParse(String content) throws ParserConfigurationException, FactoryConfigurationError {
 
  79                 // create new table model.
 
  80                 document = docBuilder.newDocument();
 
  81                 tableElement = document.createElement("table");
 
  83                 String[][] cells = stringDivider.divide(content);
 
  85                         for (int i = 0; i < cells.length; i++) {
 
  86                                 String[] rows = cells[i];
 
  87                                 Element tr = document.createElement("tr");
 
  88                                 for (int j = 0; j < rows.length; j++) {
 
  89                                         Element e = document.createElement("td");
 
  90                                         e.appendChild(document.createTextNode(rows[j]));
 
  93                                 tableElement.appendChild(tr);
 
  96                         setColumnCount(cells[0].length);
 
  98                         Element tr = document.createElement("tr");
 
  99                         Element td = document.createElement("td");
 
 100                         td.appendChild(document.createTextNode(""));
 
 102                         tableElement.appendChild(tr);
 
 108         String[] createColumnProperties(){
 
 109                 int len = getColumnCount();
 
 110                 String[] props = new String[len];
 
 111                 for(int i=0; i<len; i++){
 
 112                         props[i] = toColumnName(i);
 
 117         public void setRowCount(int rowCount){
 
 118                 Element[] rows = getRows();
 
 119                 if(rowCount > rows.length){
 
 120                         for(int i=rows.length; i<rowCount; i++){
 
 121                                 tableElement.appendChild( createRowElement());
 
 123                 }else if(rowCount < rows.length){
 
 124                         for(int i=rowCount; i<rows.length; i++){
 
 125                                 tableElement.removeChild(rows[i]);
 
 130         public Element[] getRows(){
 
 131                 ArrayList rows = new ArrayList();
 
 132                 NodeList nodes = tableElement.getElementsByTagName("tr");
 
 133                 for(int i=0; i<nodes.getLength(); i++){
 
 134                         rows.add( nodes.item(i) );
 
 136                 return (Element[])rows.toArray(new Element[rows.size()]);
 
 139         public int getRowCount(){
 
 140                 return getRows().length;
 
 143         Element createRowElement(){
 
 144                 Element tr = document.createElement("tr");
 
 145                 for(int i=0, columnCount = getColumnCount(); i<columnCount; i++){
 
 146                         Element td = document.createElement("td");
 
 147                         td.appendChild(document.createTextNode(""));
 
 153         public void setColumnCount(int newLength){
 
 154                 NodeList trs = tableElement.getElementsByTagName("tr");
 
 155                 for(int i=0; i<trs.getLength(); i++){
 
 156                         Element tr = (Element)trs.item(i);
 
 157                         Element[] cells = chooseCellElements(tr);
 
 158                         int colLen = cells.length;
 
 160                         if( newLength > colLen ){
 
 161                                 for(int j=0, len = newLength - colLen; j<len; j++){
 
 162                                         Element cell = document.createElement("td");
 
 163                                         cell.appendChild(document.createTextNode(""));
 
 164                                         tr.appendChild(cell);
 
 166                         }else if( newLength < colLen ){
 
 167                                 for(int j=newLength; j<colLen; j++){
 
 168                                         tr.removeChild( cells[j]);
 
 172                 columnProperties = createColumnProperties();
 
 175         public int getColumnCount(){
 
 176                 NodeList trs = tableElement.getElementsByTagName("tr");
 
 177                 if( trs.getLength() > 0){
 
 178                         Element tr = (Element)trs.item(0);
 
 179                         return chooseCellElements(tr).length;
 
 185         public static Element[] chooseCellElements(Element tr){
 
 186                 NodeList nodeList = tr.getChildNodes();
 
 188                 ArrayList result = new ArrayList();
 
 189                 for(int i=0; i<nodeList.getLength(); i++){
 
 190                         Node node = nodeList.item(i);
 
 191                         if(node instanceof Element){
 
 192                                 String nodeName = node.getNodeName();
 
 193                                 if(nodeName.equals("td") || nodeName.equals("th") ){
 
 199                 return (Element[])result.toArray(new Element[result.size()]);
 
 202         public String expandCodes(){
 
 203                 return writer.expandTag(tableElement);
 
 207         public static String toColumnName(int i){
 
 208                 StringBuffer buff = new StringBuffer();
 
 209                 int u = i / CHAR_TABLE.length;
 
 211                         buff.append(CHAR_TABLE[u-1]);
 
 213                 buff.append( CHAR_TABLE[i % CHAR_TABLE.length] );
 
 214                 return buff.toString();
 
 218          * Return index of char map. If can not parse values return -1.
 
 220         public static int toNumeric(String code){
 
 222                 for(int i=0; i<code.length(); i++){
 
 223                         char c = code.charAt(i);
 
 224                         int match = Arrays.binarySearch(CHAR_TABLE, c);
 
 230                                 int u = code.length()-1-i;
 
 232                                         v = CHAR_TABLE.length * u * (v+1);
 
 240         public void move(Element tr, int moveCount){
 
 241                 Element[] rows = getRows();
 
 243                 for(int i=0;i<rows.length; i++){
 
 244                         if(tr.equals(rows[i])){
 
 249                         throw new IllegalArgumentException("Invalid row node (not countained in this table):" + tr);
 
 253                         for(int i=index; i<moveCount+index && i<rows.length-1; i++){
 
 254                                 tableElement.insertBefore(rows[i+1], rows[i]);
 
 256                 }else if(moveCount < 0){
 
 258                         for(int i=index; i>=moveCount+index+1 && i >= 1; i--){
 
 259                                 tableElement.insertBefore(rows[index], rows[i-1]);
 
 266         public void insertNewRowBefore(Element tr){
 
 267                 Element newRow = createRowElement();
 
 269                         tableElement.appendChild(newRow);
 
 271                         tableElement.insertBefore(newRow, tr);
 
 275         public void removeRow(Element tr){
 
 276                 tableElement.removeChild(tr);
 
 279         public String[] getColumnProperties() {
 
 280                 return (String[])columnProperties.clone();