File: dsrcomvl.cc

package info (click to toggle)
dcmtk 3.6.9-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,648 kB
  • sloc: ansic: 426,874; cpp: 318,177; makefile: 6,401; sh: 4,341; yacc: 1,026; xml: 482; lex: 321; perl: 277
file content (459 lines) | stat: -rw-r--r-- 15,326 bytes parent folder | download | duplicates (2)
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
/*
 *
 *  Copyright (C) 2000-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: dcmsr
 *
 *  Author: Joerg Riesmeier
 *
 *  Purpose:
 *    classes: DSRCompositeReferenceValue
 *
 */


#include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */

#include "dcmtk/dcmsr/dsrtypes.h"
#include "dcmtk/dcmsr/dsrcomvl.h"
#include "dcmtk/dcmsr/dsrxmld.h"

#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/dcuid.h"
#include "dcmtk/dcmdata/dcvrui.h"


DSRCompositeReferenceValue::DSRCompositeReferenceValue()
  : SOPClassUID(),
    SOPInstanceUID()
{
}


DSRCompositeReferenceValue::DSRCompositeReferenceValue(const OFString &sopClassUID,
                                                       const OFString &sopInstanceUID,
                                                       const OFBool check)
  : SOPClassUID(),
    SOPInstanceUID()
{
    /* use the set method for checking purposes */
    setReference(sopClassUID, sopInstanceUID, check);
}


DSRCompositeReferenceValue::DSRCompositeReferenceValue(const DSRCompositeReferenceValue &referenceValue)
  : SOPClassUID(referenceValue.SOPClassUID),
    SOPInstanceUID(referenceValue.SOPInstanceUID)
{
    /* do not check since this would be unexpected to the user */
}


DSRCompositeReferenceValue::~DSRCompositeReferenceValue()
{
}


DSRCompositeReferenceValue &DSRCompositeReferenceValue::operator=(const DSRCompositeReferenceValue &referenceValue)
{
    /* do not check since this would be unexpected to the user */
    SOPClassUID = referenceValue.SOPClassUID;
    SOPInstanceUID = referenceValue.SOPInstanceUID;
    return *this;
}


void DSRCompositeReferenceValue::clear()
{
    SOPClassUID.clear();
    SOPInstanceUID.clear();
}


OFBool DSRCompositeReferenceValue::isEqual(const DSRCompositeReferenceValue &referenceValue) const
{
    return (SOPClassUID == referenceValue.SOPClassUID) &&
           (SOPInstanceUID == referenceValue.SOPInstanceUID);
}


OFBool DSRCompositeReferenceValue::isNotEqual(const DSRCompositeReferenceValue &referenceValue) const
{
    return (SOPClassUID != referenceValue.SOPClassUID) ||
           (SOPInstanceUID != referenceValue.SOPInstanceUID);
}


OFBool DSRCompositeReferenceValue::isValid() const
{
    return checkSOPClassUID(SOPClassUID).good() && checkSOPInstanceUID(SOPInstanceUID).good();
}


OFBool DSRCompositeReferenceValue::isEmpty() const
{
    return SOPClassUID.empty() && SOPInstanceUID.empty();
}


OFBool DSRCompositeReferenceValue::isComplete() const
{
    return !SOPClassUID.empty() && !SOPInstanceUID.empty();
}


OFCondition DSRCompositeReferenceValue::print(STD_NAMESPACE ostream &stream,
                                              const size_t flags) const
{
    /* first, determine SOP class component */
    OFString sopClassString = "\"" + SOPClassUID + "\"";
    if (!(flags & DSRTypes::PF_printSOPClassUID))
    {
        /* look up name of known SOP classes */
        const char *className = dcmFindNameOfUID(SOPClassUID.c_str());
        if (className != NULL)
            sopClassString = className;
    }
    /* and then, print it */
    stream << "(" << sopClassString << ",";
    /* print SOP instance component (if desired) */
    if (flags & DSRTypes::PF_printSOPInstanceUID)
        stream << "\"" << SOPInstanceUID << "\"";
    stream << ")";
    return EC_Normal;
}


OFCondition DSRCompositeReferenceValue::readXML(const DSRXMLDocument &doc,
                                                DSRXMLCursor cursor,
                                                const size_t /*flags*/)
{
    OFCondition result = SR_EC_CorruptedXMLStructure;
    /* go one node level down */
    if (cursor.gotoChild().valid())
    {
        /* retrieve SOP Class UID and SOP Instance UID from XML tag (required) */
        doc.getStringFromAttribute(doc.getNamedNode(cursor, "sopclass"), SOPClassUID, "uid");
        doc.getStringFromAttribute(doc.getNamedNode(cursor, "instance"), SOPInstanceUID, "uid");
        /* check whether value is valid */
        result = isValid() ? EC_Normal : SR_EC_InvalidValue;
    }
    return result;
}


OFCondition DSRCompositeReferenceValue::writeXML(STD_NAMESPACE ostream &stream,
                                                 const size_t flags) const
{
    if ((flags & DSRTypes::XF_writeEmptyTags) || !isEmpty())
    {
        stream << "<sopclass uid=\"" << SOPClassUID << "\">";
        /* retrieve name of SOP class (if known) */
        stream << dcmFindNameOfUID(SOPClassUID.c_str(), "" /* empty value as default */);
        stream << "</sopclass>" << OFendl;
        stream << "<instance uid=\"" << SOPInstanceUID << "\"/>" << OFendl;
    }
    return EC_Normal;
}


OFCondition DSRCompositeReferenceValue::readItem(DcmItem &dataset,
                                                 const size_t flags)
{
    const OFBool acceptViolation = (flags & DSRTypes::RF_acceptInvalidContentItemValue) > 0;
    /* read ReferencedSOPClassUID */
    OFCondition result = DSRTypes::getAndCheckStringValueFromDataset(dataset, DCM_ReferencedSOPClassUID, SOPClassUID, "1", "1", "ReferencedSOPSequence", acceptViolation);
    /* read ReferencedSOPInstanceUID */
    if (result.good())
        result = DSRTypes::getAndCheckStringValueFromDataset(dataset, DCM_ReferencedSOPInstanceUID, SOPInstanceUID, "1", "1", "ReferencedSOPSequence", acceptViolation);
    return result;
}


OFCondition DSRCompositeReferenceValue::writeItem(DcmItem &dataset) const
{
    /* write ReferencedSOPClassUID */
    OFCondition result = DSRTypes::putStringValueToDataset(dataset, DCM_ReferencedSOPClassUID, SOPClassUID);
    /* write ReferencedSOPInstanceUID */
    if (result.good())
        result = DSRTypes::putStringValueToDataset(dataset, DCM_ReferencedSOPInstanceUID, SOPInstanceUID);
    return result;
}


OFCondition DSRCompositeReferenceValue::readSequence(DcmItem &dataset,
                                                     const DcmTagKey &tagKey,
                                                     const OFString &type,
                                                     const size_t flags)
{
    /* read specified sequence with its item */
    DcmSequenceOfItems *dseq = NULL;
    OFCondition result = dataset.findAndGetSequence(tagKey, dseq);
    DSRTypes::checkElementValue(dseq, tagKey, "1", type, result, "content item");
    if (result.good())
    {
        /* read first item */
        DcmItem *ditem = dseq->getItem(0);
        if (ditem != NULL)
            result = readItem(*ditem, flags);
        else
            result = SR_EC_InvalidDocumentTree;
    }
    return result;
}


OFCondition DSRCompositeReferenceValue::writeSequence(DcmItem &dataset,
                                                      const DcmTagKey &tagKey) const
{
    OFCondition result = EC_MemoryExhausted;
    /* write specified sequence with its item */
    DcmSequenceOfItems *dseq = new DcmSequenceOfItems(tagKey);
    if (dseq != NULL)
    {
        DcmItem *ditem = new DcmItem();
        if (ditem != NULL)
        {
            /* write item */
            result = writeItem(*ditem);
            if (result.good())
                dseq->insert(ditem);
            else
                delete ditem;
        } else
            result = EC_MemoryExhausted;
        /* write sequence */
        if (result.good())
            result = dataset.insert(dseq, OFTrue /*replaceOld*/);
        if (result.bad())
            delete dseq;
    }
    return result;
}


OFCondition DSRCompositeReferenceValue::renderHTML(STD_NAMESPACE ostream &docStream,
                                                   STD_NAMESPACE ostream & /*annexStream*/,
                                                   size_t & /*annexNumber*/,
                                                   const size_t /*flags*/,
                                                   const char *urlPrefix) const
{
    /* render reference */
    docStream << "<a href=\"" << (urlPrefix == NULL ? DEFAULT_HTML_HYPERLINK_PREFIX_FOR_COMPOSITE_OBJECTS : urlPrefix);
    docStream << "?composite=" << SOPClassUID << "+" << SOPInstanceUID << "\">";
    /* retrieve name of SOP class (if known) */
    docStream << dcmFindNameOfUID(SOPClassUID.c_str(), "unknown composite object");
    docStream << "</a>";
    return EC_Normal;
}


const OFString DSRCompositeReferenceValue::getSOPClassName(const OFString &defaultName) const
{
    /* lookup name associated with the SOP class UID */
    return SOPClassUID.empty() ? "" : dcmFindNameOfUID(SOPClassUID.c_str(), defaultName.c_str());
}


OFCondition DSRCompositeReferenceValue::getValue(DSRCompositeReferenceValue &referenceValue) const
{
    referenceValue = *this;
    return EC_Normal;
}


OFCondition DSRCompositeReferenceValue::setValue(const DSRCompositeReferenceValue &referenceValue,
                                                 const OFBool check)
{
    return setReference(referenceValue.SOPClassUID, referenceValue.SOPInstanceUID, check);
}


OFCondition DSRCompositeReferenceValue::setReference(const OFString &sopClassUID,
                                                     const OFString &sopInstanceUID,
                                                     const OFBool check)
{
    OFCondition result = EC_Normal;
    /* first, make sure that the mandatory values are non-empty */
    if (sopClassUID.empty() || sopInstanceUID.empty())
        result = EC_IllegalParameter;
    else if (check)
    {
        /* then, check whether the passed values are valid */
        result = checkSOPClassUID(sopClassUID);
        if (result.good())
            result = checkSOPInstanceUID(sopInstanceUID);
    }
    if (result.good())
    {
        SOPClassUID = sopClassUID;
        SOPInstanceUID = sopInstanceUID;
    }
    return result;
}


OFCondition DSRCompositeReferenceValue::setReference(DcmItem &dataset,
                                                     const OFBool check)
{
    OFCondition result = setSOPClassUID(dataset, DCM_SOPClassUID, 0 /*pos*/, check);
    if (result.good())
        result = setSOPInstanceUID(dataset, DCM_SOPInstanceUID, 0 /*pos*/, check);
    return result;
}


OFCondition DSRCompositeReferenceValue::setSOPClassUID(const OFString &sopClassUID,
                                                       const OFBool check)
{
    OFCondition result = EC_Normal;
    /* first, make sure that the mandatory value is non-empty */
    if (sopClassUID.empty())
        result = EC_IllegalParameter;
    else if (check)
    {
        /* then, check whether the passed value is valid */
        result = checkSOPClassUID(sopClassUID);
    }
    if (result.good())
        SOPClassUID = sopClassUID;
    return result;
}


OFCondition DSRCompositeReferenceValue::setSOPClassUID(const DcmElement &delem,
                                                       const unsigned long pos,
                                                       const OFBool check)
{
    OFString sopClassUID;
    /* first, get the value from the element (need to cast away "const") */
    OFCondition result = OFconst_cast(DcmElement &, delem).getOFString(sopClassUID, pos);
    if (result.good())
    {
        /* then, check and set the value */
        result = setSOPClassUID(sopClassUID, check);
    }
    return result;
}


OFCondition DSRCompositeReferenceValue::setSOPClassUID(DcmItem &dataset,
                                                       const DcmTagKey &tagKey,
                                                       const unsigned long pos,
                                                       const OFBool check)
{
    OFString sopClassUID;
    /* first, get the element value from the dataset */
    OFCondition result = DSRTypes::getStringValueFromDataset(dataset, tagKey, sopClassUID, pos);
    if (result.good())
    {
        /* then, check and set the value */
        result = setSOPClassUID(sopClassUID, check);
    }
    return result;
}


OFCondition DSRCompositeReferenceValue::setSOPInstanceUID(const OFString &sopInstanceUID,
                                                          const OFBool check)
{
    OFCondition result = EC_Normal;
    /* first, make sure that the mandatory value is non-empty */
    if (sopInstanceUID.empty())
        result = EC_IllegalParameter;
    else if (check)
    {
        /* then, check whether the passed value is valid */
        result = checkSOPInstanceUID(sopInstanceUID);
    }
    if (result.good())
        SOPInstanceUID = sopInstanceUID;
    return result;
}


OFCondition DSRCompositeReferenceValue::setSOPInstanceUID(const DcmElement &delem,
                                                          const unsigned long pos,
                                                          const OFBool check)
{
    OFString sopInstanceUID;
    /* first, get the value from the element (need to cast away "const") */
    OFCondition result = OFconst_cast(DcmElement &, delem).getOFString(sopInstanceUID, pos);
    if (result.good())
    {
        /* then, check and set the value */
        result = setSOPInstanceUID(sopInstanceUID, check);
    }
    return result;
}


OFCondition DSRCompositeReferenceValue::setSOPInstanceUID(DcmItem &dataset,
                                                          const DcmTagKey &tagKey,
                                                          const unsigned long pos,
                                                          const OFBool check)
{
    OFString sopInstanceUID;
    /* first, get the element value from the dataset */
    OFCondition result = DSRTypes::getStringValueFromDataset(dataset, tagKey, sopInstanceUID, pos);
    if (result.good())
    {
        /* then, check and set the value */
        result = setSOPInstanceUID(sopInstanceUID, check);
    }
    return result;
}


OFCondition DSRCompositeReferenceValue::checkSOPClassUID(const OFString &sopClassUID,
                                                         const OFBool /*reportWarnings*/) const
{
    OFCondition result = sopClassUID.empty() ? SR_EC_InvalidValue : EC_Normal;
    if (result.good())
        result = DcmUniqueIdentifier::checkStringValue(sopClassUID, "1");
    return result;
}


OFCondition DSRCompositeReferenceValue::checkSOPInstanceUID(const OFString &sopInstanceUID,
                                                            const OFBool /*reportWarnings*/) const
{
    OFCondition result = sopInstanceUID.empty() ? SR_EC_InvalidValue : EC_Normal;
    if (result.good())
        result = DcmUniqueIdentifier::checkStringValue(sopInstanceUID, "1");
    return result;
}


OFCondition DSRCompositeReferenceValue::checkCurrentValue(const OFBool reportWarnings) const
{
    OFCondition result = checkSOPClassUID(SOPClassUID, reportWarnings);
    if (result.good())
        result = checkSOPInstanceUID(SOPInstanceUID, reportWarnings);
    return result;
}


// comparison operators

OFBool operator==(const DSRCompositeReferenceValue &lhs,
                  const DSRCompositeReferenceValue &rhs)
{
    return lhs.isEqual(rhs);
}


OFBool operator!=(const DSRCompositeReferenceValue &lhs,
                  const DSRCompositeReferenceValue &rhs)
{
    return lhs.isNotEqual(rhs);
}