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 @ 44386
History | View | Annotate | Download (4.73 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 |
|
11 |
public class CutAttributeNamesTranslator implements AttributeNamesTranslator { |
12 |
|
13 |
public final int maxNameLen; |
14 |
|
15 |
public CutAttributeNamesTranslator(int maxNameLen) { |
16 |
this.maxNameLen = maxNameLen;
|
17 |
} |
18 |
|
19 |
private String buildTranslatedName(String source_name, Collection<String> current_names) { |
20 |
|
21 |
int len = source_name.length();
|
22 |
if (source_name.isEmpty()) {
|
23 |
source_name = "Field";
|
24 |
} |
25 |
if (len <= this.maxNameLen && !current_names.contains(source_name)) { |
26 |
/*
|
27 |
* Should not happen
|
28 |
*/
|
29 |
return source_name;
|
30 |
} |
31 |
String target_name = source_name;
|
32 |
/*
|
33 |
* GDAL field name truncation method (extended from 100 to 255)
|
34 |
* THISISAVERYLONGNAME => THISISAVER, THISISAV_1 ... THISISAV_9,
|
35 |
* THISISAV10 ... THISISAV99, THISISA100 ... THISISA255
|
36 |
* (255 = max number of fields in a SHP)
|
37 |
*/
|
38 |
for (int i = 0; i < 255; i++) { |
39 |
if (len <= this.maxNameLen) { |
40 |
if (i <= 9) { |
41 |
if (len == this.maxNameLen) { |
42 |
target_name = source_name.substring(0, 8) + "_" + i; |
43 |
} else {
|
44 |
target_name = source_name + "_" + i;
|
45 |
} |
46 |
} else if (i <= 99) { |
47 |
if (len == this.maxNameLen) { |
48 |
target_name = source_name.substring(0, 8) + i;; |
49 |
} else {
|
50 |
target_name = source_name + i; |
51 |
} |
52 |
} else {
|
53 |
if (len == this.maxNameLen-1) { |
54 |
target_name = source_name.substring(0, 7) + i;; |
55 |
} else {
|
56 |
target_name = source_name + i; |
57 |
} |
58 |
} |
59 |
|
60 |
} else {
|
61 |
if (i == 0) { |
62 |
target_name = source_name.substring(0, 10); |
63 |
} else if (i <= 9) { |
64 |
target_name = source_name.substring(0, 8) + "_" + i; |
65 |
} else if (i <= 99) { |
66 |
target_name = source_name.substring(0, 8) + i; |
67 |
} else {
|
68 |
target_name = source_name.substring(0, 7) + i; |
69 |
} |
70 |
} |
71 |
if (!current_names.contains(target_name)) {
|
72 |
return target_name;
|
73 |
} |
74 |
|
75 |
} |
76 |
/*
|
77 |
* Should not get here
|
78 |
*/
|
79 |
return source_name.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000); |
80 |
} |
81 |
|
82 |
@Override
|
83 |
public boolean isValidName(ExportAttributes attributes, int index, String name) { |
84 |
ExportAttribute selectedAttr = attributes.get(index); |
85 |
//
|
86 |
String selfName = this.getNameSuggestion(attributes, index, name); |
87 |
if (!selfName.equals(name)) {
|
88 |
return false; |
89 |
} |
90 |
|
91 |
// check with others values
|
92 |
boolean valid = true; |
93 |
for (int i = 0; i < attributes.size(); i++) { |
94 |
if (i == index) {
|
95 |
continue;
|
96 |
} |
97 |
ExportAttribute attr = attributes.get(i); |
98 |
String newName = attr.getNewName();
|
99 |
if (newName.isEmpty()) {
|
100 |
return false; |
101 |
} |
102 |
if (newName.equals(name)) {
|
103 |
return false; |
104 |
} |
105 |
if (newName.equals(this.getNameSuggestion(attributes, index, name))) { |
106 |
return false; |
107 |
} |
108 |
} |
109 |
return valid;
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public String getNameSuggestion(ExportAttributes attributes, int index, String name) { |
114 |
ArrayList names = new ArrayList(); |
115 |
boolean oneTime = true; |
116 |
for (ExportAttribute attribute : attributes) {
|
117 |
if (attribute.getNewName().equals(name) && oneTime) {
|
118 |
oneTime = false;
|
119 |
continue;
|
120 |
} |
121 |
names.add(attribute.getNewName()); |
122 |
} |
123 |
String target_name = buildTranslatedName(name, names);
|
124 |
return target_name;
|
125 |
} |
126 |
|
127 |
@Override
|
128 |
public CutAttributeNamesTranslator clone() {
|
129 |
CutAttributeNamesTranslator clone = null;
|
130 |
try {
|
131 |
clone = ( CutAttributeNamesTranslator)super.clone();
|
132 |
} catch (CloneNotSupportedException ex) { |
133 |
Logger.getLogger(CutAttributeNamesTranslator.class.getName()).log(Level.SEVERE, null, ex); |
134 |
} |
135 |
|
136 |
return clone;
|
137 |
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public String getNameSuggestion(String name) { |
142 |
String s = StringUtils.left(name, maxNameLen);
|
143 |
return s;
|
144 |
} |
145 |
} |