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 @ 2189

History | View | Annotate | Download (4.89 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 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
}