XalanDOMString& FindNodeAndGetAttributeValue( XalanNode* theStartNode, const XalanDOMString& theNodeName, const XalanDOMString& theAttributeName, XalanDOMString& theResult, MemoryManagerType& theMemoryManager) { NodeNameTreeWalker theTreeWalker(theMemoryManager); theTreeWalker.findMatchingNodes(theNodeName, theStartNode); const NodeNameTreeWalker::NodeVectorType& theMatchingNodes = theTreeWalker.getMatchingNodes(); if (theMatchingNodes.size() == 1) { const XalanNode* const theMatchingNode = theMatchingNodes[0]; if (theMatchingNode != 0) { GetAttributeFromNode( theMatchingNode, theAttributeName, theResult); } } return theResult; }
QStringList DicomImageSet::getOrderedDcmFiles(const QString &datasetFileName, const QString & queryString) { HRESULT hr = S_OK; IXMLDOMDocument *pXMLDom = NULL; IXMLDOMNodeList *pFileNodes = NULL; VARIANT_BOOL varStatus; VARIANT varFileName; BSTR bstrQuery; QStringList fileList; CHK_HR(CreateAndInitDOM(&pXMLDom)); CHK_HR(VariantFromString(datasetFileName.toStdWString().c_str(), varFileName)); CHK_HR(pXMLDom->load(varFileName, &varStatus)); if (varStatus != VARIANT_TRUE) { CHK_HR(ReportParseError(pXMLDom, "Failed to load DOM from stocks.xml.")); } bstrQuery = SysAllocString(queryString.toStdWString().c_str()); CHK_ALLOC(bstrQuery); CHK_HR(pXMLDom->selectNodes(bstrQuery, &pFileNodes)); if (pFileNodes) { long nFiles; CHK_HR(pFileNodes->get_length(&nFiles)); for (long i=0; i<nFiles; ++i) { VARIANT varValue; IXMLDOMNode * pFile = NULL; CHK_HR(pFileNodes->get_item(i, &pFile)); CHK_HR(GetAttributeFromNode(pFile, L"path", &varValue)); fileList <<QString::fromWCharArray(_bstr_t(varValue)); SAFE_RELEASE(pFile); } } CleanUp: SAFE_RELEASE(pXMLDom); SAFE_RELEASE(pFileNodes); return fileList; }
QString DicomImageSet::getDTIFileName(const QString &datasetFolderName, const QString &queryString) { QString datasetFileName = datasetFolderName+DATASET_FILE_NAME; HRESULT hr = S_OK; IXMLDOMDocument *pXMLDom = NULL; IXMLDOMNode *pFileNode = NULL; VARIANT_BOOL varStatus; VARIANT varFileName; BSTR bstrQuery; QStringList fileList; CHK_HR(CreateAndInitDOM(&pXMLDom)); CHK_HR(VariantFromString(datasetFileName.toStdWString().c_str(), varFileName)); CHK_HR(pXMLDom->load(varFileName, &varStatus)); if (varStatus != VARIANT_TRUE) { CHK_HR(ReportParseError(pXMLDom, "Failed to load DOM from stocks.xml.")); } bstrQuery = SysAllocString(QString(queryString+"/DTI").toStdWString().c_str()); CHK_ALLOC(bstrQuery); CHK_HR(pXMLDom->selectSingleNode(bstrQuery, &pFileNode)); if (pFileNode) { VARIANT varValue; CHK_HR(GetAttributeFromNode(pFileNode, L"file_name", &varValue)); return QString::fromWCharArray(_bstr_t(varValue)); } CleanUp: SAFE_RELEASE(pXMLDom); SAFE_RELEASE(pFileNode); return QString(); }
int DicomImageSet::insertFileItem(IXMLDOMDocument *pDom, IXMLDOMNode * pRoot, const QString &folderName, const QString &fileName) { DicomImage * dicomImage = new DicomImage (); if (!dicomImage->readHeader((QDir(folderName).absoluteFilePath(fileName)).toStdString().c_str())) { //fail to resolve header information return 0; } int nElement; char * patientsName = dicomImage->getCharTag(DicomImage::TAG_PATIENTS_NAME, nElement); char * patientsId = dicomImage->getCharTag(DicomImage::TAG_PATIENT_ID, nElement); char * protocolName = dicomImage->getCharTag(DicomImage::TAG_PROTOCOL_NAME, nElement); double * pInstanceNumber = dicomImage->getNumericTag(DicomImage::TAG_INSTANCE_NUMBER, nElement); double * pAcquisitionNumber = dicomImage->getNumericTag(DicomImage::TAG_ACQUISITION_NUMBER, nElement); double * pAcquisitionDate = dicomImage->getNumericTag(DicomImage::TAG_ACQUISITION_DATE, nElement); int instanceNumber = pInstanceNumber?(int)pInstanceNumber[0]:-1; int acquisitionNumber = pAcquisitionNumber?(int)pAcquisitionNumber[0]:-1; int acquisitionDate = pAcquisitionDate?(int)pAcquisitionDate[0]:0; IXMLDOMNode * pParent = findParentNode(pDom, pRoot, patientsName, patientsId, acquisitionDate, protocolName, acquisitionNumber); if (!pParent) { return 0; } HRESULT hr = S_OK; IXMLDOMElement *pNode = NULL; CHK_HR(CreateElement(pDom, L"File", &pNode)); CHK_HR(CreateAndAddAttributeNode(pDom, L"path", fileName.toStdWString().c_str(), pNode)); CHK_HR(CreateAndAddAttributeNode(pDom, L"instance_number", QString::number(instanceNumber).toStdWString().c_str(), pNode)); IXMLDOMNode * pSibling = NULL; CHK_HR(pParent->get_firstChild(&pSibling)); int sibInstanceNumber = -1; VARIANT varInstanceNumber; DOMNodeType nodeType; while (pSibling && instanceNumber >= sibInstanceNumber) { CHK_HR(pSibling->get_nodeType(&nodeType)); if(nodeType == NODE_ELEMENT) { CHK_HR(GetAttributeFromNode(pSibling, L"instance_number", &varInstanceNumber)); sibInstanceNumber = QString::fromWCharArray(_bstr_t(varInstanceNumber)).toInt(); if (instanceNumber < sibInstanceNumber) { break; } } IXMLDOMNode * tmpNode = NULL; CHK_HR(pSibling->get_nextSibling(&tmpNode)); SAFE_RELEASE(pSibling); pSibling = tmpNode; } if (pSibling) { IXMLDOMNode * tmpNode = NULL; CHK_HR(pSibling->get_previousSibling(&tmpNode)); SAFE_RELEASE(pSibling); pSibling = tmpNode; } CHK_HR(CreateAndAddTextNodeBefore(pDom, L"\n", pParent, pSibling)); CHK_HR(InsertChildToParent(pNode, pSibling, pParent)); CleanUp: SAFE_RELEASE(pNode); return 1; }
int DicomImageSet::readDatasetFile(const QString &datasetFileName, QTreeWidget *treeWidget) { HRESULT hr = S_OK; IXMLDOMDocument *pXMLDom = NULL; IXMLDOMNodeList *pPatients = NULL; IXMLDOMNode *pPatient = NULL; DOMNodeType nodeType; VARIANT_BOOL varStatus; VARIANT varFileName; VARIANT varValue; BSTR bstrQuery; VariantInit(&varFileName); VariantInit(&varValue); QString queryPatient, queryDate, queryProtocol, queryCollection; CHK_HR(CreateAndInitDOM(&pXMLDom)); CHK_HR(VariantFromString(datasetFileName.toStdWString().c_str(), varFileName)); CHK_HR(pXMLDom->load(varFileName, &varStatus)); if (varStatus != VARIANT_TRUE) { CHK_HR(ReportParseError(pXMLDom, "Failed to load DOM from stocks.xml.")); } treeWidget->clear(); treeWidget->setColumnCount(1); CHK_HR(pXMLDom->getElementsByTagName(L"Patient", &pPatients)); if (pPatients) { long nPatients; CHK_HR(pPatients->get_length(&nPatients)); for (long i=0; i<nPatients; ++i) { CHK_HR(pPatients->get_item(i, &pPatient)); CHK_HR(GetAttributeFromNode(pPatient, L"patients_name", &varValue)); queryPatient = QString::fromWCharArray(_bstr_t(varValue)); QTreeWidgetItem * patientItem = new QTreeWidgetItem(treeWidget, QStringList(queryPatient)); patientItem->setExpanded(true); IXMLDOMNodeList * pDates = NULL; CHK_HR(pPatient->get_childNodes(&pDates)); long nDates; CHK_HR(pDates->get_length(&nDates)); for (long j=0; j<nDates; ++j) { IXMLDOMNode * pDate = NULL; CHK_HR(pDates->get_item(j, &pDate)); CHK_HR(pDate->get_nodeType(&nodeType)); if(nodeType!=NODE_ELEMENT) { continue; } CHK_HR(GetAttributeFromNode(pDate, L"acquisition_date", &varValue)); queryDate = QString::fromWCharArray(_bstr_t(varValue)); int intDate = queryDate.toInt(); QDate date = (QDate::fromString("1900-01-01", "yyyy-MM-dd")).addDays(intDate-693962); QTreeWidgetItem * dateItem = new QTreeWidgetItem(patientItem, QStringList(date.toString("yyyy-MM-dd"))); dateItem->setExpanded(true); IXMLDOMNodeList * pProtocols = NULL; CHK_HR(pDate->get_childNodes(&pProtocols)); long nProtocols; CHK_HR(pProtocols->get_length(&nProtocols)); for (long j=0; j<nProtocols; ++j) { IXMLDOMNode * pProtocol = NULL; CHK_HR(pProtocols->get_item(j, &pProtocol)); CHK_HR(pProtocol->get_nodeType(&nodeType)); if(nodeType!=NODE_ELEMENT) { continue; } CHK_HR(GetAttributeFromNode(pProtocol, L"protocol_name", &varValue)); queryProtocol = QString::fromWCharArray(_bstr_t(varValue)); QTreeWidgetItem * protocolItem = new QTreeWidgetItem(dateItem, QStringList(queryProtocol)); protocolItem->setExpanded(true); IXMLDOMNodeList * pCollections = NULL; CHK_HR(pProtocol->get_childNodes(&pCollections)); long nCollections; CHK_HR(pCollections->get_length(&nCollections)); for (long j=0; j<nCollections; ++j) { IXMLDOMNode * pCollection = NULL; CHK_HR(pCollections->get_item(j, &pCollection)); CHK_HR(pCollection->get_nodeType(&nodeType)); if(nodeType!=NODE_ELEMENT) { continue; } CHK_HR(GetAttributeFromNode(pCollection, L"acquisition_number", &varValue)); queryCollection = QString::fromWCharArray(_bstr_t(varValue)); QTreeWidgetItem * collectionItem = new QTreeWidgetItem(protocolItem); QString queryStr = QString("root/Patient[@patients_name = '%1']/Date[@acquisition_date = '%2']/Protocol[@protocol_name = '%3']/Collection[@acquisition_number = '%4']") .arg(queryPatient).arg(queryDate).arg(queryProtocol).arg(queryCollection); IXMLDOMElement * pDtiNode = NULL; bstrQuery = SysAllocString(QString(queryStr+"/DTI").toStdWString().c_str()); CHK_ALLOC(bstrQuery); CHK_HR(pXMLDom->selectSingleNode(bstrQuery, (IXMLDOMNode**)&pDtiNode)); SysFreeString(bstrQuery); if (pDtiNode) { collectionItem->setText(0,queryCollection+"*"); } else { collectionItem->setText(0, queryCollection); } collectionItem->setData(0, Qt::UserRole, queryStr); collectionItem->setExpanded(true); protocolItem->addChild(collectionItem); SAFE_RELEASE(pCollection); } dateItem->addChild(protocolItem); SAFE_RELEASE(pProtocol); } patientItem->addChild(dateItem); SAFE_RELEASE(pDate); } treeWidget->insertTopLevelItem(i, patientItem); SAFE_RELEASE(pPatient); } } CleanUp: SAFE_RELEASE(pXMLDom); SAFE_RELEASE(pPatients); SAFE_RELEASE(pPatient); VariantClear(&varFileName); return SUCCEEDED(hr); }