Mercurial > hg > orthanc
annotate Core/DicomParsing/DicomWebJsonVisitor.cpp @ 3349:ef18ea68a362
merge
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Thu, 18 Apr 2019 15:52:56 +0200 |
parents | b88937ef597b |
children | eb57b7cd5f21 |
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 { | |
3312 | 327 try |
328 { | |
329 long long a = boost::math::llround<double>(value); | |
3202 | 330 |
3312 | 331 double d = fabs(value - static_cast<double>(a)); |
3202 | 332 |
3312 | 333 if (d <= std::numeric_limits<double>::epsilon() * 100.0) |
334 { | |
335 return FormatInteger(a); | |
336 } | |
337 else | |
338 { | |
339 return Json::Value(value); | |
340 } | |
341 } | |
342 catch (boost::math::rounding_error&) | |
3202 | 343 { |
3312 | 344 // Can occur if "long long" is too small to receive this value |
345 // (e.g. infinity) | |
3202 | 346 return Json::Value(value); |
347 } | |
348 } | |
349 | |
350 | |
351 #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
|
352 void DicomWebJsonVisitor::FormatXml(std::string& target) const |
3202 | 353 { |
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
|
354 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
|
355 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
|
356 Toolbox::XmlToString(target, doc); |
3202 | 357 } |
358 #endif | |
359 | |
360 | |
361 void DicomWebJsonVisitor::VisitEmptySequence(const std::vector<DicomTag>& parentTags, | |
362 const std::vector<size_t>& parentIndexes, | |
363 const DicomTag& tag) | |
364 { | |
365 if (tag.GetElement() != 0x0000) | |
366 { | |
367 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
368 node[KEY_VR] = EnumerationToString(ValueRepresentation_Sequence); | |
369 } | |
370 } | |
371 | |
372 | |
373 void DicomWebJsonVisitor::VisitBinary(const std::vector<DicomTag>& parentTags, | |
374 const std::vector<size_t>& parentIndexes, | |
375 const DicomTag& tag, | |
376 ValueRepresentation vr, | |
377 const void* data, | |
378 size_t size) | |
379 { | |
380 assert(vr == ValueRepresentation_OtherByte || | |
381 vr == ValueRepresentation_OtherDouble || | |
382 vr == ValueRepresentation_OtherFloat || | |
383 vr == ValueRepresentation_OtherLong || | |
384 vr == ValueRepresentation_OtherWord || | |
385 vr == ValueRepresentation_Unknown); | |
386 | |
387 if (tag.GetElement() != 0x0000) | |
388 { | |
389 BinaryMode mode; | |
390 std::string bulkDataUri; | |
391 | |
392 if (formatter_ == NULL) | |
393 { | |
394 mode = BinaryMode_InlineBinary; | |
395 } | |
396 else | |
397 { | |
398 mode = formatter_->Format(bulkDataUri, parentTags, parentIndexes, tag, vr); | |
399 } | |
400 | |
401 if (mode != BinaryMode_Ignore) | |
402 { | |
403 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
404 node[KEY_VR] = EnumerationToString(vr); | |
405 | |
406 switch (mode) | |
407 { | |
408 case BinaryMode_BulkDataUri: | |
409 node[KEY_BULK_DATA_URI] = bulkDataUri; | |
410 break; | |
411 | |
412 case BinaryMode_InlineBinary: | |
413 { | |
414 std::string tmp(static_cast<const char*>(data), size); | |
415 | |
416 std::string base64; | |
417 Toolbox::EncodeBase64(base64, tmp); | |
418 | |
419 node[KEY_INLINE_BINARY] = base64; | |
420 break; | |
421 } | |
422 | |
423 default: | |
424 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
425 } | |
426 } | |
427 } | |
428 } | |
429 | |
430 | |
431 void DicomWebJsonVisitor::VisitIntegers(const std::vector<DicomTag>& parentTags, | |
432 const std::vector<size_t>& parentIndexes, | |
433 const DicomTag& tag, | |
434 ValueRepresentation vr, | |
435 const std::vector<int64_t>& values) | |
436 { | |
437 if (tag.GetElement() != 0x0000 && | |
438 vr != ValueRepresentation_NotSupported) | |
439 { | |
440 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
441 node[KEY_VR] = EnumerationToString(vr); | |
442 | |
443 if (!values.empty()) | |
444 { | |
445 Json::Value content = Json::arrayValue; | |
446 for (size_t i = 0; i < values.size(); i++) | |
447 { | |
448 content.append(FormatInteger(values[i])); | |
449 } | |
450 | |
451 node[KEY_VALUE] = content; | |
452 } | |
453 } | |
454 } | |
455 | |
456 void DicomWebJsonVisitor::VisitDoubles(const std::vector<DicomTag>& parentTags, | |
457 const std::vector<size_t>& parentIndexes, | |
458 const DicomTag& tag, | |
459 ValueRepresentation vr, | |
460 const std::vector<double>& values) | |
461 { | |
462 if (tag.GetElement() != 0x0000 && | |
463 vr != ValueRepresentation_NotSupported) | |
464 { | |
465 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
466 node[KEY_VR] = EnumerationToString(vr); | |
467 | |
468 if (!values.empty()) | |
469 { | |
470 Json::Value content = Json::arrayValue; | |
471 for (size_t i = 0; i < values.size(); i++) | |
472 { | |
473 content.append(FormatDouble(values[i])); | |
474 } | |
475 | |
476 node[KEY_VALUE] = content; | |
477 } | |
478 } | |
479 } | |
480 | |
481 | |
482 void DicomWebJsonVisitor::VisitAttributes(const std::vector<DicomTag>& parentTags, | |
483 const std::vector<size_t>& parentIndexes, | |
484 const DicomTag& tag, | |
485 const std::vector<DicomTag>& values) | |
486 { | |
487 if (tag.GetElement() != 0x0000) | |
488 { | |
489 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
490 node[KEY_VR] = EnumerationToString(ValueRepresentation_AttributeTag); | |
491 | |
492 if (!values.empty()) | |
493 { | |
494 Json::Value content = Json::arrayValue; | |
495 for (size_t i = 0; i < values.size(); i++) | |
496 { | |
497 content.append(FormatTag(values[i])); | |
498 } | |
499 | |
500 node[KEY_VALUE] = content; | |
501 } | |
502 } | |
503 } | |
504 | |
505 | |
506 ITagVisitor::Action | |
507 DicomWebJsonVisitor::VisitString(std::string& newValue, | |
508 const std::vector<DicomTag>& parentTags, | |
509 const std::vector<size_t>& parentIndexes, | |
510 const DicomTag& tag, | |
511 ValueRepresentation vr, | |
512 const std::string& value) | |
513 { | |
514 if (tag.GetElement() == 0x0000 || | |
515 vr == ValueRepresentation_NotSupported) | |
516 { | |
517 return Action_None; | |
518 } | |
519 else | |
520 { | |
521 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); | |
522 node[KEY_VR] = EnumerationToString(vr); | |
523 | |
524 if (tag == DICOM_TAG_SPECIFIC_CHARACTER_SET) | |
525 { | |
526 // TODO - The JSON file has an UTF-8 encoding, thus DCMTK | |
527 // replaces the specific character set with "ISO_IR 192" | |
528 // (UNICODE UTF-8). It is unclear whether the source | |
529 // character set should be kept: We thus mimic DCMTK. | |
530 node[KEY_VALUE].append("ISO_IR 192"); | |
531 } | |
532 else | |
533 { | |
534 std::string truncated; | |
535 | |
536 if (!value.empty() && | |
537 value[value.size() - 1] == '\0') | |
538 { | |
539 truncated = value.substr(0, value.size() - 1); | |
540 } | |
541 else | |
542 { | |
543 truncated = value; | |
544 } | |
545 | |
546 if (!truncated.empty()) | |
547 { | |
548 std::vector<std::string> tokens; | |
549 Toolbox::TokenizeString(tokens, truncated, '\\'); | |
550 | |
551 node[KEY_VALUE] = Json::arrayValue; | |
552 for (size_t i = 0; i < tokens.size(); i++) | |
553 { | |
554 try | |
555 { | |
556 switch (vr) | |
557 { | |
558 case ValueRepresentation_PersonName: | |
559 { | |
560 Json::Value value = Json::objectValue; | |
561 if (!tokens[i].empty()) | |
562 { | |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
563 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
|
564 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
|
565 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
566 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
|
567 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
568 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
|
569 } |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
570 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
571 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
|
572 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
573 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
|
574 } |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
575 |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
576 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
|
577 { |
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
578 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
|
579 } |
3202 | 580 } |
3221
4be505c2ac56
Separation of ideographic and phonetic characters in DICOMweb JSON and XML
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3203
diff
changeset
|
581 |
3202 | 582 node[KEY_VALUE].append(value); |
583 break; | |
584 } | |
585 | |
586 case ValueRepresentation_IntegerString: | |
587 if (tokens[i].empty()) | |
588 { | |
589 node[KEY_VALUE].append(Json::nullValue); | |
590 } | |
591 else | |
592 { | |
593 int64_t value = boost::lexical_cast<int64_t>(tokens[i]); | |
594 node[KEY_VALUE].append(FormatInteger(value)); | |
595 } | |
596 | |
597 break; | |
598 | |
599 case ValueRepresentation_DecimalString: | |
600 if (tokens[i].empty()) | |
601 { | |
602 node[KEY_VALUE].append(Json::nullValue); | |
603 } | |
604 else | |
605 { | |
606 double value = boost::lexical_cast<double>(tokens[i]); | |
607 node[KEY_VALUE].append(FormatDouble(value)); | |
608 } | |
609 break; | |
610 | |
611 default: | |
612 if (tokens[i].empty()) | |
613 { | |
614 node[KEY_VALUE].append(Json::nullValue); | |
615 } | |
616 else | |
617 { | |
618 node[KEY_VALUE].append(tokens[i]); | |
619 } | |
620 | |
621 break; | |
622 } | |
623 } | |
624 catch (boost::bad_lexical_cast&) | |
625 { | |
626 throw OrthancException(ErrorCode_BadFileFormat); | |
627 } | |
628 } | |
629 } | |
630 } | |
631 } | |
632 | |
633 return Action_None; | |
634 } | |
635 } |