/* * $Id: TableElementModel.java,v 1.1 2004-10-05 20:51:57 jsurfer Exp $ * Copyright Narushima Hironori. All rights reserved. */ package net.sourceforge.phpeclipse.wizards.html; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.Arrays; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * TableElementModel */ public class TableElementModel { final static char[] CHAR_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); StringDivider stringDivider = new StringDivider(); ElementWriter writer; DocumentBuilder docBuilder; Document document; Element tableElement; String[] columnProperties; public TableElementModel(String content, boolean parse) throws FactoryConfigurationError, ParserConfigurationException, SAXException, IOException{ docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); if(parse){ initAsParse(content); }else{ initModel(content); } columnProperties = createColumnProperties(); // create elementWriter writer = new ElementWriter(0, null); writer.setExpandOption("caption", ElementWriter.END_CHANGELINE ); writer.setExpandOption("table", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE); writer.setExpandOption("thead", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE); writer.setExpandOption("tfoot", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE); writer.setExpandOption("tbody", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE); writer.setExpandOption("tr", ElementWriter.END_CHANGELINE); } void initModel(String content) throws ParserConfigurationException, SAXException, IOException { StringReader strReader = new StringReader(content); InputSource inputSrc = new InputSource(strReader); document = docBuilder.parse(inputSrc); tableElement = document.getDocumentElement(); Element[] rows = getRows(); for (int i = 0; i < rows.length; i++) { Element[] cells = chooseCellElements(rows[i]); for (int j = 0; j < cells.length; j++) { Element cell = cells[j]; if( !cell.hasChildNodes() ){ cell.appendChild(document.createTextNode("")); } } } } public void initAsParse(String content) throws ParserConfigurationException, FactoryConfigurationError { // create new table model. document = docBuilder.newDocument(); tableElement = document.createElement("table"); String[][] cells = stringDivider.divide(content); if(cells.length > 0){ for (int i = 0; i < cells.length; i++) { String[] rows = cells[i]; Element tr = document.createElement("tr"); for (int j = 0; j < rows.length; j++) { Element e = document.createElement("td"); e.appendChild(document.createTextNode(rows[j])); tr.appendChild(e); } tableElement.appendChild(tr); } setColumnCount(cells[0].length); }else{ Element tr = document.createElement("tr"); Element td = document.createElement("td"); td.appendChild(document.createTextNode("")); tr.appendChild(td); tableElement.appendChild(tr); setColumnCount(1); } } String[] createColumnProperties(){ int len = getColumnCount(); String[] props = new String[len]; for(int i=0; i rows.length){ for(int i=rows.length; i colLen ){ for(int j=0, len = newLength - colLen; j 0){ Element tr = (Element)trs.item(0); return chooseCellElements(tr).length; }else{ return 0; } } public static Element[] chooseCellElements(Element tr){ NodeList nodeList = tr.getChildNodes(); ArrayList result = new ArrayList(); for(int i=0; i 0){ buff.append(CHAR_TABLE[u-1]); } buff.append( CHAR_TABLE[i % CHAR_TABLE.length] ); return buff.toString(); } /** * Return index of char map. If can not parse values return -1. */ public static int toNumeric(String code){ int result = -1; for(int i=0; i= 0){ if(result == -1){ result = 0; } int v = match; int u = code.length()-1-i; if(u>0){ v = CHAR_TABLE.length * u * (v+1); } result += v; } } return result; } public void move(Element tr, int moveCount){ Element[] rows = getRows(); int index = -1; for(int i=0;i 0){ // move down; for(int i=index; i=moveCount+index+1 && i >= 1; i--){ tableElement.insertBefore(rows[index], rows[i-1]); } }else{ return; } } public void insertNewRowBefore(Element tr){ Element newRow = createRowElement(); if( tr == null){ tableElement.appendChild(newRow); }else{ tableElement.insertBefore(newRow, tr); } } public void removeRow(Element tr){ tableElement.removeChild(tr); } public String[] getColumnProperties() { return (String[])columnProperties.clone(); } }