1504
|
1 /**
|
|
2 * Stone of Orthanc
|
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
|
|
4 * Department, University Hospital of Liege, Belgium
|
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
|
|
6 *
|
|
7 * This program is free software: you can redistribute it and/or
|
|
8 * modify it under the terms of the GNU Affero General Public License
|
|
9 * as published by the Free Software Foundation, either version 3 of
|
|
10 * the License, or (at your option) any later version.
|
|
11 *
|
|
12 * This program is distributed in the hope that it will be useful, but
|
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 * Affero General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU Affero General Public License
|
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 **/
|
|
20
|
|
21
|
|
22 #include "FullOrthancDataset.h"
|
|
23
|
|
24 #include <OrthancException.h>
|
|
25
|
|
26 #include <stdio.h>
|
|
27 #include <cassert>
|
|
28
|
|
29 namespace OrthancStone
|
|
30 {
|
|
31 static const Json::Value* AccessTag(const Json::Value& dataset,
|
|
32 const Orthanc::DicomTag& tag)
|
|
33 {
|
|
34 if (dataset.type() != Json::objectValue)
|
|
35 {
|
|
36 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
37 }
|
|
38
|
|
39 char name[16];
|
|
40 sprintf(name, "%04x,%04x", tag.GetGroup(), tag.GetElement());
|
|
41
|
|
42 if (!dataset.isMember(name))
|
|
43 {
|
|
44 return NULL;
|
|
45 }
|
|
46
|
|
47 const Json::Value& value = dataset[name];
|
|
48 if (value.type() != Json::objectValue ||
|
|
49 !value.isMember("Name") ||
|
|
50 !value.isMember("Type") ||
|
|
51 !value.isMember("Value") ||
|
|
52 value["Name"].type() != Json::stringValue ||
|
|
53 value["Type"].type() != Json::stringValue)
|
|
54 {
|
|
55 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
56 }
|
|
57
|
|
58 return &value;
|
|
59 }
|
|
60
|
|
61
|
|
62 static const Json::Value& GetSequenceContent(const Json::Value& sequence)
|
|
63 {
|
|
64 assert(sequence.type() == Json::objectValue);
|
|
65 assert(sequence.isMember("Type"));
|
|
66 assert(sequence.isMember("Value"));
|
|
67
|
|
68 const Json::Value& value = sequence["Value"];
|
|
69
|
|
70 if (sequence["Type"].asString() != "Sequence" ||
|
|
71 value.type() != Json::arrayValue)
|
|
72 {
|
|
73 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
74 }
|
|
75 else
|
|
76 {
|
|
77 return value;
|
|
78 }
|
|
79 }
|
|
80
|
|
81
|
|
82 static bool GetStringInternal(std::string& result,
|
|
83 const Json::Value& tag)
|
|
84 {
|
|
85 assert(tag.type() == Json::objectValue);
|
|
86 assert(tag.isMember("Type"));
|
|
87 assert(tag.isMember("Value"));
|
|
88
|
|
89 const Json::Value& value = tag["Value"];
|
|
90
|
|
91 if (tag["Type"].asString() != "String" ||
|
|
92 value.type() != Json::stringValue)
|
|
93 {
|
|
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
95 }
|
|
96 else
|
|
97 {
|
|
98 result = value.asString();
|
|
99 return true;
|
|
100 }
|
|
101 }
|
|
102
|
|
103
|
|
104 const Json::Value* FullOrthancDataset::LookupPath(const DicomPath& path) const
|
|
105 {
|
|
106 const Json::Value* content = &root_;
|
|
107
|
|
108 for (unsigned int depth = 0; depth < path.GetPrefixLength(); depth++)
|
|
109 {
|
|
110 const Json::Value* sequence = AccessTag(*content, path.GetPrefixTag(depth));
|
|
111 if (sequence == NULL)
|
|
112 {
|
|
113 return NULL;
|
|
114 }
|
|
115
|
|
116 const Json::Value& nextContent = GetSequenceContent(*sequence);
|
|
117
|
|
118 size_t index = path.GetPrefixIndex(depth);
|
|
119 if (index >= nextContent.size())
|
|
120 {
|
|
121 return NULL;
|
|
122 }
|
|
123 else
|
|
124 {
|
|
125 content = &nextContent[static_cast<Json::Value::ArrayIndex>(index)];
|
|
126 }
|
|
127 }
|
|
128
|
|
129 return AccessTag(*content, path.GetFinalTag());
|
|
130 }
|
|
131
|
|
132
|
|
133 void FullOrthancDataset::CheckRoot() const
|
|
134 {
|
|
135 if (root_.type() != Json::objectValue)
|
|
136 {
|
|
137 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
138 }
|
|
139 }
|
|
140
|
|
141
|
|
142 FullOrthancDataset::FullOrthancDataset(IOrthancConnection& orthanc,
|
|
143 const std::string& uri)
|
|
144 {
|
|
145 IOrthancConnection::RestApiGet(root_, orthanc, uri);
|
|
146 CheckRoot();
|
|
147 }
|
|
148
|
|
149
|
|
150 FullOrthancDataset::FullOrthancDataset(const std::string& content)
|
|
151 {
|
|
152 IOrthancConnection::ParseJson(root_, content);
|
|
153 CheckRoot();
|
|
154 }
|
|
155
|
|
156
|
|
157 FullOrthancDataset::FullOrthancDataset(const void* content,
|
|
158 size_t size)
|
|
159 {
|
|
160 IOrthancConnection::ParseJson(root_, content, size);
|
|
161 CheckRoot();
|
|
162 }
|
|
163
|
|
164
|
|
165 FullOrthancDataset::FullOrthancDataset(const Json::Value& root) :
|
|
166 root_(root)
|
|
167 {
|
|
168 CheckRoot();
|
|
169 }
|
|
170
|
|
171
|
|
172 bool FullOrthancDataset::GetStringValue(std::string& result,
|
|
173 const DicomPath& path) const
|
|
174 {
|
|
175 const Json::Value* value = LookupPath(path);
|
|
176
|
|
177 if (value == NULL)
|
|
178 {
|
|
179 return false;
|
|
180 }
|
|
181 else
|
|
182 {
|
|
183 return GetStringInternal(result, *value);
|
|
184 }
|
|
185 }
|
|
186
|
|
187
|
|
188 bool FullOrthancDataset::GetSequenceSize(size_t& size,
|
|
189 const DicomPath& path) const
|
|
190 {
|
|
191 const Json::Value* sequence = LookupPath(path);
|
|
192
|
|
193 if (sequence == NULL)
|
|
194 {
|
|
195 return false;
|
|
196 }
|
|
197 else
|
|
198 {
|
|
199 size = GetSequenceContent(*sequence).size();
|
|
200 return true;
|
|
201 }
|
|
202 }
|
|
203 }
|