1 |
package dk.daoas.daoadresseservice;
|
2 |
|
3 |
import org.apache.commons.lang3.StringUtils;
|
4 |
import org.apache.commons.lang3.text.WordUtils;
|
5 |
|
6 |
public class AddressUtils {
|
7 |
|
8 |
public static class SplitResult {
|
9 |
public String vej = "";
|
10 |
public String husnr = "";
|
11 |
public String litra = "";
|
12 |
public String etage = "";
|
13 |
public String lejlighed = "";
|
14 |
public String resten = "";
|
15 |
|
16 |
@Override
|
17 |
public String toString() {
|
18 |
return vej + " / " + husnr + " / " + litra;
|
19 |
}
|
20 |
}
|
21 |
|
22 |
public static char fjernAccentTegn(char c) {
|
23 |
switch (c) {
|
24 |
case 'é':
|
25 |
return 'e';
|
26 |
case 'è':
|
27 |
return 'e';
|
28 |
|
29 |
case 'û':
|
30 |
return 'u';
|
31 |
case 'ü':
|
32 |
return 'u';
|
33 |
|
34 |
case 'ö':
|
35 |
return 'o';
|
36 |
case 'ô':
|
37 |
return 'o';
|
38 |
|
39 |
case 'ä':
|
40 |
return 'a';
|
41 |
case 'â':
|
42 |
return 'a';
|
43 |
|
44 |
case 'ñ':
|
45 |
return 'n';
|
46 |
|
47 |
default:
|
48 |
return c;
|
49 |
}
|
50 |
}
|
51 |
|
52 |
public static String vaskVejnavn(String indVejnavn) {
|
53 |
StringBuilder sb = new StringBuilder();
|
54 |
char chars[] = indVejnavn.toCharArray();
|
55 |
|
56 |
for (char c: chars) {
|
57 |
if ( Character.isAlphabetic(c)) {
|
58 |
c = Character.toLowerCase(c);
|
59 |
c = fjernAccentTegn(c);
|
60 |
|
61 |
sb.append( c );
|
62 |
} else if ( Character.isDigit(c)) {
|
63 |
sb.append(c);
|
64 |
}
|
65 |
|
66 |
}
|
67 |
|
68 |
return sb.toString();
|
69 |
}
|
70 |
|
71 |
public static String injectIntoBk(String bkrute, String injected) {
|
72 |
if (bkrute == null)
|
73 |
return null;
|
74 |
|
75 |
String parts[] = bkrute.split("/");
|
76 |
|
77 |
if (parts.length != 4) {
|
78 |
System.out.println(">BK " + bkrute);
|
79 |
return bkrute;
|
80 |
}
|
81 |
|
82 |
return parts[0] + "/" + parts[1] + "/" + parts[2] + "/" + injected + parts[3];
|
83 |
}
|
84 |
|
85 |
|
86 |
/*
|
87 |
* Er en 1-1 port af SplitAdresse() fra funcSplitAdresse.php
|
88 |
*/
|
89 |
public static SplitResult splitAdresse(String indAdresse) {
|
90 |
indAdresse = indAdresse.trim();
|
91 |
|
92 |
SplitResult result = new SplitResult();
|
93 |
|
94 |
|
95 |
int i=0;
|
96 |
int len = indAdresse.length();
|
97 |
StringBuilder delStreng = new StringBuilder();
|
98 |
|
99 |
// ignorer blanke og komma og punktum
|
100 |
while (i<len && (indAdresse.charAt(i) == '.' || indAdresse.charAt(i) == ',' || indAdresse.charAt(i) == ' ') ) {
|
101 |
i++;
|
102 |
}
|
103 |
|
104 |
|
105 |
// alle tegn frem til og med første bogstav er også en del af vejnavn
|
106 |
while ((i < len) && Character.isAlphabetic(indAdresse.charAt(i))==false ) {
|
107 |
delStreng.append( indAdresse.charAt(i) );
|
108 |
i++;
|
109 |
}
|
110 |
|
111 |
|
112 |
// alle karakterer indtil det første tal er vejnavn
|
113 |
while ((i < len) && Character.isDigit(indAdresse.charAt(i))==false ) {
|
114 |
delStreng.append( indAdresse.charAt(i) );
|
115 |
i++;
|
116 |
}
|
117 |
|
118 |
|
119 |
result.vej = delStreng.toString().toLowerCase().trim();
|
120 |
if ( result.vej.endsWith(".") || result.vej.endsWith(",")) {
|
121 |
result.vej = result.vej.substring(0, result.vej.length() - 1);
|
122 |
result.vej = result.vej.trim();
|
123 |
}
|
124 |
|
125 |
//special case - hvis strengen ender med ' nr' skal ' nr' fjernes
|
126 |
if ( result.vej.endsWith(" nr")) {
|
127 |
result.vej = result.vej.substring(0, result.vej.length() - 3);
|
128 |
}
|
129 |
|
130 |
result.vej = WordUtils.capitalize( result.vej.trim() );
|
131 |
|
132 |
|
133 |
|
134 |
// første tal er husnummer
|
135 |
delStreng = new StringBuilder();
|
136 |
while ((i < len) && Character.isDigit(indAdresse.charAt(i))==true ) {
|
137 |
delStreng.append( indAdresse.charAt(i) );
|
138 |
i++;
|
139 |
}
|
140 |
result.husnr = delStreng.toString();
|
141 |
|
142 |
// ignorer blanke og komma og punktum
|
143 |
while (i<len && (indAdresse.charAt(i) == '.' || indAdresse.charAt(i) == ',' || indAdresse.charAt(i) == ' ') ) {
|
144 |
i++;
|
145 |
}
|
146 |
|
147 |
/*
|
148 |
// hvis næste delstreng er et enkelt bogstav så er det husbogstavet
|
149 |
delStreng = new StringBuilder();
|
150 |
while (i<len && Character.isDigit(indAdresse.charAt(i))==false && indAdresse.charAt(i) == '.' && indAdresse.charAt(i) != ',' && indAdresse.charAt(i) != ' ' ) {
|
151 |
delStreng.append( indAdresse.charAt(i) );
|
152 |
i++;
|
153 |
}
|
154 |
|
155 |
System.out.println(indAdresse + " >" + delStreng + "< " +i);
|
156 |
|
157 |
if (delStreng.length() == 1) {
|
158 |
|
159 |
result.litra = delStreng.toString().toUpperCase();
|
160 |
if ( !Character.isAlphabetic(result.litra.charAt(0))) {
|
161 |
result.litra = ""; //$HusBogstav må KUN være A-Z
|
162 |
}
|
163 |
delStreng = new StringBuilder();
|
164 |
|
165 |
// ignorer blanke og komma og punktum
|
166 |
while (i<len && (indAdresse.charAt(i) == '.' || indAdresse.charAt(i) == ',' || indAdresse.charAt(i) == ' ') ) {
|
167 |
i++;
|
168 |
}
|
169 |
}
|
170 |
*/
|
171 |
|
172 |
if (i>=len) //THN tilføjelse
|
173 |
return result;
|
174 |
|
175 |
if (Character.isAlphabetic(indAdresse.charAt(i) )) {
|
176 |
char current = Character.toUpperCase( indAdresse.charAt(i) );
|
177 |
|
178 |
if (current != 'Æ'&& current != 'Ø' && current != 'Å') {
|
179 |
if ( (i+1 == len ||
|
180 |
(Character.isAlphabetic(indAdresse.charAt(i+1) ) == false
|
181 |
/*&& indAdresse.charAt(i+1) != '.'*/
|
182 |
|
183 |
&& checkCo(indAdresse,i) == false // Fra sortér C/O
|
184 |
)
|
185 |
)
|
186 |
) {
|
187 |
result.litra = "" + current;
|
188 |
i++;
|
189 |
}
|
190 |
}
|
191 |
}
|
192 |
|
193 |
// ignorer blanke og komma og punktum
|
194 |
while (i<len && (indAdresse.charAt(i) == '.' || indAdresse.charAt(i) == ',' || indAdresse.charAt(i) == ' ') ) {
|
195 |
i++;
|
196 |
}
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
// første tal er etage (eller evt. st er stuen)
|
202 |
if (delStreng.toString().toLowerCase().equals("st")) {
|
203 |
result.etage = delStreng.toString().toLowerCase();
|
204 |
}
|
205 |
else {
|
206 |
delStreng = new StringBuilder();
|
207 |
String nextTwo = StringUtils.substring(indAdresse, i, i+2).toLowerCase();
|
208 |
if ( nextTwo.equals("st") ) {
|
209 |
result.etage = nextTwo;
|
210 |
i += nextTwo.length();
|
211 |
}
|
212 |
else {
|
213 |
while ((i < len) && Character.isDigit(indAdresse.charAt(i))==true ) {
|
214 |
delStreng.append( indAdresse.charAt(i) );
|
215 |
i++;
|
216 |
}
|
217 |
result.etage = delStreng.toString();
|
218 |
}
|
219 |
}
|
220 |
|
221 |
|
222 |
// Hvis vi fandt en etage angivelse skal vi også kigge efter lejlighed/side
|
223 |
if (result.etage.length() > 0 ) {
|
224 |
|
225 |
// ignorer blanke og komma og punktum
|
226 |
while (i<len && (indAdresse.charAt(i) == '.' || indAdresse.charAt(i) == ',' || indAdresse.charAt(i) == ' ') ) {
|
227 |
i++;
|
228 |
}
|
229 |
|
230 |
|
231 |
String next = StringUtils.substring(indAdresse, i, i+3).toLowerCase();
|
232 |
// Ignorer hvis næste info er 'sal'
|
233 |
if (next.equals("sal")) {
|
234 |
i += 3;
|
235 |
|
236 |
// ignorer blanke og komma og punktum
|
237 |
while (i<len && (indAdresse.charAt(i) == '.' || indAdresse.charAt(i) == ',' || indAdresse.charAt(i) == ' ') ) {
|
238 |
i++;
|
239 |
}
|
240 |
}
|
241 |
|
242 |
next = StringUtils.substring(indAdresse, i, i+3).toLowerCase();
|
243 |
// Ignorer hvis næste info er 'dør'
|
244 |
if (next.equals("dør")) {
|
245 |
i += 3;
|
246 |
|
247 |
// ignorer blanke og komma og punktum
|
248 |
while (i<len && (indAdresse.charAt(i) == '.' || indAdresse.charAt(i) == ',' || indAdresse.charAt(i) == ' ') ) {
|
249 |
i++;
|
250 |
}
|
251 |
}
|
252 |
|
253 |
// Ignorer hvis næste info er 'lejlighed' eller 'lejl.' eller 'Lej'
|
254 |
next = StringUtils.substring(indAdresse, i, i+9).toLowerCase();
|
255 |
if (next.equals("lejlighed") ) {
|
256 |
i += 9;
|
257 |
}
|
258 |
next = StringUtils.substring(indAdresse, i, i+4).toLowerCase();
|
259 |
if (next.equals("lejl") ) {
|
260 |
i += 4;
|
261 |
}
|
262 |
next = StringUtils.substring(indAdresse, i, i+3).toLowerCase();
|
263 |
if (next.equals("lej") ) {
|
264 |
i += 3;
|
265 |
}
|
266 |
|
267 |
// ignorer blanke og komma og punktum
|
268 |
while (i<len && (indAdresse.charAt(i) == '.' || indAdresse.charAt(i) == ',' || indAdresse.charAt(i) == ' ') ) {
|
269 |
i++;
|
270 |
}
|
271 |
|
272 |
// alle karakterer indtil det første blanke er lejlighed/side
|
273 |
delStreng = new StringBuilder();
|
274 |
while (i<len && indAdresse.charAt(i) != ' ') {
|
275 |
delStreng.append( indAdresse.charAt(i) );
|
276 |
i++;
|
277 |
}
|
278 |
result.lejlighed = delStreng.toString().toLowerCase();
|
279 |
result.lejlighed = result.lejlighed.replace(".", "");
|
280 |
}
|
281 |
|
282 |
// saml resten op (kan gøres smartere med en substr)
|
283 |
result.resten = indAdresse.substring(i).trim();
|
284 |
|
285 |
return result;
|
286 |
}
|
287 |
|
288 |
public static boolean checkCo(String addr, int pos) {
|
289 |
|
290 |
if ( StringUtils.substring(addr, pos, pos+3).equalsIgnoreCase("C/O") )
|
291 |
return true;
|
292 |
|
293 |
return false;
|
294 |
|
295 |
}
|
296 |
|
297 |
}
|
298 |
|
299 |
|