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 "IncludeResolver.h"
\r
26 YAML::Node IncludeResolver::readToYamlNode(const YAML::Node &yamlFile )
\r
28 std::string value = yamlFile.as<std::string>();
\r
31 YAML::Node yamlInclueNode = YAML::LoadFile(value);
\r
32 return yamlInclueNode;
\r
34 catch (YAML::ParserException &e)
\r
36 throw RamlParserException(e.mark, e.msg);
\r
38 catch (YAML::RepresentationException &e)
\r
40 throw RamlRepresentationException(e.mark, e.msg);
\r
42 catch (YAML::BadFile &e)
\r
44 throw RamlBadFile(e.mark, e.msg);
\r
46 catch (JsonException &e)
\r
52 IncludeResolver::FileType IncludeResolver::getFileType(const YAML::Node &yamlNode )
\r
54 IncludeResolver::FileType fileType = IncludeResolver::FileType::NOTAG;
\r
55 if (yamlNode.Tag() == "!include")
\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
61 fileType = IncludeResolver::FileType::FILE;
\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
71 fileType = IncludeResolver::FileType::FILE;
\r
76 cJSON *IncludeResolver::readToJson(const YAML::Node &jsonFile)
\r
78 return cJSON_Parse(readFromFile(jsonFile).c_str());
\r
81 std::string IncludeResolver::readFromFile(const YAML::Node &file )
\r
83 std::string val = file.as<std::string>();
\r
84 std::ifstream fin((m_path + val).c_str());
\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
91 cJSON *IncludeResolver::readToJson(const std::string &jsonFileName)
\r
93 std::ifstream fin((m_path + jsonFileName).c_str());
\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