1 /******************************************************************
\r
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
19 ******************************************************************/
\r
21 #include "RamlParser.h"
\r
26 RamlPtr RamlParser::build()
\r
28 YAML::Node yamlRootNode = YAML::LoadFile(m_fileLocation + m_ramlName);
\r
29 m_ramlPtr->readRamlFromYaml(yamlRootNode);
\r
34 RamlPtr RamlParser::getRamlPtr()
\r
38 void RamlParser::setDataFromRoot()
\r
40 setTypes(getRamlPtr()->getResources());
\r
41 setTraits(getRamlPtr()->getResources());
\r
42 setBodyDefaultMediaType(getRamlPtr()->getResources());
\r
43 setBodySchema(getRamlPtr()->getResources());
\r
45 void RamlParser::setBodyDefaultMediaType(std::map<std::string, RamlResource> resource)
\r
47 if (getRamlPtr()->getMediaType().empty())
\r
51 for (auto it : resource)
\r
53 std::string type = getRamlPtr()->getMediaType();
\r
55 for (auto action : it.second.getActions())
\r
57 if (action.second.getRequestBody().empty())
\r
59 std::string resName = it.first;
\r
60 getRamlPtr()->getResource(resName).getAction(action.first).setRequestBody(type);
\r
62 for (auto response : action.second.getResponses())
\r
64 if (response.second.getResponseBody().empty())
\r
66 std::string resName = it.first;
\r
67 std::string responseCode = response.first;
\r
68 getRamlPtr()->getResource(resName).getAction(action.first).getResponse(
\r
69 responseCode).setResponseBody(
\r
76 void RamlParser::setBodySchema(std::map<std::string, RamlResource> resource)
\r
78 if (getRamlPtr()->getSchemas().empty())
\r
82 for (auto it : resource)
\r
84 for (auto action : it.second.getActions())
\r
86 for (auto body : action.second.getRequestBody())
\r
88 Schema *schema = body.second.getSchema();
\r
92 std::string schemaValue = schema->getSchema();
\r
93 auto pos = std::find_if(getRamlPtr()->getSchemas().begin(), getRamlPtr()->getSchemas().end(),
\r
94 [schemaValue](std::pair<std::string, Schema> const & pair)
\r
96 return (pair.first == schemaValue);
\r
98 if (pos != getRamlPtr()->getSchemas().end())
\r
100 schema->setSchema((pos->second.getSchema()));
\r
104 for (auto response : action.second.getResponses())
\r
106 for (auto body : response.second.getResponseBody())
\r
108 Schema *schema = body.second.getSchema();
\r
109 if (schema != NULL)
\r
111 std::string schemaValue = schema->getSchema();
\r
112 auto schemas = getRamlPtr()->getSchemas();
\r
114 auto iter = schemas.begin();
\r
115 for (; iter != schemas.end(); iter++)
\r
117 if (((*iter).first) == schemaValue)
\r
120 if (iter != schemas.end())
\r
122 schema->setSchema((*iter).second.getSchema());
\r
130 void RamlParser::setTypes(std::map<std::string, RamlResource> resource)
\r
132 if (getRamlPtr()->getResourceTypes().empty())
\r
136 for (auto it : resource)
\r
138 auto resourceTypes = getRamlPtr()->getResourceTypes();
\r
139 std::string typeValue = it.second.getResourceType();
\r
141 auto iter = resourceTypes.begin();
\r
142 for (; iter != resourceTypes.end(); iter++)
\r
144 if (((*iter).first) == typeValue)
\r
147 if (iter != resourceTypes.end())
\r
149 std::string resName = it.first;
\r
150 RamlResource &res = getRamlPtr()->getResource(resName);
\r
151 RamlResource resType = (*iter).second;
\r
153 if (resType.getActions().empty())
\r
156 for (auto resActions : resType.getActions())
\r
158 if (res.getActions().count(resActions.first) == 0)
\r
159 res.setAction(resActions.first, resActions.second);
\r
164 void RamlParser::setTraits(std::map<std::string, RamlResource> resource)
\r
166 if (getRamlPtr()->getTraits().empty())
\r
170 for (auto it : resource)
\r
172 auto trait = getRamlPtr()->getTraits();
\r
173 for (auto act : it.second.getActions())
\r
175 for (std::string traitValue : act.second.getTraits())
\r
177 auto iter = trait.begin();
\r
178 for (; iter != trait.end(); iter++)
\r
180 if (((*iter).first) == traitValue)
\r
183 if (iter != trait.end())
\r
185 std::string resName = it.first;
\r
186 RamlResource &res = getRamlPtr()->getResource(resName);
\r
187 Action resTrait = (*iter).second;
\r
189 Action &action = res.getAction(act.first);
\r
190 for (auto head : resTrait.getHeaders())
\r
192 if (action.getHeaders().count(head.first) == 0)
\r
193 action.setHeader(head.first, head.second);
\r
195 for (auto query : resTrait.getQueryParameters())
\r
197 if (action.getQueryParameters().count(query.first) == 0)
\r
198 action.setQueryParameter(query.first, query.second);
\r
200 for (auto resp : resTrait.getResponses())
\r
202 if (action.getResponses().count(resp.first) == 0)
\r
203 action.setResponse(resp.first, resp.second);
\r
208 for (std::string traitValue : it.second.getTraits())
\r
210 auto iter = trait.begin();
\r
211 for (; iter != trait.end(); iter++)
\r
213 if (((*iter).first) == traitValue)
\r
216 if (iter != trait.end())
\r
218 std::string resName = it.first;
\r
219 RamlResource &res = getRamlPtr()->getResource(resName);
\r
220 Action resTrait = (*iter).second;
\r
222 for (auto act : res.getActions())
\r
224 Action &action = res.getAction(act.first);
\r
225 for (auto head : resTrait.getHeaders())
\r
227 if (action.getHeaders().count(head.first) == 0)
\r
228 action.setHeader(head.first, head.second);
\r
230 for (auto query : resTrait.getQueryParameters())
\r
232 if (action.getQueryParameters().count(query.first) == 0)
\r
233 action.setQueryParameter(query.first, query.second);
\r
235 for (auto resp : resTrait.getResponses())
\r
237 if (action.getResponses().count(resp.first) == 0)
\r
238 action.setResponse(resp.first, resp.second);
\r