Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / namestranslator / TrimNamesTranslator.java @ 2217

History | View | Annotate | Download (5.38 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 org.apache.commons.lang3.StringUtils;
27
import org.gvsig.tools.ToolsLocator;
28
import org.gvsig.tools.dynobject.DynStruct;
29
import org.gvsig.tools.persistence.PersistenceManager;
30
import org.gvsig.tools.persistence.PersistentState;
31
import org.gvsig.tools.persistence.exception.PersistenceException;
32

    
33
/**
34
 *
35
 * @author jjdelcerro
36
 */
37
public class TrimNamesTranslator
38
        extends BaseNamesTranslator {
39

    
40
    protected int maxNameLen;
41

    
42
    public TrimNamesTranslator() {
43

    
44
    }
45

    
46
    protected TrimNamesTranslator(int maxNameLen) {
47
        if (maxNameLen < 8) {
48
            throw new IllegalArgumentException("Invalid max name len, can be greater than 7.");
49
        }
50
        this.maxNameLen = maxNameLen;
51
    }
52

    
53
    @Override
54
    public NamesTranslator clone() throws CloneNotSupportedException {
55
        TrimNamesTranslator clone = (TrimNamesTranslator) super.clone();
56
        return clone;
57
    }
58

    
59
    @Override
60
    public boolean isValid(String name) {
61
        String sourceName = name;
62
        if (StringUtils.isBlank(sourceName)) {
63
            sourceName = "field";
64
        }
65
        int len = sourceName.length();
66
        return len <= this.maxNameLen && !this.translatedNames.contains(sourceName);
67
    }
68

    
69
    @Override
70
    public String getSuggestion(String name) {
71
        String sourceName = name;
72
        if (StringUtils.isBlank(sourceName)) {
73
            sourceName = "field";
74
        }
75
        int len = sourceName.length();
76
        if (len <= this.maxNameLen && !this.translatedNames.contains(sourceName)) {
77
            return sourceName;
78
        }
79
        String translatedName;
80
        for (int i = 0; i < 255; i++) {
81
            if (len <= this.maxNameLen) {
82
                if (i <= 9) {
83
                    if (len == this.maxNameLen) {
84
                        translatedName = sourceName.substring(0, this.maxNameLen - 3) + "_" + i;
85
                    } else {
86
                        translatedName = sourceName + "_" + i;
87
                    }
88
                } else if (i <= 99) {
89
                    if (len == this.maxNameLen) {
90
                        translatedName = sourceName.substring(0, this.maxNameLen - 3) + i;
91
                    } else {
92
                        translatedName = sourceName + i;
93
                    }
94
                } else {
95
                    if (len == this.maxNameLen - 1) {
96
                        translatedName = sourceName.substring(0, this.maxNameLen - 4) + i;
97
                    } else {
98
                        translatedName = sourceName + i;
99
                    }
100
                }
101

    
102
            } else {
103
                if (i == 0) {
104
                    translatedName = sourceName.substring(0, this.maxNameLen - 1);
105
                } else if (i <= 9) {
106
                    translatedName = sourceName.substring(0, this.maxNameLen - 3) + "_" + i;
107
                } else if (i <= 99) {
108
                    translatedName = sourceName.substring(0, this.maxNameLen - 3) + i;
109
                } else {
110
                    translatedName = sourceName.substring(0, this.maxNameLen - 4) + i;
111
                }
112
            }
113
            if (!this.translatedNames.contains(translatedName)) {
114
                return translatedName;
115
            }
116

    
117
        }
118
        /*
119
                 * Should not get here
120
         */
121
        return sourceName.substring(0, this.maxNameLen - 7) + "_" + (System.currentTimeMillis() % 1000000);
122
    }
123

    
124
    @Override
125
    public int setTranslation(String sourceName, String translatedName) {
126
        translatedName = StringUtils.left(translatedName, this.maxNameLen);
127
        return super.setTranslation(sourceName, translatedName);
128
    }
129

    
130
    public static void registerPersistence() {
131
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
132
        if (manager.getDefinition("TrimNamesTranslator") == null) {
133
            DynStruct definition = manager.addDefinition(TrimNamesTranslator.class,
134
                    "TrimNamesTranslator", "TrimNamesTranslator persistence definition", null, null);
135
            definition.addDynFieldInt("maxNameLen");
136
            definition.extend(manager.getDefinition("BaseNamesTranslator"));
137
        }
138
    }
139

    
140
    @Override
141
    public void saveToState(PersistentState state) throws PersistenceException {
142
        super.saveToState(state);
143
        state.set("maxNameLen", maxNameLen);
144
    }
145

    
146
    @Override
147
    public void loadFromState(PersistentState state) throws PersistenceException {
148
        super.loadFromState(state);
149
        this.maxNameLen = state.getInt("maxNameLen");
150
    }
151
}