2
* Copyright (C) 2009 Bernhard Heibler
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 2 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* The Locator tries to fill missing country, region, postal coude information
15
* To do so we analyse the is_in information and if this doesn't helps us we
16
* try to get info from the next known city
18
* Author: Bernhard Heibler
19
* Create date: 02-Jan-2009
21
package uk.me.parabola.mkgmap.build;
23
import java.io.FileInputStream;
24
import java.io.InputStream;
25
import java.util.HashMap;
28
import javax.xml.parsers.DocumentBuilder;
29
import javax.xml.parsers.DocumentBuilderFactory;
31
import org.w3c.dom.Document;
32
import org.w3c.dom.NamedNodeMap;
33
import org.w3c.dom.Node;
35
public class LocatorConfig {
37
private final Map<String,String> variantMap = new HashMap<String,String>();
38
private final Map<String,String> abrMap = new HashMap<String,String>();
39
private final Map<String,Boolean> geoDbMap = new HashMap<String,Boolean>();
40
private final Map<String,Integer> regOffsetMap = new HashMap<String,Integer>();
41
private final Map<String,Integer> poiDispFlagMap = new HashMap<String,Integer>();
42
private final Map<String,Boolean> continentMap = new HashMap<String,Boolean>();
45
public LocatorConfig()
47
loadConfig("/LocatorConfig.xml");
50
private void loadConfig(String fileName)
54
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
60
inStream = new FileInputStream("resources/" + fileName);
67
if(inStream == null) // If not loaded from disk use from jar file
68
inStream = this.getClass().getResourceAsStream(fileName);
70
Document document = builder.parse(inStream);
72
Node rootNode = document.getDocumentElement();
74
if(rootNode.getNodeName().equals("locator"))
76
Node cNode = rootNode.getFirstChild();
80
if(cNode.getNodeName().equals("continent"))
82
NamedNodeMap attr = cNode.getAttributes();
87
nameTag = attr.getNamedItem("name");
89
addContinent(nameTag.getNodeValue());
94
if(cNode.getNodeName().equals("country"))
96
NamedNodeMap attr = cNode.getAttributes();
101
nameTag = attr.getNamedItem("name");
103
Node abrTag = attr.getNamedItem("abr");
105
if(abrTag != null && nameTag != null)
106
addAbr(nameTag.getNodeValue(),abrTag.getNodeValue());
108
if(abrTag == null && nameTag != null)
109
addAbr(nameTag.getNodeValue(),"");
111
Node geoTag = attr.getNamedItem("geodb");
113
if(nameTag != null && geoTag != null)
115
if(geoTag.getNodeValue().equals("1"))
116
addOpenGeoDb(nameTag.getNodeValue());
119
Node regionOffsetTag = attr.getNamedItem("regionOffset");
121
if(regionOffsetTag != null && nameTag != null)
123
addRegionOffset(nameTag.getNodeValue(),Integer.parseInt(regionOffsetTag.getNodeValue()));
126
Node poiDispTag = attr.getNamedItem("poiDispFlag");
128
if(poiDispTag != null && nameTag != null)
130
addPoiDispTag(nameTag.getNodeValue(),Integer.decode(poiDispTag.getNodeValue()));
134
Node cEntryNode = cNode.getFirstChild();
135
while(cEntryNode != null)
137
if(cEntryNode.getNodeName().equals("variant"))
139
Node nodeText = cEntryNode.getFirstChild();
141
if(nodeText != null && nameTag != null)
142
addVariant(nameTag.getNodeValue(), nodeText.getNodeValue());
145
cEntryNode = cEntryNode.getNextSibling();
149
cNode = cNode.getNextSibling();
154
System.out.println(fileName + "contains invalid root tag " + rootNode.getNodeName());
159
ex.printStackTrace();
160
//System.out.println("Something is wrong here");
164
private void addVariant(String country, String variant)
166
String cStr = country.toUpperCase().trim();
167
String vStr = variant.toUpperCase().trim();
169
//System.out.println(vStr + " -> " + cStr);
171
variantMap.put(vStr,cStr);
174
private void addAbr(String country, String abr)
176
String cStr = country.toUpperCase().trim();
177
String aStr = abr.toUpperCase().trim();
179
//System.out.println(cStr + " -> " + aStr);
181
abrMap.put(cStr,aStr);
184
private void addRegionOffset(String country, Integer offset)
186
String cStr = country.toUpperCase().trim();
188
//System.out.println(cStr + " -> " + offset);
190
regOffsetMap.put(cStr,offset);
193
private void addPoiDispTag(String country, Integer flag)
195
String cStr = country.toUpperCase().trim();
197
//System.out.println(cStr + " -> " + flag);
199
poiDispFlagMap.put(cStr,flag);
202
private void addOpenGeoDb(String country)
204
String cStr = country.toUpperCase().trim();
206
//System.out.println(cStr + " openGeoDb");
208
geoDbMap.put(cStr,true);
212
private void addContinent(String continent)
214
String cStr = continent.toUpperCase().trim();
216
//System.out.println(cStr + " continent");
218
continentMap.put(cStr,true);
223
public void setDefaultCountry(String country, String abbr)
225
addAbr(country, abbr);
228
public String fixCountryString(String country)
230
String cStr = country.toUpperCase().trim();
232
String fixedString = variantMap.get(cStr);
234
if(fixedString != null)
240
public String isCountry(String country)
242
String cStr = fixCountryString(country);
244
if(getCountryCode(cStr) != null)
251
public String getCountryCode(String country)
253
String cStr = country.toUpperCase().trim();
254
return abrMap.get(cStr);
257
public int getRegionOffset(String country)
259
String cStr = country.toUpperCase().trim();
261
Integer regOffset = regOffsetMap.get(cStr);
263
if(regOffset != null)
266
return 1; // Default is 1 the next string after before country
269
public int getPoiDispFlag(String country)
271
String cStr = country.toUpperCase().trim();
273
Integer flag = poiDispFlagMap.get(cStr);
278
return 0; // Default is 1 the next string after before country
281
public boolean isOpenGeoDBCountry(String country)
283
// Countries that have open geo db data in osm
284
// Right now this are only germany, austria and swizerland
286
String cStr = country.toUpperCase().trim();
288
if(geoDbMap.get(cStr) != null)
294
public boolean isContinent(String continent)
296
String s = continent.toUpperCase().trim();
298
if(continentMap.get(s) != null)