1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 //  1. Redistributions of source code must retain the above copyright notice,
7 //     this list of conditions and the following disclaimer.
8 //  2. Redistributions in binary form must reproduce the above copyright notice,
9 //     this list of conditions and the following disclaimer in the documentation
10 //     and/or other materials provided with the distribution.
11 //  3. Neither the name of Google Inc. nor the names of its contributors may be
12 //     used to endorse or promote products derived from this software without
13 //     specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 // This file contains the implementation of the <xal:AddressDetails> elements.
27 // TODO: handle <xs:choice> better.
28 
29 #include "kml/dom/xal.h"
30 #include "kml/base/attributes.h"
31 #include "kml/dom/kml_cast.h"
32 #include "kml/dom/serializer.h"
33 
34 using kmlbase::Attributes;
35 
36 namespace kmldom {
37 
38 // <xal:AddressDetails>
XalAddressDetails()39 XalAddressDetails::XalAddressDetails() {
40   set_xmlns(kmlbase::XMLNS_XAL);
41 }
42 
AddElement(const ElementPtr & element)43 void XalAddressDetails::AddElement(const ElementPtr& element) {
44   if (!element) {
45     return;
46   }
47 
48   switch(element->Type()) {
49     case Type_XalCountry:
50       set_country(AsXalCountry(element));
51       break;
52     default:
53       Element::AddElement(element);
54   }
55 }
56 
Serialize(Serializer & serializer) const57 void XalAddressDetails::Serialize(Serializer& serializer) const {
58   ElementSerializer element_serializer(*this, serializer);
59   // <xal:Country>
60   if (has_country()) {
61     serializer.SaveElement(get_country());
62   }
63 }
64 
65 // <xal:AdministrativeArea>
XalAdministrativeArea()66 XalAdministrativeArea::XalAdministrativeArea()
67     : has_administrativeareaname_(false) {
68   set_xmlns(kmlbase::XMLNS_XAL);
69 }
70 
AddElement(const ElementPtr & element)71 void XalAdministrativeArea::AddElement(const ElementPtr& element) {
72   if (!element) {
73     return;
74   }
75   switch(element->Type()) {
76     case Type_xalAdministrativeAreaName:
77       has_administrativeareaname_ =
78           element->SetString(&administrativeareaname_);
79       break;
80     case Type_XalLocality:
81       set_locality(AsXalLocality(element));
82       break;
83     case Type_XalSubAdministrativeArea:
84       set_subadministrativearea(AsXalSubAdministrativeArea(element));
85       break;
86     default:
87       Element::AddElement(element);
88   }
89 }
90 
Serialize(Serializer & serializer) const91 void XalAdministrativeArea::Serialize(Serializer& serializer) const {
92   ElementSerializer element_serializer(*this, serializer);
93   if (has_administrativeareaname()) {
94     serializer.SaveFieldById(Type_xalAdministrativeAreaName,
95                              get_administrativeareaname());
96   }
97   if (has_locality()) {
98     serializer.SaveElement(get_locality());
99   }
100   if (has_subadministrativearea()) {
101     serializer.SaveElement(get_subadministrativearea());
102   }
103 }
104 
105 // <xal:Country>
XalCountry()106 XalCountry::XalCountry()
107     : has_countrynamecode_(false) {
108   set_xmlns(kmlbase::XMLNS_XAL);
109 }
110 
AddElement(const ElementPtr & element)111 void XalCountry::AddElement(const ElementPtr& element) {
112   if (!element) {
113     return;
114   }
115   switch(element->Type()) {
116     case Type_xalCountryNameCode:
117       has_countrynamecode_ = element->SetString(&countrynamecode_);
118       break;
119     case Type_XalAdministrativeArea:
120       set_administrativearea(AsXalAdministrativeArea(element));
121       break;
122     default:
123       Element::AddElement(element);
124   }
125 }
126 
Serialize(Serializer & serializer) const127 void XalCountry::Serialize(Serializer& serializer) const {
128   ElementSerializer element_serializer(*this, serializer);
129   // <xal:CountryNameCode>
130   if (has_countrynamecode()) {
131     serializer.SaveFieldById(Type_xalCountryNameCode, get_countrynamecode());
132   }
133   // <xal:AdministrativeArea>
134   if (has_administrativearea()) {
135     serializer.SaveElement(get_administrativearea());
136   }
137 }
138 
139 // <xal:Locality>
XalLocality()140 XalLocality::XalLocality()
141     : has_localityname_(false) {
142   set_xmlns(kmlbase::XMLNS_XAL);
143 }
144 
AddElement(const ElementPtr & element)145 void XalLocality::AddElement(const ElementPtr& element) {
146   if (!element) {
147     return;
148   }
149   switch(element->Type()) {
150     case Type_xalLocalityName:
151       has_localityname_ = element->SetString(&localityname_);
152       break;
153     case Type_XalPostalCode:
154       set_postalcode(AsXalPostalCode(element));
155       break;
156     case Type_XalThoroughfare:
157       set_thoroughfare(AsXalThoroughfare(element));
158       break;
159     default:
160       Element::AddElement(element);
161       break;
162   }
163 }
164 
Serialize(Serializer & serializer) const165 void XalLocality::Serialize(Serializer& serializer) const {
166   ElementSerializer element_serializer(*this, serializer);
167   if (has_thoroughfare()) {
168     serializer.SaveElement(get_thoroughfare());
169   }
170   if (has_postalcode()) {
171     serializer.SaveElement(get_postalcode());
172   }
173 }
174 
175 // <xal:PostalCode>
XalPostalCode()176 XalPostalCode::XalPostalCode()
177     : has_postalcodenumber_(false) {
178   set_xmlns(kmlbase::XMLNS_XAL);
179 }
180 
AddElement(const ElementPtr & element)181 void XalPostalCode::AddElement(const ElementPtr& element) {
182   if (!element) {
183     return;
184   }
185   switch(element->Type()) {
186     case Type_xalPostalCodeNumber:
187       has_postalcodenumber_ = element->SetString(&postalcodenumber_);
188       break;
189     default:
190       Element::AddElement(element);
191       break;
192   }
193 }
194 
Serialize(Serializer & serializer) const195 void XalPostalCode::Serialize(Serializer& serializer) const {
196   ElementSerializer element_serializer(*this, serializer);
197   if (has_postalcodenumber()) {
198     serializer.SaveFieldById(Type_xalPostalCodeNumber, get_postalcodenumber());
199   }
200 }
201 
202 // <xal:SubAdministrativeArea>
XalSubAdministrativeArea()203 XalSubAdministrativeArea::XalSubAdministrativeArea()
204     : has_subadministrativeareaname_(false) {
205   set_xmlns(kmlbase::XMLNS_XAL);
206 }
207 
AddElement(const ElementPtr & element)208 void XalSubAdministrativeArea::AddElement(const ElementPtr& element) {
209   if (!element) {
210     return;
211   }
212   switch(element->Type()) {
213     case Type_xalSubAdministrativeAreaName:
214       has_subadministrativeareaname_ =
215           element->SetString(&subadministrativeareaname_);
216       break;
217     case Type_XalLocality:
218       set_locality(AsXalLocality(element));
219       break;
220     default:
221       Element::AddElement(element);
222   }
223 }
224 
Serialize(Serializer & serializer) const225 void XalSubAdministrativeArea::Serialize(Serializer& serializer) const {
226   ElementSerializer element_serializer(*this, serializer);
227   if (has_subadministrativeareaname()) {
228     serializer.SaveFieldById(Type_xalSubAdministrativeAreaName,
229                              get_subadministrativeareaname());
230   }
231   if (has_locality()) {
232     serializer.SaveElement(get_locality());
233   }
234 }
235 
236 // <xal:Thoroughfare>
XalThoroughfare()237 XalThoroughfare::XalThoroughfare()
238     : has_thoroughfarename_(false),
239       has_thoroughfarenumber_(false) {
240   set_xmlns(kmlbase::XMLNS_XAL);
241 }
242 
AddElement(const ElementPtr & element)243 void XalThoroughfare::AddElement(const ElementPtr& element) {
244   if (!element) {
245     return;
246   }
247   switch(element->Type()) {
248     case Type_xalThoroughfareName:
249       has_thoroughfarename_ = element->SetString(&thoroughfarename_);
250       break;
251     case Type_xalThoroughfareNumber:
252       has_thoroughfarenumber_ = element->SetString(&thoroughfarenumber_);
253       break;
254     default:
255       Element::AddElement(element);
256       break;
257   }
258 }
259 
Serialize(Serializer & serializer) const260 void XalThoroughfare::Serialize(Serializer& serializer) const {
261   ElementSerializer element_serializer(*this, serializer);
262   if (has_thoroughfarenumber()) {
263     serializer.SaveFieldById(Type_xalThoroughfareNumber,
264                              get_thoroughfarenumber());
265   }
266   if (has_thoroughfarename()) {
267     serializer.SaveFieldById(Type_xalThoroughfareName, get_thoroughfarename());
268   }
269 }
270 
271 }  // end namespace kmldom
272