svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.prov / org.gvsig.exportto.swing.prov.jdbc / src / main / java / org / gvsig / export / jdbc / service / ExportJDBCAttributeNamesTranslator.java @ 44300
History | View | Annotate | Download (4.5 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.export.jdbc.service; |
7 |
|
8 |
import java.util.ArrayList; |
9 |
import java.util.Collection; |
10 |
import org.apache.commons.lang3.StringUtils; |
11 |
import org.gvsig.export.ExportAttributes; |
12 |
import org.gvsig.export.spi.AttributeNamesTranslator; |
13 |
|
14 |
/**
|
15 |
*
|
16 |
* @author osc
|
17 |
*/
|
18 |
public class ExportJDBCAttributeNamesTranslator implements AttributeNamesTranslator { |
19 |
|
20 |
private boolean removeSpacesInIdentifiers; |
21 |
private boolean translateIdentifiersToLowerCase; |
22 |
private boolean translateHyphens; |
23 |
|
24 |
public ExportJDBCAttributeNamesTranslator() {
|
25 |
} |
26 |
|
27 |
private String buildTranslatedName(String source_name, Collection<String> current_names) { |
28 |
|
29 |
String target_name = source_name;
|
30 |
if (this.removeSpacesInIdentifiers) { |
31 |
//target_name = target_name.replaceAll("\\s","");
|
32 |
target_name = StringUtils.normalizeSpace(target_name).replace(" ", "_"); |
33 |
} |
34 |
if (this.translateHyphens) { |
35 |
target_name = target_name.replace("-", "_"); |
36 |
} |
37 |
if (this.translateIdentifiersToLowerCase) { |
38 |
target_name = target_name.toLowerCase(); |
39 |
} |
40 |
if (!current_names.contains(target_name)) {
|
41 |
return target_name;
|
42 |
} |
43 |
|
44 |
if (!current_names.contains(target_name)) {
|
45 |
return target_name;
|
46 |
} |
47 |
|
48 |
for (int i = 0; i < 255; i++) { |
49 |
target_name = source_name + i; |
50 |
if (!current_names.contains(target_name)) {
|
51 |
return target_name;
|
52 |
} |
53 |
} |
54 |
return source_name.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000); |
55 |
} |
56 |
|
57 |
@Override
|
58 |
public boolean isValidName(ExportAttributes attributes, int index, String name) { |
59 |
ExportAttributes.ExportAttribute selectedAttr = attributes.get(index); |
60 |
//
|
61 |
String selfName = this.getNameSuggestion(attributes, index, name); |
62 |
if (!selfName.equals(name)) {
|
63 |
return false; |
64 |
} |
65 |
|
66 |
// check with others values
|
67 |
boolean valid = true; |
68 |
for (int i = 0; i < attributes.size(); i++) { |
69 |
if (i == index) {
|
70 |
continue;
|
71 |
} |
72 |
ExportAttributes.ExportAttribute attr = attributes.get(i); |
73 |
String newName = attr.getNewName();
|
74 |
if (newName.equals(name)) {
|
75 |
return false; |
76 |
} |
77 |
if (newName.equals(selfName)) {
|
78 |
return false; |
79 |
} |
80 |
if (newName.isEmpty()) {
|
81 |
return false; |
82 |
} |
83 |
if (this.removeSpacesInIdentifiers) { |
84 |
//if (newName.replaceAll("\\s","") != newName) {
|
85 |
String s = StringUtils.normalizeSpace(newName).replace(" ", "_"); |
86 |
if (!newName.equals(s)) {
|
87 |
return false; |
88 |
} |
89 |
} |
90 |
if (this.translateHyphens) { |
91 |
if (newName.contains("-")) { |
92 |
return false; |
93 |
} |
94 |
} |
95 |
if (this.translateIdentifiersToLowerCase) { |
96 |
if (newName.toLowerCase() != newName) {
|
97 |
return false; |
98 |
} |
99 |
} |
100 |
} |
101 |
return valid;
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public String getNameSuggestion(ExportAttributes attributes, int index, String name) { |
106 |
ArrayList names = new ArrayList(); |
107 |
boolean oneTime = true; |
108 |
for (ExportAttributes.ExportAttribute attribute : attributes) {
|
109 |
if (attribute.getNewName().equals(name) && oneTime) {
|
110 |
oneTime = false;
|
111 |
continue;
|
112 |
} |
113 |
names.add(attribute.getNewName()); |
114 |
} |
115 |
String target_name = buildTranslatedName(name, names);
|
116 |
return target_name;
|
117 |
} |
118 |
|
119 |
|
120 |
public boolean getRemoveSpacesInIdentifiers() { |
121 |
return this.removeSpacesInIdentifiers; |
122 |
} |
123 |
|
124 |
public void setRemoveSpacesInIdentifiers(boolean selected) { |
125 |
this.removeSpacesInIdentifiers = selected;
|
126 |
} |
127 |
|
128 |
public boolean getTranslateIdentifiersToLowerCase() { |
129 |
return this.translateIdentifiersToLowerCase; |
130 |
} |
131 |
|
132 |
public void setTranslateIdentifiersToLowerCase(boolean selected) { |
133 |
this.translateIdentifiersToLowerCase = selected;
|
134 |
} |
135 |
|
136 |
public boolean getTranslateHyphens() { |
137 |
return this.translateHyphens; |
138 |
} |
139 |
public void setTranslateHyphens(boolean selected) { |
140 |
this.translateHyphens = selected;
|
141 |
} |
142 |
|
143 |
} |