|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
LocalizedNameGenerator.java | 87.5% | 94.3% | 100% | 92.8% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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 |
* Used in a wide variety of resource searches. Generates
|
|
24 |
* a series of name variations from a base name, a
|
|
25 |
* {@link java.util.Locale} and an optional suffix.
|
|
26 |
*
|
|
27 |
* @author Howard Lewis Ship
|
|
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 | 30 |
public LocalizedNameGenerator(String baseName, Locale locale, String suffix)
|
50 |
{ |
|
51 | 30 |
_baseNameLength = baseName.length(); |
52 |
|
|
53 | 30 |
if (locale != null) |
54 |
{ |
|
55 | 26 |
_language = locale.getLanguage(); |
56 | 26 |
_country = locale.getCountry(); |
57 | 26 |
_variant = locale.getVariant(); |
58 |
} |
|
59 |
|
|
60 | 30 |
_state = INITIAL; |
61 | 30 |
_prevState = INITIAL; |
62 |
|
|
63 | 30 |
_suffix = suffix; |
64 |
|
|
65 | 30 |
_buffer = new StringBuffer(baseName);
|
66 |
|
|
67 | 30 |
advance(); |
68 |
} |
|
69 |
|
|
70 | 90 |
private void advance() |
71 |
{ |
|
72 | 90 |
_prevState = _state; |
73 |
|
|
74 | 173 |
while (_state != EXHAUSTED)
|
75 |
{ |
|
76 | 173 |
_state++; |
77 |
|
|
78 | 173 |
switch (_state)
|
79 |
{ |
|
80 |
case LCV :
|
|
81 |
|
|
82 | 30 |
if (HiveMind.isBlank(_variant))
|
83 | 29 |
continue;
|
84 |
|
|
85 | 1 |
return;
|
86 |
|
|
87 |
case LC :
|
|
88 |
|
|
89 | 30 |
if (HiveMind.isBlank(_country))
|
90 | 20 |
continue;
|
91 |
|
|
92 | 10 |
return;
|
93 |
|
|
94 |
case LV :
|
|
95 |
|
|
96 |
// If _country is null, then we've already generated this string
|
|
97 |
// as state LCV and we can continue directly to state L
|
|
98 |
|
|
99 | 30 |
if (HiveMind.isBlank(_variant) || HiveMind.isBlank(_country))
|
100 | 30 |
continue;
|
101 |
|
|
102 | 0 |
return;
|
103 |
|
|
104 |
case L :
|
|
105 |
|
|
106 | 30 |
if (HiveMind.isBlank(_language))
|
107 | 4 |
continue;
|
108 |
|
|
109 | 26 |
return;
|
110 |
|
|
111 |
case BARE :
|
|
112 |
default :
|
|
113 | 53 |
return;
|
114 |
} |
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
/**
|
|
119 |
* Returns true if there are more name variants to be
|
|
120 |
* returned, false otherwise.
|
|
121 |
*
|
|
122 |
**/
|
|
123 |
|
|
124 | 77 |
public boolean more() |
125 |
{ |
|
126 | 77 |
return _state != EXHAUSTED;
|
127 |
} |
|
128 |
|
|
129 |
/**
|
|
130 |
* Returns the next localized variant.
|
|
131 |
*
|
|
132 |
* @throws NoSuchElementException if all variants have been
|
|
133 |
* returned.
|
|
134 |
*
|
|
135 |
**/
|
|
136 |
|
|
137 | 61 |
public String next()
|
138 |
{ |
|
139 | 61 |
if (_state == EXHAUSTED)
|
140 | 1 |
throw new NoSuchElementException(); |
141 |
|
|
142 | 60 |
String result = build(); |
143 |
|
|
144 | 60 |
advance(); |
145 |
|
|
146 | 60 |
return result;
|
147 |
} |
|
148 |
|
|
149 | 60 |
private String build()
|
150 |
{ |
|
151 | 60 |
_buffer.setLength(_baseNameLength); |
152 |
|
|
153 | 60 |
if (_state == LC || _state == LCV || _state == L)
|
154 |
{ |
|
155 | 37 |
_buffer.append('_'); |
156 | 37 |
_buffer.append(_language); |
157 |
} |
|
158 |
|
|
159 |
// For LV, we want two underscores between language
|
|
160 |
// and variant.
|
|
161 |
|
|
162 | 60 |
if (_state == LC || _state == LCV || _state == LV)
|
163 |
{ |
|
164 | 11 |
_buffer.append('_'); |
165 |
|
|
166 | 11 |
if (_state != LV)
|
167 | 11 |
_buffer.append(_country); |
168 |
} |
|
169 |
|
|
170 | 60 |
if (_state == LV || _state == LCV)
|
171 |
{ |
|
172 | 1 |
_buffer.append('_'); |
173 | 1 |
_buffer.append(_variant); |
174 |
} |
|
175 |
|
|
176 | 60 |
if (_suffix != null) |
177 | 59 |
_buffer.append(_suffix); |
178 |
|
|
179 | 60 |
return _buffer.toString();
|
180 |
} |
|
181 |
|
|
182 | 24 |
public Locale getCurrentLocale()
|
183 |
{ |
|
184 | 24 |
switch (_prevState)
|
185 |
{ |
|
186 |
case LCV :
|
|
187 |
|
|
188 | 0 |
return new Locale(_language, _country, _variant); |
189 |
|
|
190 |
case LC :
|
|
191 |
|
|
192 | 4 |
return new Locale(_language, _country, ""); |
193 |
|
|
194 |
case LV :
|
|
195 |
|
|
196 | 0 |
return new Locale(_language, "", _variant); |
197 |
|
|
198 |
case L :
|
|
199 |
|
|
200 | 10 |
return new Locale(_language, "", ""); |
201 |
|
|
202 |
default :
|
|
203 | 10 |
return null; |
204 |
} |
|
205 |
} |
|
206 |
} |
|
207 |
|
|