001: /**
002: * ========================================
003: * JFreeReport : a free Java report library
004: * ========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * $Id: BufferedReportTarget.java 3048 2007-07-28 18:02:42Z tmorgner $
027: * ------------
028: * (C) Copyright 2000-2005, by Object Refinery Limited.
029: * (C) Copyright 2005-2007, by Pentaho Corporation.
030: */package org.jfree.report.flow.layoutprocessor;
031:
032: import java.util.ArrayList;
033:
034: import org.jfree.layouting.namespace.NamespaceDefinition;
035: import org.jfree.layouting.util.AttributeMap;
036: import org.jfree.report.DataFlags;
037: import org.jfree.report.DataSourceException;
038: import org.jfree.report.ReportProcessingException;
039: import org.jfree.report.flow.ReportStructureRoot;
040: import org.jfree.report.flow.ReportTarget;
041:
042: /**
043: * Todo: Document me!
044: *
045: * @author Thomas Morgner
046: * @since 05.03.2007
047: */
048: public class BufferedReportTarget implements ReportTarget, Cloneable {
049: public static class RecordedCall {
050: private int methodId;
051: private Object parameters;
052:
053: public RecordedCall(final int method, final Object parameters) {
054: this .methodId = method;
055: this .parameters = parameters;
056: }
057:
058: public int getMethod() {
059: return methodId;
060: }
061:
062: public Object getParameters() {
063: return parameters;
064: }
065: }
066:
067: private static final int MTH_START_REPORT = 1;
068: private static final int MTH_START_ELEMENT = 2;
069: private static final int MTH_PROCESS_TEXT = 3;
070: private static final int MTH_PROCESS_CONTENT = 4;
071: private static final int MTH_END_ELEMENT = 5;
072: private static final int MTH_END_REPORT = 6;
073:
074: private ReportTarget target;
075: private ArrayList calls;
076:
077: public BufferedReportTarget() {
078: this .calls = new ArrayList();
079: }
080:
081: public ReportTarget getTarget() {
082: return target;
083: }
084:
085: public void setTarget(final ReportTarget target) {
086: this .target = target;
087: }
088:
089: public void startReport(final ReportStructureRoot report)
090: throws DataSourceException, ReportProcessingException {
091: calls.add(new RecordedCall(
092: BufferedReportTarget.MTH_START_REPORT, report));
093: }
094:
095: public void startElement(final AttributeMap attrs)
096: throws DataSourceException, ReportProcessingException {
097: try {
098: calls.add(new RecordedCall(
099: BufferedReportTarget.MTH_START_ELEMENT, attrs
100: .clone()));
101: } catch (CloneNotSupportedException e) {
102: throw new ReportProcessingException(
103: "Failed to clone attributes", e);
104: }
105: }
106:
107: public void processText(final String text)
108: throws DataSourceException, ReportProcessingException {
109: calls.add(new RecordedCall(
110: BufferedReportTarget.MTH_PROCESS_TEXT, text));
111: }
112:
113: public void processContent(final DataFlags value)
114: throws DataSourceException, ReportProcessingException {
115: calls.add(new RecordedCall(
116: BufferedReportTarget.MTH_PROCESS_CONTENT, value));
117: }
118:
119: public void endElement(final AttributeMap attrs)
120: throws DataSourceException, ReportProcessingException {
121: try {
122: calls
123: .add(new RecordedCall(
124: BufferedReportTarget.MTH_END_ELEMENT, attrs
125: .clone()));
126: } catch (CloneNotSupportedException e) {
127: throw new ReportProcessingException(
128: "Failed to clone attributes", e);
129: }
130: }
131:
132: public void endReport(final ReportStructureRoot report)
133: throws DataSourceException, ReportProcessingException {
134: calls.add(new RecordedCall(BufferedReportTarget.MTH_END_REPORT,
135: report));
136: }
137:
138: public String getExportDescriptor() {
139: return target.getExportDescriptor();
140: }
141:
142: public NamespaceDefinition getNamespaceByUri(final String uri) {
143: return target.getNamespaceByUri(uri);
144: }
145:
146: public void commit() throws ReportProcessingException {
147: // ignored ..
148: }
149:
150: public void close(final ReportTarget target)
151: throws ReportProcessingException, DataSourceException {
152: final RecordedCall[] objects = (RecordedCall[]) calls
153: .toArray(new RecordedCall[calls.size()]);
154:
155: for (int i = 0; i < objects.length; i++) {
156: final RecordedCall call = objects[i];
157: switch (call.getMethod()) {
158: case BufferedReportTarget.MTH_START_REPORT: {
159: target.startReport((ReportStructureRoot) call
160: .getParameters());
161: break;
162: }
163: case BufferedReportTarget.MTH_START_ELEMENT: {
164: target
165: .startElement((AttributeMap) call
166: .getParameters());
167: break;
168: }
169: case BufferedReportTarget.MTH_PROCESS_TEXT: {
170: target.processText((String) call.getParameters());
171: break;
172: }
173: case BufferedReportTarget.MTH_PROCESS_CONTENT: {
174: target.processContent((DataFlags) call.getParameters());
175: break;
176: }
177: case BufferedReportTarget.MTH_END_ELEMENT: {
178: target.endElement((AttributeMap) call.getParameters());
179: break;
180: }
181: case BufferedReportTarget.MTH_END_REPORT: {
182: target.endReport((ReportStructureRoot) call
183: .getParameters());
184: break;
185: }
186: default:
187: throw new IllegalStateException(
188: "Invalid call recorded.");
189: }
190: }
191: }
192:
193: public Object clone() {
194: try {
195: final BufferedReportTarget o = (BufferedReportTarget) super
196: .clone();
197: o.calls = (ArrayList) calls.clone();
198: return o;
199: } catch (CloneNotSupportedException e) {
200: throw new IllegalStateException("Clone failed");
201: }
202: }
203: }
|