[hmath-commits] org.hmath.server/WEB-INF/src/org/hartmath/server/macro/table MinFunction.java,NONE,1
Status: Pre-Alpha
Brought to you by:
jsurfer
|
From: <js...@us...> - 2004-03-09 20:38:27
|
Update of /cvsroot/hmath/org.hmath.server/WEB-INF/src/org/hartmath/server/macro/table In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21210/WEB-INF/src/org/hartmath/server/macro/table Added Files: MinFunction.java FunctionRepository.java AvgFunction.java TableBuilder.java SumFunction.java Function.java MaxFunction.java Table.java FunctionLoader.java Log Message: initial version --- NEW FILE: MinFunction.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel * All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * --LICENSE NOTICE-- */ package org.hartmath.server.macro.table; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A function that finds the max of table cells * * @author stephan * @version $Id: MinFunction.java,v 1.1 2004/03/09 20:13:06 jsurfer Exp $ */ public class MinFunction implements Function { private static Log log = LogFactory.getLog(MinFunction.class); public String getName() { return "MIN"; } public void execute(Table table, int posx, int posy, int startX, int startY, int endX, int endY) { float min = Float.MAX_VALUE; boolean floating = false; for (int x = startX; x <= endX; x++) { for (int y = startY; y <= endY; y++) { //Logger.debug("x="+x+" y="+y+" >"+getXY(x,y)); float value = 0; try { value += Integer.parseInt((String) table.getXY(x, y)); } catch (Exception e) { try { value += Float.parseFloat((String) table.getXY(x, y)); floating = true; } catch (NumberFormatException e1) { log.debug("SumFunction: unable to parse " + table.getXY(x, y)); } } if (min > value) { min = value; } } } //Logger.debug("Sum="+sum); if (floating) { table.setXY(posx, posy, "" + min); } else { table.setXY(posx, posy, "" + (int) min); } } } --- NEW FILE: FunctionRepository.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel * All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * --LICENSE NOTICE-- */ package org.hartmath.server.macro.table; import org.radeox.macro.PluginRepository; import org.radeox.macro.Repository; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * Repository for functions * * @author Stephan J. Schmidt * @version $Id: FunctionRepository.java,v 1.1 2004/03/09 20:13:06 jsurfer Exp $ */ public class FunctionRepository extends PluginRepository { protected static Repository instance; protected List loaders; public synchronized static Repository getInstance() { if (null == instance) { instance = new FunctionRepository(); } return instance; } private void load() { Iterator iterator = loaders.iterator(); while (iterator.hasNext()) { FunctionLoader loader = (FunctionLoader) iterator.next(); loader.loadPlugins(this); } } private FunctionRepository() { loaders = new ArrayList(); loaders.add(new FunctionLoader()); load(); } } --- NEW FILE: AvgFunction.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel * All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * --LICENSE NOTICE-- */ package org.hartmath.server.macro.table; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A function that calculates the average table cells * * @author stephan * @version $Id: AvgFunction.java,v 1.1 2004/03/09 20:13:06 jsurfer Exp $ */ public class AvgFunction implements Function { private static Log log = LogFactory.getLog(AvgFunction.class); public String getName() { return "AVG"; } public void execute(Table table, int posx, int posy, int startX, int startY, int endX, int endY) { float sum = 0; int count = 0; for (int x = startX; x <= endX; x++) { for (int y = startY; y <= endY; y++) { //Logger.debug("x="+x+" y="+y+" >"+getXY(x,y)); try { sum += Integer.parseInt((String) table.getXY(x, y)); count++; } catch (Exception e) { try { sum += Float.parseFloat((String) table.getXY(x, y)); count++; } catch (NumberFormatException e1) { log.debug("SumFunction: unable to parse " + table.getXY(x, y)); } } } } //Logger.debug("Sum="+sum); table.setXY(posx, posy, "" + sum / count); } } --- NEW FILE: TableBuilder.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser * General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any * later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --LICENSE NOTICE-- */ package org.hartmath.server.macro.table; import org.radeox.api.engine.context.RenderContext; /** * Built a table from a string * * @author stephan * @version $Id: TableBuilder.java,v 1.1 2004/03/09 20:13:06 jsurfer Exp $ */ public class TableBuilder { // public static Table build(String content, RenderContext context) { // Table table = new Table(context); // StringTokenizer tokenizer = new StringTokenizer(content, "|\n", true); // String lastToken = null; // while (tokenizer.hasMoreTokens()) { // String token = tokenizer.nextToken(); // if ("\n".equals(token)) { // // Handles "\n" - "|\n" // if (null == lastToken || "|".equals(lastToken)) { // table.addCell(" "); // } // table.newRow(); // } else if (!"|".equals(token)) { // table.addCell(token); // } else if (null == lastToken || "|".equals(lastToken)) { // // Handles "|" "||" // table.addCell(" "); // } // lastToken = token; // } // return table; // } public static Table build(String content, RenderContext context) { Table table = new Table(context); char[] source = content.toCharArray(); char currentChar = ' '; char lastChar = ' '; int startPosition = 0; int currentPosition = 0; StringBuffer buffer = new StringBuffer(128); // StringTokenizer tokenizer = new StringTokenizer(content, "|\n", true); // String lastToken = null; try { while (true) { currentChar = source[currentPosition++]; switch (currentChar) { case '\n' : // Handles "\n" - "|\n" if (lastChar == '|') { table.addCell(" "); } else { table.addCell(buffer.toString()); buffer = new StringBuffer(128); } table.newRow(); break; case '|' : if (lastChar == '\\') { buffer.insert(buffer.length() - 1, currentChar); } else { table.addCell(buffer.toString()); buffer = new StringBuffer(128); } break; case '{' : if (lastChar == '\\') { buffer.append(currentChar); } else { int tempPosition = currentPosition; char tempChar = ' '; try { lastChar = currentChar; buffer.append(currentChar); while ((tempChar = source[tempPosition++]) != '}' || lastChar == '\\') { buffer.append(tempChar); lastChar = tempChar; } buffer.append(tempChar); currentPosition = tempPosition; } catch (IndexOutOfBoundsException e) { table.addCell(buffer.toString()); buffer = new StringBuffer(128); currentPosition = tempPosition; } } break; default : buffer.append(currentChar); } lastChar = currentChar; } } catch (IndexOutOfBoundsException e) { } return table; } } --- NEW FILE: SumFunction.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel * All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * --LICENSE NOTICE-- */ package org.hartmath.server.macro.table; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A function that summerizes table cells * * @author stephan * @version $Id: SumFunction.java,v 1.1 2004/03/09 20:13:06 jsurfer Exp $ */ public class SumFunction implements Function { private static Log log = LogFactory.getLog(SumFunction.class); public String getName() { return "SUM"; } public void execute(Table table, int posx, int posy, int startX, int startY, int endX, int endY) { float sum = 0; boolean floating = false; for (int x = startX; x <= endX; x++) { for (int y = startY; y <= endY; y++) { //Logger.debug("x="+x+" y="+y+" >"+getXY(x,y)); try { sum += Integer.parseInt((String) table.getXY(x, y)); } catch (Exception e) { try { sum += Float.parseFloat((String) table.getXY(x, y)); floating = true; } catch (NumberFormatException e1) { log.debug("SumFunction: unable to parse " + table.getXY(x, y)); } } } } //Logger.debug("Sum="+sum); if (floating) { table.setXY(posx, posy, "" + sum); } else { table.setXY(posx, posy, "" + (int) sum); } } } --- NEW FILE: Function.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel * All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * --LICENSE NOTICE-- */ package org.hartmath.server.macro.table; /** * A function interface for functions in table cells like * =SUM(A1:A3) * * @author stephan * @version $Id: Function.java,v 1.1 2004/03/09 20:13:06 jsurfer Exp $ */ public interface Function { public String getName(); public void execute(Table table, int posx, int posy, int startX, int startY, int endX, int endY); } --- NEW FILE: MaxFunction.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel * All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * --LICENSE NOTICE-- */ package org.hartmath.server.macro.table; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A function that finds the max of table cells * * @author stephan * @version $Id: MaxFunction.java,v 1.1 2004/03/09 20:13:06 jsurfer Exp $ */ public class MaxFunction implements Function { private static Log log = LogFactory.getLog(MaxFunction.class); public String getName() { return "MAX"; } public void execute(Table table, int posx, int posy, int startX, int startY, int endX, int endY) { float max = 0; boolean floating = false; for (int x = startX; x <= endX; x++) { for (int y = startY; y <= endY; y++) { //Logger.debug("x="+x+" y="+y+" >"+getXY(x,y)); float value = 0; try { value += Integer.parseInt((String) table.getXY(x, y)); } catch (Exception e) { try { value += Float.parseFloat((String) table.getXY(x, y)); floating = true; } catch (NumberFormatException e1) { log.debug("SumFunction: unable to parse " + table.getXY(x, y)); } } if (max < value) { max = value; } } } //Logger.debug("Sum="+sum); if (floating) { table.setXY(posx, posy, "" + max); } else { table.setXY(posx, posy, "" + (int) max); } } } --- NEW FILE: Table.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser * General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any * later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --LICENSE NOTICE-- */ package org.hartmath.server.macro.table; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.radeox.api.engine.RenderEngine; import org.radeox.api.engine.context.RenderContext; import org.radeox.macro.Repository; /** * A Table implementation primarly for the table macro * * @author stephan * @version $Id: Table.java,v 1.1 2004/03/09 20:13:06 jsurfer Exp $ */ public class Table { // current number of rows private int indexRow = 0; // current number of cols private int indexCol = 0; private List rows; private List currentRow; private List functionOccurences; private RenderContext fContext; private RenderEngine fEngine; private Repository functions; public Table(RenderContext context) { rows = new ArrayList(10); currentRow = new ArrayList(10); functions = FunctionRepository.getInstance(); fContext = context; fEngine = context.getRenderEngine(); } private void addFunction(Function function) { functions.put(function.getName().toLowerCase(), function); } public Object getXY(int x, int y) { // perhaps move everything to a twodim array first return ((List) rows.get(y)).get(x); } public void setXY(int x, int y, Object content) { ((List) rows.get(y)).set(x, content); } /** * Add a cell to the current row of the table * * @param content * Content of the cell */ public void addCell(String content) { content = content.trim(); if (content.startsWith("=")) { //Logger.debug("Table.addCell: function found."); if (null == functionOccurences) { functionOccurences = new ArrayList(); } functionOccurences.add(new int[] { indexCol, indexRow }); // function } // jsurfer start - filter the current tables cell String temp = fEngine.render(content, fContext); if (temp != null) { currentRow.add(temp); } else { currentRow.add(content); } // currentRow.add(content);+ // jsurfer end indexCol++; } /** * Finishes current row and starts a new one */ public void newRow() { rows.add(currentRow); indexRow++; // create new row with number of cells of // the last row, this is a good guess currentRow = new ArrayList(indexCol); indexCol = 0; } /** * Recalculate all cells. Currently does nothing. */ public void calc() { if (null != functionOccurences) { Iterator iterator = functionOccurences.iterator(); while (iterator.hasNext()) { int[] position = (int[]) iterator.next(); String functionString = ((String) getXY(position[0], position[1])).trim(); // better use RegEx String name = functionString.substring(1, functionString.indexOf("(")).trim().toLowerCase(); String range = functionString.substring(functionString.indexOf("(") + 1, functionString.indexOf(")")); int colon = range.indexOf(":"); String start = range.substring(0, colon).trim(); String end = range.substring(colon + 1).trim(); int startX = start.charAt(0) - 'A'; int startY = Integer.parseInt(start.substring(1)) - 1; int endX = end.charAt(0) - 'A'; int endY = Integer.parseInt(end.substring(1)) - 1; // normalize range, start is left top, end is bottom right if (startX > endX) { int tmp = startX; startX = endX; endX = tmp; } if (startY > endY) { int tmp = startY; startY = endY; endY = tmp; } //Logger.debug("Calc: " + position[0] + " " + position[1] + " " + function + " " + start + " " + end); //Logger.debug("Calc: " + startX+","+startY+" - "+endX+","+endY); if (functions.containsKey(name)) { Function function = (Function) functions.get(name); function.execute(this, position[0], position[1], startX, startY, endX, endY); } } } return; } /** * Serialize table by appending it to a writer. The output format is HTML. * * @param writer * Writer to append the table object to * * @return writer Writer the table object appended itself to */ public Writer appendTo(Writer writer) throws IOException { writer.write("<table class=\"wiki-table\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"); List[] outputRows = (List[]) rows.toArray(new List[0]); int rowSize = outputRows.length; boolean odd = true; for (int i = 0; i < rowSize; i++) { writer.write("<tr"); if (i == 0) { writer.write(">"); } else if (odd) { writer.write(" class=\"table-odd\">"); odd = false; } else { writer.write(" class=\"table-even\">"); odd = true; } String[] outputCols = (String[]) outputRows[i].toArray(new String[0]); int colSize = outputCols.length; for (int j = 0; j < colSize; j++) { writer.write(i == 0 ? "<th>" : "<td>"); if (outputCols[j] == null || outputCols[j].trim().length() == 0) { writer.write(" "); } else { writer.write(outputCols[j]); } writer.write(i == 0 ? "</th>" : "</td>"); } writer.write("</tr>"); } writer.write("</table>"); return writer; } } --- NEW FILE: FunctionLoader.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel * All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * --LICENSE NOTICE-- */ package org.hartmath.server.macro.table; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.radeox.macro.PluginLoader; import org.radeox.macro.Repository; /** * Plugin loader for table functions * * @author Stephan J. Schmidt * @version $Id: FunctionLoader.java,v 1.1 2004/03/09 20:13:06 jsurfer Exp $ */ public class FunctionLoader extends PluginLoader { private static Log log = LogFactory.getLog(FunctionLoader.class); protected static FunctionLoader instance; public static synchronized PluginLoader getInstance() { if (null == instance) { instance = new FunctionLoader(); } return instance; } public Class getLoadClass() { return Function.class; } /** * Add a plugin to the known plugin map * * @param plugin Function to add */ public void add(Repository repository, Object plugin) { if (plugin instanceof Function) { repository.put(((Function) plugin).getName().toLowerCase(), plugin); } else { log.debug("FunctionLoader: " + plugin.getClass() + " not of Type " + getLoadClass()); } } } |