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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
// Copyright 2020 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See https://github.com/danielaparker/jsoncons for latest version
#ifndef JSONCONS_JSONSCHEMA_SCHEMA_LOCATION_HPP
#define JSONCONS_JSONSCHEMA_SCHEMA_LOCATION_HPP
#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/uri.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
#include <jsoncons_ext/jsonschema/jsonschema_error.hpp>
namespace jsoncons {
namespace jsonschema {
class schema_location
{
jsoncons::uri uri_;
std::string identifier_;
public:
schema_location()
{
}
schema_location(const std::string& uri)
{
auto pos = uri.find('#');
if (pos != std::string::npos)
{
identifier_ = uri.substr(pos + 1);
unescape_percent(identifier_);
}
uri_ = jsoncons::uri(uri);
}
jsoncons::uri uri() const
{
return uri_;
}
bool has_fragment() const
{
return !identifier_.empty();
}
bool has_identifier() const
{
return !identifier_.empty() && identifier_.front() != '/';
}
jsoncons::string_view base() const
{
return uri_.base();
}
jsoncons::string_view path() const
{
return uri_.path();
}
bool is_absolute() const
{
return uri_.is_absolute();
}
std::string identifier() const
{
return identifier_;
}
std::string fragment() const
{
return identifier_;
}
schema_location resolve(const schema_location& uri) const
{
schema_location new_uri;
new_uri.identifier_ = identifier_;
new_uri.uri_ = uri_.resolve(uri.uri_);
return new_uri;
}
int compare(const schema_location& other) const
{
int result = uri_.compare(other.uri_);
if (result != 0)
{
return result;
}
return result;
}
schema_location append(const std::string& field) const
{
if (has_identifier())
return *this;
jsoncons::jsonpointer::json_pointer pointer(std::string(uri_.fragment()));
pointer /= field;
jsoncons::uri new_uri(uri_.scheme(),
uri_.userinfo(),
uri_.host(),
uri_.port(),
uri_.path(),
uri_.query(),
pointer.to_string());
schema_location wrapper;
wrapper.uri_ = new_uri;
wrapper.identifier_ = pointer.to_string();
return wrapper;
}
schema_location append(std::size_t index) const
{
if (has_identifier())
return *this;
jsoncons::jsonpointer::json_pointer pointer(std::string(uri_.fragment()));
pointer /= index;
jsoncons::uri new_uri(uri_.scheme(),
uri_.userinfo(),
uri_.host(),
uri_.port(),
uri_.path(),
uri_.query(),
pointer.to_string());
schema_location wrapper;
wrapper.uri_ = new_uri;
wrapper.identifier_ = pointer.to_string();
return wrapper;
}
std::string string() const
{
std::string s = uri_.string();
return s;
}
friend bool operator==(const schema_location& lhs, const schema_location& rhs)
{
return lhs.compare(rhs) == 0;
}
friend bool operator!=(const schema_location& lhs, const schema_location& rhs)
{
return lhs.compare(rhs) != 0;
}
friend bool operator<(const schema_location& lhs, const schema_location& rhs)
{
return lhs.compare(rhs) < 0;
}
friend bool operator<=(const schema_location& lhs, const schema_location& rhs)
{
return lhs.compare(rhs) <= 0;
}
friend bool operator>(const schema_location& lhs, const schema_location& rhs)
{
return lhs.compare(rhs) > 0;
}
friend bool operator>=(const schema_location& lhs, const schema_location& rhs)
{
return lhs.compare(rhs) >= 0;
}
private:
static void unescape_percent(std::string& s)
{
if (s.size() >= 3)
{
std::size_t pos = s.size() - 2;
while (pos-- >= 1)
{
if (s[pos] == '%')
{
std::string hex = s.substr(pos + 1, 2);
char ch = (char) std::strtoul(hex.c_str(), nullptr, 16);
s.replace(pos, 3, 1, ch);
}
}
}
}
};
} // namespace jsonschema
} // namespace jsoncons
#endif // JSONCONS_JSONSCHEMA_RULE_HPP
|