1 /*=============================================================================
2 
3   Library: XNAT/Core
4 
5   Copyright (c) German Cancer Research Center,
6     Division of Medical and Biological Informatics
7 
8   Licensed under the Apache License, Version 2.0 (the "License");
9   you may not use this file except in compliance with the License.
10   You may obtain a copy of the License at
11 
12     http://www.apache.org/licenses/LICENSE-2.0
13 
14   Unless required by applicable law or agreed to in writing, software
15   distributed under the License is distributed on an "AS IS" BASIS,
16   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   See the License for the specific language governing permissions and
18   limitations under the License.
19 
20 =============================================================================*/
21 
22 #include "ctkXnatResourceCatalogXmlParser.h"
23 
24 #include <QDebug>
25 #include <QXmlStreamReader>
26 
27 //----------------------------------------------------------------------------
28 class ctkXnatResourceCatalogXmlParserPrivate
29 {
30 public:
31 
ctkXnatResourceCatalogXmlParserPrivate()32   ctkXnatResourceCatalogXmlParserPrivate()
33   {
34   }
35 
36   QXmlStreamReader xmlReader;
37 };
38 
ctkXnatResourceCatalogXmlParser()39 ctkXnatResourceCatalogXmlParser::ctkXnatResourceCatalogXmlParser()
40   : d_ptr (new ctkXnatResourceCatalogXmlParserPrivate())
41 {
42 }
~ctkXnatResourceCatalogXmlParser()43 ctkXnatResourceCatalogXmlParser::~ctkXnatResourceCatalogXmlParser()
44 {
45   delete d_ptr;
46 }
47 
setData(const QByteArray & xmlInput)48 void ctkXnatResourceCatalogXmlParser::setData(const QByteArray &xmlInput)
49 {
50   Q_D(ctkXnatResourceCatalogXmlParser);
51   d->xmlReader.addData(xmlInput);
52 }
53 
parseXml(QList<QVariantMap> & result)54 void ctkXnatResourceCatalogXmlParser::parseXml(QList<QVariantMap>& result)
55 {
56   Q_D(ctkXnatResourceCatalogXmlParser);
57 
58   while (!d->xmlReader.atEnd())
59   {
60     if (d->xmlReader.name().compare(QLatin1String("entry")) == 0)
61     {
62       QVariantMap map;
63       QXmlStreamAttributes attributes = d->xmlReader.attributes();
64 
65       if( attributes.hasAttribute("name") && attributes.hasAttribute("digest"))
66       {
67         QString name("");
68         name += attributes.value("name");
69         QString md5("");
70         md5 += attributes.value("digest");
71         map[name] = md5;
72         result.append(map);
73       }
74     }
75     d->xmlReader.readNext();
76   }
77   if (d->xmlReader.hasError())
78   {
79     qWarning()<<"Error parsing XNAT resource catalog xml!";
80   }
81 }
82