001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.platform.csv;
028:
029: import org.databene.model.consumer.AbstractConsumer;
030: import org.databene.model.data.ComponentDescriptor;
031: import org.databene.model.data.Entity;
032: import org.databene.model.data.EntityDescriptor;
033: import org.databene.commons.BeanUtil;
034: import org.databene.commons.CollectionUtil;
035: import org.databene.commons.StringUtil;
036: import org.databene.commons.ArrayFormat;
037: import org.databene.commons.SystemInfo;
038: import org.databene.commons.IOUtil;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: import java.io.*;
043: import java.util.Collection;
044: import java.util.List;
045:
046: /**
047: * Exports Entities to a CSV file.<br/>
048: * <br/>
049: * Created: 21.08.2007 21:16:59
050: * @author Volker Bergmann
051: */
052: public class CSVEntityExporter extends AbstractConsumer<Entity> {
053:
054: private static final Log logger = LogFactory
055: .getLog(CSVEntityExporter.class);
056:
057: // defaults --------------------------------------------------------------------------------------------------------
058:
059: private static final char DEFAULT_SEPARATOR = ',';
060: private static final String DEFAULT_ENCODING = SystemInfo
061: .fileEncoding();
062: private static final String DEFAULT_URI = "export.csv";
063:
064: // attributes ------------------------------------------------------------------------------------------------------
065:
066: private String uri;
067: private String[] propertyNames;
068: private String encoding;
069: private char separator;
070:
071: private PrintWriter printer;
072:
073: // constructors ----------------------------------------------------------------------------------------------------
074:
075: public CSVEntityExporter() {
076: this (DEFAULT_URI, "");
077: }
078:
079: public CSVEntityExporter(String uri, String attributes) {
080: this (uri, attributes, DEFAULT_SEPARATOR, DEFAULT_ENCODING);
081: }
082:
083: public CSVEntityExporter(String uri, String attributes,
084: char separator, String encoding) {
085: this .uri = uri;
086: setProperties(attributes);
087: this .separator = separator;
088: this .encoding = encoding;
089: }
090:
091: public CSVEntityExporter(EntityDescriptor descriptor) {
092: this (descriptor.getName() + ".csv", descriptor);
093: }
094:
095: public CSVEntityExporter(String uri, EntityDescriptor descriptor) {
096: this (uri, descriptor, DEFAULT_SEPARATOR, DEFAULT_ENCODING);
097: }
098:
099: public CSVEntityExporter(String uri, EntityDescriptor descriptor,
100: char separator, String encoding) {
101: this .uri = uri;
102: Collection<ComponentDescriptor> componentDescriptors = descriptor
103: .getComponentDescriptors();
104: List<String> componentNames = BeanUtil.extractProperties(
105: componentDescriptors, "name");
106: this .propertyNames = CollectionUtil.toArray(componentNames,
107: String.class);
108: this .separator = separator;
109: this .encoding = encoding;
110: }
111:
112: // properties ------------------------------------------------------------------------------------------------------
113:
114: public String getUri() {
115: return uri;
116: }
117:
118: public void setUri(String uri) {
119: this .uri = uri;
120: }
121:
122: public void setProperties(String attributes) {
123: this .propertyNames = StringUtil.tokenize(attributes, ',');
124: StringUtil.trimAll(propertyNames);
125: }
126:
127: public void setSeparator(char separator) {
128: this .separator = separator;
129: }
130:
131: public void setEncoding(String encoding) {
132: this .encoding = encoding;
133: }
134:
135: // Consumer interface ----------------------------------------------------------------------------------------------
136:
137: public void startConsuming(Entity entity) {
138: try {
139: if (logger.isDebugEnabled())
140: logger.debug("exporting " + entity);
141: if (printer == null)
142: initPrinter();
143: for (int i = 0; i < propertyNames.length; i++) {
144: if (i > 0)
145: printer.print(separator);
146: Object value = entity.getComponent(propertyNames[i]);
147: String s = String.valueOf(value);
148: if (s.indexOf(separator) >= 0)
149: s = '"' + s + '"';
150: printer.print(s);
151: }
152: printer.println();
153: } catch (IOException e) {
154: throw new RuntimeException(e);
155: }
156: }
157:
158: public void flush() {
159: if (printer != null)
160: printer.flush();
161: }
162:
163: public void close() {
164: if (printer != null)
165: printer.close();
166: }
167:
168: // private helpers -------------------------------------------------------------------------------------------------
169:
170: private void initPrinter() throws IOException {
171: // create file
172: printer = IOUtil.getPrinterForURI(uri, encoding);
173: // write header
174: for (int i = 0; i < propertyNames.length; i++) {
175: if (i > 0)
176: printer.print(separator);
177: printer.print(propertyNames[i]);
178: }
179: printer.println();
180: }
181:
182: // java.lang.Object overrides --------------------------------------------------------------------------------------
183:
184: public String toString() {
185: return getClass().getSimpleName() + '('
186: + ArrayFormat.format(propertyNames) + ") -> " + uri;
187: }
188:
189: }
|