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 SimpleField and Schema elements.
27
28 #include "kml/dom/schema.h"
29 #include "kml/base/attributes.h"
30 #include "kml/base/xml_namespaces.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 // <SimpleField>
SimpleField()39 SimpleField::SimpleField()
40 : has_type_(false),
41 has_name_(false),
42 has_displayname_(false) {
43 set_xmlns(kmlbase::XMLNS_KML22);
44 }
45
~SimpleField()46 SimpleField::~SimpleField() {}
47
48 static const char kSimpleFieldTypeAttr[] = "type";
49 static const char kSimpleFieldNameAttr[] = "name";
50
ParseAttributes(Attributes * attributes)51 void SimpleField::ParseAttributes(Attributes* attributes) {
52 if (!attributes) {
53 return;
54 }
55 has_type_ = attributes->CutValue(kSimpleFieldTypeAttr, &type_);
56 has_name_ = attributes->CutValue(kSimpleFieldNameAttr, &name_);
57 AddUnknownAttributes(attributes);
58 }
59
SerializeAttributes(Attributes * attributes) const60 void SimpleField::SerializeAttributes(Attributes* attributes) const {
61 Element::SerializeAttributes(attributes);
62 if (has_type_) {
63 attributes->SetValue(kSimpleFieldTypeAttr, type_);
64 }
65 if (has_name_) {
66 attributes->SetValue(kSimpleFieldNameAttr, name_);
67 }
68 }
69
AddElement(const ElementPtr & element)70 void SimpleField::AddElement(const ElementPtr& element) {
71 if (!element) {
72 return;
73 }
74 if (element->Type() == Type_displayName) {
75 has_displayname_ = element->SetString(&displayname_);
76 } else {
77 Element::AddElement(element);
78 }
79 }
80
Serialize(Serializer & serializer) const81 void SimpleField::Serialize(Serializer& serializer) const {
82 ElementSerializer element_serializer(*this, serializer);
83 if (has_displayname()) {
84 serializer.SaveFieldById(Type_displayName, get_displayname());
85 }
86 }
87
Accept(Visitor * visitor)88 void SimpleField::Accept(Visitor* visitor) {
89 visitor->VisitSimpleField(SimpleFieldPtr(this));
90 }
91
92 // <GxSimpleArrayField>
GxSimpleArrayField()93 GxSimpleArrayField::GxSimpleArrayField() {
94 set_xmlns(kmlbase::XMLNS_GX22);
95 }
96
~GxSimpleArrayField()97 GxSimpleArrayField::~GxSimpleArrayField() {}
98
Accept(Visitor * visitor)99 void GxSimpleArrayField::Accept(Visitor* visitor) {
100 visitor->VisitGxSimpleArrayField(GxSimpleArrayFieldPtr(this));
101 }
102
103 // <Schema>
Schema()104 Schema::Schema()
105 : has_name_(false) {
106 }
107
~Schema()108 Schema::~Schema() {}
109
110 static const char kSchemaNameAttr[] = "name";
111
ParseAttributes(Attributes * attributes)112 void Schema::ParseAttributes(Attributes* attributes) {
113 if (!attributes) {
114 return;
115 }
116 has_name_ = attributes->CutValue(kSchemaNameAttr, &name_);
117 Object::ParseAttributes(attributes);
118 }
119
SerializeAttributes(Attributes * attributes) const120 void Schema::SerializeAttributes(Attributes* attributes) const {
121 Object::SerializeAttributes(attributes);
122 if (has_name_) {
123 attributes->SetValue(kSchemaNameAttr, name_);
124 }
125 }
126
AddElement(const ElementPtr & element)127 void Schema::AddElement(const ElementPtr& element) {
128 if (!element) {
129 return;
130 }
131 switch (element->Type()) {
132 case Type_SimpleField:
133 add_simplefield(AsSimpleField(element));
134 break;
135 case Type_GxSimpleArrayField:
136 add_gx_simplearrayfield(AsGxSimpleArrayField(element));
137 break;
138 default:
139 Object::AddElement(element);
140 }
141 }
142
Serialize(Serializer & serializer) const143 void Schema::Serialize(Serializer& serializer) const {
144 ElementSerializer element_serializer(*this, serializer);
145 serializer.SaveElementArray(simplefield_array_);
146 serializer.SaveElementArray(gx_simplearrayfield_array_);
147 }
148
Accept(Visitor * visitor)149 void Schema::Accept(Visitor* visitor) {
150 visitor->VisitSchema(SchemaPtr(this));
151 }
152
AcceptChildren(VisitorDriver * driver)153 void Schema::AcceptChildren(VisitorDriver* driver) {
154 Object::AcceptChildren(driver);
155 Element::AcceptRepeated<SimpleFieldPtr>(&simplefield_array_, driver);
156 Element::AcceptRepeated<GxSimpleArrayFieldPtr>(&gx_simplearrayfield_array_,
157 driver);
158 }
159
160 } // end namespace kmldom
161