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 #include "kml/xsd/xsd_primitive_type.h"
27 
28 namespace kmlxsd {
29 
30 // This exactly matches the XsdPrimitiveType enum.
31 static const char* XsdPrimitiveTypeName[] = {
32   NULL,  // XSD_INVALID
33   "string",  // 3.2.1
34   "boolean",  // 3.2.2
35   "decimal",  // 3.2.3
36   "float",  // 3.2.4
37   "double",  // 3.2.5
38   "duration",  // 3.2.6
39   "dateTime",  // 3.2.7
40   "time",  // 3.2.8
41   "date",  // 3.2.9
42   "gYearMonth",  // 3.2.10
43   "gYear",  // 3.2.11
44   "gMonthDay",  // 3.2.12
45   "gDay",  // 3.2.13
46   "gMonth",  // 3.2.14
47   "hexBinary",  // 3.2.15
48   "base64Binary",  // 3.2.16
49   "anyURI",  // 3.2.17
50   "QNAME",  // 3.2.18
51   "NOTATION",  // 3.2.19
52   // NOTE: this includes only the types involved in the xsd:int derivation.
53   "integer",  // 3.3.13.  Is-a XSD_DECIMAL.
54   "long",  // 3.3.16.  Is-a XSD_INTEGER.
55   "int",  // 3.3.17.  Is-a XSD_LONG.
56 };
57 
GetTypeName(TypeId type_id)58 const string XsdPrimitiveType::GetTypeName(TypeId type_id) {
59   return XsdPrimitiveTypeName[type_id];
60 }
61 
GetTypeId(const string & type_name)62 XsdPrimitiveType::TypeId XsdPrimitiveType::GetTypeId(
63     const string& type_name) {
64   // TODO: make a map and save it somewhere...
65   size_t size = sizeof(XsdPrimitiveTypeName)/sizeof(XsdPrimitiveTypeName[0]);
66   for (size_t i = 1; i < size; ++i) {
67     if (type_name.compare(XsdPrimitiveTypeName[i]) == 0) {
68       return static_cast<TypeId>(i);
69     }
70   }
71   return XSD_INVALID;
72 }
73 
74 }  // end namespace kmlxsd
75