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: LibLayoutReportTarget.java 3525 2007-10-16 11:43:48Z 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;
031:
032: import java.util.Iterator;
033: import java.util.Map;
034:
035: import org.jfree.layouting.LayoutProcess;
036: import org.jfree.layouting.LayoutProcessState;
037: import org.jfree.layouting.StateException;
038: import org.jfree.layouting.input.style.StyleSheet;
039: import org.jfree.layouting.layouter.context.DocumentContext;
040: import org.jfree.layouting.layouter.feed.InputFeed;
041: import org.jfree.layouting.layouter.feed.InputFeedException;
042: import org.jfree.layouting.namespace.NamespaceCollection;
043: import org.jfree.layouting.namespace.NamespaceDefinition;
044: import org.jfree.layouting.output.OutputProcessor;
045: import org.jfree.layouting.util.AttributeMap;
046: import org.jfree.report.DataFlags;
047: import org.jfree.report.DataSourceException;
048: import org.jfree.report.JFreeReport;
049: import org.jfree.report.JFreeReportInfo;
050: import org.jfree.report.ReportProcessingException;
051: import org.jfree.resourceloader.ResourceKey;
052: import org.jfree.resourceloader.ResourceManager;
053:
054: /**
055: * Creation-Date: 07.03.2006, 18:56:37
056: *
057: * @author Thomas Morgner
058: */
059: public class LibLayoutReportTarget extends AbstractReportTarget
060: implements StatefullReportTarget {
061: protected static class LibLayoutReportTargetState implements
062: ReportTargetState {
063: private LayoutProcessState layoutProcess;
064: private ReportJob reportJob;
065: private ResourceKey baseResourceKey;
066: private ResourceManager resourceManager;
067: private NamespaceCollection namespaceCollection;
068:
069: protected LibLayoutReportTargetState() {
070: }
071:
072: public void fill(final LibLayoutReportTarget target)
073: throws StateException {
074: this .layoutProcess = target.getLayoutProcess().saveState();
075: this .reportJob = target.getReportJob();
076: this .baseResourceKey = target.getBaseResource();
077: this .resourceManager = target.getResourceManager();
078: this .namespaceCollection = target.getNamespaces();
079: }
080:
081: public ReportTarget restore(final OutputProcessor out)
082: throws StateException {
083: final LayoutProcess layoutProcess = this .layoutProcess
084: .restore(out);
085: return new LibLayoutReportTarget(reportJob,
086: baseResourceKey, resourceManager, layoutProcess,
087: namespaceCollection);
088: }
089: }
090:
091: private InputFeed feed;
092: private NamespaceCollection namespaces;
093: private LayoutProcess layoutProcess;
094:
095: /**
096: *
097: * @param reportJob
098: * @param baseResourceKey may be null, if the report has not gone through the parser
099: * @param resourceManager may be null, a generic resource manager will be built
100: * @param layoutProcess
101: */
102: public LibLayoutReportTarget(final ReportJob reportJob,
103: final ResourceKey baseResourceKey,
104: final ResourceManager resourceManager,
105: final LayoutProcess layoutProcess) {
106: super (reportJob, resourceManager, baseResourceKey);
107:
108: if (layoutProcess == null) {
109: throw new NullPointerException();
110: }
111: this .layoutProcess = layoutProcess;
112: this .feed = layoutProcess.getInputFeed();
113: }
114:
115: protected LibLayoutReportTarget(final ReportJob reportJob,
116: final ResourceKey baseResource,
117: final ResourceManager resourceManager,
118: final LayoutProcess layoutProcess,
119: final NamespaceCollection namespaces) {
120: this (reportJob, baseResource, resourceManager, layoutProcess);
121: this .namespaces = namespaces;
122: }
123:
124: public ReportTargetState saveState() throws StateException {
125: final LibLayoutReportTargetState state = new LibLayoutReportTargetState();
126: state.fill(this );
127: return state;
128: }
129:
130: public void commit() {
131:
132: }
133:
134: public NamespaceCollection getNamespaces() {
135: return namespaces;
136: }
137:
138: public boolean isPagebreakEncountered() {
139: return layoutProcess.isPagebreakEncountered();
140: }
141:
142: protected LayoutProcess getLayoutProcess() {
143: return layoutProcess;
144: }
145:
146: protected InputFeed getInputFeed() {
147: return feed;
148: }
149:
150: public void startReport(final ReportStructureRoot report)
151: throws DataSourceException, ReportProcessingException {
152: try {
153: final InputFeed feed = getInputFeed();
154: feed.startDocument();
155: feed.startMetaInfo();
156:
157: feed.addDocumentAttribute(
158: DocumentContext.BASE_RESOURCE_ATTR, report
159: .getBaseResource());
160: feed.addDocumentAttribute(
161: DocumentContext.RESOURCE_MANAGER_ATTR, report
162: .getResourceManager());
163:
164: final String strictStyleMode = "false";
165: if ("true".equals(strictStyleMode)) {
166: feed
167: .addDocumentAttribute(
168: DocumentContext.STRICT_STYLE_MODE,
169: Boolean.TRUE);
170: }
171:
172: final NamespaceDefinition[] namespaces = createDefaultNameSpaces();
173: for (int i = 0; i < namespaces.length; i++) {
174: final NamespaceDefinition definition = namespaces[i];
175: feed.startMetaNode();
176: feed.setMetaNodeAttribute("type", "namespace");
177: feed.setMetaNodeAttribute("definition", definition);
178: feed.endMetaNode();
179: }
180:
181: if (report instanceof JFreeReport) {
182: final JFreeReport realReport = (JFreeReport) report;
183: final int size = realReport.getStyleSheetCount();
184: for (int i = 0; i < size; i++) {
185: final StyleSheet styleSheet = realReport
186: .getStyleSheet(i);
187: feed.startMetaNode();
188: feed.setMetaNodeAttribute("type", "style");
189: feed.setMetaNodeAttribute("#content", styleSheet);
190: feed.endMetaNode();
191: }
192: }
193:
194: feed.endMetaInfo();
195: this .namespaces = feed.getNamespaceCollection();
196: } catch (InputFeedException dse) {
197: dse.printStackTrace();
198: throw new ReportProcessingException(
199: "Failed to process inputfeed", dse);
200: }
201:
202: }
203:
204: public void startElement(final AttributeMap attrs)
205: throws DataSourceException, ReportProcessingException {
206: try {
207: final String namespace = ReportTargetUtil
208: .getNamespaceFromAttribute(attrs);
209: final String type = ReportTargetUtil
210: .getElemenTypeFromAttribute(attrs);
211: final InputFeed feed = getInputFeed();
212: feed.startElement(namespace, type);
213: handleAttributes(attrs);
214: } catch (InputFeedException e) {
215: throw new ReportProcessingException(
216: "Failed to process inputfeed", e);
217: }
218: }
219:
220: public void processText(final String text)
221: throws DataSourceException, ReportProcessingException {
222: try {
223: final InputFeed feed = getInputFeed();
224: feed.addContent(text);
225: } catch (InputFeedException e) {
226: throw new ReportProcessingException(
227: "Failed to process inputfeed", e);
228: }
229: }
230:
231: public void processContent(final DataFlags value)
232: throws DataSourceException, ReportProcessingException {
233: final InputFeed feed = getInputFeed();
234: try {
235: feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE,
236: "content", value.getValue());
237: feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE,
238: "isChanged", String.valueOf(value.isChanged()));
239: feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE,
240: "isDate", String.valueOf(value.isDate()));
241: feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE,
242: "isNegative", String.valueOf(value.isNegative()));
243: feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE,
244: "isNull", String.valueOf(value.isNull()));
245: feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE,
246: "isNumber", String.valueOf(value.isNumeric()));
247: feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE,
248: "isPositive", String.valueOf(value.isPositive()));
249: feed.setAttribute(JFreeReportInfo.REPORT_NAMESPACE,
250: "isZero", String.valueOf(value.isZero()));
251: } catch (InputFeedException e) {
252: throw new ReportProcessingException(
253: "Failed to process inputfeed", e);
254: }
255: }
256:
257: public NamespaceDefinition getNamespaceByUri(final String uri) {
258: if (uri == null) {
259: return null;
260: }
261: return namespaces.getDefinition(uri);
262: }
263:
264: protected void handleAttributes(final AttributeMap map)
265: throws ReportProcessingException {
266: try {
267: final InputFeed feed = getInputFeed();
268: final String[] namespaces = map.getNameSpaces();
269: for (int i = 0; i < namespaces.length; i++) {
270: final String namespace = namespaces[i];
271: final Map localAttrs = map.getAttributes(namespace);
272: final Iterator it = localAttrs.entrySet().iterator();
273: while (it.hasNext()) {
274: final Map.Entry entry = (Map.Entry) it.next();
275: feed.setAttribute(namespace, (String) entry
276: .getKey(), entry.getValue());
277: }
278: }
279: } catch (InputFeedException e) {
280: throw new ReportProcessingException(
281: "Failed to set attribute", e);
282: }
283: }
284:
285: public void endElement(final AttributeMap attrs)
286: throws DataSourceException, ReportProcessingException {
287: final InputFeed feed = getInputFeed();
288: try {
289: feed.endElement();
290: } catch (InputFeedException e) {
291: throw new ReportProcessingException(
292: "Failed to process inputfeed", e);
293: }
294: }
295:
296: public void endReport(final ReportStructureRoot report)
297: throws DataSourceException, ReportProcessingException {
298: try {
299: getInputFeed().endDocument();
300: } catch (InputFeedException e) {
301: throw new ReportProcessingException(
302: "Failed to process inputfeed", e);
303: }
304: }
305:
306: public void resetPagebreakFlag() {
307: getInputFeed().resetPageBreakFlag();
308: }
309:
310: public String getExportDescriptor() {
311: return getLayoutProcess().getOutputMetaData()
312: .getExportDescriptor();
313: }
314: }
|