001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023:
024: package org.radeox.macro.table;
025:
026: import java.io.IOException;
027: import java.io.Writer;
028: import java.util.ArrayList;
029: import java.util.Arrays;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Set;
034:
035: import org.radeox.macro.Repository;
036:
037: /**
038: * A Table implementation primarly for the table macro
039: *
040: * @author stephan
041: * @version $Id: Table.java 14994 2006-09-20 15:49:14Z andrew@caret.cam.ac.uk $
042: */
043:
044: public class Table {
045: // current number of rows
046: private int indexRow = 0;
047:
048: // current number of cols
049: private int indexCol = 0;
050:
051: private List rows;
052:
053: private List currentRow;
054:
055: private String currentCell;
056:
057: private List functionOccurences;
058:
059: private Repository functions;
060:
061: private HashMap tableAttributes = new HashMap();
062:
063: public Table() {
064: rows = new ArrayList(10);
065: currentRow = new ArrayList(10);
066: currentCell = "";
067: functions = FunctionRepository.getInstance();
068: tableAttributes.put("border", "0");
069: tableAttributes.put("cellspacing", "0");
070: tableAttributes.put("cellpadding", "0");
071: tableAttributes.put("class", "wiki-table");
072:
073: }
074:
075: public void setAttribute(String attributeName, String attributeValue) {
076: //FIXME do some checks here
077: tableAttributes.put(attributeName, attributeValue);
078: }
079:
080: private void addFunction(Function function) {
081: functions.put(function.getName().toLowerCase(), function);
082: }
083:
084: public Object getXY(int x, int y) {
085: // perhaps move everything to a twodim array first
086: return ((List) rows.get(y)).get(x);
087: }
088:
089: public void setXY(int x, int y, Object content) {
090: ((List) rows.get(y)).set(x, content);
091: }
092:
093: /**
094: * Add a cell to the current row of the table
095: *
096: * @param content
097: * Content of the cell
098: */
099: public void addCell(String content) {
100: if (!currentCell.equals("")) {
101: newCell();
102: }
103: addText(content);
104: }
105:
106: public void newCell() {
107: currentCell = currentCell.trim();
108: if (currentCell.startsWith("=")) {
109: // Logger.debug("Table.addCell: function found.");
110: if (null == functionOccurences) {
111: functionOccurences = new ArrayList();
112: }
113: functionOccurences.add(new int[] { indexCol, indexRow });
114: // function
115: }
116: currentRow.add(currentCell);
117: indexCol++;
118:
119: currentCell = "";
120: }
121:
122: public void addText(String content) {
123: currentCell += content;
124: }
125:
126: /**
127: * Finishes current row and starts a new one
128: */
129: public void newRow() {
130: newCell();
131: rows.add(currentRow);
132: indexRow++;
133: // create new row with number of cells of
134: // the last row, this is a good guess
135: currentRow = new ArrayList(indexCol);
136: indexCol = 0;
137: }
138:
139: /**
140: * Recalculate all cells. Currently does nothing.
141: */
142: public void calc() {
143: if (null != functionOccurences) {
144: Iterator iterator = functionOccurences.iterator();
145: while (iterator.hasNext()) {
146: int[] position = (int[]) iterator.next();
147: String functionString = ((String) getXY(position[0],
148: position[1])).trim();
149: // better use RegEx
150: String name = functionString.substring(1,
151: functionString.indexOf("(")).trim()
152: .toLowerCase();
153:
154: String range = functionString.substring(functionString
155: .indexOf("(") + 1, functionString.indexOf(")"));
156: int colon = range.indexOf(":");
157: String start = range.substring(0, colon).trim();
158: String end = range.substring(colon + 1).trim();
159:
160: int startX = start.charAt(0) - 'A';
161: int startY = Integer.parseInt(start.substring(1)) - 1;
162: int endX = end.charAt(0) - 'A';
163: int endY = Integer.parseInt(end.substring(1)) - 1;
164:
165: // normalize range, start is left top, end is bottom right
166: if (startX > endX) {
167: int tmp = startX;
168: startX = endX;
169: endX = tmp;
170: }
171:
172: if (startY > endY) {
173: int tmp = startY;
174: startY = endY;
175: endY = tmp;
176: }
177:
178: // Logger.debug("Calc: " + position[0] + " " + position[1] + " "
179: // + function + " " + start + " " + end);
180: // Logger.debug("Calc: " + startX+","+startY+" -
181: // "+endX+","+endY);
182:
183: if (functions.containsKey(name)) {
184: Function function = (Function) functions.get(name);
185: function.execute(this , position[0], position[1],
186: startX, startY, endX, endY);
187: }
188: }
189: }
190: return;
191:
192: }
193:
194: /**
195: * Serialize table by appending it to a writer. The output format is HTML.
196: *
197: * @param writer
198: * Writer to append the table object to
199: * @return writer Writer the table object appended itself to
200: */
201: public Writer appendTo(Writer writer) throws IOException {
202: writer.write("<table");// class=\"wiki-table\" cellpadding=\"0\"
203: // cellspacing=\"0\" border=\"0\">");
204: Set keySet = tableAttributes.keySet();
205: String[] toSort = new String[keySet.size()];
206: {
207: int i = 0;
208: for (Iterator it = keySet.iterator(); it.hasNext(); i++) {
209: toSort[i] = (String) it.next();
210: }
211: }
212: Arrays.sort(toSort);
213:
214: for (int i = 0; i < toSort.length; i++) {
215: String key = toSort[i];
216: writer.write(' ');
217: writer.write(key);
218: writer.write("=\"");
219: writer.write((String) tableAttributes.get(key));
220: writer.write("\"");
221: }
222:
223: writer.write(">");
224: List[] outputRows = (List[]) rows
225: .toArray(new List[rows.size()]);
226: int rowSize = outputRows.length;
227: boolean odd = true;
228: for (int i = 0; i < rowSize; i++) {
229: writer.write("<tr");
230: if (i == 0) {
231: writer.write(">");
232: } else if (odd) {
233: writer.write(" class=\"table-odd\">");
234: odd = false;
235: } else {
236: writer.write(" class=\"table-even\">");
237: odd = true;
238: }
239: String[] outputCols = (String[]) outputRows[i]
240: .toArray(new String[outputRows[i].size()]);
241: int colSize = outputCols.length;
242: for (int j = 0; j < colSize; j++) {
243: writer.write(i == 0 ? "<th>" : "<td>");
244: if (outputCols[j] == null
245: || outputCols[j].trim().length() == 0) {
246: writer.write(" ");
247: } else {
248: writer.write(outputCols[j]);
249: }
250: writer.write(i == 0 ? "</th>" : "</td>");
251: }
252: writer.write("</tr>");
253: }
254: writer.write("</table>");
255: return writer;
256: }
257: }
|