Adding JsonSchema parser implementation for simulator and Updating Raml Parser.
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / model / RamlResource.cpp
1 /******************************************************************\r
2  *\r
3  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
4  *\r
5  *\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  ******************************************************************/\r
20 \r
21 #include "RamlResource.h"\r
22 \r
23 \r
24 namespace RAML\r
25 {\r
26     std::string RamlResource::getDisplayName() const\r
27     {\r
28         return m_displayName;\r
29     }\r
30     void RamlResource::setDisplayName(const std::string &displayName)\r
31     {\r
32         m_displayName = displayName;\r
33     }\r
34     std::string RamlResource::getDescription() const\r
35     {\r
36         return m_description;\r
37     }\r
38     void RamlResource::setDescription(const std::string &description)\r
39     {\r
40         m_description = description;\r
41     }\r
42     std::string RamlResource::getParentUri() const\r
43     {\r
44         return m_parentUri;\r
45     }\r
46     void RamlResource::setParentUri(const std::string &parentUri)\r
47     {\r
48         m_parentUri = parentUri;\r
49     }\r
50     std::string RamlResource::getRelativeUri() const\r
51     {\r
52         return m_relativeUri;\r
53     }\r
54     void RamlResource::setRelativeUri(const std::string &relativeUri)\r
55     {\r
56         m_relativeUri = relativeUri;\r
57     }\r
58     std::map<std::string, UriParameterPtr> const &RamlResource::getUriParameters() const\r
59     {\r
60         return m_uriParameters;\r
61     }\r
62     void RamlResource::setUriParameter(const std::string &paramName,\r
63                                        const UriParameterPtr &uriParameter)\r
64     {\r
65         m_uriParameters[paramName] = uriParameter;\r
66     }\r
67     std::map<std::string, UriParameterPtr > const &RamlResource::getBaseUriParameters() const\r
68     {\r
69         return m_baseUriParameters;\r
70     }\r
71     void RamlResource::setBaseUriParameter(const std::string &paramName,\r
72                                            const UriParameterPtr &baseUriParameter)\r
73     {\r
74         m_baseUriParameters[paramName] = baseUriParameter;\r
75     }\r
76     ActionPtr RamlResource::getAction(ActionType actionType)\r
77     {\r
78         return m_actions[actionType];\r
79     }\r
80 \r
81     std::map<ActionType , ActionPtr> const &RamlResource::getActions() const\r
82     {\r
83         return m_actions;\r
84     }\r
85     void RamlResource::setAction(const ActionType &actiontype , const ActionPtr &action )\r
86     {\r
87         m_actions[actiontype] = action;\r
88     }\r
89     std::map<std::string, std::shared_ptr<RamlResource> > const &RamlResource::getResources() const\r
90     {\r
91         return m_resources;\r
92     }\r
93     void RamlResource::setResource(const std::string &resourceName,\r
94                                    const std::shared_ptr<RamlResource> &resources)\r
95     {\r
96         m_resources[resourceName] = resources;\r
97     }\r
98     std::list<std::string> const &RamlResource::getTraits() const\r
99     {\r
100         return m_traits;\r
101     }\r
102     void RamlResource::setTrait(const std::string &trait)\r
103     {\r
104         m_traits.push_back(trait);\r
105     }\r
106     std::string RamlResource::getResourceType() const\r
107     {\r
108         return m_resourceType;\r
109     }\r
110     void RamlResource::setResourceType(const std::string &type)\r
111     {\r
112         m_resourceType = type;\r
113     }\r
114     std::string RamlResource::getResourceUri() const\r
115     {\r
116         return (m_parentUri + m_relativeUri);\r
117     }\r
118     void RamlResource::readResource(const std::string resourceKey, const YAML::Node &yamlNode,\r
119                                     const std::string &parentUri)\r
120     {\r
121         m_relativeUri = resourceKey;\r
122         m_parentUri = parentUri;\r
123         for ( YAML::const_iterator it = yamlNode.begin(); it != yamlNode.end(); ++it )\r
124         {\r
125             std::string key = READ_NODE_AS_STRING(it->first);\r
126 \r
127             if (key == Keys::DisplayName)\r
128                 setDisplayName(READ_NODE_AS_STRING(it->second));\r
129             else if (key == Keys::Description)\r
130                 setDescription(READ_NODE_AS_STRING(it->second));\r
131             else if (std::find(Keys::ActionType.begin(), Keys::ActionType.end(), key) != Keys::ActionType.end())\r
132             {\r
133                 ActionType actionType = GET_ACTION_TYPE(key);\r
134 \r
135                 setAction(actionType, std::make_shared<Action>(actionType, it->second, m_includeResolver));\r
136             }\r
137             else if (key == Keys::UriParameters)\r
138             {\r
139                 YAML::Node paramNode = it->second;\r
140                 for ( YAML::const_iterator tt = paramNode.begin(); tt != paramNode.end(); ++tt )\r
141                 {\r
142                     setUriParameter(READ_NODE_AS_STRING(tt->first), std::make_shared<UriParameter>(tt->second));\r
143                 }\r
144             }\r
145             else if (key == Keys::BaseUriParameters)\r
146             {\r
147                 YAML::Node paramNode = it->second;\r
148                 for ( YAML::const_iterator tt = paramNode.begin(); tt != paramNode.end(); ++tt )\r
149                 {\r
150                     setBaseUriParameter(READ_NODE_AS_STRING(tt->first), std::make_shared<UriParameter>(tt->second));\r
151                 }\r
152             }\r
153             else if (key == Keys::IsTrait)\r
154             {\r
155                 YAML::Node traitNode = it->second;\r
156                 for ( YAML::const_iterator tt = traitNode.begin(); tt != traitNode.end(); ++tt )\r
157                 {\r
158                     setTrait(READ_NODE_AS_STRING(*tt));\r
159                 }\r
160             }\r
161             else if (key == Keys::Type)\r
162             {\r
163                 setResourceType(READ_NODE_AS_STRING(it->second));\r
164             }\r
165             else if (key.compare(0, Keys::Resource.length(), Keys::Resource)  == 0)\r
166             {\r
167                 setResource(key, std::make_shared<RamlResource>(key, it->second, m_includeResolver,\r
168                             getResourceUri()));\r
169             }\r
170         }\r
171     }\r
172 \r
173 }\r