~ubuntu-branches/ubuntu/oneiric/mkgmap/oneiric

« back to all changes in this revision

Viewing changes to src/uk/me/parabola/mkgmap/build/LocatorConfig.java

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine, Andreas Putzo, Francesco Paolo Lovergine
  • Date: 2009-07-16 11:10:16 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090716111016-yycxqya1f26xmti7
Tags: 0.0.0+svn1067-1
[ Andreas Putzo ]
* New upstream snapshot.
* Added ${misc:Depends} among dependencies to fix a lintian warning.
* Bumped debhelper compatibility level to 7.
* Updated long description.
* Updated Homepage in debian/control, debian/copyright, debian/watch.
* Added numerous files from /doc to debian/docs.
* Mentioned Bernhard Heibler in debian/copyright and updated copyright
  year of software and packaging.
* Bumped policy to 3.8.2, without changes.
* Added DM-Upload-Allowed to debian/control.

[ Francesco Paolo Lovergine ]
* Added me as Uploader to avoid possible inappropriate NMU notices.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009 Bernhard Heibler
 
3
 * 
 
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.
 
7
 * 
 
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.
 
12
 * 
 
13
 *  The Locator tries to fill missing country, region, postal coude information
 
14
 *
 
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
 
17
 * 
 
18
 * Author: Bernhard Heibler
 
19
 * Create date: 02-Jan-2009
 
20
 */
 
21
package uk.me.parabola.mkgmap.build;
 
22
 
 
23
import java.io.FileInputStream;
 
24
import java.io.InputStream;
 
25
import java.util.HashMap;
 
26
import java.util.Map;
 
27
 
 
28
import javax.xml.parsers.DocumentBuilder;
 
29
import javax.xml.parsers.DocumentBuilderFactory;
 
30
 
 
31
import org.w3c.dom.Document;
 
32
import org.w3c.dom.NamedNodeMap;
 
33
import org.w3c.dom.Node;
 
34
 
 
35
public class LocatorConfig {
 
36
 
 
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>();
 
43
 
 
44
 
 
45
        public LocatorConfig()
 
46
        {
 
47
                loadConfig("/LocatorConfig.xml");
 
48
        }
 
49
 
 
50
        private void loadConfig(String fileName)
 
51
        {
 
52
                try 
 
53
                {
 
54
                        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 
55
 
 
56
                        InputStream inStream;
 
57
 
 
58
                        try
 
59
                        {
 
60
                                inStream = new FileInputStream("resources/" + fileName);
 
61
                        }
 
62
                        catch (Exception ex)
 
63
                        {
 
64
                                inStream = null;
 
65
                        }
 
66
 
 
67
                        if(inStream == null)    // If not loaded from disk use from jar file
 
68
                                inStream = this.getClass().getResourceAsStream(fileName);
 
69
 
 
70
                        Document document = builder.parse(inStream);
 
71
                
 
72
                        Node rootNode = document.getDocumentElement();
 
73
                        
 
74
                        if(rootNode.getNodeName().equals("locator"))
 
75
                        {
 
76
                                  Node cNode = rootNode.getFirstChild();
 
77
 
 
78
                                        while(cNode != null)
 
79
                                        {
 
80
                                                if(cNode.getNodeName().equals("continent"))
 
81
                                                {
 
82
                                                        NamedNodeMap attr = cNode.getAttributes();
 
83
                                                        Node nameTag;
 
84
        
 
85
                                                        if(attr != null)
 
86
                                                        {
 
87
                                                                nameTag = attr.getNamedItem("name");
 
88
                                                                if(nameTag != null)
 
89
                                                                        addContinent(nameTag.getNodeValue());
 
90
                                                        }
 
91
 
 
92
                                                }
 
93
 
 
94
                                                if(cNode.getNodeName().equals("country"))
 
95
                                                {
 
96
                                                        NamedNodeMap attr = cNode.getAttributes();
 
97
                                                        Node nameTag = null;
 
98
 
 
99
                                                        if(attr != null)
 
100
                                                        {
 
101
                                                                nameTag = attr.getNamedItem("name");
 
102
                                                                
 
103
                                                                Node abrTag = attr.getNamedItem("abr");
 
104
 
 
105
                                                                if(abrTag != null && nameTag != null)
 
106
                                                                        addAbr(nameTag.getNodeValue(),abrTag.getNodeValue());
 
107
                                                                
 
108
                                                                if(abrTag == null && nameTag != null)                                   
 
109
                                                                        addAbr(nameTag.getNodeValue(),"");
 
110
 
 
111
                                                                Node geoTag = attr.getNamedItem("geodb");
 
112
 
 
113
                                                                if(nameTag != null && geoTag != null)
 
114
                                                                {
 
115
                                                                        if(geoTag.getNodeValue().equals("1"))
 
116
                                                                                addOpenGeoDb(nameTag.getNodeValue());
 
117
                                                                }
 
118
 
 
119
                                                                Node regionOffsetTag = attr.getNamedItem("regionOffset");
 
120
 
 
121
                                                                if(regionOffsetTag != null && nameTag != null)
 
122
                                                                {
 
123
                                                                        addRegionOffset(nameTag.getNodeValue(),Integer.parseInt(regionOffsetTag.getNodeValue()));
 
124
                                                                }
 
125
 
 
126
                                                                Node poiDispTag = attr.getNamedItem("poiDispFlag");
 
127
 
 
128
                                                                if(poiDispTag != null && nameTag != null)
 
129
                                                                {
 
130
                                                                        addPoiDispTag(nameTag.getNodeValue(),Integer.decode(poiDispTag.getNodeValue()));
 
131
                                                                }
 
132
                                                        }
 
133
 
 
134
                                                        Node cEntryNode = cNode.getFirstChild();
 
135
                                                        while(cEntryNode != null)
 
136
                                                        {
 
137
                                                                if(cEntryNode.getNodeName().equals("variant"))
 
138
                                                                {
 
139
                                                                        Node nodeText = cEntryNode.getFirstChild();
 
140
 
 
141
                                                                        if(nodeText != null && nameTag != null)
 
142
                                                                                addVariant(nameTag.getNodeValue(), nodeText.getNodeValue());
 
143
                                                                                
 
144
                                                                }
 
145
                                                                cEntryNode = cEntryNode.getNextSibling();
 
146
                                                        }
 
147
                                                }
 
148
 
 
149
                                                cNode = cNode.getNextSibling();
 
150
                                        }
 
151
                        }
 
152
                        else
 
153
                        {
 
154
                                System.out.println(fileName + "contains invalid root tag " + rootNode.getNodeName());
 
155
                        }
 
156
        }
 
157
                catch (Exception ex)
 
158
                {
 
159
                        ex.printStackTrace();
 
160
                        //System.out.println("Something is wrong here");
 
161
                }
 
162
        }
 
163
 
 
164
        private void addVariant(String country, String variant)
 
165
        {
 
166
                String cStr = country.toUpperCase().trim();
 
167
                String vStr = variant.toUpperCase().trim();
 
168
 
 
169
                //System.out.println(vStr + " -> " + cStr);
 
170
 
 
171
                variantMap.put(vStr,cStr);
 
172
        }
 
173
 
 
174
        private void addAbr(String country, String abr)
 
175
        {
 
176
                String cStr = country.toUpperCase().trim();
 
177
                String aStr = abr.toUpperCase().trim();
 
178
 
 
179
                //System.out.println(cStr + " -> " + aStr);
 
180
 
 
181
                abrMap.put(cStr,aStr);
 
182
        }
 
183
 
 
184
        private void addRegionOffset(String country, Integer offset)
 
185
        {
 
186
                String cStr = country.toUpperCase().trim();
 
187
 
 
188
                //System.out.println(cStr + " -> " + offset);
 
189
 
 
190
                regOffsetMap.put(cStr,offset);
 
191
        }
 
192
 
 
193
        private void addPoiDispTag(String country, Integer flag)
 
194
        {
 
195
                String cStr = country.toUpperCase().trim();
 
196
 
 
197
                //System.out.println(cStr + " -> " + flag);
 
198
 
 
199
                poiDispFlagMap.put(cStr,flag);
 
200
        }
 
201
 
 
202
        private void addOpenGeoDb(String country)
 
203
        {
 
204
                String cStr = country.toUpperCase().trim();
 
205
                
 
206
                //System.out.println(cStr + " openGeoDb");
 
207
                
 
208
                geoDbMap.put(cStr,true);
 
209
                
 
210
        }
 
211
 
 
212
        private void addContinent(String continent)
 
213
        {
 
214
                String cStr = continent.toUpperCase().trim();
 
215
                
 
216
                //System.out.println(cStr + " continent");
 
217
                
 
218
                continentMap.put(cStr,true);
 
219
                
 
220
        }
 
221
 
 
222
 
 
223
        public void setDefaultCountry(String country, String abbr)
 
224
        {
 
225
                addAbr(country, abbr);
 
226
        }
 
227
 
 
228
        public String fixCountryString(String country)
 
229
        {
 
230
                String cStr = country.toUpperCase().trim();
 
231
                
 
232
                String fixedString = variantMap.get(cStr);
 
233
 
 
234
                if(fixedString != null)
 
235
                        return fixedString;
 
236
                else
 
237
                        return(cStr);
 
238
        }
 
239
 
 
240
        public String isCountry(String country)
 
241
        {
 
242
                String cStr = fixCountryString(country);
 
243
 
 
244
                if(getCountryCode(cStr) != null)
 
245
                        return cStr;
 
246
                else
 
247
                        return null;
 
248
        
 
249
        }
 
250
 
 
251
        public String getCountryCode(String country)
 
252
        {
 
253
                String cStr = country.toUpperCase().trim();
 
254
                return abrMap.get(cStr);
 
255
        }
 
256
 
 
257
        public int getRegionOffset(String country)
 
258
        {
 
259
                String cStr = country.toUpperCase().trim();
 
260
                
 
261
                Integer regOffset = regOffsetMap.get(cStr);
 
262
 
 
263
                if(regOffset != null)
 
264
                        return regOffset;
 
265
                else
 
266
                        return 1; // Default is 1 the next string after before country
 
267
        }
 
268
 
 
269
        public int getPoiDispFlag(String country)
 
270
        {
 
271
                String cStr = country.toUpperCase().trim();
 
272
                
 
273
                Integer flag = poiDispFlagMap.get(cStr);
 
274
 
 
275
                if(flag != null)
 
276
                        return flag;
 
277
                else
 
278
                        return 0; // Default is 1 the next string after before country
 
279
        }
 
280
 
 
281
        public boolean isOpenGeoDBCountry(String country)
 
282
        {
 
283
                // Countries that have open geo db data in osm
 
284
                // Right now this are only germany, austria and swizerland
 
285
 
 
286
                String cStr = country.toUpperCase().trim();
 
287
 
 
288
                if(geoDbMap.get(cStr) != null)
 
289
                        return true;
 
290
                
 
291
                return false;
 
292
        }
 
293
 
 
294
        public boolean isContinent(String continent)
 
295
        {
 
296
                String s = continent.toUpperCase().trim();
 
297
 
 
298
                if(continentMap.get(s) != null)
 
299
                        return(true);
 
300
 
 
301
                return false;
 
302
        }               
 
303
}
 
304