1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
|
/*
*
* Copyright (C) 2003-2024, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: dcmdata
*
* Author: Joerg Riesmeier
*
* Purpose: Helper class for converting an XML document to DICOM file or data set
*
*/
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/dcmdata/dcmxml/xml2dcm.h"
#ifdef WITH_LIBXML
#include "dcmtk/dcmdata/dctypes.h"
#include "dcmtk/dcmdata/dcsequen.h"
#include "dcmtk/dcmdata/dcitem.h"
#include "dcmtk/dcmdata/dcpixseq.h"
#include "dcmtk/dcmdata/dcpixel.h"
#include "dcmtk/dcmdata/dcpxitem.h"
#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/dcfilefo.h"
#include "dcmtk/dcmdata/dcmetinf.h"
#include "dcmtk/dcmdata/dcswap.h"
#include <libxml/parser.h>
// This function is also used in dcmsr, try to stay in sync!
#if defined(HAVE_VSNPRINTF) && defined(HAVE_PROTOTYPE_VSNPRINTF)
extern "C" void xml2dcm_errorFunction(void * ctx, const char *msg, ...)
{
// Classic C requires us to declare variables at the beginning of the function.
OFString &buffer = *OFstatic_cast(OFString*, ctx);
#else
extern "C" void xml2dcm_errorFunction(void * /* ctx */, const char *msg, ...)
{
#endif
if (!DCM_dcmdataLogger.isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
return;
#if defined(HAVE_VSNPRINTF) && defined(HAVE_PROTOTYPE_VSNPRINTF)
// libxml calls us multiple times for one line of log output which would
// result in garbled output. To avoid this, we buffer the output in a local
// string in the caller which we get through our 'ctx' parameter. Then, we
// output this string on one go when we receive a newline.
va_list ap;
char buf[1024];
va_start(ap, msg);
#ifdef HAVE_PROTOTYPE_STD__VSNPRINTF
std::vsnprintf(buf, 1024, msg, ap);
#else
vsnprintf(buf, 1024, msg, ap);
#endif
va_end(ap);
// Since we can't do anything about a too small buffer for vsnprintf(), we
// ignore it. But we do make sure the buffer is null-terminated!
buf[1023] = '\0';
buffer += buf;
// If there is a full line in the buffer...
size_t pos = buffer.find('\n');
while (pos != OFString_npos)
{
// ..output it and remove it from the buffer
DCMDATA_DEBUG(buffer.substr(0, pos));
buffer.erase(0, pos + 1);
pos = buffer.find('\n');
}
#else
// No vsnprint, but at least vfprintf. Output the messages directly to stderr.
va_list ap;
va_start(ap, msg);
#ifdef HAVE_PROTOTYPE_STD__VFPRINTF
std::vfprintf(stderr, msg, ap);
#else
vfprintf(stderr, msg, ap);
#endif
va_end(ap);
#endif
}
static OFBool xmlIsBlankNodeOrComment(xmlNode *node)
{
if (xmlIsBlankNode(node)) return OFTrue;
return (0 == xmlStrcmp(node->name, OFreinterpret_cast(const xmlChar *, "comment")));
}
DcmXMLParseHelper::DcmXMLParseHelper()
: EncodingHandler(NULL)
{
}
DcmXMLParseHelper::~DcmXMLParseHelper()
{
}
void DcmXMLParseHelper::initLibrary()
{
/* check for compatible libxml version */
LIBXML_TEST_VERSION
/* initialize the XML library (only required for MT-safety) */
xmlInitParser();
#if LIBXML_VERSION < 20703
/*
* the following settings have been deprecated in newer versions of libxml,
* or they are not needed any more:
*/
/* do not substitute entities (other than the standard ones) */
xmlSubstituteEntitiesDefault(0);
/* add line number to debug messages */
xmlLineNumbersDefault(1);
/* enable node indenting for tree output */
xmlIndentTreeOutput = 1;
/* remove ignorable whitespace */
xmlKeepBlanksDefault(0);
/* enable libxml warnings and error messages */
xmlGetWarningsDefaultValue = 1;
#endif
}
void DcmXMLParseHelper::cleanupLibrary()
{
xmlCleanupParser();
}
OFBool DcmXMLParseHelper::convertUtf8ToCharset(
const xmlChar *fromString,
OFString &toString)
{
OFBool result = OFFalse;
if (EncodingHandler != NULL)
{
/* prepare input/output buffers */
xmlBufferPtr fromBuffer = xmlBufferCreate();
xmlBufferPtr toBuffer = xmlBufferCreate();
xmlBufferCat(fromBuffer, fromString);
/* convert character encoding of given string */
result = (xmlCharEncOutFunc(EncodingHandler, toBuffer, fromBuffer) >= 0);
if (result)
toString = OFreinterpret_cast(const char *, xmlBufferContent(toBuffer));
/* free allocated memory */
xmlBufferFree(toBuffer);
xmlBufferFree(fromBuffer);
}
return result;
}
OFCondition DcmXMLParseHelper::checkNode(
xmlNodePtr current,
const char *name)
{
OFCondition result = EC_Normal;
/* check whether node is valid at all */
if (current != NULL)
{
/* check whether node has expected name */
if (xmlStrcmp(current->name, OFreinterpret_cast(const xmlChar *, name)) != 0)
{
DCMDATA_ERROR("document of the wrong type, was '" << current->name << "', '" << name << "' expected");
result = EC_XMLParseError;
}
} else {
DCMDATA_ERROR("document of the wrong type, '" << name << "' expected");
result = EC_XMLParseError;
}
return result;
}
OFCondition DcmXMLParseHelper::createNewElement(
xmlNodePtr current,
DcmElement *&newElem)
{
OFCondition result = EC_IllegalCall;
/* check whether node is valid */
if (current != NULL)
{
/* get required information from XML element */
xmlChar *elemTag = xmlGetProp(current, OFreinterpret_cast(const xmlChar *, "tag"));
xmlChar *elemVR = xmlGetProp(current, OFreinterpret_cast(const xmlChar *, "vr"));
/* convert tag string */
DcmTagKey dcmTagKey;
unsigned int group = 0xffff;
unsigned int elem = 0xffff;
/* make sure that "tag" attribute exists */
if (elemTag == NULL)
{
DCMDATA_WARN("missing 'tag' attribute for node '" << current->name << "'");
result = EC_XMLParseError;
}
/* determine group and element number from "tag" */
else if (sscanf(OFreinterpret_cast(char *, elemTag), "%x,%x", &group, &elem ) == 2)
{
dcmTagKey.set(OFstatic_cast(Uint16, group), OFstatic_cast(Uint16, elem));
DcmTag dcmTag(dcmTagKey);
/* convert vr string */
const DcmVR dcmVR(OFreinterpret_cast(char *, elemVR));
if (dcmVR.isUnknown() || dcmVR.isInvalid())
{
/* check whether "vr" attribute exists */
if (elemVR == NULL)
{
DCMDATA_WARN("missing 'vr' attribute for " << dcmTag
<< ", using the tag's VR (" << dcmTag.getVR().getVRName() << ")");
} else {
DCMDATA_WARN("invalid 'vr' attribute (" << elemVR << ") for " << dcmTag
<< ", using the tag's VR (" << dcmTag.getVR().getVRName() << ")");
}
} else {
const DcmEVR dcmEVR = dcmVR.getEVR();
const DcmEVR tagEVR = dcmTag.getEVR();
/* check for correct vr */
if ((tagEVR != dcmEVR) && (tagEVR != EVR_UNKNOWN) &&
((dcmTagKey != DCM_LUTData) || ((dcmEVR != EVR_US) && (dcmEVR != EVR_SS) && (dcmEVR != EVR_OW))) &&
((tagEVR != EVR_xs) || ((dcmEVR != EVR_US) && (dcmEVR != EVR_SS))) &&
(((tagEVR != EVR_ox) && (tagEVR != EVR_px)) || ((dcmEVR != EVR_OB) && (dcmEVR != EVR_OW))))
{
DCMDATA_WARN("element " << dcmTag << " has wrong VR (" << dcmVR.getVRName()
<< "), correct is '" << dcmTag.getVR().getVRName() << "'");
}
dcmTag.setVR(dcmVR);
}
/* create DICOM element */
result = DcmItem::newDicomElementWithVR(newElem, dcmTag);
} else {
DCMDATA_WARN("invalid 'tag' attribute (" << elemTag << ") for node '" << current->name << "'");
result = EC_XMLParseError;
}
if (result.bad())
{
/* delete new element if an error occurred */
delete newElem;
newElem = NULL;
}
/* free allocated memory */
xmlFree(elemTag);
xmlFree(elemVR);
}
return result;
}
OFCondition DcmXMLParseHelper::putElementContent(
xmlNodePtr current,
DcmElement *element)
{
OFCondition result = EC_Normal;
/* check whether node and element are valid */
if ((current != NULL) && (element != NULL))
{
DcmEVR dcmEVR = element->getVR();
/* get the XML node content */
xmlChar *elemVal = xmlNodeGetContent(current);
xmlChar *attrVal = xmlGetProp(current, OFreinterpret_cast(const xmlChar *, "binary"));
/* check whether node content is present */
if (xmlStrcmp(attrVal, OFreinterpret_cast(const xmlChar *, "hidden")) == 0)
{
DCMDATA_WARN("content of node " << element->getTag() << " is 'hidden', empty element inserted");
/* return an error unless the element is part of the file meta information */
if (element->getGTag() != 0x0002)
result = EC_MissingValue;
}
/* check whether node content is base64 encoded */
else if (xmlStrcmp(attrVal, OFreinterpret_cast(const xmlChar *, "base64")) == 0)
{
Uint8 *data = NULL;
const size_t length = OFStandard::decodeBase64(OFreinterpret_cast(char *, elemVal), data);
if (length > 0)
{
if (dcmEVR == EVR_OW)
{
/* Base64 decoder produces big endian output data, convert to local byte order */
swapIfNecessary(gLocalByteOrder, EBO_BigEndian, data, OFstatic_cast(Uint32, length), sizeof(Uint16));
}
result = element->putUint8Array(data, OFstatic_cast(Uint32, length));
/* delete buffer since data is copied into the element */
delete[] data;
}
}
/* check whether node content is stored in a file */
else if (xmlStrcmp(attrVal, OFreinterpret_cast(const xmlChar *, "file")) == 0)
{
if (xmlStrlen(elemVal) > 0)
{
const char *filename = OFreinterpret_cast(char *, elemVal);
/* try to open binary file */
FILE *f = fopen(filename, "rb");
if (f != NULL)
{
/* determine filesize */
const size_t fileSize = OFStandard::getFileSize(filename);
size_t buflen = fileSize;
/* if odd then make even (DICOM requires even length values) */
if (buflen & 1)
buflen++;
Uint8 *buf = NULL;
/* create buffer of OB or OW data */
if (dcmEVR == EVR_OW)
{
Uint16 *buf16 = NULL;
result = element->createUint16Array(OFstatic_cast(Uint32, buflen / 2), buf16);
buf = OFreinterpret_cast(Uint8 *, buf16);
} else
result = element->createUint8Array(OFstatic_cast(Uint32, buflen), buf);
if (result.good())
{
DCMDATA_INFO("reading " << fileSize << " bytes from binary data file: " << filename);
DCMDATA_DEBUG(" and storing it in the element " << element->getTag());
/* read binary file into the buffer */
if (fread(buf, 1, OFstatic_cast(size_t, fileSize), f) != fileSize)
{
DCMDATA_WARN("cannot read binary data file: " << filename << ": " << OFStandard::getLastSystemErrorCode().message());
result = EC_CorruptedData;
}
else if (dcmEVR == EVR_OW)
{
/* swap 16 bit OW data (if necessary) */
swapIfNecessary(gLocalByteOrder, EBO_LittleEndian, buf, OFstatic_cast(Uint32, buflen), sizeof(Uint16));
}
}
fclose(f);
} else {
DCMDATA_WARN("cannot open binary data file: " << filename);
result = EC_InvalidTag;
}
} else
DCMDATA_WARN("filename for element " << element->getTag() << " is missing, empty element inserted");
} else {
OFString dicomVal;
/* convert character set from UTF-8 (for specific VRs only) */
if (element->isAffectedBySpecificCharacterSet() &&
(xmlStrlen(elemVal) > 0) && convertUtf8ToCharset(elemVal, dicomVal))
{
result = element->putOFStringArray(dicomVal);
} else {
/* set the value of the newly created element */
result = element->putString(OFreinterpret_cast(char *, elemVal));
}
if (result.bad())
DCMDATA_WARN("cannot put content to element " << element->getTag() << ": " << result.text());
}
/* free allocated memory */
xmlFree(elemVal);
xmlFree(attrVal);
} else
result = EC_IllegalCall;
return result;
}
OFCondition DcmXMLParseHelper::parseElement(
DcmItem *dataset,
xmlNodePtr current)
{
DcmElement *newElem = NULL;
/* create new DICOM element from XML element */
OFCondition result = createNewElement(current, newElem);
if (result.good())
{
/* retrieve specific character set (only on main dataset level) */
if ((EncodingHandler == NULL) && (dataset->ident() == EVR_dataset) &&
(newElem->getTag() == DCM_SpecificCharacterSet))
{
const char *encString = NULL;
xmlChar *elemVal = xmlNodeGetContent(current);
/* check for known character set */
if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 6")) == 0)
encString = "UTF-8";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 192")) == 0)
encString = "UTF-8";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 100")) == 0)
encString = "ISO-8859-1";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 101")) == 0)
encString = "ISO-8859-2";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 109")) == 0)
encString = "ISO-8859-3";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 110")) == 0)
encString = "ISO-8859-4";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 144")) == 0)
encString = "ISO-8859-5";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 127")) == 0)
encString = "ISO-8859-6";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 126")) == 0)
encString = "ISO-8859-7";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 138")) == 0)
encString = "ISO-8859-8";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 148")) == 0)
encString = "ISO-8859-9";
else if (xmlStrcmp(elemVal, OFreinterpret_cast(const xmlChar *, "ISO_IR 203")) == 0)
encString = "ISO-8859-15";
else if (xmlStrlen(elemVal) > 0)
DCMDATA_WARN("character set '" << elemVal << "' not supported");
if (encString != NULL)
{
/* find appropriate encoding handler */
EncodingHandler = xmlFindCharEncodingHandler(encString);
}
xmlFree(elemVal);
}
/* set the element value */
result = putElementContent(current, newElem);
/* insert the new element into the dataset */
if (result.good())
result = dataset->insert(newElem, OFTrue /*replaceOld*/);
if (result.bad())
{
/* delete element if insertion or putting the value failed */
delete newElem;
}
}
return result;
}
OFCondition DcmXMLParseHelper::parseSequence(
DcmSequenceOfItems *sequence,
xmlNodePtr current,
E_TransferSyntax xfer,
const OFBool stopOnError)
{
OFCondition result = EC_Normal;
if (sequence != NULL)
{
/* ignore blank (empty or whitespace only) nodes */
while ((current != NULL) && xmlIsBlankNodeOrComment(current))
current = current->next;
while (current != NULL)
{
/* ignore non-item nodes */
if (xmlStrcmp(current->name, OFreinterpret_cast(const xmlChar *, "item")) == 0)
{
/* create new sequence item */
DcmItem *newItem = new DcmItem();
if (newItem != NULL)
{
sequence->insert(newItem);
/* proceed parsing the item content */
result = parseDataSet(newItem, current->xmlChildrenNode, xfer, stopOnError);
if (result.bad())
DCMDATA_WARN("cannot parse invalid item: " << result.text());
}
} else if (!xmlIsBlankNodeOrComment(current))
DCMDATA_WARN("unexpected node '" << current->name << "', 'item' expected, skipping");
/* check for errors */
if (result.bad())
{
if (stopOnError)
{
/* exit the loop and return with an error */
break;
} else {
DCMDATA_DEBUG("ignoring error as requested by the user");
/* ignore the error */
result = EC_Normal;
}
}
/* proceed with next node */
current = current->next;
}
} else
result = EC_IllegalCall;
return result;
}
OFCondition DcmXMLParseHelper::parsePixelSequence(
DcmPixelSequence *sequence,
xmlNodePtr current,
const OFBool stopOnError)
{
OFCondition result = EC_Normal;
if (sequence != NULL)
{
/* ignore blank (empty or whitespace only) nodes */
while ((current != NULL) && xmlIsBlankNodeOrComment(current))
current = current->next;
while (current != NULL)
{
/* ignore non-pixel-item nodes */
if (xmlStrcmp(current->name, OFreinterpret_cast(const xmlChar *, "pixel-item")) == 0)
{
/* create new pixel item */
DcmPixelItem *newItem = new DcmPixelItem(DCM_PixelItemTag);
if (newItem != NULL)
{
sequence->insert(newItem);
/* put pixel data into the item */
result = putElementContent(current, newItem);
if (result.bad())
DCMDATA_WARN("cannot parse invalid pixel-item: " << result.text());
}
} else if (!xmlIsBlankNodeOrComment(current))
DCMDATA_WARN("unexpected node '" << current->name << "', 'pixel-item' expected, skipping");
/* check for errors */
if (result.bad())
{
if (stopOnError)
{
/* exit the loop and return with an error */
break;
} else {
DCMDATA_DEBUG("ignoring error as requested by the user");
/* ignore the error */
result = EC_Normal;
}
}
/* proceed with next node */
current = current->next;
}
} else
result = EC_IllegalCall;
return result;
}
OFCondition DcmXMLParseHelper::parseMetaHeader(
DcmMetaInfo *metainfo,
xmlNodePtr current,
const OFBool parse,
const OFBool stopOnError)
{
/* check for valid node and correct name */
OFCondition result = checkNode(current, "meta-header");
if (result.good() && parse)
{
/* get child nodes */
current = current->xmlChildrenNode;
while (current != NULL)
{
/* ignore non-element nodes */
if (xmlStrcmp(current->name, OFreinterpret_cast(const xmlChar *, "element")) == 0)
result = parseElement(metainfo, current);
else if (!xmlIsBlankNodeOrComment(current))
DCMDATA_WARN("unexpected node '" << current->name << "', 'element' expected, skipping");
/* check for errors */
if (result.bad())
{
if (stopOnError)
{
/* exit the loop and return with an error */
break;
} else {
DCMDATA_DEBUG("ignoring error as requested by the user");
/* ignore the error */
result = EC_Normal;
}
}
/* proceed with next node */
current = current->next;
}
}
return result;
}
OFCondition DcmXMLParseHelper::parseDataSet(
DcmItem *dataset,
xmlNodePtr current,
E_TransferSyntax xfer,
const OFBool stopOnError)
{
OFCondition result = EC_Normal;
/* ignore blank (empty or whitespace only) nodes */
while ((current != NULL) && xmlIsBlankNodeOrComment(current))
current = current->next;
while (current != NULL)
{
/* ignore non-element/sequence nodes */
if (xmlStrcmp(current->name, OFreinterpret_cast(const xmlChar *, "element")) == 0)
result = parseElement(dataset, current);
else if (xmlStrcmp(current->name, OFreinterpret_cast(const xmlChar *, "sequence")) == 0)
{
DcmElement *newElem = NULL;
/* create new sequence element */
if (createNewElement(current, newElem).good())
{
/* insert new sequence element into the dataset */
result = dataset->insert(newElem, OFTrue /*replaceOld*/);
if (result.good())
{
/* special handling for compressed pixel data */
if (newElem->getTag() == DCM_PixelData)
{
/* create new pixel sequence */
DcmPixelSequence *sequence = new DcmPixelSequence(DCM_PixelSequenceTag);
if (sequence != NULL)
{
if (newElem->ident() == EVR_PixelData)
{
/* ... insert it into the dataset and proceed with the pixel items */
OFstatic_cast(DcmPixelData *, newElem)->putOriginalRepresentation(xfer, NULL, sequence);
result = parsePixelSequence(sequence, current->xmlChildrenNode, stopOnError);
} else
DCMDATA_WARN("wrong VR for 'sequence' element with pixel data, ignoring child nodes");
}
} else {
/* proceed parsing the items of the sequence */
if (newElem->ident() == EVR_SQ)
result = parseSequence(OFstatic_cast(DcmSequenceOfItems *, newElem), current->xmlChildrenNode, xfer, stopOnError);
else
DCMDATA_WARN("wrong VR for 'sequence' element, ignoring child nodes");
}
} else {
/* delete element if insertion failed */
delete newElem;
}
}
} else if (!xmlIsBlankNodeOrComment(current))
DCMDATA_WARN("unexpected node '" << current->name << "', skipping");
/* check for errors */
if (result.bad())
{
if (stopOnError)
{
/* exit the loop and return with an error */
break;
} else {
DCMDATA_DEBUG("ignoring error as requested by the user");
/* ignore the error */
result = EC_Normal;
}
}
/* proceed with next node */
current = current->next;
}
return result;
}
OFCondition DcmXMLParseHelper::validateXmlDocument(xmlDocPtr doc)
{
OFCondition result = EC_Normal;
DCMDATA_INFO("validating XML document ...");
xmlGenericError(xmlGenericErrorContext, "--- libxml validating ---\n");
/* temporary buffer needed for xml2dcm_errorFunction - more detailed explanation there */
OFString tmpErrorString;
/* create context for document validation */
xmlValidCtxt cvp;
cvp.userData = &tmpErrorString;
cvp.error = xml2dcm_errorFunction;
cvp.warning = xml2dcm_errorFunction;
/* validate the document */
const int valid = xmlValidateDocument(&cvp, doc);
xmlGenericError(xmlGenericErrorContext, "-------------------------\n");
if (!valid)
{
DCMDATA_ERROR("document does not validate");
result = EC_XMLValidationFailure;
}
return result;
}
OFCondition DcmXMLParseHelper::readXmlFile(
const char *ifname,
DcmFileFormat &fileformat,
E_TransferSyntax &xfer,
const OFBool metaInfo,
const OFBool checkNamespace,
const OFBool validateDocument,
const OFBool stopOnError)
{
OFCondition result = EC_Normal;
xfer = EXS_Unknown;
/* temporary buffer needed for xml2dcm_errorFunction - more detailed explanation there */
OFString tmpErrorString;
xmlSetGenericErrorFunc(&tmpErrorString, xml2dcm_errorFunction);
xmlGenericError(xmlGenericErrorContext, "--- libxml parsing ------\n");
/* build an XML tree from the file */
#if LIBXML_VERSION >= 20703
/*
* Starting with libxml version 2.7.3, the maximum length of XML element values
* is limited to 10 MB. The following code disables this default limitation.
*
* Other flags are now also passed to the function instead of using global
* settings (some of them have been deprecated, see initLibrary()).
*/
xmlDocPtr doc = xmlReadFile(ifname, NULL /*encoding*/, XML_PARSE_HUGE | XML_PARSE_NOBLANKS | XML_PARSE_NONET);
#else
xmlDocPtr doc = xmlParseFile(ifname);
#endif
xmlGenericError(xmlGenericErrorContext, "-------------------------\n");
if (doc != NULL)
{
/* validate document */
if (validateDocument)
result = validateXmlDocument(doc);
if (result.good())
{
/* check whether the document is of the right kind */
xmlNodePtr current = xmlDocGetRootElement(doc);
if (current != NULL)
{
/* check namespace declaration (if required) */
if (!checkNamespace || (xmlSearchNsByHref(doc, current, OFreinterpret_cast(const xmlChar *, DCMTK_XML_NAMESPACE_URI)) != NULL))
{
/* check whether to parse a "file-format" or "data-set" */
if (xmlStrcmp(current->name, OFreinterpret_cast(const xmlChar *, "file-format")) == 0)
{
DCMDATA_INFO("parsing file-format ...");
if (metaInfo)
DCMDATA_INFO("parsing meta-header ...");
else
DCMDATA_INFO("skipping meta-header ...");
current = current->xmlChildrenNode;
/* ignore blank (empty or whitespace only) nodes */
while ((current != NULL) && xmlIsBlankNodeOrComment(current))
current = current->next;
/* parse/skip "meta-header" */
result = parseMetaHeader(fileformat.getMetaInfo(), current, metaInfo /*parse*/, stopOnError);
if (result.good())
{
current = current->next;
/* ignore blank (empty or whitespace only) nodes */
while ((current != NULL) && xmlIsBlankNodeOrComment(current))
current = current->next;
} else
DCMDATA_ERROR("cannot parse invalid meta-header");
}
/* there should always be a "data-set" node */
if (result.good())
{
DCMDATA_INFO("parsing data-set ...");
/* parse "data-set" */
result = checkNode(current, "data-set");
if (result.good())
{
/* determine stored transfer syntax */
xmlChar *xferUID = xmlGetProp(current, OFreinterpret_cast(const xmlChar *, "xfer"));
if (xferUID != NULL)
xfer = DcmXfer(OFreinterpret_cast(char *, xferUID)).getXfer();
result = parseDataSet(fileformat.getDataset(), current->xmlChildrenNode, xfer, stopOnError);
/* free allocated memory */
xmlFree(xferUID);
if (result.bad())
DCMDATA_ERROR("cannot parse invalid data-set");
}
}
if (result.bad())
{
if (DCM_dcmdataLogger.isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
{
DCMDATA_DEBUG("--- libxml dump ---------");
/* dump XML document for debugging purposes */
xmlChar *str;
int size;
xmlDocDumpFormatMemory(doc, &str, &size, 1);
DCMDATA_DEBUG(str);
xmlFree(str);
DCMDATA_DEBUG("-------------------------");
}
DCMDATA_ERROR("cannot read invalid document: " << ifname);
}
} else {
DCMDATA_ERROR("document has wrong type, dcmtk namespace not found");
result = EC_XMLParseError;
}
} else {
DCMDATA_ERROR("document is empty: " << ifname);
result = EC_XMLParseError;
}
}
} else {
DCMDATA_ERROR("could not parse document: " << ifname);
result = EC_XMLParseError;
}
/* Reset to default function because we used a local string as context for
* the error function.
*/
xmlSetGenericErrorFunc(NULL, NULL);
/* free allocated memory */
xmlFreeDoc(doc);
return result;
}
#else /* WITH_LIBXML */
/* make sure that the object file is not completely empty if compiled
* without libxml2 because some linkers might fail otherwise.
*/
void xml2dcm_lib_dummy_function()
{
return;
}
#endif /* WITH_LIBXML */
|