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