1:
31:
32: package ;
33:
34: import ;
35:
36: import ;
37: import ;
38: import ;
39: import ;
40: import ;
41: import ;
42: import ;
43:
44:
50: public class BufferedReportTarget implements ReportTarget, Cloneable
51: {
52: public static class RecordedCall
53: {
54: private int methodId;
55: private Object parameters;
56:
57: public RecordedCall(final int method, final Object parameters)
58: {
59: this.methodId = method;
60: this.parameters = parameters;
61: }
62:
63: public int getMethod()
64: {
65: return methodId;
66: }
67:
68: public Object getParameters()
69: {
70: return parameters;
71: }
72: }
73:
74: private static final int MTH_START_REPORT = 1;
75: private static final int MTH_START_ELEMENT = 2;
76: private static final int MTH_PROCESS_TEXT = 3;
77: private static final int MTH_PROCESS_CONTENT = 4;
78: private static final int MTH_END_ELEMENT = 5;
79: private static final int MTH_END_REPORT = 6;
80:
81: private ReportTarget target;
82: private ArrayList calls;
83:
84: public BufferedReportTarget()
85: {
86: this.calls = new ArrayList();
87: }
88:
89: public ReportTarget getTarget()
90: {
91: return target;
92: }
93:
94: public void setTarget(final ReportTarget target)
95: {
96: this.target = target;
97: }
98:
99: public void startReport(final ReportStructureRoot report)
100: throws DataSourceException, ReportProcessingException
101: {
102: calls.add(new RecordedCall
103: (BufferedReportTarget.MTH_START_REPORT, report));
104: }
105:
106: public void startElement(final AttributeMap attrs)
107: throws DataSourceException, ReportProcessingException
108: {
109: try
110: {
111: calls.add(new RecordedCall
112: (BufferedReportTarget.MTH_START_ELEMENT, attrs.clone()));
113: }
114: catch (CloneNotSupportedException e)
115: {
116: throw new ReportProcessingException("Failed to clone attributes", e);
117: }
118: }
119:
120: public void processText(final String text)
121: throws DataSourceException, ReportProcessingException
122: {
123: calls.add(new RecordedCall
124: (BufferedReportTarget.MTH_PROCESS_TEXT, text));
125: }
126:
127: public void processContent(final DataFlags value)
128: throws DataSourceException, ReportProcessingException
129: {
130: calls.add(new RecordedCall
131: (BufferedReportTarget.MTH_PROCESS_CONTENT, value));
132: }
133:
134: public void endElement(final AttributeMap attrs)
135: throws DataSourceException, ReportProcessingException
136: {
137: try
138: {
139: calls.add(new RecordedCall
140: (BufferedReportTarget.MTH_END_ELEMENT, attrs.clone()));
141: }
142: catch (CloneNotSupportedException e)
143: {
144: throw new ReportProcessingException("Failed to clone attributes", e);
145: }
146: }
147:
148: public void endReport(final ReportStructureRoot report)
149: throws DataSourceException, ReportProcessingException
150: {
151: calls.add(new RecordedCall
152: (BufferedReportTarget.MTH_END_REPORT, report));
153: }
154:
155: public String getExportDescriptor()
156: {
157: return target.getExportDescriptor();
158: }
159:
160: public NamespaceDefinition getNamespaceByUri(final String uri)
161: {
162: return target.getNamespaceByUri(uri);
163: }
164:
165: public void commit()
166: throws ReportProcessingException
167: {
168:
169: }
170:
171: public void close(final ReportTarget target)
172: throws ReportProcessingException, DataSourceException
173: {
174: final RecordedCall[] objects = (RecordedCall[])
175: calls.toArray(new RecordedCall[calls.size()]);
176:
177: for (int i = 0; i < objects.length; i++)
178: {
179: final RecordedCall call = objects[i];
180: switch(call.getMethod())
181: {
182: case BufferedReportTarget.MTH_START_REPORT:
183: {
184: target.startReport((ReportStructureRoot) call.getParameters());
185: break;
186: }
187: case BufferedReportTarget.MTH_START_ELEMENT:
188: {
189: target.startElement((AttributeMap) call.getParameters());
190: break;
191: }
192: case BufferedReportTarget.MTH_PROCESS_TEXT:
193: {
194: target.processText((String) call.getParameters());
195: break;
196: }
197: case BufferedReportTarget.MTH_PROCESS_CONTENT:
198: {
199: target.processContent((DataFlags) call.getParameters());
200: break;
201: }
202: case BufferedReportTarget.MTH_END_ELEMENT:
203: {
204: target.endElement((AttributeMap) call.getParameters());
205: break;
206: }
207: case BufferedReportTarget.MTH_END_REPORT:
208: {
209: target.endReport((ReportStructureRoot) call.getParameters());
210: break;
211: }
212: default:
213: throw new IllegalStateException("Invalid call recorded.");
214: }
215: }
216: }
217:
218: public Object clone ()
219: {
220: try
221: {
222: final BufferedReportTarget o = (BufferedReportTarget) super.clone();
223: o.calls = (ArrayList) calls.clone();
224: return o;
225: }
226: catch (CloneNotSupportedException e)
227: {
228: throw new IllegalStateException("Clone failed");
229: }
230: }
231: }