Revision 44300 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
DefaultAttributeNamesTranslator.java | ||
---|---|---|
16 | 16 |
|
17 | 17 |
public DefaultAttributeNamesTranslator(int maxNameLen) { |
18 | 18 |
this.maxNameLen = maxNameLen; |
19 |
// for (FeatureAttributeDescriptor attr : type) { |
|
20 |
// String source_name = attr.getName(); |
|
21 |
// String target_name = buildTranslatedName(source_name, this.sourceToTarget.values()); |
|
22 |
// if (!source_name.equals(target_name)) { |
|
23 |
// this.sourceToTarget.put(source_name, target_name); |
|
24 |
// this.targetToSource.put(target_name, source_name); |
|
25 |
// } |
|
26 |
// } |
|
27 | 19 |
} |
28 | 20 |
|
29 | 21 |
private String buildTranslatedName(String source_name, Collection<String> current_names) { |
30 | 22 |
|
31 | 23 |
int len = source_name.length(); |
32 |
if (len <= this.maxNameLen) { |
|
24 |
if (source_name.isEmpty()) { |
|
25 |
source_name = "Field"; |
|
26 |
} |
|
27 |
if (len <= this.maxNameLen && !current_names.contains(source_name)) { |
|
33 | 28 |
/* |
34 | 29 |
* Should not happen |
35 | 30 |
*/ |
36 | 31 |
return source_name; |
37 | 32 |
} |
38 |
String target_name; |
|
33 |
String target_name = source_name;
|
|
39 | 34 |
/* |
40 | 35 |
* GDAL field name truncation method (extended from 100 to 255) |
41 | 36 |
* THISISAVERYLONGNAME => THISISAVER, THISISAV_1 ... THISISAV_9, |
... | ... | |
43 | 38 |
* (255 = max number of fields in a SHP) |
44 | 39 |
*/ |
45 | 40 |
for (int i = 0; i < 255; i++) { |
46 |
if (i == 0) { |
|
47 |
target_name = source_name.substring(0, 10); |
|
48 |
} else if (i <= 9) { |
|
49 |
target_name = source_name.substring(0, 8) + "_" + i; |
|
50 |
} else if (i <= 99) { |
|
51 |
target_name = source_name.substring(0, 8) + i; |
|
41 |
if (len <= this.maxNameLen) { |
|
42 |
if (i <= 9) { |
|
43 |
if (len == this.maxNameLen) { |
|
44 |
target_name = source_name.substring(0, 8) + "_" + i; |
|
45 |
} else { |
|
46 |
target_name = source_name + "_" + i; |
|
47 |
} |
|
48 |
} else if (i <= 99) { |
|
49 |
if (len == this.maxNameLen) { |
|
50 |
target_name = source_name.substring(0, 8) + i;; |
|
51 |
} else { |
|
52 |
target_name = source_name + i; |
|
53 |
} |
|
54 |
} else { |
|
55 |
if (len == this.maxNameLen-1) { |
|
56 |
target_name = source_name.substring(0, 7) + i;; |
|
57 |
} else { |
|
58 |
target_name = source_name + i; |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
52 | 62 |
} else { |
53 |
target_name = source_name.substring(0, 7) + i; |
|
63 |
if (i == 0) { |
|
64 |
target_name = source_name.substring(0, 10); |
|
65 |
} else if (i <= 9) { |
|
66 |
target_name = source_name.substring(0, 8) + "_" + i; |
|
67 |
} else if (i <= 99) { |
|
68 |
target_name = source_name.substring(0, 8) + i; |
|
69 |
} else { |
|
70 |
target_name = source_name.substring(0, 7) + i; |
|
71 |
} |
|
54 | 72 |
} |
55 | 73 |
if (!current_names.contains(target_name)) { |
56 | 74 |
return target_name; |
57 | 75 |
} |
76 |
|
|
58 | 77 |
} |
59 | 78 |
/* |
60 | 79 |
* Should not get here |
... | ... | |
65 | 84 |
@Override |
66 | 85 |
public boolean isValidName(ExportAttributes attributes, int index, String name) { |
67 | 86 |
ExportAttribute selectedAttr = attributes.get(index); |
87 |
// |
|
88 |
String selfName = this.getNameSuggestion(attributes, index, name); |
|
89 |
if (!selfName.equals(name)) { |
|
90 |
return false; |
|
91 |
} |
|
92 |
|
|
93 |
// check with others values |
|
68 | 94 |
boolean valid = true; |
69 | 95 |
for (int i = 0; i < attributes.size(); i++) { |
70 | 96 |
if (i == index) { |
71 | 97 |
continue; |
72 | 98 |
} |
73 | 99 |
ExportAttribute attr = attributes.get(i); |
74 |
if (attr.getNewName() == name) { |
|
100 |
String newName = attr.getNewName(); |
|
101 |
if (newName.isEmpty()) { |
|
75 | 102 |
return false; |
76 | 103 |
} |
77 |
if (attr.getNewName() == this.getNameSuggestion(attributes, index, name)) {
|
|
104 |
if (newName.equals(name)) {
|
|
78 | 105 |
return false; |
79 | 106 |
} |
107 |
if (newName.equals(this.getNameSuggestion(attributes, index, name))) { |
|
108 |
return false; |
|
109 |
} |
|
80 | 110 |
} |
81 | 111 |
return valid; |
82 | 112 |
} |
... | ... | |
84 | 114 |
@Override |
85 | 115 |
public String getNameSuggestion(ExportAttributes attributes, int index, String name) { |
86 | 116 |
ArrayList names = new ArrayList(); |
117 |
boolean oneTime = true; |
|
87 | 118 |
for (ExportAttribute attribute : attributes) { |
88 |
if (attribute.getNewName() == name) { |
|
119 |
if (attribute.getNewName().equals(name) && oneTime) { |
|
120 |
oneTime = false; |
|
89 | 121 |
continue; |
90 | 122 |
} |
91 | 123 |
names.add(attribute.getNewName()); |
Also available in: Unified diff