Statistics
| Revision:

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 / export / impl / service / DefaultAttributeNamesTranslator.java @ 43925

History | View | Annotate | Download (2.7 KB)

1 43504 jjdelcerro
2 43925 jjdelcerro
package org.gvsig.export.impl.service;
3 43504 jjdelcerro
4
import java.util.Collection;
5
import java.util.HashMap;
6
import java.util.Map;
7 43925 jjdelcerro
import org.gvsig.export.spi.AttributeNamesTranslator;
8 43504 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
9
import org.gvsig.fmap.dal.feature.FeatureType;
10
11
12 43925 jjdelcerro
public class DefaultAttributeNamesTranslator implements AttributeNamesTranslator {
13 43504 jjdelcerro
14
    private final Map<String,String> sourceToTarget;
15
    private final Map<String,String> targetToSource;
16
    private final int maxNameLen;
17
18 43925 jjdelcerro
    public DefaultAttributeNamesTranslator(FeatureType type, int maxNameLen) {
19 43504 jjdelcerro
        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 43925 jjdelcerro
    @Override
33 43504 jjdelcerro
    public String getTargetName(String name) {
34
         return this.sourceToTarget.getOrDefault(name, name);
35
    }
36
37 43925 jjdelcerro
    @Override
38 43504 jjdelcerro
    public String getSourceName(String name) {
39
         return this.targetToSource.getOrDefault(name, name);
40
    }
41
42 43920 jjdelcerro
    @Override
43 43504 jjdelcerro
    public boolean hasTranslations() {
44
        return !this.sourceToTarget.isEmpty();
45
    }
46
47 43925 jjdelcerro
    @Override
48 43504 jjdelcerro
    public boolean hasTranslation(String name) {
49
        return this.sourceToTarget.get(name)!=null;
50
    }
51
52
        private String buildTranslatedName(String source_name, Collection<String> current_names) {
53
54
                int len = source_name.length();
55
                if (len <= this.maxNameLen) {
56
                        /*
57
                         * Should not happen
58
                         */
59
                        return source_name;
60
                }
61
                String target_name;
62
                /*
63
                 * GDAL field name truncation method (extended from 100 to 255)
64
                 * THISISAVERYLONGNAME => THISISAVER, THISISAV_1 ... THISISAV_9,
65
                 * THISISAV10 ... THISISAV99, THISISA100 ... THISISA255
66
                 * (255 = max number of fields in a SHP)
67
                 */
68
                for (int i=0; i<255; i++) {
69
                    if (i==0) {
70
                target_name = source_name.substring(0, 10);
71
            } else if (i<=9) {
72
                target_name = source_name.substring(0, 8) + "_" + i;
73
            } else if (i<=99) {
74
                target_name = source_name.substring(0, 8) + i;
75
            } else {
76
                target_name = source_name.substring(0, 7) + i;
77
            }
78
                    if (!current_names.contains(target_name)) {
79
                return target_name;
80
            }
81
                }
82
                /*
83
                 * Should not get here
84
                 */
85
                return source_name.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000);
86
        }
87
}