-
Notifications
You must be signed in to change notification settings - Fork 0
/
termtype_helper.cpp
177 lines (167 loc) · 4.11 KB
/
termtype_helper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "termtype_helper.h"
#include <unordered_set>
#include <stdexcept>
#include "rml_uris.h"
/**
* @brief Encodes a URL by replacing special characters with their respective percent-encoded values.
*
* @param node The input string to be URL-encoded.
* @return std::string The URL-encoded version of the input string.
*/
std::string url_encode(const std::string& node) {
std::string result = "";
for (char c : node) {
switch (c) {
case ' ':
result += "%20";
break;
case '!':
result += "%21";
break;
case '\"':
result += "%22";
break;
case '#':
result += "%23";
break;
case '$':
result += "%24";
break;
case '%':
result += "%25";
break;
case '&':
result += "%26";
break;
case '\'':
result += "%27";
break;
case '(':
result += "%28";
break;
case ')':
result += "%29";
break;
case '*':
result += "%2A";
break;
case '+':
result += "%2B";
break;
case ',':
result += "%2C";
break;
case '/':
result += "%2F";
break;
case ':':
result += "%3A";
break;
case ';':
result += "%3B";
break;
case '<':
result += "%3C";
break;
case '=':
result += "%3D";
break;
case '>':
result += "%3E";
break;
case '?':
result += "%3F";
break;
case '@':
result += "%40";
break;
case '[':
result += "%5B";
break;
case '\\':
result += "%5C";
break;
case ']':
result += "%5D";
break;
case '{':
result += "%7B";
break;
case '|':
result += "%7C";
break;
case '}':
result += "%7D";
break;
default:
result += c;
break;
}
}
return result;
}
/**
*@brief Handles the term mapping for IRIs by replacing all white space with %20 and encloses string in < >.
*
* @param node - The element to be converted into an IRI.
*
* @return std::string - The processed node.
*/
std::string handle_term_type_IRI(const std::string& node) {
static const std::string error_chars = " !\"'(),[]";
// Check if IRI is valid; if not, throw error
if (node.find_first_of(error_chars) != std::string::npos) {
std::string message = "Runtime error occurred handling IRI. Invalid IRI detected for node: " + node;
throw std::runtime_error(message);
return "";
}
return "<" + node + ">";
}
/**
*@brief Handles the term mapping for blank nodes by adding _: in front of string.
*
* @param node - The element to be converted into a blank node.
*
* @return std::string - The processed node.
*/
std::string handle_term_type_BlankNode(const std::string& node) {
return "_:" + node;
}
/**
*@brief Handles the term mapping for literals by enclosing the string in " ".
*
* @param node - The element to be converted into a literal.
*
* @return std::string - The processed node.
*/
std::string handle_term_type_Literal(const std::string& node) {
// remove all '\' in string
std::string cleanedNode;
for (char c : node) {
if (c != '\\') {
cleanedNode += c;
}
}
// Enclose in " "
return "\"" + cleanedNode + "\"";
}
/**
*@brief Handles the term mapping for a node based on its term type.
*
* @param term_type - The string containing the term type (e.g., IRI, BlankNode, Literal).
* @param node - The element to be modified based on its term type.
*
* @return std::string - The processed node.
*/
std::string handle_term_type(const std::string& term_type, const std::string& node) {
if (term_type == IRI_TERM_TYPE) {
return handle_term_type_IRI(node);
} else if (term_type == BLANKNODE_TERM_TYPE) {
return handle_term_type_BlankNode(node);
} else if (term_type == LITERAL_TERM_TYPE) {
return handle_term_type_Literal(node);
} else {
throw std::runtime_error("Runtime error occurred handling term type. Unknown term type found.");
return "";
}
}