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 | 44386 | omartinez | package org.gvsig.export.spi; |
---|---|---|---|
2 | 43504 | jjdelcerro | |
3 | 44270 | omartinez | import java.util.ArrayList; |
4 | 43504 | jjdelcerro | import java.util.Collection; |
5 | 44386 | omartinez | import java.util.logging.Level; |
6 | import java.util.logging.Logger; |
||
7 | import org.apache.commons.lang3.StringUtils; |
||
8 | 44270 | omartinez | import org.gvsig.export.ExportAttributes; |
9 | import org.gvsig.export.ExportAttributes.ExportAttribute; |
||
10 | 43504 | jjdelcerro | |
11 | 44386 | omartinez | public class CutAttributeNamesTranslator implements AttributeNamesTranslator { |
12 | 43504 | jjdelcerro | |
13 | 44386 | omartinez | public final int maxNameLen; |
14 | 44270 | omartinez | |
15 | 44386 | omartinez | public CutAttributeNamesTranslator(int maxNameLen) { |
16 | 43504 | jjdelcerro | this.maxNameLen = maxNameLen;
|
17 | } |
||
18 | |||
19 | 44270 | omartinez | private String buildTranslatedName(String source_name, Collection<String> current_names) { |
20 | 43504 | jjdelcerro | |
21 | 44270 | omartinez | int len = source_name.length();
|
22 | 44300 | omartinez | if (source_name.isEmpty()) {
|
23 | source_name = "Field";
|
||
24 | } |
||
25 | if (len <= this.maxNameLen && !current_names.contains(source_name)) { |
||
26 | 44270 | omartinez | /*
|
27 | 43504 | jjdelcerro | * Should not happen
|
28 | 44270 | omartinez | */
|
29 | return source_name;
|
||
30 | } |
||
31 | 44300 | omartinez | String target_name = source_name;
|
32 | 44270 | omartinez | /*
|
33 | 43504 | jjdelcerro | * 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 | 44270 | omartinez | */
|
38 | for (int i = 0; i < 255; i++) { |
||
39 | 44300 | omartinez | 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 | 43504 | jjdelcerro | } else {
|
61 | 44300 | omartinez | 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 | 43504 | jjdelcerro | } |
71 | 44270 | omartinez | if (!current_names.contains(target_name)) {
|
72 | 43504 | jjdelcerro | return target_name;
|
73 | } |
||
74 | 44300 | omartinez | |
75 | 44270 | omartinez | } |
76 | /*
|
||
77 | 43504 | jjdelcerro | * Should not get here
|
78 | 44270 | omartinez | */
|
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 | 44300 | omartinez | //
|
86 | String selfName = this.getNameSuggestion(attributes, index, name); |
||
87 | if (!selfName.equals(name)) {
|
||
88 | return false; |
||
89 | } |
||
90 | |||
91 | // check with others values
|
||
92 | 44270 | omartinez | 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 | 44300 | omartinez | String newName = attr.getNewName();
|
99 | if (newName.isEmpty()) {
|
||
100 | 44270 | omartinez | return false; |
101 | } |
||
102 | 44300 | omartinez | if (newName.equals(name)) {
|
103 | 44270 | omartinez | return false; |
104 | } |
||
105 | 44300 | omartinez | if (newName.equals(this.getNameSuggestion(attributes, index, name))) { |
106 | return false; |
||
107 | } |
||
108 | 44270 | omartinez | } |
109 | return valid;
|
||
110 | } |
||
111 | |||
112 | @Override
|
||
113 | public String getNameSuggestion(ExportAttributes attributes, int index, String name) { |
||
114 | ArrayList names = new ArrayList(); |
||
115 | 44300 | omartinez | boolean oneTime = true; |
116 | 44270 | omartinez | for (ExportAttribute attribute : attributes) {
|
117 | 44300 | omartinez | if (attribute.getNewName().equals(name) && oneTime) {
|
118 | oneTime = false;
|
||
119 | 44270 | omartinez | continue;
|
120 | } |
||
121 | names.add(attribute.getNewName()); |
||
122 | } |
||
123 | String target_name = buildTranslatedName(name, names);
|
||
124 | return target_name;
|
||
125 | } |
||
126 | 44386 | omartinez | |
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 | 43504 | jjdelcerro | } |