gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / namestranslator / TrimNamesTranslator.java @ 2192
History | View | Annotate | Download (8.67 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.namestranslator; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Collections; |
28 |
import java.util.HashMap; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
import javax.json.Json; |
32 |
import javax.json.JsonArrayBuilder; |
33 |
import javax.json.JsonObject; |
34 |
import javax.json.JsonObjectBuilder; |
35 |
import org.apache.commons.lang3.StringUtils; |
36 |
|
37 |
/**
|
38 |
*
|
39 |
* @author jjdelcerro
|
40 |
*/
|
41 |
public class TrimNamesTranslator |
42 |
extends AbstractNamesTranslator
|
43 |
implements NamesTranslator
|
44 |
{ |
45 |
|
46 |
protected List<String> translatedNames; |
47 |
protected Map<String,String> source2translation; |
48 |
protected Map<String,String> translation2source; |
49 |
protected boolean hasTranslations; |
50 |
protected int maxNameLen; |
51 |
|
52 |
protected TrimNamesTranslator(int maxNameLen) { |
53 |
if( maxNameLen<8 ) { |
54 |
throw new IllegalArgumentException("Invalid max name len, can be greater than 7."); |
55 |
} |
56 |
this.maxNameLen = maxNameLen;
|
57 |
} |
58 |
|
59 |
@Override
|
60 |
protected void build() { |
61 |
this.translatedNames = new ArrayList<>(); |
62 |
this.hasTranslations = false; |
63 |
for (String sourceName : this.sourceNames) { |
64 |
String translatedName = this.getSuggestion(sourceName); |
65 |
this.translatedNames.add(translatedName);
|
66 |
if( !sourceName.equals(translatedName) ) {
|
67 |
this.hasTranslations = true; |
68 |
} |
69 |
} |
70 |
this.source2translation = null; |
71 |
this.translation2source = null; |
72 |
this.sourceNames = Collections.unmodifiableList(this.sourceNames); |
73 |
} |
74 |
|
75 |
@Override
|
76 |
public List<String> getSourceNames() { |
77 |
return this.sourceNames; |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public List<String> getTranslatedNames() { |
82 |
return this.translatedNames; |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public String getTranslation(String sourceName) { |
87 |
if( this.hasTranslations ) { |
88 |
if( this.source2translation==null ) { |
89 |
this.source2translation = new HashMap<>(); |
90 |
for (int i = 0; i < this.sourceNames.size(); i++) { |
91 |
this.source2translation.put(this.sourceNames.get(i), this.translatedNames.get(i)); |
92 |
} |
93 |
} |
94 |
return this.source2translation.get(sourceName); |
95 |
} |
96 |
return sourceName;
|
97 |
} |
98 |
|
99 |
@Override
|
100 |
public String getSource(String translatedName) { |
101 |
if( this.hasTranslations ) { |
102 |
if( this.translation2source==null ) { |
103 |
this.translation2source = new HashMap<>(); |
104 |
for (int i = 0; i < this.sourceNames.size(); i++) { |
105 |
this.translation2source.put(this.translatedNames.get(i), this.sourceNames.get(i)); |
106 |
} |
107 |
} |
108 |
return this.translation2source.get(translatedName); |
109 |
} |
110 |
return translatedName;
|
111 |
} |
112 |
|
113 |
@Override
|
114 |
public boolean isValid(String name) { |
115 |
String sourceName = name;
|
116 |
if ( StringUtils.isBlank(sourceName) ) {
|
117 |
sourceName = "field";
|
118 |
} |
119 |
sourceName = sourceName.toLowerCase(); |
120 |
int len = sourceName.length();
|
121 |
return len <= this.maxNameLen && !this.translatedNames.contains(sourceName); |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public String getSuggestion(String name) { |
126 |
String sourceName = name;
|
127 |
if ( StringUtils.isBlank(sourceName) ) {
|
128 |
sourceName = "field";
|
129 |
} |
130 |
sourceName = sourceName.toLowerCase(); |
131 |
int len = sourceName.length();
|
132 |
if (len <= this.maxNameLen && !this.translatedNames.contains(sourceName)) { |
133 |
return sourceName;
|
134 |
} |
135 |
@SuppressWarnings("UnusedAssignment") |
136 |
String translatedName = sourceName;
|
137 |
for (int i = 0; i < 255; i++) { |
138 |
if (len <= this.maxNameLen) { |
139 |
if (i <= 9) { |
140 |
if (len == this.maxNameLen) { |
141 |
translatedName = sourceName.substring(0, this.maxNameLen-3) + "_" + i; |
142 |
} else {
|
143 |
translatedName = sourceName + "_" + i;
|
144 |
} |
145 |
} else if (i <= 99) { |
146 |
if (len == this.maxNameLen) { |
147 |
translatedName = sourceName.substring(0, this.maxNameLen-3) + i; |
148 |
} else {
|
149 |
translatedName = sourceName + i; |
150 |
} |
151 |
} else {
|
152 |
if (len == this.maxNameLen - 1) { |
153 |
translatedName = sourceName.substring(0, this.maxNameLen-4) + i; |
154 |
} else {
|
155 |
translatedName = sourceName + i; |
156 |
} |
157 |
} |
158 |
|
159 |
} else {
|
160 |
if (i == 0) { |
161 |
translatedName = sourceName.substring(0, this.maxNameLen-1); |
162 |
} else if (i <= 9) { |
163 |
translatedName = sourceName.substring(0, this.maxNameLen-3) + "_" + i; |
164 |
} else if (i <= 99) { |
165 |
translatedName = sourceName.substring(0, this.maxNameLen-3) + i; |
166 |
} else {
|
167 |
translatedName = sourceName.substring(0, this.maxNameLen-4) + i; |
168 |
} |
169 |
} |
170 |
if (!this.translatedNames.contains(translatedName)) { |
171 |
return translatedName;
|
172 |
} |
173 |
|
174 |
} |
175 |
/*
|
176 |
* Should not get here
|
177 |
*/
|
178 |
return sourceName.substring(0, this.maxNameLen-7) + "_" + (System.currentTimeMillis() % 1000000); |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public String getTranslation(int index) { |
183 |
return this.translatedNames.get(index); |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public int setTranslation(String sourceName, String translatedName) { |
188 |
if( StringUtils.isBlank(sourceName) ) {
|
189 |
throw new IllegalArgumentException("Source name not valid (null or empty string)."); |
190 |
} |
191 |
if( StringUtils.isBlank(translatedName) ) {
|
192 |
throw new IllegalArgumentException("Translated name not valid (null or empty string)."); |
193 |
} |
194 |
int translationToSet = this.sourceNames.indexOf(sourceName.toLowerCase()); |
195 |
if( translationToSet<0 ) { |
196 |
throw new IllegalArgumentException("Don't exist name '"+sourceName+"'."); |
197 |
} |
198 |
translatedName = StringUtils.left(translatedName.toLowerCase(),this.maxNameLen);
|
199 |
int translationToFix = -1; |
200 |
for (int i = 0; i < translatedNames.size(); i++) { |
201 |
if( i==translationToSet ) {
|
202 |
continue;
|
203 |
} |
204 |
if( translatedName.equals(this.translatedNames.get(i)) ) { |
205 |
translationToFix = i; |
206 |
break;
|
207 |
} |
208 |
} |
209 |
this.translatedNames.set(translationToSet, translatedName);
|
210 |
if( !this.hasTranslations ) { |
211 |
this.hasTranslations = this.translatedNames.get(translationToSet).equals( |
212 |
this.sourceNames.get(translationToSet)
|
213 |
); |
214 |
} |
215 |
if( translationToFix>=0 ) { |
216 |
this.translatedNames.set(
|
217 |
translationToFix, |
218 |
this.getSuggestion(this.sourceNames.get(translationToFix)) |
219 |
); |
220 |
if( !this.hasTranslations ) { |
221 |
this.hasTranslations = this.translatedNames.get(translationToFix).equals( |
222 |
this.sourceNames.get(translationToFix)
|
223 |
); |
224 |
} |
225 |
} |
226 |
this.source2translation = null; |
227 |
this.translation2source = null; |
228 |
return translationToFix;
|
229 |
} |
230 |
|
231 |
@Override
|
232 |
public JsonObject toJson() {
|
233 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
234 |
JsonArrayBuilder translatedNamesBuilder = Json.createArrayBuilder(); |
235 |
for (String translatedName : translatedNames) { |
236 |
translatedNamesBuilder.add(translatedName); |
237 |
} |
238 |
JsonArrayBuilder sourceNamesBuilder = Json.createArrayBuilder(); |
239 |
for (String sourceName : sourceNames) { |
240 |
sourceNamesBuilder.add(sourceName); |
241 |
} |
242 |
builder.add("maxNameLen", this.maxNameLen); |
243 |
builder.add("sourceNames", sourceNamesBuilder);
|
244 |
builder.add("translatedNames", translatedNamesBuilder);
|
245 |
return builder.build();
|
246 |
} |
247 |
|
248 |
@Override
|
249 |
public void fromJson(JsonObject json) { |
250 |
this.sourceNames = new ArrayList<>(); |
251 |
this.translatedNames = new ArrayList<>(); |
252 |
this.maxNameLen = json.getInt("maxNameLen"); |
253 |
for (int i = 0; i < json.getJsonArray("sourceNames").size(); i++) { |
254 |
String sourceName = json.getJsonArray("sourceNames").get(i).toString(); |
255 |
String translatedName = json.getJsonArray("translatedNames").get(i).toString(); |
256 |
this.sourceNames.add(sourceName);
|
257 |
this.translatedNames.add(translatedName);
|
258 |
if( !sourceName.equals(translatedName) ) {
|
259 |
this.hasTranslations = true; |
260 |
} |
261 |
} |
262 |
this.source2translation = null; |
263 |
this.translation2source = null; |
264 |
this.sourceNames = Collections.unmodifiableList(this.sourceNames); |
265 |
} |
266 |
|
267 |
} |