Mercurial > hg > orthanc
annotate Core/DicomParsing/DicomWebJsonVisitor.cpp @ 3250:212abfffc44d
still increasing heap size
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 16 Feb 2019 11:38:54 +0100 |
parents | 4be505c2ac56 |
children | b88937ef597b |
rev | line source |
---|---|
3202 | 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-2019 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 "../PrecompiledHeaders.h" | |
35 #include "DicomWebJsonVisitor.h" | |
36 | |
37 #include "../OrthancException.h" | |
38 #include "../Toolbox.h" | |
39 #include "FromDcmtkBridge.h" | |
40 | |
41 #include <boost/math/special_functions/round.hpp> | |
42 #include <boost/lexical_cast.hpp> | |
43 | |
44 | |
45 static const char* const KEY_ALPHABETIC = "Alphabetic"; | |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
46 static const char* const KEY_IDEOGRAPHIC = "Ideographic"; |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
47 static const char* const KEY_PHONETIC = "Phonetic"; |
3202 | 48 static const char* const KEY_BULK_DATA_URI = "BulkDataURI"; |
49 static const char* const KEY_INLINE_BINARY = "InlineBinary"; | |
50 static const char* const KEY_SQ = "SQ"; | |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
51 static const char* const KEY_TAG = "tag"; |
3202 | 52 static const char* const KEY_VALUE = "Value"; |
53 static const char* const KEY_VR = "vr"; | |
54 | |
55 | |
56 namespace Orthanc | |
57 { | |
58 #if ORTHANC_ENABLE_PUGIXML == 1 | |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
59 static void DecomposeXmlPersonName(pugi::xml_node& target, |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
60 const std::string& source) |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
61 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
62 std::vector<std::string> tokens; |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
63 Toolbox::TokenizeString(tokens, source, '^'); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
64 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
65 if (tokens.size() >= 1) |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
66 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
67 target.append_child("FamilyName").text() = tokens[0].c_str(); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
68 } |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
69 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
70 if (tokens.size() >= 2) |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
71 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
72 target.append_child("GivenName").text() = tokens[1].c_str(); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
73 } |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
74 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
75 if (tokens.size() >= 3) |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
76 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
77 target.append_child("MiddleName").text() = tokens[2].c_str(); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
78 } |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
79 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
80 if (tokens.size() >= 4) |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
81 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
82 target.append_child("NamePrefix").text() = tokens[3].c_str(); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
83 } |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
84 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
85 if (tokens.size() >= 5) |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
86 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
87 target.append_child("NameSuffix").text() = tokens[4].c_str(); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
88 } |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
89 } |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
90 |
3202 | 91 static void ExploreXmlDataset(pugi::xml_node& target, |
92 const Json::Value& source) | |
93 { | |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
94 // http://dicom.nema.org/medical/dicom/current/output/chtml/part18/sect_F.3.html#table_F.3.1-1 |
3202 | 95 assert(source.type() == Json::objectValue); |
96 | |
97 Json::Value::Members members = source.getMemberNames(); | |
98 for (size_t i = 0; i < members.size(); i++) | |
99 { | |
100 const DicomTag tag = FromDcmtkBridge::ParseTag(members[i]); | |
101 const Json::Value& content = source[members[i]]; | |
102 | |
103 assert(content.type() == Json::objectValue && | |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
104 content.isMember(KEY_VR) && |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
105 content[KEY_VR].type() == Json::stringValue); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
106 const std::string vr = content[KEY_VR].asString(); |
3202 | 107 |
108 const std::string keyword = FromDcmtkBridge::GetTagName(tag, ""); | |
109 | |
110 pugi::xml_node node = target.append_child("DicomAttribute"); | |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
111 node.append_attribute(KEY_TAG).set_value(members[i].c_str()); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
112 node.append_attribute(KEY_VR).set_value(vr.c_str()); |
3202 | 113 |
114 if (keyword != std::string(DcmTag_ERROR_TagName)) | |
115 { | |
116 node.append_attribute("keyword").set_value(keyword.c_str()); | |
117 } | |
118 | |
119 if (content.isMember(KEY_VALUE)) | |
120 { | |
121 assert(content[KEY_VALUE].type() == Json::arrayValue); | |
122 | |
123 for (Json::Value::ArrayIndex j = 0; j < content[KEY_VALUE].size(); j++) | |
124 { | |
125 std::string number = boost::lexical_cast<std::string>(j + 1); | |
126 | |
127 if (vr == "SQ") | |
128 { | |
129 if (content[KEY_VALUE][j].type() == Json::objectValue) | |
130 { | |
131 pugi::xml_node child = node.append_child("Item"); | |
132 child.append_attribute("number").set_value(number.c_str()); | |
133 ExploreXmlDataset(child, content[KEY_VALUE][j]); | |
134 } | |
135 } | |
136 if (vr == "PN") | |
137 { | |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
138 bool hasAlphabetic = (content[KEY_VALUE][j].isMember(KEY_ALPHABETIC) && |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
139 content[KEY_VALUE][j][KEY_ALPHABETIC].type() == Json::stringValue); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
140 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
141 bool hasIdeographic = (content[KEY_VALUE][j].isMember(KEY_IDEOGRAPHIC) && |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
142 content[KEY_VALUE][j][KEY_IDEOGRAPHIC].type() == Json::stringValue); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
143 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
144 bool hasPhonetic = (content[KEY_VALUE][j].isMember(KEY_PHONETIC) && |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
145 content[KEY_VALUE][j][KEY_PHONETIC].type() == Json::stringValue); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
146 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
147 if (hasAlphabetic || |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
148 hasIdeographic || |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
149 hasPhonetic) |
3202 | 150 { |
151 pugi::xml_node child = node.append_child("PersonName"); | |
152 child.append_attribute("number").set_value(number.c_str()); | |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
153 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
154 if (hasAlphabetic) |
3202 | 155 { |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
156 pugi::xml_node name = child.append_child(KEY_ALPHABETIC); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
157 DecomposeXmlPersonName(name, content[KEY_VALUE][j][KEY_ALPHABETIC].asString()); |
3202 | 158 } |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
159 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
160 if (hasIdeographic) |
3202 | 161 { |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
162 pugi::xml_node name = child.append_child(KEY_IDEOGRAPHIC); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
163 DecomposeXmlPersonName(name, content[KEY_VALUE][j][KEY_IDEOGRAPHIC].asString()); |
3202 | 164 } |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
165 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
166 if (hasPhonetic) |
3202 | 167 { |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
168 pugi::xml_node name = child.append_child(KEY_PHONETIC); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
169 DecomposeXmlPersonName(name, content[KEY_VALUE][j][KEY_PHONETIC].asString()); |
3202 | 170 } |
171 } | |
172 } | |
173 else | |
174 { | |
175 pugi::xml_node child = node.append_child("Value"); | |
176 child.append_attribute("number").set_value(number.c_str()); | |
177 | |
178 switch (content[KEY_VALUE][j].type()) | |
179 { | |
180 case Json::stringValue: | |
181 child.text() = content[KEY_VALUE][j].asCString(); | |
182 break; | |
183 | |
184 case Json::realValue: | |
185 child.text() = content[KEY_VALUE][j].asFloat(); | |
186 break; | |
187 | |
188 case Json::intValue: | |
189 child.text() = content[KEY_VALUE][j].asInt(); | |
190 break; | |
191 | |
192 case Json::uintValue: | |
193 child.text() = content[KEY_VALUE][j].asUInt(); | |
194 break; | |
195 | |
196 default: | |
197 break; | |
198 } | |
199 } | |
200 } | |
201 } | |
202 else if (content.isMember(KEY_BULK_DATA_URI) && | |
203 content[KEY_BULK_DATA_URI].type() == Json::stringValue) | |
204 { | |
205 pugi::xml_node child = node.append_child("BulkData"); | |
206 child.append_attribute("URI").set_value(content[KEY_BULK_DATA_URI].asCString()); | |
207 } | |
208 else if (content.isMember(KEY_INLINE_BINARY) && | |
209 content[KEY_INLINE_BINARY].type() == Json::stringValue) | |
210 { | |
211 pugi::xml_node child = node.append_child("InlineBinary"); | |
212 child.text() = content[KEY_INLINE_BINARY].asCString(); | |
213 } | |
214 } | |
215 } | |
216 #endif | |
217 | |
218 | |
219 #if ORTHANC_ENABLE_PUGIXML == 1 | |
220 static void DicomWebJsonToXml(pugi::xml_document& target, | |
221 const Json::Value& source) | |
222 { | |
223 pugi::xml_node root = target.append_child("NativeDicomModel"); | |
224 root.append_attribute("xmlns").set_value("http://dicom.nema.org/PS3.19/models/NativeDICOM"); | |
225 root.append_attribute("xsi:schemaLocation").set_value("http://dicom.nema.org/PS3.19/models/NativeDICOM"); | |
226 root.append_attribute("xmlns:xsi").set_value("http://www.w3.org/2001/XMLSchema-instance"); | |
227 | |
228 ExploreXmlDataset(root, source); | |
229 | |
230 pugi::xml_node decl = target.prepend_child(pugi::node_declaration); | |
231 decl.append_attribute("version").set_value("1.0"); | |
232 decl.append_attribute("encoding").set_value("utf-8"); | |
233 } | |
234 #endif | |
235 | |
236 | |
237 std::string DicomWebJsonVisitor::FormatTag(const DicomTag& tag) | |
238 { | |
239 char buf[16]; | |
240 sprintf(buf, "%04X%04X", tag.GetGroup(), tag.GetElement()); | |
241 return std::string(buf); | |
242 } | |
243 | |
244 | |
245 Json::Value& DicomWebJsonVisitor::CreateNode(const std::vector<DicomTag>& parentTags, | |
246 const std::vector<size_t>& parentIndexes, | |
247 const DicomTag& tag) | |
248 { | |
249 assert(parentTags.size() == parentIndexes.size()); | |
250 | |
251 Json::Value* node = &result_; | |
252 | |
253 for (size_t i = 0; i < parentTags.size(); i++) | |
254 { | |
255 std::string t = FormatTag(parentTags[i]); | |
256 | |
257 if (!node->isMember(t)) | |
258 { | |
259 Json::Value item = Json::objectValue; | |
260 item[KEY_VR] = KEY_SQ; | |
261 item[KEY_VALUE] = Json::arrayValue; | |
262 item[KEY_VALUE].append(Json::objectValue); | |
263 (*node) [t] = item; | |
264 | |
265 node = &(*node)[t][KEY_VALUE][0]; | |
266 } | |
267 else if ((*node) [t].type() != Json::objectValue || | |
268 !(*node) [t].isMember(KEY_VR) || | |
269 (*node) [t][KEY_VR].type() != Json::stringValue || | |
270 (*node) [t][KEY_VR].asString() != KEY_SQ || | |
271 !(*node) [t].isMember(KEY_VALUE) || | |
272 (*node) [t][KEY_VALUE].type() != Json::arrayValue) | |
273 { | |
274 throw OrthancException(ErrorCode_InternalError); | |
275 } | |
276 else | |
277 { | |
278 size_t currentSize = (*node) [t][KEY_VALUE].size(); | |
279 | |
280 if (parentIndexes[i] < currentSize) | |
281 { | |
282 // The node already exists | |
283 } | |
284 else if (parentIndexes[i] == currentSize) | |
285 { | |
286 (*node) [t][KEY_VALUE].append(Json::objectValue); | |
287 } | |
288 else | |
289 { | |
290 throw OrthancException(ErrorCode_InternalError); | |
291 } | |
292 | |
293 node = &(*node) [t][KEY_VALUE][Json::ArrayIndex(parentIndexes[i])]; | |
294 } | |
295 } | |
296 | |
297 assert(node->type() == Json::objectValue); | |
298 | |
299 std::string t = FormatTag(tag); | |
300 if (node->isMember(t)) | |
301 { | |
302 throw OrthancException(ErrorCode_InternalError); | |
303 } | |
304 else | |
305 { | |
306 (*node) [t] = Json::objectValue; | |
307 return (*node) [t]; | |
308 } | |
309 } | |
310 | |
311 | |
312 Json::Value DicomWebJsonVisitor::FormatInteger(int64_t value) | |
313 { | |
314 if (value < 0) | |
315 { | |
316 return Json::Value(static_cast<int32_t>(value)); | |
317 } | |
318 else | |
319 { | |
320 return Json::Value(static_cast<uint32_t>(value)); | |
321 } | |
322 } | |
323 | |
324 | |
325 Json::Value DicomWebJsonVisitor::FormatDouble(double value) | |
326 { | |
327 long long a = boost::math::llround<double>(value); | |
328 | |
329 double d = fabs(value - static_cast<double>(a)); | |
330 | |
331 if (d <= std::numeric_limits<double>::epsilon() * 100.0) | |
332 { | |
333 return FormatInteger(a); | |
334 } | |
335 else | |
336 { | |
337 return Json::Value(value); | |
338 } | |
339 } | |
340 | |
341 | |
342 #if ORTHANC_ENABLE_PUGIXML == 1 | |
3203
810772486249
URI "/instances/.../file" can return DICOMweb JSON or XML, depending on Accept header
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3202
diff
changeset
|
343 void DicomWebJsonVisitor::FormatXml(std::string& target) const |
3202 | 344 { |
3203
810772486249
URI "/instances/.../file" can return DICOMweb JSON or XML, depending on Accept header
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3202
diff
changeset
|
345 pugi::xml_document doc; |
810772486249
URI "/instances/.../file" can return DICOMweb JSON or XML, depending on Accept header
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3202
diff
changeset
|
346 DicomWebJsonToXml(doc, result_); |
810772486249
URI "/instances/.../file" can return DICOMweb JSON or XML, depending on Accept header
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3202
diff
changeset
|
347 Toolbox::XmlToString(target, doc); |
3202 | 348 } |
349 #endif | |
350 | |
351 | |
352 void DicomWebJsonVisitor::VisitEmptySequence(const std::vector<DicomTag>& parentTags, | |
353 const std::vector<size_t>& parentIndexes, | |
354 const DicomTag& tag) | |
355 { | |
356 if (tag.GetElement() != 0x0000) | |
357 { | |
358 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
359 node[KEY_VR] = EnumerationToString(ValueRepresentation_Sequence); | |
360 } | |
361 } | |
362 | |
363 | |
364 void DicomWebJsonVisitor::VisitBinary(const std::vector<DicomTag>& parentTags, | |
365 const std::vector<size_t>& parentIndexes, | |
366 const DicomTag& tag, | |
367 ValueRepresentation vr, | |
368 const void* data, | |
369 size_t size) | |
370 { | |
371 assert(vr == ValueRepresentation_OtherByte || | |
372 vr == ValueRepresentation_OtherDouble || | |
373 vr == ValueRepresentation_OtherFloat || | |
374 vr == ValueRepresentation_OtherLong || | |
375 vr == ValueRepresentation_OtherWord || | |
376 vr == ValueRepresentation_Unknown); | |
377 | |
378 if (tag.GetElement() != 0x0000) | |
379 { | |
380 BinaryMode mode; | |
381 std::string bulkDataUri; | |
382 | |
383 if (formatter_ == NULL) | |
384 { | |
385 mode = BinaryMode_InlineBinary; | |
386 } | |
387 else | |
388 { | |
389 mode = formatter_->Format(bulkDataUri, parentTags, parentIndexes, tag, vr); | |
390 } | |
391 | |
392 if (mode != BinaryMode_Ignore) | |
393 { | |
394 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
395 node[KEY_VR] = EnumerationToString(vr); | |
396 | |
397 switch (mode) | |
398 { | |
399 case BinaryMode_BulkDataUri: | |
400 node[KEY_BULK_DATA_URI] = bulkDataUri; | |
401 break; | |
402 | |
403 case BinaryMode_InlineBinary: | |
404 { | |
405 std::string tmp(static_cast<const char*>(data), size); | |
406 | |
407 std::string base64; | |
408 Toolbox::EncodeBase64(base64, tmp); | |
409 | |
410 node[KEY_INLINE_BINARY] = base64; | |
411 break; | |
412 } | |
413 | |
414 default: | |
415 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
416 } | |
417 } | |
418 } | |
419 } | |
420 | |
421 | |
422 void DicomWebJsonVisitor::VisitIntegers(const std::vector<DicomTag>& parentTags, | |
423 const std::vector<size_t>& parentIndexes, | |
424 const DicomTag& tag, | |
425 ValueRepresentation vr, | |
426 const std::vector<int64_t>& values) | |
427 { | |
428 if (tag.GetElement() != 0x0000 && | |
429 vr != ValueRepresentation_NotSupported) | |
430 { | |
431 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
432 node[KEY_VR] = EnumerationToString(vr); | |
433 | |
434 if (!values.empty()) | |
435 { | |
436 Json::Value content = Json::arrayValue; | |
437 for (size_t i = 0; i < values.size(); i++) | |
438 { | |
439 content.append(FormatInteger(values[i])); | |
440 } | |
441 | |
442 node[KEY_VALUE] = content; | |
443 } | |
444 } | |
445 } | |
446 | |
447 void DicomWebJsonVisitor::VisitDoubles(const std::vector<DicomTag>& parentTags, | |
448 const std::vector<size_t>& parentIndexes, | |
449 const DicomTag& tag, | |
450 ValueRepresentation vr, | |
451 const std::vector<double>& values) | |
452 { | |
453 if (tag.GetElement() != 0x0000 && | |
454 vr != ValueRepresentation_NotSupported) | |
455 { | |
456 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
457 node[KEY_VR] = EnumerationToString(vr); | |
458 | |
459 if (!values.empty()) | |
460 { | |
461 Json::Value content = Json::arrayValue; | |
462 for (size_t i = 0; i < values.size(); i++) | |
463 { | |
464 content.append(FormatDouble(values[i])); | |
465 } | |
466 | |
467 node[KEY_VALUE] = content; | |
468 } | |
469 } | |
470 } | |
471 | |
472 | |
473 void DicomWebJsonVisitor::VisitAttributes(const std::vector<DicomTag>& parentTags, | |
474 const std::vector<size_t>& parentIndexes, | |
475 const DicomTag& tag, | |
476 const std::vector<DicomTag>& values) | |
477 { | |
478 if (tag.GetElement() != 0x0000) | |
479 { | |
480 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
481 node[KEY_VR] = EnumerationToString(ValueRepresentation_AttributeTag); | |
482 | |
483 if (!values.empty()) | |
484 { | |
485 Json::Value content = Json::arrayValue; | |
486 for (size_t i = 0; i < values.size(); i++) | |
487 { | |
488 content.append(FormatTag(values[i])); | |
489 } | |
490 | |
491 node[KEY_VALUE] = content; | |
492 } | |
493 } | |
494 } | |
495 | |
496 | |
497 ITagVisitor::Action | |
498 DicomWebJsonVisitor::VisitString(std::string& newValue, | |
499 const std::vector<DicomTag>& parentTags, | |
500 const std::vector<size_t>& parentIndexes, | |
501 const DicomTag& tag, | |
502 ValueRepresentation vr, | |
503 const std::string& value) | |
504 { | |
505 if (tag.GetElement() == 0x0000 || | |
506 vr == ValueRepresentation_NotSupported) | |
507 { | |
508 return Action_None; | |
509 } | |
510 else | |
511 { | |
512 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
513 node[KEY_VR] = EnumerationToString(vr); | |
514 | |
515 if (tag == DICOM_TAG_SPECIFIC_CHARACTER_SET) | |
516 { | |
517 // TODO - The JSON file has an UTF-8 encoding, thus DCMTK | |
518 // replaces the specific character set with "ISO_IR 192" | |
519 // (UNICODE UTF-8). It is unclear whether the source | |
520 // character set should be kept: We thus mimic DCMTK. | |
521 node[KEY_VALUE].append("ISO_IR 192"); | |
522 } | |
523 else | |
524 { | |
525 std::string truncated; | |
526 | |
527 if (!value.empty() && | |
528 value[value.size() - 1] == '\0') | |
529 { | |
530 truncated = value.substr(0, value.size() - 1); | |
531 } | |
532 else | |
533 { | |
534 truncated = value; | |
535 } | |
536 | |
537 if (!truncated.empty()) | |
538 { | |
539 std::vector<std::string> tokens; | |
540 Toolbox::TokenizeString(tokens, truncated, '\\'); | |
541 | |
542 node[KEY_VALUE] = Json::arrayValue; | |
543 for (size_t i = 0; i < tokens.size(); i++) | |
544 { | |
545 try | |
546 { | |
547 switch (vr) | |
548 { | |
549 case ValueRepresentation_PersonName: | |
550 { | |
551 Json::Value value = Json::objectValue; | |
552 if (!tokens[i].empty()) | |
553 { | |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
554 std::vector<std::string> components; |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
555 Toolbox::TokenizeString(components, tokens[i], '='); |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
556 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
557 if (components.size() >= 1) |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
558 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
559 value[KEY_ALPHABETIC] = components[0]; |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
560 } |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
561 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
562 if (components.size() >= 2) |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
563 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
564 value[KEY_IDEOGRAPHIC] = components[1]; |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
565 } |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
566 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
567 if (components.size() >= 3) |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
568 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
569 value[KEY_PHONETIC] = components[2]; |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
570 } |
3202 | 571 } |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
572 |
3202 | 573 node[KEY_VALUE].append(value); |
574 break; | |
575 } | |
576 | |
577 case ValueRepresentation_IntegerString: | |
578 if (tokens[i].empty()) | |
579 { | |
580 node[KEY_VALUE].append(Json::nullValue); | |
581 } | |
582 else | |
583 { | |
584 int64_t value = boost::lexical_cast<int64_t>(tokens[i]); | |
585 node[KEY_VALUE].append(FormatInteger(value)); | |
586 } | |
587 | |
588 break; | |
589 | |
590 case ValueRepresentation_DecimalString: | |
591 if (tokens[i].empty()) | |
592 { | |
593 node[KEY_VALUE].append(Json::nullValue); | |
594 } | |
595 else | |
596 { | |
597 double value = boost::lexical_cast<double>(tokens[i]); | |
598 node[KEY_VALUE].append(FormatDouble(value)); | |
599 } | |
600 break; | |
601 | |
602 default: | |
603 if (tokens[i].empty()) | |
604 { | |
605 node[KEY_VALUE].append(Json::nullValue); | |
606 } | |
607 else | |
608 { | |
609 node[KEY_VALUE].append(tokens[i]); | |
610 } | |
611 | |
612 break; | |
613 } | |
614 } | |
615 catch (boost::bad_lexical_cast&) | |
616 { | |
617 throw OrthancException(ErrorCode_BadFileFormat); | |
618 } | |
619 } | |
620 } | |
621 } | |
622 } | |
623 | |
624 return Action_None; | |
625 } | |
626 } |