View Javadoc
1   /*
2    * Copyright (C) 2010-2014 Hamburg Sud and the contributors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.aludratest.service.flatfile.impl;
17  
18  import java.text.ParseException;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Locale;
22  
23  import org.aludratest.config.Preferences;
24  import org.aludratest.exception.AutomationException;
25  import org.aludratest.service.flatfile.FlatFileService;
26  import org.databene.commons.StringUtil;
27  import org.databene.formats.fixedwidth.FixedWidthRowTypeDescriptor;
28  import org.databene.formats.fixedwidth.FixedWidthUtil;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Reads the configuration for a {@link FlatFileService} instance.
34   * @author Volker Bergmann
35   */
36  public final class FlatFileConfig {
37  
38      /** The {@link Locale} to apply if none has been configured explicitly. */
39      private static final Locale DEFAULT_LOCALE = Locale.US;
40  
41      /** Logger of the {@link FlatFileConfig}. */
42      private static final Logger LOGGER = LoggerFactory.getLogger(FlatFileConfig.class);
43  
44      /** Creates a new FlatFileConfig instance which uses the given configuration object.
45       * 
46       * @param configuration The configuration object containing required configuration. */
47      public FlatFileConfig(Preferences configuration) {
48  
49          // parse locale
50          String localeName = configuration.getStringValue("locale");
51          if (StringUtil.isEmpty(localeName)) {
52              this.locale = DEFAULT_LOCALE;
53              LOGGER.debug("'locale' not set in configuration, defaulting to " + this.locale);
54          }
55          else {
56              this.locale = new Locale(localeName);
57          }
58  
59          // parse formats
60          try {
61              this.rowFormats = new ArrayList<FixedWidthRowTypeDescriptor>();
62              for (String key : configuration.getKeyNames()) {
63                  if (key.startsWith("beanformats.")) {
64                      String spec = configuration.getStringValue(key);
65                      String rowType = StringUtil.splitOnFirstSeparator(key, '.')[1];
66                      FixedWidthRowTypeDescriptor rowDescriptor = FixedWidthUtil.parseBeanColumnsSpec(spec, rowType, "", locale);
67                      rowFormats.add(rowDescriptor);
68                  }
69                  else if (key.startsWith("arrayformats.")) {
70                      String spec = configuration.getStringValue(key);
71                      String rowType = StringUtil.splitOnFirstSeparator(key, '.')[1];
72                      FixedWidthRowTypeDescriptor descriptor = FixedWidthUtil.parseArrayColumnsSpec(spec, rowType, "", locale);
73                      rowFormats.add(descriptor);
74                  }
75              }
76          }
77          catch (ParseException pe) {
78              throw new AutomationException("Invalid flatfile configuration", pe);
79          }
80      }
81  
82      /** The locale to use in formatting, defaults to 'en_US' */
83      private Locale locale;
84  
85      /** Maps each row type/bean class to an array of column format descriptors. */
86      private List<FixedWidthRowTypeDescriptor> rowFormats;
87  
88      // interface ---------------------------------------------------------------
89  
90      /** Returns the {@link #locale}
91       *  @return the locale to use */
92      public Locale getLocale() {
93          return locale;
94      }
95  
96      /** returns the {@link #rowFormats}.
97       *  @return the row formats */
98      public List<FixedWidthRowTypeDescriptor> getRowFormats() {
99          return rowFormats;
100     }
101 
102 }