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.platform.array.Array2EntityConverter;
030: import org.databene.model.data.EntityIterable;
031: import org.databene.model.data.Entity;
032: import org.databene.model.data.EntityDescriptor;
033: import org.databene.document.csv.CSVLineIterable;
034: import org.databene.commons.Converter;
035: import org.databene.commons.SystemInfo;
036: import org.databene.commons.converter.ArrayConverter;
037: import org.databene.commons.converter.ConverterChain;
038: import org.databene.commons.converter.NoOpConverter;
039: import org.databene.commons.iterator.ConvertingIterator;
040:
041: import java.util.Iterator;
042:
043: /**
044: * Imports Entites from a CSV file.<br/>
045: * <br/>
046: * Created: 26.08.2007 12:16:08
047: * @author Volker Bergmann
048: */
049: public class CSVEntityIterable implements EntityIterable {
050:
051: private String uri;
052: private char separator;
053: private String encoding;
054: private Converter<String, String> preprocessor;
055:
056: private Iterable<String[]> source;
057: private EntityDescriptor entityDescriptor;
058:
059: // constructors ----------------------------------------------------------------------------------------------------
060:
061: public CSVEntityIterable() {
062: this (null, null);
063: }
064:
065: public CSVEntityIterable(String uri, String entityName) {
066: this (uri, entityName, ',', SystemInfo.fileEncoding());
067: }
068:
069: public CSVEntityIterable(String uri, String entityName,
070: char separator) {
071: this (uri, entityName, separator, SystemInfo.fileEncoding());
072: }
073:
074: public CSVEntityIterable(String uri, String entityName,
075: char separator, String encoding) {
076: this (uri, new EntityDescriptor(entityName, false),
077: new NoOpConverter<String>(), separator, encoding); // TODO v0.5 finalize capitalization concept
078: }
079:
080: public CSVEntityIterable(String uri, String entityName,
081: Converter<String, String> preprocessor, char separator,
082: String encoding) {
083: this (uri, new EntityDescriptor(entityName, false),
084: preprocessor, separator, encoding); // TODO v0.5 finalize capitalization concept
085: }
086:
087: public CSVEntityIterable(String uri, EntityDescriptor descriptor,
088: Converter<String, String> preprocessor, char separator,
089: String encoding) {
090: this .uri = uri;
091: this .separator = separator;
092: this .encoding = encoding;
093: this .entityDescriptor = descriptor;
094: this .preprocessor = preprocessor;
095: }
096:
097: // properties ------------------------------------------------------------------------------------------------------
098:
099: public String getUri() {
100: return uri;
101: }
102:
103: public void setUri(String uri) {
104: this .uri = uri;
105: }
106:
107: public char getSeparator() {
108: return separator;
109: }
110:
111: public void setSeparator(char separator) {
112: this .separator = separator;
113: }
114:
115: public String getEncoding() {
116: return encoding;
117: }
118:
119: public void setEncoding(String encoding) {
120: this .encoding = encoding;
121: }
122:
123: public String getEntityName() {
124: return entityDescriptor.getName();
125: }
126:
127: public void setEntityName(String entityName) {
128: this .entityDescriptor = new EntityDescriptor(entityName, false); // TODO v0.5 finalize case concept
129: }
130:
131: // EntityIterable interface ----------------------------------------------------------------------------------------
132:
133: public Class<Entity> getType() {
134: return Entity.class;
135: }
136:
137: public Iterator<Entity> iterator() {
138: if (source == null)
139: init();
140: Iterator<String[]> arrayIterator = source.iterator();
141: String[] featureNames = arrayIterator.next();
142: Converter<String[], String[]> arrayConverter = new ArrayConverter<String, String>(
143: String.class, preprocessor);
144: Array2EntityConverter<String> a2eConverter = new Array2EntityConverter<String>(
145: entityDescriptor, featureNames);
146: Converter<String[], Entity> converter = new ConverterChain<String[], Entity>(
147: arrayConverter, a2eConverter);
148: return new ConvertingIterator<String[], Entity>(arrayIterator,
149: converter);
150: }
151:
152: // java.lang.Object overrides --------------------------------------------------------------------------------------
153:
154: public String toString() {
155: return getClass().getSimpleName() + "[uri=" + uri
156: + ", encoding=" + encoding + ", separator=" + separator
157: + ", entityName=" + entityDescriptor.getName() + "]";
158: }
159:
160: // private helpers -------------------------------------------------------------------------------------------------
161:
162: private void init() {
163: this .source = new CSVLineIterable(uri, separator, true,
164: encoding);
165: }
166: }
|