001: /*
002: * (c) Copyright 2006 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.benerator.csv;
028:
029: import org.databene.region.RegionUtil;
030: import org.databene.benerator.sample.WeightedCSVSampleGenerator;
031: import org.databene.commons.ConfigurationError;
032: import org.databene.commons.Converter;
033: import org.databene.commons.converter.NoOpConverter;
034:
035: import java.util.Locale;
036:
037: /**
038: * Generates data from a localized csv file.
039: * For different locales, different CSV versions may be provided by appending region suffixes,
040: * similar to the JDK ResourceBundle handling.<br/>
041: * <br/>
042: * Created: 07.06.2007 17:21:24
043: * @author Volker Bergmann
044: */
045: public class LocalCSVGenerator<E> extends WeightedCSVSampleGenerator<E> {
046:
047: private String baseName;
048: private Locale locale;
049: private String suffix;
050:
051: // constructors ----------------------------------------------------------------------------------------------------
052:
053: public LocalCSVGenerator() {
054: this (null, null);
055: }
056:
057: public LocalCSVGenerator(String baseName, String suffix) {
058: this (baseName, Locale.getDefault(), suffix);
059: }
060:
061: public LocalCSVGenerator(String baseName, Locale locale,
062: String suffix) {
063: this (baseName, locale, suffix, NoOpConverter.getInstance());
064: }
065:
066: public LocalCSVGenerator(String baseName, Locale locale,
067: String suffix, Converter<String, E> converter) {
068: super (availableUri(baseName, locale, suffix), converter);
069: this .baseName = baseName;
070: this .locale = locale;
071: this .suffix = suffix;
072: }
073:
074: // properties ------------------------------------------------------------------------------------------------------
075:
076: public Locale getLocale() {
077: return locale;
078: }
079:
080: public void setLocale(Locale locale) {
081: super .setUrl(availableUri(baseName, locale, suffix));
082: this .locale = locale;
083: }
084:
085: // private helpers -------------------------------------------------------------------------------------------------
086:
087: private static String availableUri(String baseName, Locale locale,
088: String suffix) {
089: if (baseName == null || suffix == null)
090: return null;
091: String uri = RegionUtil.availableLocaleUrl(baseName, locale,
092: suffix);
093: if (uri == null)
094: throw new ConfigurationError("No localization found for "
095: + baseName + suffix);
096: return uri;
097: }
098:
099: // java.lang.Object overrides --------------------------------------------------------------------------------------
100:
101: public String toString() {
102: return getClass().getSimpleName() + '[' + baseName + ','
103: + locale + ',' + suffix + ']';
104: }
105: }
|