svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.lib / org.gvsig.exportto.lib.impl / src / main / java / org / gvsig / exportto / impl / AttributeNamesTranslator.java @ 43920
History | View | Annotate | Download (2.71 KB)
1 |
|
---|---|
2 |
package org.gvsig.exportto.impl; |
3 |
|
4 |
import java.util.Collection; |
5 |
import java.util.HashMap; |
6 |
import java.util.Iterator; |
7 |
import java.util.Map; |
8 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
9 |
import org.gvsig.fmap.dal.feature.FeatureType; |
10 |
|
11 |
|
12 |
public class AttributeNamesTranslator implements Iterable<String> { |
13 |
|
14 |
private final Map<String,String> sourceToTarget; |
15 |
private final Map<String,String> targetToSource; |
16 |
private final int maxNameLen; |
17 |
|
18 |
public AttributeNamesTranslator(FeatureType type, int maxNameLen) { |
19 |
this.maxNameLen = maxNameLen;
|
20 |
this.sourceToTarget = new HashMap<>(); |
21 |
this.targetToSource = new HashMap<>(); |
22 |
for( FeatureAttributeDescriptor attr : type) {
|
23 |
String source_name = attr.getName();
|
24 |
String target_name = buildTranslatedName(source_name, this.sourceToTarget.values()); |
25 |
if( !source_name.equals(target_name) ) {
|
26 |
this.sourceToTarget.put(source_name, target_name);
|
27 |
this.targetToSource.put(target_name, source_name);
|
28 |
} |
29 |
} |
30 |
} |
31 |
|
32 |
public String getTargetName(String name) { |
33 |
return this.sourceToTarget.getOrDefault(name, name); |
34 |
} |
35 |
|
36 |
public String getSourceName(String name) { |
37 |
return this.targetToSource.getOrDefault(name, name); |
38 |
} |
39 |
|
40 |
@Override
|
41 |
public Iterator<String> iterator() { |
42 |
return this.sourceToTarget.keySet().iterator(); |
43 |
} |
44 |
|
45 |
public boolean hasTranslations() { |
46 |
return !this.sourceToTarget.isEmpty(); |
47 |
} |
48 |
|
49 |
public boolean hasTranslation(String name) { |
50 |
return this.sourceToTarget.get(name)!=null; |
51 |
} |
52 |
|
53 |
private String buildTranslatedName(String source_name, Collection<String> current_names) { |
54 |
|
55 |
int len = source_name.length();
|
56 |
if (len <= this.maxNameLen) { |
57 |
/*
|
58 |
* Should not happen
|
59 |
*/
|
60 |
return source_name;
|
61 |
} |
62 |
String target_name;
|
63 |
/*
|
64 |
* GDAL field name truncation method (extended from 100 to 255)
|
65 |
* THISISAVERYLONGNAME => THISISAVER, THISISAV_1 ... THISISAV_9,
|
66 |
* THISISAV10 ... THISISAV99, THISISA100 ... THISISA255
|
67 |
* (255 = max number of fields in a SHP)
|
68 |
*/
|
69 |
for (int i=0; i<255; i++) { |
70 |
if (i==0) { |
71 |
target_name = source_name.substring(0, 10); |
72 |
} else if (i<=9) { |
73 |
target_name = source_name.substring(0, 8) + "_" + i; |
74 |
} else if (i<=99) { |
75 |
target_name = source_name.substring(0, 8) + i; |
76 |
} else {
|
77 |
target_name = source_name.substring(0, 7) + i; |
78 |
} |
79 |
if (!current_names.contains(target_name)) {
|
80 |
return target_name;
|
81 |
} |
82 |
} |
83 |
/*
|
84 |
* Should not get here
|
85 |
*/
|
86 |
return source_name.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000); |
87 |
} |
88 |
} |