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