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 abstract element TimePrimitive
27 // and the concrete elements TimeSpan, TimeStamp.
28 
29 #include "kml/dom/timeprimitive.h"
30 #include "kml/base/attributes.h"
31 #include "kml/dom/serializer.h"
32 
33 using kmlbase::Attributes;
34 
35 namespace kmldom {
36 
TimePrimitive()37 TimePrimitive::TimePrimitive() {}
38 
~TimePrimitive()39 TimePrimitive::~TimePrimitive() {}
40 
AddElement(const ElementPtr & element)41 void TimePrimitive::AddElement(const ElementPtr& element) {
42   Object::AddElement(element);
43 }
44 
TimeSpan()45 TimeSpan::TimeSpan()
46   : has_begin_(false),
47     has_end_(false) {
48 }
49 
~TimeSpan()50 TimeSpan::~TimeSpan() {}
51 
AddElement(const ElementPtr & element)52 void TimeSpan::AddElement(const ElementPtr& element) {
53   if (!element) {
54     return;
55   }
56   switch (element->Type()) {
57     case Type_begin:
58       has_begin_ = element->SetString(&begin_);
59       break;
60     case Type_end:
61       has_end_ = element->SetString(&end_);
62       break;
63     default:
64      TimePrimitive::AddElement(element);
65   }
66 }
67 
Serialize(Serializer & serializer) const68 void TimeSpan::Serialize(Serializer& serializer) const {
69   ElementSerializer element_serializer(*this, serializer);
70   TimePrimitive::Serialize(serializer);
71   if (has_begin()) {
72     serializer.SaveFieldById(Type_begin, begin_);
73   }
74   if (has_end()) {
75     serializer.SaveFieldById(Type_end, end_);
76   }
77 }
78 
Accept(Visitor * visitor)79 void TimeSpan::Accept(Visitor* visitor) {
80   visitor->VisitTimeSpan(TimeSpanPtr(this));
81 }
82 
TimeStamp()83 TimeStamp::TimeStamp()
84   : has_when_(false) {
85 }
86 
~TimeStamp()87 TimeStamp::~TimeStamp() {}
88 
AddElement(const ElementPtr & element)89 void TimeStamp::AddElement(const ElementPtr& element) {
90   if (!element) {
91     return;
92   }
93   if (element->Type() == Type_when) {
94       has_when_ = element->SetString(&when_);
95   } else {
96      TimePrimitive::AddElement(element);
97   }
98 }
99 
Serialize(Serializer & serializer) const100 void TimeStamp::Serialize(Serializer& serializer) const {
101   ElementSerializer element_serializer(*this, serializer);
102   TimePrimitive::Serialize(serializer);
103   if (has_when()) {
104     serializer.SaveFieldById(Type_when, when_);
105   }
106 }
107 
Accept(Visitor * visitor)108 void TimeStamp::Accept(Visitor* visitor) {
109   visitor->VisitTimeStamp(TimeStampPtr(this));
110 }
111 
112 }  // end namespace kmldom
113