comparison Resources/Orthanc/Plugins/Samples/Common/FullOrthancDataset.cpp @ 200:03afbee0cc7b

integration of Orthanc core into Stone
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Mar 2018 11:04:03 +0100
parents
children
comparison
equal deleted inserted replaced
199:dabe9982fca3 200:03afbee0cc7b
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 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 General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "FullOrthancDataset.h"
35
36 #include "OrthancPluginException.h"
37
38 #include <stdio.h>
39 #include <cassert>
40
41 namespace OrthancPlugins
42 {
43 static const Json::Value* AccessTag(const Json::Value& dataset,
44 const DicomTag& tag)
45 {
46 if (dataset.type() != Json::objectValue)
47 {
48 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
49 }
50
51 char name[16];
52 sprintf(name, "%04x,%04x", tag.GetGroup(), tag.GetElement());
53
54 if (!dataset.isMember(name))
55 {
56 return NULL;
57 }
58
59 const Json::Value& value = dataset[name];
60 if (value.type() != Json::objectValue ||
61 !value.isMember("Name") ||
62 !value.isMember("Type") ||
63 !value.isMember("Value") ||
64 value["Name"].type() != Json::stringValue ||
65 value["Type"].type() != Json::stringValue)
66 {
67 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
68 }
69
70 return &value;
71 }
72
73
74 static const Json::Value& GetSequenceContent(const Json::Value& sequence)
75 {
76 assert(sequence.type() == Json::objectValue);
77 assert(sequence.isMember("Type"));
78 assert(sequence.isMember("Value"));
79
80 const Json::Value& value = sequence["Value"];
81
82 if (sequence["Type"].asString() != "Sequence" ||
83 value.type() != Json::arrayValue)
84 {
85 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
86 }
87 else
88 {
89 return value;
90 }
91 }
92
93
94 static bool GetStringInternal(std::string& result,
95 const Json::Value& tag)
96 {
97 assert(tag.type() == Json::objectValue);
98 assert(tag.isMember("Type"));
99 assert(tag.isMember("Value"));
100
101 const Json::Value& value = tag["Value"];
102
103 if (tag["Type"].asString() != "String" ||
104 value.type() != Json::stringValue)
105 {
106 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
107 }
108 else
109 {
110 result = value.asString();
111 return true;
112 }
113 }
114
115
116 const Json::Value* FullOrthancDataset::LookupPath(const DicomPath& path) const
117 {
118 const Json::Value* content = &root_;
119
120 for (unsigned int depth = 0; depth < path.GetPrefixLength(); depth++)
121 {
122 const Json::Value* sequence = AccessTag(*content, path.GetPrefixTag(depth));
123 if (sequence == NULL)
124 {
125 return NULL;
126 }
127
128 const Json::Value& nextContent = GetSequenceContent(*sequence);
129
130 size_t index = path.GetPrefixIndex(depth);
131 if (index >= nextContent.size())
132 {
133 return NULL;
134 }
135 else
136 {
137 content = &nextContent[static_cast<Json::Value::ArrayIndex>(index)];
138 }
139 }
140
141 return AccessTag(*content, path.GetFinalTag());
142 }
143
144
145 void FullOrthancDataset::CheckRoot() const
146 {
147 if (root_.type() != Json::objectValue)
148 {
149 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
150 }
151 }
152
153
154 FullOrthancDataset::FullOrthancDataset(IOrthancConnection& orthanc,
155 const std::string& uri)
156 {
157 IOrthancConnection::RestApiGet(root_, orthanc, uri);
158 CheckRoot();
159 }
160
161
162 FullOrthancDataset::FullOrthancDataset(const std::string& content)
163 {
164 IOrthancConnection::ParseJson(root_, content);
165 CheckRoot();
166 }
167
168
169 FullOrthancDataset::FullOrthancDataset(const void* content,
170 size_t size)
171 {
172 IOrthancConnection::ParseJson(root_, content, size);
173 CheckRoot();
174 }
175
176
177 FullOrthancDataset::FullOrthancDataset(const Json::Value& root) :
178 root_(root)
179 {
180 CheckRoot();
181 }
182
183
184 bool FullOrthancDataset::GetStringValue(std::string& result,
185 const DicomPath& path) const
186 {
187 const Json::Value* value = LookupPath(path);
188
189 if (value == NULL)
190 {
191 return false;
192 }
193 else
194 {
195 return GetStringInternal(result, *value);
196 }
197 }
198
199
200 bool FullOrthancDataset::GetSequenceSize(size_t& size,
201 const DicomPath& path) const
202 {
203 const Json::Value* sequence = LookupPath(path);
204
205 if (sequence == NULL)
206 {
207 return false;
208 }
209 else
210 {
211 size = GetSequenceContent(*sequence).size();
212 return true;
213 }
214 }
215 }