001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, 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: * CSVPrinter.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.output.table.csv;
030:
031: import java.awt.Shape;
032: import java.io.IOException;
033: import java.io.OutputStream;
034: import java.io.OutputStreamWriter;
035: import java.io.PrintWriter;
036:
037: import org.jfree.fonts.encoding.EncodingRegistry;
038: import org.jfree.report.Anchor;
039: import org.jfree.report.ImageContainer;
040: import org.jfree.report.InvalidReportStateException;
041: import org.jfree.report.layout.model.LogicalPageBox;
042: import org.jfree.report.layout.model.RenderBox;
043: import org.jfree.report.layout.output.ContentProcessingException;
044: import org.jfree.report.layout.output.OutputProcessorMetaData;
045: import org.jfree.report.modules.output.table.base.DefaultTextExtractor;
046: import org.jfree.report.modules.output.table.base.SheetLayout;
047: import org.jfree.report.modules.output.table.base.TableContentProducer;
048: import org.jfree.report.modules.output.table.base.TableRectangle;
049: import org.jfree.report.util.CSVQuoter;
050: import org.jfree.repository.ContentCreationException;
051: import org.jfree.repository.ContentIOException;
052: import org.jfree.repository.ContentItem;
053: import org.jfree.repository.ContentLocation;
054: import org.jfree.repository.NameGenerator;
055:
056: /**
057: * Creation-Date: 09.05.2007, 14:52:05
058: *
059: * @author Thomas Morgner
060: */
061: public class CSVPrinter {
062: private ContentLocation contentLocation;
063: private NameGenerator contentNameGenerator;
064: private String encoding;
065: private ContentItem documentContentItem;
066: private PrintWriter writer;
067:
068: public CSVPrinter() {
069: encoding = EncodingRegistry.getPlatformDefaultEncoding();
070: }
071:
072: public String getEncoding() {
073: return encoding;
074: }
075:
076: public void setEncoding(final String encoding) {
077: if (encoding == null) {
078: throw new NullPointerException();
079: }
080: this .encoding = encoding;
081: }
082:
083: public ContentLocation getContentLocation() {
084: return contentLocation;
085: }
086:
087: public void setContentLocation(final ContentLocation contentLocation) {
088: this .contentLocation = contentLocation;
089: }
090:
091: public NameGenerator getContentNameGenerator() {
092: return contentNameGenerator;
093: }
094:
095: public void setContentNameGenerator(
096: final NameGenerator contentNameGenerator) {
097: this .contentNameGenerator = contentNameGenerator;
098: }
099:
100: public void print(final LogicalPageBox logicalPage,
101: final TableContentProducer contentProducer,
102: final OutputProcessorMetaData metaData,
103: final boolean incremental)
104: throws ContentProcessingException {
105: try {
106: final String separator = metaData.getConfiguration()
107: .getConfigProperty(
108: CSVTableModule.CONFIGURATION_PREFIX + '.'
109: + CSVTableModule.SEPARATOR_KEY,
110: CSVTableModule.SEPARATOR_DEFAULT);
111:
112: if (separator.length() == 0) {
113: throw new IllegalArgumentException(
114: "CSV separate cannot be an empty string.");
115: }
116:
117: if (documentContentItem == null) {
118: documentContentItem = contentLocation
119: .createItem(contentNameGenerator.generateName(
120: "content", "text/csv"));
121: final OutputStream out = documentContentItem
122: .getOutputStream();
123: writer = new PrintWriter(new OutputStreamWriter(out,
124: encoding));
125: }
126:
127: final CSVQuoter quoter = new CSVQuoter(separator.charAt(0));
128: final SheetLayout sheetLayout = contentProducer
129: .getSheetLayout();
130: final int columnCount = contentProducer.getColumnCount();
131: final int lastColumn = columnCount - 1;
132:
133: final int startRow = contentProducer.getFinishedRows();
134: final int finishRow = contentProducer.getFilledRows();
135:
136: for (int row = startRow; row < finishRow; row++) {
137: for (short col = 0; col < columnCount; col++) {
138: final RenderBox content = contentProducer
139: .getContent(row, col);
140: if (content == null) {
141: writer.print(quoter.getSeparator());
142: continue;
143: }
144:
145: if (content.isCommited() == false) {
146: throw new InvalidReportStateException(
147: "Uncommited content encountered");
148: }
149:
150: final TableRectangle rectangle = sheetLayout
151: .getTableBounds(content.getX(), content
152: .getY()
153: + contentProducer.getContentOffset(
154: row, col), content
155: .getWidth(), content.getHeight(),
156: null);
157: if (rectangle.isOrigin(col, row) == false) {
158: // A spanned cell ..
159: writer.print(quoter.getSeparator());
160: continue;
161: }
162:
163: final DefaultTextExtractor textExtractor = new DefaultTextExtractor(
164: metaData);
165: final Object o = textExtractor.compute(content);
166: if (o instanceof Shape == false
167: && o instanceof ImageContainer == false
168: && o instanceof Anchor == false) {
169: writer.print(quoter
170: .doQuoting(String.valueOf(o)));
171: }
172: if (col < lastColumn) {
173: writer.print(quoter.getSeparator());
174: }
175: content.setFinished(true);
176: }
177:
178: writer.println();
179:
180: }
181: if (incremental == false) {
182: // cleanup ..
183: writer.flush();
184: writer.close();
185:
186: writer = null;
187: documentContentItem = null;
188: }
189: } catch (IOException e) {
190: writer = null;
191: documentContentItem = null;
192:
193: throw new ContentProcessingException(
194: "Failed to write content", e);
195: } catch (ContentCreationException e) {
196: writer = null;
197: documentContentItem = null;
198:
199: throw new ContentProcessingException(
200: "Failed to write content", e);
201: } catch (ContentIOException e) {
202: writer = null;
203: documentContentItem = null;
204:
205: throw new ContentProcessingException(
206: "Failed to write content", e);
207: }
208:
209: }
210:
211: public void close() {
212:
213: }
214: }
|