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
|
/*
*
* Copyright (C) 2015-2024, Open Connections GmbH
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation are maintained by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: dcmiod
*
* Author: Michael Onken
*
* Purpose: Class for managing attribute rules as found in DICOM modules
*
*/
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmiod/iodrules.h"
#include "dcmtk/dcmdata/dcelem.h"
#include "dcmtk/dcmdata/dcitem.h"
#include "dcmtk/dcmdata/dctagkey.h"
#include "dcmtk/dcmiod/iodtypes.h"
IODRules::IODRules()
: m_Rules()
{
// nothing to do
}
IODRules* IODRules::clone()
{
IODRules* newRules = new IODRules();
if (newRules)
{
OFMap<DcmTagKey, IODRule*>::iterator it = m_Rules.begin();
while (it != m_Rules.end())
{
if (it->second)
{
IODRule* newRule = it->second->clone();
if (newRule)
{
newRules->addRule(newRule);
}
else
{
DCMIOD_WARN("Cannot create new IODRule, memory exhausted?");
}
}
else
{
DCMIOD_WARN("Found NULL IODRule, cannot clone");
}
it++;
}
}
else
{
DCMIOD_WARN("Cannot create new IODRules, memory exhausted?");
}
return newRules;
}
IODRule* IODRules::getByTag(const DcmTagKey& key) const
{
IODRules::const_iterator it = m_Rules.find(key);
if (it != m_Rules.end())
return (*it).second;
else
return NULL;
}
IODRules::iterator IODRules::begin()
{
return m_Rules.begin();
}
IODRules::iterator IODRules::end()
{
return m_Rules.end();
}
void IODRules::clear()
{
while (m_Rules.size() > 0)
{
IODRules::iterator it = m_Rules.begin();
IODRule* rule = (*it).second;
m_Rules.erase(it);
delete rule;
rule = NULL;
}
}
OFBool IODRules::addRule(IODRule* rule, const OFBool overwriteExisting)
{
if (rule == NULL)
{
DCMIOD_ERROR("Cannot add IOD rule: NULL pointer");
return OFFalse;
}
DcmTagKey key = rule->getTagKey();
iterator it = m_Rules.find(key);
if (it != m_Rules.end())
{
if (overwriteExisting)
{
delete (*it).second;
(*it).second = rule;
}
else
{
DCMIOD_DEBUG("IOD rule for tag " << key << " not inserted (already existing and overwriting disabled)");
return OFFalse;
}
}
else
{
m_Rules.insert(OFMake_pair(key, rule));
}
return OFTrue;
}
const OFVector<IODRule*> IODRules::getByModule(const OFString& moduleName)
{
OFVector<IODRule*> result;
IODRules::iterator it = m_Rules.begin();
while (it != m_Rules.end())
{
if ((*it).second->getModule() == moduleName)
{
result.push_back((*it).second);
}
it++;
}
return result;
}
OFBool IODRules::deleteRule(const DcmTagKey key)
{
IODRules::iterator it = m_Rules.find(key);
if (it != m_Rules.end())
{
IODRule* rule = (*it).second;
m_Rules.erase(it);
delete rule;
return OFTrue;
}
return OFFalse;
}
void IODRules::dump(STD_NAMESPACE ostream& out)
{
IODRules::iterator it = m_Rules.begin();
while (it != m_Rules.end())
{
out << (*it).first << ": Type \"" << (*it).second->getType() << "\", VM \"" << (*it).second->getType() << "\""
<< OFendl;
it++;
}
}
IODRules::~IODRules()
{
clear();
}
IODRule::IODRule(const DcmTagKey& key,
const OFString& VM,
const OFString& type,
const OFString& module,
const DcmIODTypes::IOD_IE ie,
const OFString& defaultValue,
const OFString& privateCreator)
: m_Key(key)
, m_VM(VM)
, m_Type(type)
, m_Module(module)
, m_IE(ie)
, m_DefaultValue(defaultValue)
, m_PrivateCreator(privateCreator)
{
// nothing else to do
}
IODRule::~IODRule()
{
// nothing to do
}
IODRule* IODRule::clone()
{
return new IODRule(m_Key, m_VM, m_Type, m_Module, m_IE, m_DefaultValue, m_PrivateCreator);
}
OFString IODRule::getPrivateCreator() const
{
return m_PrivateCreator;
}
DcmTagKey IODRule::getTagKey() const
{
return m_Key;
}
OFString IODRule::getModule() const
{
return m_Module;
}
OFString IODRule::getType() const
{
return m_Type;
}
OFString IODRule::getVM() const
{
return m_VM;
}
OFString IODRule::getDefaultValue() const
{
return m_DefaultValue;
}
DcmIODTypes::IOD_IE IODRule::getIE() const
{
return m_IE;
}
OFBool IODRule::setType(const OFString& val)
{
if ((val != "1") && (val != "1C") && (val != "2") && (val != "2C") && (val != "3"))
{
return OFFalse;
}
m_Type = val;
return OFTrue;
}
OFBool IODRule::setModule(const OFString& val)
{
if (val.empty())
{
return OFFalse;
}
m_Module = val;
return OFTrue;
}
OFBool IODRule::setPrivateCreator(const OFString& val)
{
if (val.empty())
{
return OFFalse;
}
m_PrivateCreator = val;
return OFTrue;
}
OFBool IODRule::setDefaultValue(const OFString& val)
{
if (val.empty())
{
return OFFalse;
}
m_DefaultValue = val;
return OFTrue;
}
OFBool IODRule::setVM(const OFString& val)
{
// One could check whether this is a valid VM string...
m_VM = val;
return OFTrue;
}
OFCondition IODRule::check(DcmItem& item, const OFBool quiet)
{
OFCondition result;
const OFString tagName = DcmTag(m_Key).getTagName();
DcmElement* elem = NULL;
OFCondition searchCond = item.findAndGetElement(m_Key, elem, OFFalse /* only this level */);
/* NB: type 1C and 2C cannot be checked, assuming to be optional */
if (((m_Type == "1") || (m_Type == "2")) && searchCond.bad())
{
if (!quiet)
DCMIOD_WARN(tagName << " " << m_Key << " absent in " << m_Module << " (type " << m_Type << ")");
result = EC_MissingAttribute;
}
else if ((elem == NULL) || elem->isEmpty(OFTrue /*normalize*/))
{
/* however, type 1C should never be present with empty value */
if (((m_Type == "1") || (m_Type == "1C")) && searchCond.good())
{
if (!quiet)
DCMIOD_WARN(tagName << " " << m_Key << " empty in " << m_Module << " (type " << m_Type << ")");
result = EC_MissingValue;
}
}
else
{
const OFCondition checkResult = elem->checkValue(m_VM, OFTrue /*oldFormat*/);
if (checkResult == EC_InvalidCharacter)
{
if (!quiet)
DCMIOD_WARN(tagName << " " << m_Key << " contains invalid character(s) in " << m_Module);
result = checkResult;
}
else if (checkResult == EC_ValueRepresentationViolated)
{
if (!quiet)
DCMIOD_WARN(tagName << " " << m_Key << " violates VR definition in " << m_Module);
result = checkResult;
}
else if (checkResult == EC_ValueMultiplicityViolated)
{
const OFString vmText = (elem->getVR() == EVR_SQ) ? " #items" : " VM";
if (!quiet)
DCMIOD_WARN(tagName << " " << m_Key << vmText << " != " << m_VM << " in " << m_Module);
result = checkResult;
}
else if (checkResult == EC_MaximumLengthViolated)
{
if (!quiet)
DCMIOD_WARN(tagName << " " << m_Key << " violates maximum VR length in " << m_Module);
result = checkResult;
}
else if (checkResult.bad())
{
if (!quiet)
DCMIOD_DEBUG("INTERNAL ERROR while checking value of " << tagName << " " << m_Key << " in "
<< m_Module);
result = EC_InternalError;
}
}
return result;
}
|