1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.util; |
16 |
| |
17 |
| import java.util.Locale; |
18 |
| import java.util.NoSuchElementException; |
19 |
| |
20 |
| import org.apache.hivemind.HiveMind; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class LocalizedNameGenerator |
31 |
| { |
32 |
| private int _baseNameLength; |
33 |
| private String _suffix; |
34 |
| private StringBuffer _buffer; |
35 |
| private String _language; |
36 |
| private String _country; |
37 |
| private String _variant; |
38 |
| private int _state; |
39 |
| private int _prevState; |
40 |
| |
41 |
| private static final int INITIAL = 0; |
42 |
| private static final int LCV = 1; |
43 |
| private static final int LC = 2; |
44 |
| private static final int LV = 3; |
45 |
| private static final int L = 4; |
46 |
| private static final int BARE = 5; |
47 |
| private static final int EXHAUSTED = 6; |
48 |
| |
49 |
34
| public LocalizedNameGenerator(String baseName, Locale locale, String suffix)
|
50 |
| { |
51 |
34
| _baseNameLength = baseName.length();
|
52 |
| |
53 |
34
| if (locale != null)
|
54 |
| { |
55 |
29
| _language = locale.getLanguage();
|
56 |
29
| _country = locale.getCountry();
|
57 |
29
| _variant = locale.getVariant();
|
58 |
| } |
59 |
| |
60 |
34
| _state = INITIAL;
|
61 |
34
| _prevState = INITIAL;
|
62 |
| |
63 |
34
| _suffix = suffix;
|
64 |
| |
65 |
34
| _buffer = new StringBuffer(baseName);
|
66 |
| |
67 |
34
| advance();
|
68 |
| } |
69 |
| |
70 |
102
| private void advance()
|
71 |
| { |
72 |
102
| _prevState = _state;
|
73 |
| |
74 |
196
| while (_state != EXHAUSTED)
|
75 |
| { |
76 |
196
| _state++;
|
77 |
| |
78 |
196
| switch (_state)
|
79 |
| { |
80 |
34
| case LCV :
|
81 |
| |
82 |
34
| if (HiveMind.isBlank(_variant))
|
83 |
33
| continue;
|
84 |
| |
85 |
1
| return;
|
86 |
| |
87 |
34
| case LC :
|
88 |
| |
89 |
34
| if (HiveMind.isBlank(_country))
|
90 |
22
| continue;
|
91 |
| |
92 |
12
| return;
|
93 |
| |
94 |
34
| case LV :
|
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
34
| if (HiveMind.isBlank(_variant) || HiveMind.isBlank(_country))
|
100 |
34
| continue;
|
101 |
| |
102 |
0
| return;
|
103 |
| |
104 |
34
| case L :
|
105 |
| |
106 |
34
| if (HiveMind.isBlank(_language))
|
107 |
5
| continue;
|
108 |
| |
109 |
29
| return;
|
110 |
| |
111 |
34
| case BARE :
|
112 |
26
| default :
|
113 |
60
| return;
|
114 |
| } |
115 |
| } |
116 |
| } |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
86
| public boolean more()
|
125 |
| { |
126 |
86
| return _state != EXHAUSTED;
|
127 |
| } |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
69
| public String next()
|
138 |
| { |
139 |
69
| if (_state == EXHAUSTED)
|
140 |
1
| throw new NoSuchElementException();
|
141 |
| |
142 |
68
| String result = build();
|
143 |
| |
144 |
68
| advance();
|
145 |
| |
146 |
68
| return result;
|
147 |
| } |
148 |
| |
149 |
68
| private String build()
|
150 |
| { |
151 |
68
| _buffer.setLength(_baseNameLength);
|
152 |
| |
153 |
68
| if (_state == LC || _state == LCV || _state == L)
|
154 |
| { |
155 |
42
| _buffer.append('_');
|
156 |
42
| _buffer.append(_language);
|
157 |
| } |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
68
| if (_state == LC || _state == LCV || _state == LV)
|
163 |
| { |
164 |
13
| _buffer.append('_');
|
165 |
| |
166 |
13
| if (_state != LV)
|
167 |
13
| _buffer.append(_country);
|
168 |
| } |
169 |
| |
170 |
68
| if (_state == LV || _state == LCV)
|
171 |
| { |
172 |
1
| _buffer.append('_');
|
173 |
1
| _buffer.append(_variant);
|
174 |
| } |
175 |
| |
176 |
68
| if (_suffix != null)
|
177 |
67
| _buffer.append(_suffix);
|
178 |
| |
179 |
68
| return _buffer.toString();
|
180 |
| } |
181 |
| |
182 |
29
| public Locale getCurrentLocale()
|
183 |
| { |
184 |
29
| switch (_prevState)
|
185 |
| { |
186 |
0
| case LCV :
|
187 |
| |
188 |
0
| return new Locale(_language, _country, _variant);
|
189 |
| |
190 |
5
| case LC :
|
191 |
| |
192 |
5
| return new Locale(_language, _country, "");
|
193 |
| |
194 |
0
| case LV :
|
195 |
| |
196 |
0
| return new Locale(_language, "", _variant);
|
197 |
| |
198 |
12
| case L :
|
199 |
| |
200 |
12
| return new Locale(_language, "", "");
|
201 |
| |
202 |
12
| default :
|
203 |
12
| return null;
|
204 |
| } |
205 |
| } |
206 |
| } |