001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     */
015    
016    package org.chenillekit.google.utils.geocode;
017    
018    import org.chenillekit.google.utils.JSONException;
019    import org.chenillekit.google.utils.JSONObject;
020    
021    /**
022     * @version $Id: AddressDetails.java 351 2008-11-25 12:40:18Z homburgs $
023     */
024    public class AddressDetails
025    {
026            private Integer accuracy;
027            private String administrativeAreaName;
028            private String dependentLocalityName;
029            private String postalCodeNumber;
030            private String thoroughfareName;
031            private String localityName;
032            private String subAdministrativeAreaName;
033            private String countryNameCode;
034    
035            public AddressDetails(JSONObject json)
036            {
037                    buildFromJSON(json);
038            }
039    
040            private void buildFromJSON(JSONObject json)
041            {
042                    try
043                    {
044                            accuracy = (Integer) getValue(json, "Accuracy");
045                            administrativeAreaName = (String) getValue(json, "Country", "AdministrativeArea", "AdministrativeAreaName");
046                            countryNameCode = (String) getValue(json, "Country", "CountryNameCode");
047    
048                            dependentLocalityName = (String) getValue(json, "Country", "AdministrativeArea", "SubAdministrativeArea", "Locality",
049                                                                                                              "DependentLocality", "DependentLocalityName");
050                            postalCodeNumber = (String) getValue(json, "Country", "AdministrativeArea", "SubAdministrativeArea", "Locality",
051                                                                                                     "DependentLocality", "PostalCode", "PostalCodeNumber");
052                            thoroughfareName = (String) getValue(json, "Country", "AdministrativeArea", "SubAdministrativeArea", "Locality",
053                                                                                                     "DependentLocality", "Thoroughfare", "ThoroughfareName");
054    
055                            localityName = (String) getValue(json, "Country", "AdministrativeArea", "SubAdministrativeArea", "Locality",
056                                                                                             "LocalityName");
057                            subAdministrativeAreaName = (String) getValue(json, "Country", "AdministrativeArea", "SubAdministrativeArea",
058                                                                                                                      "SubAdministrativeAreaName");
059                    }
060                    catch (JSONException e)
061                    {
062                            throw new RuntimeException(e);
063                    }
064            }
065    
066            public int getAccuracy()
067            {
068                    return accuracy;
069            }
070    
071            public String getAdministrativeAreaName()
072            {
073                    return administrativeAreaName;
074            }
075    
076            public String getDependentLocalityName()
077            {
078                    return dependentLocalityName;
079            }
080    
081            public String getPostalCodeNumber()
082            {
083                    return postalCodeNumber;
084            }
085    
086            public String getThoroughfareName()
087            {
088                    return thoroughfareName;
089            }
090    
091            public String getLocalityName()
092            {
093                    return localityName;
094            }
095    
096            public String getSubAdministrativeAreaName()
097            {
098                    return subAdministrativeAreaName;
099            }
100    
101            public String getCountryNameCode()
102            {
103                    return countryNameCode;
104            }
105    
106            public Object getValue(JSONObject jsonObject, String... keys)
107            {
108                    JSONObject tempObject = jsonObject;
109                    Object value = null;
110    
111                    for (int i = 0; i < keys.length; i++)
112                    {
113                            String key = keys[i];
114                            if (!tempObject.has(key))
115                                    break;
116    
117                            if ((i + 1) == keys.length)
118                                    value = tempObject.get(key);
119                            else
120                                    tempObject = tempObject.getJSONObject(key);
121    
122                    }
123    
124                    return value;
125            }
126    }