01: /*
02: * This file is part of "SnipSnap Radeox Rendering Engine".
03: *
04: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
05: * All Rights Reserved.
06: *
07: * Please visit http://radeox.org/ for updates and contact.
08: *
09: * --LICENSE NOTICE--
10: * Licensed under the Apache License, Version 2.0 (the "License");
11: * you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software
17: * distributed under the License is distributed on an "AS IS" BASIS,
18: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19: * See the License for the specific language governing permissions and
20: * limitations under the License.
21: * --LICENSE NOTICE--
22: */
23:
24: package org.radeox.macro.table;
25:
26: import org.apache.commons.logging.Log;
27: import org.apache.commons.logging.LogFactory;
28:
29: /**
30: * A function that summerizes table cells
31: *
32: * @author stephan
33: * @version $Id: SumFunction.java 7756 2006-04-13 12:25:49Z ian@caret.cam.ac.uk $
34: */
35:
36: public class SumFunction implements Function {
37: private static Log log = LogFactory.getLog(SumFunction.class);
38:
39: public String getName() {
40: return "SUM";
41: }
42:
43: public void execute(Table table, int posx, int posy, int startX,
44: int startY, int endX, int endY) {
45: float sum = 0;
46: boolean floating = false;
47: for (int x = startX; x <= endX; x++) {
48: for (int y = startY; y <= endY; y++) {
49: // Logger.debug("x="+x+" y="+y+" >"+getXY(x,y));
50: try {
51: sum += Integer.parseInt((String) table.getXY(x, y));
52: } catch (Exception e) {
53: try {
54: sum += Float.parseFloat((String) table.getXY(x,
55: y));
56: floating = true;
57: } catch (NumberFormatException e1) {
58: log.debug("SumFunction: unable to parse "
59: + table.getXY(x, y));
60: }
61: }
62: }
63: }
64: // Logger.debug("Sum="+sum);
65: if (floating) {
66: table.setXY(posx, posy, "" + sum);
67: } else {
68: table.setXY(posx, posy, "" + (int) sum);
69: }
70: }
71:
72: }
|