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 "SimplifiedOrthancDataset.h"
|
|
23
|
1571
|
24 #include <OrthancException.h>
|
|
25 #include <DicomParsing/FromDcmtkBridge.h>
|
|
26
|
1504
|
27 namespace OrthancStone
|
|
28 {
|
|
29 const Json::Value* SimplifiedOrthancDataset::LookupPath(const DicomPath& path) const
|
|
30 {
|
|
31 const Json::Value* content = &root_;
|
|
32
|
|
33 for (unsigned int depth = 0; depth < path.GetPrefixLength(); depth++)
|
|
34 {
|
1571
|
35 const std::string name = Orthanc::FromDcmtkBridge::GetTagName(
|
|
36 path.GetPrefixTag(depth), "" /* no private creator */);
|
1504
|
37 if (content->type() != Json::objectValue)
|
|
38 {
|
|
39 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
40 }
|
|
41
|
|
42 if (!content->isMember(name))
|
|
43 {
|
|
44 return NULL;
|
|
45 }
|
|
46
|
|
47 const Json::Value& sequence = (*content) [name];
|
|
48 if (sequence.type() != Json::arrayValue)
|
|
49 {
|
|
50 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
51 }
|
|
52
|
|
53 size_t index = path.GetPrefixIndex(depth);
|
|
54 if (index >= sequence.size())
|
|
55 {
|
|
56 return NULL;
|
|
57 }
|
|
58 else
|
|
59 {
|
|
60 content = &sequence[static_cast<Json::Value::ArrayIndex>(index)];
|
|
61 }
|
|
62 }
|
|
63
|
1571
|
64 const std::string name = Orthanc::FromDcmtkBridge::GetTagName(
|
|
65 path.GetFinalTag(), "" /* no private creator */);
|
1504
|
66
|
|
67 if (content->type() != Json::objectValue)
|
|
68 {
|
|
69 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
70 }
|
|
71 if (!content->isMember(name))
|
|
72 {
|
|
73 return NULL;
|
|
74 }
|
|
75 else
|
|
76 {
|
|
77 return &((*content) [name]);
|
|
78 }
|
|
79 }
|
|
80
|
|
81
|
|
82 void SimplifiedOrthancDataset::CheckRoot() const
|
|
83 {
|
|
84 if (root_.type() != Json::objectValue)
|
|
85 {
|
|
86 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
87 }
|
|
88 }
|
|
89
|
|
90
|
|
91 SimplifiedOrthancDataset::SimplifiedOrthancDataset(IOrthancConnection& orthanc,
|
|
92 const std::string& uri)
|
|
93 {
|
|
94 IOrthancConnection::RestApiGet(root_, orthanc, uri);
|
|
95 CheckRoot();
|
|
96 }
|
|
97
|
|
98
|
|
99 SimplifiedOrthancDataset::SimplifiedOrthancDataset(const std::string& content)
|
|
100 {
|
|
101 IOrthancConnection::ParseJson(root_, content);
|
|
102 CheckRoot();
|
|
103 }
|
|
104
|
|
105
|
|
106 bool SimplifiedOrthancDataset::GetStringValue(std::string& result,
|
|
107 const DicomPath& path) const
|
|
108 {
|
|
109 const Json::Value* value = LookupPath(path);
|
|
110
|
|
111 if (value == NULL)
|
|
112 {
|
|
113 return false;
|
|
114 }
|
|
115 else if (value->type() != Json::stringValue)
|
|
116 {
|
|
117 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
118 }
|
|
119 else
|
|
120 {
|
|
121 result = value->asString();
|
|
122 return true;
|
|
123 }
|
|
124 }
|
|
125
|
|
126
|
|
127 bool SimplifiedOrthancDataset::GetSequenceSize(size_t& size,
|
|
128 const DicomPath& path) const
|
|
129 {
|
|
130 const Json::Value* sequence = LookupPath(path);
|
|
131
|
|
132 if (sequence == NULL)
|
|
133 {
|
|
134 // Inexistent path
|
|
135 return false;
|
|
136 }
|
|
137 else if (sequence->type() != Json::arrayValue)
|
|
138 {
|
|
139 // Not a sequence
|
|
140 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
|
|
141 }
|
|
142 else
|
|
143 {
|
|
144 size = sequence->size();
|
|
145 return true;
|
|
146 }
|
|
147 }
|
|
148 }
|