Revision 2189
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/DummyNamesTranslator.java | ||
---|---|---|
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 |
|
|
25 |
package org.gvsig.tools.namestranslator; |
|
26 |
|
|
27 |
import java.util.ArrayList; |
|
28 |
import java.util.Arrays; |
|
29 |
import java.util.Collections; |
|
30 |
import java.util.List; |
|
31 |
import java.util.function.Function; |
|
32 |
|
|
33 |
/** |
|
34 |
* |
|
35 |
* @author jjdelcerro |
|
36 |
*/ |
|
37 |
public class DummyNamesTranslator |
|
38 |
extends AbstractNamesTranslator |
|
39 |
implements NamesTranslator |
|
40 |
{ |
|
41 |
|
|
42 |
private DummyNamesTranslator() { |
|
43 |
|
|
44 |
} |
|
45 |
|
|
46 |
public static final NamesTranslator create() { |
|
47 |
return new DummyNamesTranslator(); |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
protected void build() { |
|
52 |
this.sourceNames = Collections.unmodifiableList(this.sourceNames); |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public List<String> getSourceNames() { |
|
57 |
return this.sourceNames; |
|
58 |
} |
|
59 |
|
|
60 |
@Override |
|
61 |
public List<String> getTranslatedNames() { |
|
62 |
return this.sourceNames; |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public String getTranslation(String name) { |
|
67 |
return name; |
|
68 |
} |
|
69 |
|
|
70 |
@Override |
|
71 |
public String getSource(String name) { |
|
72 |
return name; |
|
73 |
} |
|
74 |
|
|
75 |
@Override |
|
76 |
public String getSuggestion(String name) { |
|
77 |
return name; |
|
78 |
} |
|
79 |
|
|
80 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/TrimNamesTranslator.java | ||
---|---|---|
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 org.apache.commons.lang3.StringUtils; |
|
32 |
|
|
33 |
/** |
|
34 |
* |
|
35 |
* @author jjdelcerro |
|
36 |
*/ |
|
37 |
public class TrimNamesTranslator |
|
38 |
extends AbstractNamesTranslator |
|
39 |
implements NamesTranslator |
|
40 |
{ |
|
41 |
|
|
42 |
private List<String> translatedNames; |
|
43 |
private Map<String,String> source2translation; |
|
44 |
private Map<String,String> translation2source; |
|
45 |
private boolean hasTranslations; |
|
46 |
private final int maxNameLen; |
|
47 |
|
|
48 |
private TrimNamesTranslator(int maxNameLen) { |
|
49 |
this.maxNameLen = maxNameLen; |
|
50 |
} |
|
51 |
|
|
52 |
public static final NamesTranslator create(int maxNameLen) { |
|
53 |
return new TrimNamesTranslator(maxNameLen); |
|
54 |
} |
|
55 |
|
|
56 |
@Override |
|
57 |
protected void build() { |
|
58 |
this.translatedNames = new ArrayList<>(); |
|
59 |
this.hasTranslations = false; |
|
60 |
for (String sourceName : this.sourceNames) { |
|
61 |
String translatedName = this.getSuggestion(sourceName); |
|
62 |
this.translatedNames.add(translatedName); |
|
63 |
if( !sourceName.equals(translatedName) ) { |
|
64 |
this.hasTranslations = true; |
|
65 |
} |
|
66 |
} |
|
67 |
if( this.hasTranslations ) { |
|
68 |
this.source2translation = new HashMap<>(); |
|
69 |
this.translation2source = new HashMap<>(); |
|
70 |
for (int i = 0; i < this.sourceNames.size(); i++) { |
|
71 |
this.source2translation.put(this.sourceNames.get(i), this.translatedNames.get(i)); |
|
72 |
this.translation2source.put(this.translatedNames.get(i), this.sourceNames.get(i)); |
|
73 |
} |
|
74 |
} else { |
|
75 |
this.source2translation = null; |
|
76 |
this.translation2source = null; |
|
77 |
} |
|
78 |
this.sourceNames = Collections.unmodifiableList(this.sourceNames); |
|
79 |
this.translatedNames = Collections.unmodifiableList(this.translatedNames); |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public List<String> getSourceNames() { |
|
84 |
return this.sourceNames; |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
|
88 |
public List<String> getTranslatedNames() { |
|
89 |
return this.translatedNames; |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public String getTranslation(String sourceName) { |
|
94 |
if( this.hasTranslations ) { |
|
95 |
return this.source2translation.get(sourceName); |
|
96 |
} |
|
97 |
return sourceName; |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public String getSource(String translatedName) { |
|
102 |
if( this.hasTranslations ) { |
|
103 |
return this.translation2source.get(translatedName); |
|
104 |
} |
|
105 |
return translatedName; |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public String getSuggestion(String name) { |
|
110 |
String sourceName = name; |
|
111 |
if ( StringUtils.isBlank(sourceName) ) { |
|
112 |
sourceName = "Field"; |
|
113 |
} |
|
114 |
int len = sourceName.length(); |
|
115 |
if (len <= this.maxNameLen && !this.translatedNames.contains(sourceName)) { |
|
116 |
return sourceName; |
|
117 |
} |
|
118 |
@SuppressWarnings("UnusedAssignment") |
|
119 |
String translatedName = sourceName; |
|
120 |
for (int i = 0; i < 255; i++) { |
|
121 |
if (len <= this.maxNameLen) { |
|
122 |
if (i <= 9) { |
|
123 |
if (len == this.maxNameLen) { |
|
124 |
translatedName = sourceName.substring(0, 8) + "_" + i; |
|
125 |
} else { |
|
126 |
translatedName = sourceName + "_" + i; |
|
127 |
} |
|
128 |
} else if (i <= 99) { |
|
129 |
if (len == this.maxNameLen) { |
|
130 |
translatedName = sourceName.substring(0, 8) + i; |
|
131 |
} else { |
|
132 |
translatedName = sourceName + i; |
|
133 |
} |
|
134 |
} else { |
|
135 |
if (len == this.maxNameLen - 1) { |
|
136 |
translatedName = sourceName.substring(0, 7) + i; |
|
137 |
} else { |
|
138 |
translatedName = sourceName + i; |
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 |
} else { |
|
143 |
if (i == 0) { |
|
144 |
translatedName = sourceName.substring(0, 10); |
|
145 |
} else if (i <= 9) { |
|
146 |
translatedName = sourceName.substring(0, 8) + "_" + i; |
|
147 |
} else if (i <= 99) { |
|
148 |
translatedName = sourceName.substring(0, 8) + i; |
|
149 |
} else { |
|
150 |
translatedName = sourceName.substring(0, 7) + i; |
|
151 |
} |
|
152 |
} |
|
153 |
if (!this.translatedNames.contains(translatedName)) { |
|
154 |
return translatedName; |
|
155 |
} |
|
156 |
|
|
157 |
} |
|
158 |
/* |
|
159 |
* Should not get here |
|
160 |
*/ |
|
161 |
return sourceName.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000); |
|
162 |
} |
|
163 |
|
|
164 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/NamesTranslator.java | ||
---|---|---|
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.List; |
|
27 |
import java.util.function.Function; |
|
28 |
|
|
29 |
/** |
|
30 |
* |
|
31 |
* @author jjdelcerro |
|
32 |
*/ |
|
33 |
public interface NamesTranslator { |
|
34 |
|
|
35 |
public void setSourceNames(String[] names); |
|
36 |
|
|
37 |
public void setSourceNames(Iterable<String> names); |
|
38 |
|
|
39 |
public void setSourceNames(Iterable objs, Function<Object, String> name_getter); |
|
40 |
|
|
41 |
public List<String> getSourceNames(); |
|
42 |
|
|
43 |
public List<String> getTranslatedNames(); |
|
44 |
|
|
45 |
public String[] getTranslatedNamesAsArray(); |
|
46 |
|
|
47 |
public String getTranslation(String sourceName); |
|
48 |
|
|
49 |
public String getSource(String translatedName); |
|
50 |
|
|
51 |
public String getSuggestion(String name); |
|
52 |
|
|
53 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/AbstractNamesTranslator.java | ||
---|---|---|
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.Arrays; |
|
28 |
import java.util.List; |
|
29 |
import java.util.function.Function; |
|
30 |
|
|
31 |
/** |
|
32 |
* |
|
33 |
* @author jjdelcerro |
|
34 |
*/ |
|
35 |
public abstract class AbstractNamesTranslator implements NamesTranslator { |
|
36 |
|
|
37 |
protected List<String>sourceNames; |
|
38 |
|
|
39 |
@Override |
|
40 |
public void setSourceNames(Iterable<String> names) { |
|
41 |
this.sourceNames = new ArrayList<>(); |
|
42 |
for (String name : names) { |
|
43 |
this.sourceNames.add(name); |
|
44 |
} |
|
45 |
this.build(); |
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
public void setSourceNames(String[] names) { |
|
50 |
this.sourceNames = Arrays.asList(names); |
|
51 |
this.build(); |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public void setSourceNames(Iterable objs, Function<Object, String> name_getter) { |
|
56 |
this.sourceNames = new ArrayList<>(); |
|
57 |
for (Object obj : objs) { |
|
58 |
String name = name_getter.apply(obj); |
|
59 |
this.sourceNames.add(name.toLowerCase()); |
|
60 |
} |
|
61 |
this.build(); |
|
62 |
} |
|
63 |
|
|
64 |
@Override |
|
65 |
public String[] getTranslatedNamesAsArray() { |
|
66 |
List<String> l = this.getTranslatedNames(); |
|
67 |
return l.toArray(new String[l.size()]); |
|
68 |
} |
|
69 |
|
|
70 |
protected abstract void build(); |
|
71 |
|
|
72 |
|
|
73 |
} |
Also available in: Unified diff