Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / IncludeResolver.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 "IncludeResolver.h"\r
22 \r
23 namespace RAML\r
24 {\r
25 \r
26     YAML::Node IncludeResolver::readToYamlNode(const YAML::Node &yamlFile )\r
27     {\r
28         std::string value = yamlFile.as<std::string>();\r
29         try\r
30         {\r
31             YAML::Node yamlInclueNode = YAML::LoadFile(value);\r
32             return yamlInclueNode;\r
33         }\r
34         catch (YAML::ParserException &e)\r
35         {\r
36             throw RamlParserException(e.mark, e.msg);\r
37         }\r
38         catch (YAML::RepresentationException &e)\r
39         {\r
40             throw RamlRepresentationException(e.mark, e.msg);\r
41         }\r
42         catch (YAML::BadFile &e)\r
43         {\r
44             throw RamlBadFile(e.mark, e.msg);\r
45         }\r
46         catch (JsonException &e)\r
47         {\r
48             throw;\r
49         }\r
50     }\r
51 \r
52     IncludeResolver::FileType IncludeResolver::getFileType(const YAML::Node &yamlNode )\r
53     {\r
54         IncludeResolver::FileType fileType = IncludeResolver::FileType::NOTAG;\r
55         if (yamlNode.Tag() == "!include")\r
56         {\r
57             std::string value = yamlNode.as<std::string>();\r
58             std::size_t found = value.find_last_of(".");\r
59             if (found > value.length())\r
60             {\r
61                 fileType = IncludeResolver::FileType::FILE;\r
62                 return fileType;\r
63             }\r
64             std::string extension = value.substr(found + 1);\r
65             if (std::find(Keys::AllowedRamlYamlTypes.begin(), Keys::AllowedRamlYamlTypes.end(),\r
66                           extension) != Keys::AllowedRamlYamlTypes.end())\r
67                 fileType = IncludeResolver::FileType::NODE;\r
68             else if (extension == Keys::Json)\r
69                 fileType = IncludeResolver::FileType::JSON;\r
70             else\r
71                 fileType = IncludeResolver::FileType::FILE;\r
72         }\r
73         return fileType;\r
74     }\r
75 \r
76     cJSON *IncludeResolver::readToJson(const YAML::Node &jsonFile)\r
77     {\r
78         return cJSON_Parse(readFromFile(jsonFile).c_str());\r
79     }\r
80 \r
81     std::string IncludeResolver::readFromFile(const YAML::Node &file )\r
82     {\r
83         std::string val = file.as<std::string>();\r
84         std::ifstream fin((m_path + val).c_str());\r
85         if (!fin)\r
86             throw RamlParserException("Error Include File not present " + m_path + val);\r
87         std::stringstream buffer;\r
88         buffer << fin.rdbuf();\r
89         return buffer.str();\r
90     }\r
91     cJSON *IncludeResolver::readToJson(const std::string &jsonFileName)\r
92     {\r
93         std::ifstream fin((m_path + jsonFileName).c_str());\r
94         if (!fin)\r
95             throw JsonException("Error Json Referenced File not present " + m_path + jsonFileName);\r
96         std::stringstream buffer;\r
97         buffer << fin.rdbuf();\r
98         std::string str = buffer.str();\r
99         return cJSON_Parse(str.c_str());\r
100     }\r
101 \r
102 }\r