1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36 public final class FlatFileConfig {
37
38
39 private static final Locale DEFAULT_LOCALE = Locale.US;
40
41
42 private static final Logger LOGGER = LoggerFactory.getLogger(FlatFileConfig.class);
43
44
45
46
47 public FlatFileConfig(Preferences configuration) {
48
49
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
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
83 private Locale locale;
84
85
86 private List<FixedWidthRowTypeDescriptor> rowFormats;
87
88
89
90
91
92 public Locale getLocale() {
93 return locale;
94 }
95
96
97
98 public List<FixedWidthRowTypeDescriptor> getRowFormats() {
99 return rowFormats;
100 }
101
102 }