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
22 #include <algorithm>
\r
27 YAML::Node IncludeResolver::readToYamlNode(const YAML::Node &yamlFile )
\r
29 std::string value = yamlFile.as<std::string>();
\r
32 YAML::Node yamlInclueNode = YAML::LoadFile(value);
\r
33 return yamlInclueNode;
\r
35 catch (YAML::ParserException &e)
\r
37 throw RamlParserException(e.mark, e.msg);
\r
39 catch (YAML::RepresentationException &e)
\r
41 throw RamlRepresentationException(e.mark, e.msg);
\r
43 catch (YAML::BadFile &e)
\r
45 throw RamlBadFile(e.mark, e.msg);
\r
47 catch (JsonException &e)
\r
53 IncludeResolver::FileType IncludeResolver::getFileType(const YAML::Node &yamlNode )
\r
55 IncludeResolver::FileType fileType = IncludeResolver::FileType::NOTAG;
\r
56 if (yamlNode.Tag() == "!include")
\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
62 fileType = IncludeResolver::FileType::FILE;
\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
72 fileType = IncludeResolver::FileType::FILE;
\r
77 cJSON *IncludeResolver::readToJson(const YAML::Node &jsonFile)
\r
79 return cJSON_Parse(readFromFile(jsonFile).c_str());
\r
82 std::string IncludeResolver::readFromFile(const YAML::Node &file )
\r
84 std::string val = file.as<std::string>();
\r
85 std::ifstream fin((m_path + val).c_str());
\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
92 cJSON *IncludeResolver::readToJson(const std::string &jsonFileName)
\r
94 std::ifstream fin((m_path + jsonFileName).c_str());
\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