Revision 2191

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/DummyNamesTranslator.java
25 25
package org.gvsig.tools.namestranslator;
26 26

  
27 27
import java.util.ArrayList;
28
import java.util.Arrays;
29 28
import java.util.Collections;
30 29
import java.util.List;
31
import java.util.function.Function;
30
import javax.json.Json;
31
import javax.json.JsonArrayBuilder;
32
import javax.json.JsonObject;
33
import javax.json.JsonObjectBuilder;
32 34

  
33 35
/**
34 36
 *
......
39 41
        implements NamesTranslator 
40 42
  {
41 43

  
42
  private DummyNamesTranslator() {
44
  private ArrayList<String> translatedNames;
45

  
46
  protected DummyNamesTranslator() {
43 47
    
44 48
  }
45 49
  
46
  public static final NamesTranslator create() {
47
    return new DummyNamesTranslator();
48
  }
49
  
50 50
  @Override
51 51
  protected void build() {
52
    this.translatedNames = new ArrayList<>(sourceNames);
52 53
    this.sourceNames = Collections.unmodifiableList(this.sourceNames);
53 54
  }
54 55

  
......
59 60

  
60 61
  @Override
61 62
  public List<String> getTranslatedNames() {
62
    return this.sourceNames;
63
    return this.translatedNames;
63 64
  }
64 65

  
65 66
  @Override
66
  public String getTranslation(String name) {
67
    return name;
67
  public String getTranslation(String sourceName) {
68
    int n = this.sourceNames.indexOf(sourceName);
69
    if( n<0 ) {
70
      return null;
71
    }
72
    return this.translatedNames.get(n);
68 73
  }
69 74

  
70 75
  @Override
......
77 82
    return name;
78 83
  }
79 84

  
85
  @Override
86
  public String getTranslation(int index) {
87
    return this.translatedNames.get(index);
88
  }
89

  
90
  @Override
91
  public int setTranslation(String sourceName, String translatedName) {
92
    int n = this.sourceNames.indexOf(sourceName);
93
    if( n>=0 ) {
94
      this.translatedNames.set(n, translatedName);
95
    }
96
    return -1;
97
  }
98

  
99
  @Override
100
  public JsonObject toJson() {
101
    JsonObjectBuilder builder = Json.createObjectBuilder();
102
    JsonArrayBuilder translatedNamesBuilder = Json.createArrayBuilder();
103
    for (String translatedName : translatedNames) {
104
      translatedNamesBuilder.add(translatedName);
105
    }
106
    JsonArrayBuilder sourceNamesBuilder = Json.createArrayBuilder();
107
    for (String sourceName : sourceNames) {
108
      sourceNamesBuilder.add(sourceName);
109
    }
110
    builder.add("sourceNames", sourceNamesBuilder);
111
    builder.add("translatedNames", translatedNamesBuilder);
112
    return builder.build();
113
  }
114

  
115
  @Override
116
  public void fromJson(JsonObject json) {
117
    this.sourceNames = new ArrayList<>();
118
    this.translatedNames = new ArrayList<>();
119
    for (int i = 0; i < json.getJsonArray("sourceNames").size(); i++) {
120
      String sourceName = json.getJsonArray("sourceNames").get(i).toString();
121
      String translatedName = json.getJsonArray("translatedNames").get(i).toString();
122
      this.sourceNames.add(sourceName);
123
      this.translatedNames.add(translatedName);
124
    }
125
    this.sourceNames = Collections.unmodifiableList(this.sourceNames);
126
  }
127
  
80 128
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/TrimNamesTranslator.java
28 28
import java.util.HashMap;
29 29
import java.util.List;
30 30
import java.util.Map;
31
import javax.json.Json;
32
import javax.json.JsonArrayBuilder;
33
import javax.json.JsonObject;
34
import javax.json.JsonObjectBuilder;
31 35
import org.apache.commons.lang3.StringUtils;
32 36

  
33 37
/**
......
43 47
  private Map<String,String> source2translation;
44 48
  private Map<String,String> translation2source;
45 49
  private boolean hasTranslations;
46
  private final int maxNameLen;
50
  private int maxNameLen;
47 51

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

  
52
  public static final NamesTranslator create(int maxNameLen) {
53
    return new TrimNamesTranslator(maxNameLen);
54
  }
55 58
  
56 59
  @Override
57 60
  protected void build() {
......
64 67
        this.hasTranslations = true;
65 68
      }
66 69
    }
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
    }
70
    this.source2translation = null;
71
    this.translation2source = null;      
78 72
    this.sourceNames = Collections.unmodifiableList(this.sourceNames);
79
    this.translatedNames = Collections.unmodifiableList(this.translatedNames);
80 73
  }
81 74

  
82 75
  @Override
......
92 85
  @Override
93 86
  public String getTranslation(String sourceName) {
94 87
    if( this.hasTranslations ) {
88
      if( this.source2translation==null ) {
89
        this.source2translation = new HashMap<>();
90
        for (int i = 0; i < this.sourceNames.size(); i++) {
91
          this.source2translation.put(this.sourceNames.get(i), this.translatedNames.get(i));
92
        }
93
      }
95 94
      return this.source2translation.get(sourceName);
96 95
    }
97 96
    return sourceName;
......
100 99
  @Override
101 100
  public String getSource(String translatedName) {
102 101
    if( this.hasTranslations ) {
102
      if( this.translation2source==null ) {
103
        this.translation2source = new HashMap<>();
104
        for (int i = 0; i < this.sourceNames.size(); i++) {
105
          this.translation2source.put(this.translatedNames.get(i), this.sourceNames.get(i));
106
        }
107
      }
103 108
      return this.translation2source.get(translatedName);
104 109
    }
105 110
    return translatedName;
106 111
  }
107 112

  
108 113
  @Override
114
  public boolean isValid(String name) {
115
    String sourceName = name;
116
    if ( StringUtils.isBlank(sourceName) ) {
117
      sourceName = "field";
118
    }
119
    sourceName = sourceName.toLowerCase();
120
    int len = sourceName.length();
121
    return len <= this.maxNameLen && !this.translatedNames.contains(sourceName);
122
  }
123

  
124
  @Override
109 125
  public String getSuggestion(String name) {
110 126
    String sourceName = name;
111 127
    if ( StringUtils.isBlank(sourceName) ) {
112
      sourceName = "Field";
128
      sourceName = "field";
113 129
    }
130
    sourceName = sourceName.toLowerCase();
114 131
    int len = sourceName.length();
115 132
    if (len <= this.maxNameLen && !this.translatedNames.contains(sourceName)) {
116 133
      return sourceName;
......
121 138
      if (len <= this.maxNameLen) {
122 139
        if (i <= 9) {
123 140
          if (len == this.maxNameLen) {
124
            translatedName = sourceName.substring(0, 8) + "_" + i;
141
            translatedName = sourceName.substring(0, this.maxNameLen-3) + "_" + i;
125 142
          } else {
126 143
            translatedName = sourceName + "_" + i;
127 144
          }
128 145
        } else if (i <= 99) {
129 146
          if (len == this.maxNameLen) {
130
            translatedName = sourceName.substring(0, 8) + i;
147
            translatedName = sourceName.substring(0, this.maxNameLen-3) + i;
131 148
          } else {
132 149
            translatedName = sourceName + i;
133 150
          }
134 151
        } else {
135 152
          if (len == this.maxNameLen - 1) {
136
            translatedName = sourceName.substring(0, 7) + i;
153
            translatedName = sourceName.substring(0, this.maxNameLen-4) + i;
137 154
          } else {
138 155
            translatedName = sourceName + i;
139 156
          }
......
141 158

  
142 159
      } else {
143 160
        if (i == 0) {
144
          translatedName = sourceName.substring(0, 10);
161
          translatedName = sourceName.substring(0, this.maxNameLen-1);
145 162
        } else if (i <= 9) {
146
          translatedName = sourceName.substring(0, 8) + "_" + i;
163
          translatedName = sourceName.substring(0, this.maxNameLen-3) + "_" + i;
147 164
        } else if (i <= 99) {
148
          translatedName = sourceName.substring(0, 8) + i;
165
          translatedName = sourceName.substring(0, this.maxNameLen-3) + i;
149 166
        } else {
150
          translatedName = sourceName.substring(0, 7) + i;
167
          translatedName = sourceName.substring(0, this.maxNameLen-4) + i;
151 168
        }
152 169
      }
153 170
      if (!this.translatedNames.contains(translatedName)) {
......
158 175
    /*
159 176
		 * Should not get here
160 177
     */
161
    return sourceName.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000);
178
    return sourceName.substring(0, this.maxNameLen-7) + "_" + (System.currentTimeMillis() % 1000000);
162 179
  }
180

  
181
  @Override
182
  public String getTranslation(int index) {
183
    return this.translatedNames.get(index);
184
  }
185

  
186
  @Override
187
  public int setTranslation(String sourceName, String translatedName) {
188
    if( StringUtils.isBlank(sourceName) ) {
189
      throw new IllegalArgumentException("Source name not valid (null or empty string).");
190
    }
191
    if( StringUtils.isBlank(translatedName) ) {
192
      throw new IllegalArgumentException("Translated name not valid (null or empty string).");
193
    }
194
    int translationToSet = this.sourceNames.indexOf(sourceName.toLowerCase());
195
    if( translationToSet<0 ) {
196
      throw new IllegalArgumentException("Don't exist name '"+sourceName+"'.");
197
    }
198
    translatedName = StringUtils.left(translatedName.toLowerCase(),this.maxNameLen);
199
    int translationToFix = -1;
200
    for (int i = 0; i < translatedNames.size(); i++) {
201
      if( i==translationToSet ) {
202
        continue;
203
      }
204
      if( translatedName.equals(this.translatedNames.get(i)) ) {
205
        translationToFix = i;
206
        break;
207
      }
208
    }
209
    this.translatedNames.set(translationToSet, translatedName);
210
    if( !this.hasTranslations ) {
211
      this.hasTranslations = this.translatedNames.get(translationToSet).equals(
212
              this.sourceNames.get(translationToSet)
213
      );
214
    }
215
    if( translationToFix>=0 ) {
216
      this.translatedNames.set(
217
              translationToFix, 
218
              this.getSuggestion(this.sourceNames.get(translationToFix))
219
      );
220
      if( !this.hasTranslations ) {
221
        this.hasTranslations = this.translatedNames.get(translationToFix).equals(
222
                this.sourceNames.get(translationToFix)
223
        );
224
      }
225
    }     
226
    this.source2translation = null;
227
    this.translation2source = null;      
228
    return translationToFix;
229
  }
163 230
  
231
  @Override
232
  public JsonObject toJson() {
233
    JsonObjectBuilder builder = Json.createObjectBuilder();
234
    JsonArrayBuilder translatedNamesBuilder = Json.createArrayBuilder();
235
    for (String translatedName : translatedNames) {
236
      translatedNamesBuilder.add(translatedName);
237
    }
238
    JsonArrayBuilder sourceNamesBuilder = Json.createArrayBuilder();
239
    for (String sourceName : sourceNames) {
240
      sourceNamesBuilder.add(sourceName);
241
    }
242
    builder.add("maxNameLen", this.maxNameLen);
243
    builder.add("sourceNames", sourceNamesBuilder);
244
    builder.add("translatedNames", translatedNamesBuilder);
245
    return builder.build();
246
  }
247

  
248
  @Override
249
  public void fromJson(JsonObject json) {
250
    this.sourceNames = new ArrayList<>();
251
    this.translatedNames = new ArrayList<>();
252
    this.maxNameLen = json.getInt("maxNameLen");
253
    for (int i = 0; i < json.getJsonArray("sourceNames").size(); i++) {
254
      String sourceName = json.getJsonArray("sourceNames").get(i).toString();
255
      String translatedName = json.getJsonArray("translatedNames").get(i).toString();
256
      this.sourceNames.add(sourceName);
257
      this.translatedNames.add(translatedName);
258
      if( !sourceName.equals(translatedName) ) {
259
        this.hasTranslations = true;
260
      }
261
    }
262
    this.source2translation = null;
263
    this.translation2source = null;      
264
    this.sourceNames = Collections.unmodifiableList(this.sourceNames);
265
  }
266
  
164 267
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/NamesTranslator.java
25 25

  
26 26
import java.util.List;
27 27
import java.util.function.Function;
28
import javax.json.JsonObject;
28 29

  
29 30
/**
30 31
 *
......
32 33
 */
33 34
public interface NamesTranslator {
34 35

  
36
  public static NamesTranslator createTrimTranslator(int maxNameLen) {
37
    return new TrimNamesTranslator(maxNameLen);
38
  }
39

  
40
  public static NamesTranslator createTrimTranslator(JsonObject json) {
41
    NamesTranslator translator = new TrimNamesTranslator(100);
42
    translator.fromJson(json);
43
    return translator;
44
  }
45

  
46
  public static NamesTranslator createDummyTranslator() {
47
    return new DummyNamesTranslator();
48
  }
49

  
50
  public static NamesTranslator createDummyTranslator(JsonObject json) {
51
    NamesTranslator translator = new DummyNamesTranslator();
52
    translator.fromJson(json);
53
    return translator;
54
  }
55

  
56
  /**
57
   * Assign the list of source names and generate the list of translations from them.
58
   * If any of the source names collide with another, it will create the 
59
   * translation using the "getSugestion" method.
60
   * 
61
   * @param names 
62
   */
35 63
  public void setSourceNames(String[] names);
36 64
  
65
  /**
66
   * Assign the list of source names and generate the list of translations from them.
67
   * If any of the source names collide with another, it will create the 
68
   * translation using the "getSugestion" method.
69
   *
70
   * @param names 
71
   */
37 72
  public void setSourceNames(Iterable<String> names);
38 73

  
74
  /**
75
   * Assign the list of source names and generate the list of translations from them.
76
   * If any of the source names collide with another, it will create the 
77
   * translation using the "getSugestion" method.
78
   *
79
   * @param objs
80
   * @param name_getter 
81
   */
39 82
  public void setSourceNames(Iterable objs, Function<Object, String> name_getter);
40 83

  
84
  /** 
85
   * Return the list of source names.
86
   * 
87
   * @return 
88
   */
41 89
  public List<String> getSourceNames();
42 90

  
91
  /** 
92
   * Return the list of translated names.
93
   * 
94
   * @return 
95
   */
43 96
  public List<String> getTranslatedNames();
44 97

  
98
  /** 
99
   * Return the list of translated names as a String array.
100
   * 
101
   * @return 
102
   */
45 103
  public String[] getTranslatedNamesAsArray();
46 104

  
105
  /**
106
   * Returns the translation corresponding to the source name indicated 
107
   * in the "sourceName" parameter.
108
   * 
109
   * @param sourceName
110
   * @return 
111
   */
47 112
  public String getTranslation(String sourceName);
48 113

  
114
  /**
115
   * Returns the source name corresponding to the translation indicated 
116
   * in the "translationName" parameter.
117
   * 
118
   * @param translatedName
119
   * @return 
120
   */
49 121
  public String getSource(String translatedName);
50 122

  
123
  /**
124
   * Returns the translation corresponding to the source name indicated 
125
   * in the "index" parameter.
126
   * 
127
   * @param index
128
   * @return 
129
   */
130
  public String getTranslation(int index);
131

  
132
  /**
133
   * Returns the source name corresponding to the translation indicated 
134
   * in the "index" parameter.
135
   * 
136
   * @param index
137
   * @return 
138
   */
139
  public String getSource(int index);
140

  
141
  /**
142
   * Check if the name passed as a parameter collides with 
143
   * any existing translation.
144
   * If it collides it generates a new name from the past as a parameter 
145
   * and returns it. If it does not collide, it returns.
146
   * 
147
   * @param name
148
   * @return 
149
   */
51 150
  public String getSuggestion(String name);
151
  
152
  /**
153
   * Returns true if the name passed as a parameter does not collide 
154
   * with any of the existing translations.
155
   * 
156
   * @param name
157
   * @return 
158
   */
159
  public boolean isValid(String name);
160
  
161
  /**
162
   * Assign the translation "translationName" to the name "sourceName".
163
   * If it collides with some other, change the translation of the colliding 
164
   * and return its index. If it does not collide it returns -1.
165
   * 
166
   * @param sourceName
167
   * @param translatedName
168
   * @return 
169
   */
170
  public int setTranslation(String sourceName, String translatedName);
52 171

  
172
  /**
173
   * Returns a "JsonObject" with the information stored in this object.
174
   * 
175
   * @return 
176
   */
177
  public JsonObject toJson();
178
  
179
  /**
180
   * Restore this object with the information stored in the "JsonObject" 
181
   * passed as a parameter.
182
   * 
183
   * @param json 
184
   */
185
  public void fromJson(JsonObject json);
186
  
53 187
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/namestranslator/AbstractNamesTranslator.java
27 27
import java.util.Arrays;
28 28
import java.util.List;
29 29
import java.util.function.Function;
30
import org.apache.commons.lang3.StringUtils;
30 31

  
31 32
/**
32 33
 *
......
36 37
  
37 38
  protected List<String>sourceNames;
38 39
  
40
  protected AbstractNamesTranslator() {
41
    
42
  }
43
  
39 44
  @Override
40 45
  public void setSourceNames(Iterable<String> names) {
41 46
    this.sourceNames = new ArrayList<>();
......
66 71
    List<String> l = this.getTranslatedNames();
67 72
    return l.toArray(new String[l.size()]);
68 73
  }
74

  
75
  @Override
76
  public String getSource(int index) {
77
    return this.sourceNames.get(index);
78
  }
79

  
80
  @Override
81
  public boolean isValid(String name) {
82
    return StringUtils.equalsIgnoreCase(name, this.getSuggestion(name));
83
  }
69 84
  
70 85
  protected abstract void build();
71 86
  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/pom.xml
15 15
    </parent>
16 16
    <dependencies>
17 17
        <dependency>
18
            <groupId>org.glassfish</groupId>
19
            <artifactId>javax.json</artifactId>
20
            <scope>compile</scope>
21
        </dependency>
22

  
23
        <dependency>
18 24
            <groupId>org.apache.commons</groupId>
19 25
            <artifactId>commons-lang3</artifactId>
20 26
            <scope>compile</scope>

Also available in: Unified diff