Revert "Change build option - IP only"
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / RamlParser.h
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 /**\r
22  * @file   RamlParser.h\r
23  *\r
24  * @brief   This file provides APIs for parsing Raml file.\r
25  */\r
26 \r
27 #ifndef RAML_PARSER_H\r
28 #define RAML_PARSER_H\r
29 \r
30 #include "yaml-cpp/yaml.h"\r
31 #include "Raml.h"\r
32 #include "Utils.h"\r
33 #include "RequestResponseBody.h"\r
34 #include "RamlResource.h"\r
35 #include "Action.h"\r
36 #include "Response.h"\r
37 #include <map>\r
38 #include "RamlErrorCodes.h"\r
39 #include "yaml-cpp/exceptions.h"\r
40 #include "RamlExceptions.h"\r
41 \r
42 namespace RAML\r
43 {\r
44     /**\r
45       * @class   RamlParser\r
46       * @brief   This class provides a set of APIs for parsing Raml file.\r
47       */\r
48     class RamlParser\r
49     {\r
50         private:\r
51             void setDataFromRoot();\r
52             void setBodyDefaultMediaType(const std::map<std::string, RamlResourcePtr> &resource);\r
53             void setBodySchema(const std::map<std::string, RamlResourcePtr> &resource);\r
54             void setTypes(const std::map<std::string, RamlResourcePtr> &resource);\r
55             void setTraits(const std::map<std::string, RamlResourcePtr> &resource);\r
56 \r
57         public:\r
58             /**\r
59                    * This method is for getting the created and Parsed RAML object from RAML file.\r
60                    *\r
61                    * @param result - Reference to RamlParserResult.\r
62                    *\r
63                    * @return pointer to Raml shared object parsed.\r
64                    */\r
65             virtual RamlPtr getRamlPtr(RamlParserResult &result);\r
66 \r
67             /**\r
68                    * This method is for getting the created and Parsed RAML object from RAML file.\r
69                    *\r
70                    * @return pointer to Raml shared object parsed.\r
71                    */\r
72             virtual RamlPtr getRamlPtr();\r
73 \r
74             /**\r
75                    * Constructor of RamlParser.\r
76                    *\r
77                    *  NOTE: Constructor would initialize the RamlParserResult with File Path Required\r
78                    */\r
79             RamlParser(): m_ramlPtr(std::make_shared<Raml>()),\r
80                 m_ramlParserResult(RAML_FILE_PATH_REQUIRED) {}\r
81 \r
82             /**\r
83                    * Constructor of RamlParser.\r
84                    *\r
85                    * @param path - RAML configuration file path.\r
86                    *\r
87                    *  NOTE: Constructor would throw RamlBadFile when invalid arguments passed, and\r
88                    * RamlException if any other error occured.\r
89                    */\r
90             RamlParser(const std::string &path): m_ramlParserResult(RAML_PARSER_ERROR)\r
91             {\r
92                 if (path.length() > 0)\r
93                 {\r
94                     std::size_t found = path.find_last_of("/\\");\r
95                     if (found < path.length())\r
96                     {\r
97                         m_fileLocation = path.substr(0, found) + "/";\r
98                         m_ramlName = path.substr(found + 1);\r
99                         try\r
100                         {\r
101                             m_ramlPtr = std::make_shared<Raml>(m_fileLocation, m_ramlName);\r
102                             setDataFromRoot();\r
103                             m_ramlParserResult = RAML_PARSER_OK;\r
104                         }\r
105                         catch (RamlException &e)\r
106                         {\r
107                             throw;\r
108                         }\r
109                     }\r
110                     else\r
111                     {\r
112                         m_ramlParserResult = RAML_FILE_PATH_REQUIRED;\r
113                         throw RamlBadFile("Raml File Path incorrect");\r
114                     }\r
115                 }\r
116                 else\r
117                 {\r
118                     m_ramlParserResult = RAML_FILE_PATH_REQUIRED;\r
119                     throw RamlBadFile("Raml File Path required");\r
120                 }\r
121             }\r
122         private:\r
123 \r
124             RamlPtr m_ramlPtr;\r
125             std::string m_fileLocation;\r
126             std::string m_ramlName;\r
127             RamlParserResult m_ramlParserResult;\r
128     };\r
129 \r
130 }\r
131 #endif\r