Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.lib / org.gvsig.exportto.lib.api / src / main / java / org / gvsig / export / spi / CutAttributeNamesTranslator.java @ 44411

History | View | Annotate | Download (5.77 KB)

1
package org.gvsig.export.spi;
2

    
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.logging.Level;
6
import java.util.logging.Logger;
7
import org.apache.commons.lang3.StringUtils;
8
import org.gvsig.export.ExportAttributes;
9
import org.gvsig.export.ExportAttributes.ExportAttribute;
10
import org.gvsig.tools.ToolsLocator;
11
import org.gvsig.tools.dynobject.DynStruct;
12
import org.gvsig.tools.persistence.PersistenceManager;
13
import org.gvsig.tools.persistence.PersistentState;
14
import org.gvsig.tools.persistence.exception.PersistenceException;
15

    
16
public class CutAttributeNamesTranslator implements AttributeNamesTranslator {
17

    
18
    public int maxNameLen;
19

    
20
    public CutAttributeNamesTranslator(int maxNameLen) {
21
        this.maxNameLen = maxNameLen;
22
    }
23

    
24
    public CutAttributeNamesTranslator() {
25

    
26
    }
27

    
28
    private String buildTranslatedName(String source_name, Collection<String> current_names) {
29

    
30
        int len = source_name.length();
31
        if (source_name.isEmpty()) {
32
            source_name = "Field";
33
        }
34
        if (len <= this.maxNameLen && !current_names.contains(source_name)) {
35
            /*
36
                         * Should not happen
37
             */
38
            return source_name;
39
        }
40
        String target_name = source_name;
41
        /*
42
                 * GDAL field name truncation method (extended from 100 to 255)
43
                 * THISISAVERYLONGNAME => THISISAVER, THISISAV_1 ... THISISAV_9,
44
                 * THISISAV10 ... THISISAV99, THISISA100 ... THISISA255
45
                 * (255 = max number of fields in a SHP)
46
         */
47
        for (int i = 0; i < 255; i++) {
48
            if (len <= this.maxNameLen) {
49
                if (i <= 9) {
50
                    if (len == this.maxNameLen) {
51
                        target_name = source_name.substring(0, 8) + "_" + i;
52
                    } else {
53
                        target_name = source_name + "_" + i;
54
                    }
55
                } else if (i <= 99) {
56
                    if (len == this.maxNameLen) {
57
                        target_name = source_name.substring(0, 8) + i;;
58
                    } else {
59
                        target_name = source_name + i;
60
                    }
61
                } else {
62
                    if (len == this.maxNameLen - 1) {
63
                        target_name = source_name.substring(0, 7) + i;;
64
                    } else {
65
                        target_name = source_name + i;
66
                    }
67
                }
68

    
69
            } else {
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
            }
80
            if (!current_names.contains(target_name)) {
81
                return target_name;
82
            }
83

    
84
        }
85
        /*
86
                 * Should not get here
87
         */
88
        return source_name.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000);
89
    }
90

    
91
    @Override
92
    public boolean isValidName(ExportAttributes attributes, int index, String name) {
93
        ExportAttribute selectedAttr = attributes.get(index);
94
        //
95
        String selfName = this.getNameSuggestion(attributes, index, name);
96
        if (!selfName.equals(name)) {
97
            return false;
98
        }
99

    
100
        // check with others values
101
        boolean valid = true;
102
        for (int i = 0; i < attributes.size(); i++) {
103
            if (i == index) {
104
                continue;
105
            }
106
            ExportAttribute attr = attributes.get(i);
107
            String newName = attr.getNewName();
108
            if (newName.isEmpty()) {
109
                return false;
110
            }
111
            if (newName.equals(name)) {
112
                return false;
113
            }
114
            if (newName.equals(this.getNameSuggestion(attributes, index, name))) {
115
                return false;
116
            }
117
        }
118
        return valid;
119
    }
120

    
121
    @Override
122
    public String getNameSuggestion(ExportAttributes attributes, int index, String name) {
123
        ArrayList names = new ArrayList();
124
        boolean oneTime = true;
125
        for (ExportAttribute attribute : attributes) {
126
            if (attribute.getNewName().equals(name) && oneTime) {
127
                oneTime = false;
128
                continue;
129
            }
130
            names.add(attribute.getNewName());
131
        }
132
        String target_name = buildTranslatedName(name, names);
133
        return target_name;
134
    }
135

    
136
    @Override
137
    public CutAttributeNamesTranslator clone() {
138
        CutAttributeNamesTranslator clone = null;
139
        try {
140
            clone = (CutAttributeNamesTranslator) super.clone();
141
        } catch (CloneNotSupportedException ex) {
142
            Logger.getLogger(CutAttributeNamesTranslator.class.getName()).log(Level.SEVERE, null, ex);
143
        }
144

    
145
        return clone;
146

    
147
    }
148

    
149
    @Override
150
    public String getNameSuggestion(String name) {
151
        String s = StringUtils.left(name, maxNameLen);
152
        return s;
153
    }
154

    
155
    public static void registerPersistence() {
156
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
157
        if (manager.getDefinition("CutAttributeNamesTranslator") == null) {
158
            DynStruct definition = manager.addDefinition(CutAttributeNamesTranslator.class,
159
                    "CutAttributeNamesTranslator", "CutAttributeNamesTranslator persistence definition", null, null);
160
            definition.addDynFieldInt("maxNameLen");
161
        }
162
    }
163

    
164
    @Override
165
    public void saveToState(PersistentState state) throws PersistenceException {
166
        state.set("maxNameLen", this.maxNameLen);
167
    }
168

    
169
    @Override
170
    public void loadFromState(PersistentState state) throws PersistenceException {
171
        this.maxNameLen = state.getInt("maxNameLen");
172
    }
173
}