~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to libscience/elementparser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
copyright            : (C) 2005, 2006 by Carsten Niehaus
 
3
email                : cniehaus@kde.org
 
4
 ***************************************************************************/
 
5
/***************************************************************************
 
6
 *                                                                         *
 
7
 *   This program is free software; you can redistribute it and/or modify  *
 
8
 *   it under the terms of the GNU General Public License as published by  *
 
9
 *   the Free Software Foundation; either version 2 of the License, or     *
 
10
 *   (at your option) any later version.                                   *
 
11
 *                                                                         *
 
12
 ***************************************************************************/
 
13
#include "elementparser.h"
 
14
 
 
15
#include "chemicaldataobject.h"
 
16
#include <kunitconversion/converter.h>
 
17
#include "element.h"
 
18
 
 
19
#include <kdebug.h>
 
20
#include <klocale.h>
 
21
 
 
22
class ElementSaxParser::Private
 
23
{
 
24
public:
 
25
    Private()
 
26
            : currentUnit(KUnitConversion::NoUnit),
 
27
            currentElement(0),
 
28
            inElement(false),
 
29
            inName(false),
 
30
            inMass(false),
 
31
            inExactMass(false),
 
32
            inAtomicNumber(false),
 
33
            inSymbol(false),
 
34
            inIonization(false),
 
35
            inElectronAffinity(false),
 
36
            inElectronegativityPauling(false),
 
37
            inRadiusCovalent(false),
 
38
            inRadiusVDW(false),
 
39
            inBoilingPoint(false),
 
40
            inMeltingPoint(false),
 
41
            inPeriodTableBlock(false),
 
42
            inNameOrigin(false),
 
43
            inDiscoveryDate(false),
 
44
            inDiscoverers(false),
 
45
            inPeriod(false),
 
46
            inCrystalstructure( false ),
 
47
            inAcidicbehaviour( false ),
 
48
            inFamily( false ),
 
49
            inGroup( false ),
 
50
            inElectronicconfiguration( false ),
 
51
            inDangerSymbol( false ),
 
52
            inRPhrase( false ),
 
53
            inSPhrase( false ),
 
54
            inCountry( false ),
 
55
            inOxidation( false )
 
56
    {}
 
57
 
 
58
    ~Private()
 
59
    {
 
60
        delete currentElement;
 
61
        //qDeleteAll(elements);
 
62
    }
 
63
 
 
64
    ChemicalDataObject currentDataObject;
 
65
    int currentUnit; // KUnitConversion::UnitId
 
66
    Element *currentElement;
 
67
 
 
68
    QList<Element*> elements;
 
69
 
 
70
    bool inElement;
 
71
    bool inName;
 
72
    bool inMass;
 
73
    bool inExactMass;
 
74
    bool inAtomicNumber;
 
75
    bool inSymbol;
 
76
    bool inIonization;
 
77
    bool inElectronAffinity;
 
78
    bool inElectronegativityPauling;
 
79
    bool inRadiusCovalent;
 
80
    bool inRadiusVDW;
 
81
    bool inBoilingPoint;
 
82
    bool inMeltingPoint;
 
83
    bool inPeriodTableBlock;
 
84
    bool inNameOrigin;
 
85
    bool inDiscoveryDate;
 
86
    bool inDiscoverers;
 
87
    bool inPeriod;
 
88
    bool inCrystalstructure;
 
89
    bool inAcidicbehaviour;
 
90
    bool inFamily;
 
91
    bool inGroup;
 
92
    bool inElectronicconfiguration;
 
93
    bool inDangerSymbol;
 
94
    bool inRPhrase;
 
95
    bool inSPhrase;
 
96
    bool inCountry;
 
97
    bool inOxidation;
 
98
};
 
99
 
 
100
ElementSaxParser::ElementSaxParser()
 
101
        : QXmlDefaultHandler(), d( new Private )
 
102
{
 
103
}
 
104
 
 
105
ElementSaxParser::~ElementSaxParser()
 
106
{
 
107
    delete d;
 
108
}
 
109
 
 
110
bool ElementSaxParser::startElement(const QString&, const QString &localName, const QString&, const QXmlAttributes &attrs)
 
111
{
 
112
    if (localName == "atom")
 
113
    {
 
114
        d->currentElement = new Element();
 
115
        d->inElement = true;
 
116
    } else if ( ( d->inElement && localName == "scalar" ) || localName == "array" )
 
117
    {
 
118
        for (int i = 0; i < attrs.length(); ++i)
 
119
        {
 
120
            if ( attrs.localName( i ) == "units" )
 
121
            {
 
122
//                 kDebug() << "value of the unit: " << attrs.value(i);
 
123
                d->currentUnit = unit( attrs.value( i ) );
 
124
//                 kDebug() << "Took " << d->currentUnit;
 
125
                continue;
 
126
            }
 
127
 
 
128
            if (attrs.value(i) == "bo:atomicNumber")
 
129
                d->inAtomicNumber = true;
 
130
            else if (attrs.value(i) == "bo:mass")
 
131
                d->inMass = true;
 
132
            else if (attrs.value(i) == "bo:exactMass")
 
133
                d->inExactMass = true;
 
134
            else if (attrs.value(i) == "bo:ionization")
 
135
                d->inIonization = true;
 
136
            else if (attrs.value(i) == "bo:electronAffinity")
 
137
                d->inElectronAffinity = true;
 
138
            else if (attrs.value(i) == "bo:electronegativityPauling")
 
139
                d->inElectronegativityPauling = true;
 
140
            else if (attrs.value(i) == "bo:radiusCovalent")
 
141
                d->inRadiusCovalent = true;
 
142
            else if (attrs.value(i) == "bo:radiusVDW")
 
143
                d->inRadiusVDW = true;
 
144
            else if (attrs.value(i) == "bo:meltingpoint")
 
145
                d->inMeltingPoint = true;
 
146
            else if (attrs.value(i) == "bo:boilingpoint")
 
147
                d->inBoilingPoint = true;
 
148
            else if (attrs.value(i) == "bo:periodTableBlock")
 
149
                d->inPeriodTableBlock = true;
 
150
            else if (attrs.value(i) == "bo:nameOrigin")
 
151
                d->inNameOrigin = true;
 
152
            else if (attrs.value(i) == "bo:discoveryDate")
 
153
                d->inDiscoveryDate = true;
 
154
            else if (attrs.value(i) == "bo:discoverers")
 
155
                d->inDiscoverers = true;
 
156
            else if (attrs.value(i) == "bo:discoveryCountry")
 
157
                d->inCountry = true;
 
158
            else if (attrs.value(i) == "bo:period")
 
159
                d->inPeriod = true;
 
160
            else if (attrs.value(i) == "bo:crystalstructure")
 
161
                d->inCrystalstructure = true;
 
162
            else if (attrs.value(i) == "bo:acidicbehaviour")
 
163
                d->inAcidicbehaviour = true;
 
164
            else if (attrs.value(i) == "bo:family")
 
165
                d->inFamily = true;
 
166
            else if (attrs.value(i) == "bo:group")
 
167
                d->inGroup = true;
 
168
            else if (attrs.value(i) == "bo:electronicConfiguration")
 
169
                d->inElectronicconfiguration = true;
 
170
            else if (attrs.value(i) == "bo:dangerSymbol")
 
171
                d->inDangerSymbol = true;
 
172
            else if (attrs.value(i) == "bo:RPhrase")
 
173
                d->inRPhrase = true;
 
174
            else if (attrs.value(i) == "bo:SPhrase")
 
175
                d->inSPhrase = true;
 
176
            else if (attrs.value(i) == "bo:oxidation")
 
177
                d->inOxidation = true;
 
178
        }
 
179
    } else if (d->inElement && localName == "label")
 
180
    {
 
181
        for (int i = 0; i < attrs.length(); ++i)
 
182
        {
 
183
            if ( attrs.localName( i ) != "dictRef" )
 
184
                continue;
 
185
 
 
186
            if (attrs.value(i) == "bo:symbol") {
 
187
                for (int i = 0; i < attrs.length(); ++i)
 
188
                {
 
189
                    if (attrs.localName(i) == "value") {
 
190
                        d->currentDataObject.setData( attrs.value(i) );
 
191
                        d->currentDataObject.setType( ChemicalDataObject::symbol );
 
192
 
 
193
                        if ( d->currentElement )
 
194
                            d->currentElement->addData( d->currentDataObject );
 
195
                    }
 
196
                }
 
197
            }
 
198
            else if ( attrs.value(i) == "bo:name" ) {
 
199
                for (int i = 0; i < attrs.length(); ++i)
 
200
                {
 
201
                    if (attrs.localName(i) == "value") {
 
202
                        d->currentDataObject.setData( i18n( attrs.value(i).toUtf8() ) );
 
203
                        d->currentDataObject.setType( ChemicalDataObject::name );
 
204
 
 
205
                        if ( d->currentElement )
 
206
                            d->currentElement->addData( d->currentDataObject );
 
207
                    }
 
208
                }
 
209
            }
 
210
        }
 
211
    }
 
212
    return true;
 
213
}
 
214
 
 
215
bool ElementSaxParser::endElement( const QString &, const QString& localName, const QString& )
 
216
{
 
217
    if ( localName == "atom" )
 
218
    {
 
219
        if ( d->currentElement->dataAsString( ChemicalDataObject::symbol ) != "Xx" )
 
220
            d->elements.append(d->currentElement);
 
221
        else
 
222
            delete d->currentElement;
 
223
 
 
224
        d->currentElement = 0;
 
225
        d->inElement = false;
 
226
    }
 
227
    else if ( localName == "scalar" || localName == "label" || localName == "array" )
 
228
    {
 
229
        d->currentDataObject.setUnit( d->currentUnit );
 
230
    }
 
231
    return true;
 
232
}
 
233
 
 
234
bool ElementSaxParser::characters(const QString &ch)
 
235
{
 
236
    d->currentDataObject = ChemicalDataObject();
 
237
    ChemicalDataObject::BlueObelisk type;
 
238
    QVariant value;
 
239
 
 
240
    if (d->inMass) {
 
241
        value = ch.toDouble();
 
242
        type = ChemicalDataObject::mass;
 
243
        d->inMass = false;
 
244
    }
 
245
    else if (d->inExactMass) {
 
246
        value = ch.toDouble();
 
247
        type = ChemicalDataObject::exactMass;
 
248
        d->inExactMass = false;
 
249
    }
 
250
    else if (d->inAtomicNumber) {
 
251
        value = ch.toInt();
 
252
        type = ChemicalDataObject::atomicNumber;
 
253
        d->inAtomicNumber = false;
 
254
    }
 
255
    else if (d->inIonization) {
 
256
        value = ch.toDouble();;
 
257
        type = ChemicalDataObject::ionization;
 
258
        d->inIonization = false;
 
259
    }
 
260
    else if (d->inElectronAffinity) {
 
261
        value = ch.toDouble();
 
262
        type = ChemicalDataObject::electronAffinity;
 
263
        d->inElectronAffinity = false;
 
264
    }
 
265
    else if (d->inElectronegativityPauling) {
 
266
        value = ch.toDouble();
 
267
        type = ChemicalDataObject::electronegativityPauling;
 
268
        d->inElectronegativityPauling = false;
 
269
    }
 
270
    else if (d->inRadiusCovalent) {
 
271
        value = ch.toDouble();
 
272
        type = ChemicalDataObject::radiusCovalent;
 
273
        d->inRadiusCovalent = false;
 
274
    }
 
275
    else if (d->inRadiusVDW) {
 
276
        value = ch.toDouble();
 
277
        type = ChemicalDataObject::radiusVDW;
 
278
        d->inRadiusVDW = false;
 
279
    }
 
280
    else if (d->inMeltingPoint) {
 
281
        value = ch.toDouble();
 
282
        type = ChemicalDataObject::meltingpoint;
 
283
        d->inMeltingPoint = false;
 
284
    }
 
285
    else if (d->inBoilingPoint) {
 
286
        value = ch.toDouble();
 
287
        type = ChemicalDataObject::boilingpoint;
 
288
        d->inBoilingPoint = false;
 
289
    }
 
290
    else if (d->inPeriodTableBlock) {
 
291
        value = ch;
 
292
        type = ChemicalDataObject::periodTableBlock;
 
293
        d->inPeriodTableBlock = false;
 
294
    }
 
295
    else if (d->inNameOrigin) {
 
296
        value = i18n( ch.toUtf8() );
 
297
        type = ChemicalDataObject::nameOrigin;
 
298
        d->inNameOrigin = false;
 
299
    }
 
300
    else if (d->inDiscoveryDate) {
 
301
        value = ch.toInt();
 
302
        type = ChemicalDataObject::date;
 
303
        d->inDiscoveryDate = false;
 
304
    }
 
305
    else if (d->inDiscoverers) {
 
306
        value = ch;
 
307
        type = ChemicalDataObject::discoverers;
 
308
        d->inDiscoverers = false;
 
309
    }
 
310
    else if (d->inPeriod) {
 
311
        value = ch.toInt();
 
312
        type = ChemicalDataObject::period;
 
313
        d->inPeriod = false;
 
314
    }
 
315
    else if (d->inCrystalstructure) {
 
316
        value = ch;
 
317
        type = ChemicalDataObject::crystalstructure;
 
318
        d->inCrystalstructure = false;
 
319
    }
 
320
    else if (d->inAcidicbehaviour) {
 
321
        value = ch.toInt();
 
322
        type = ChemicalDataObject::acidicbehaviour;
 
323
        d->inAcidicbehaviour = false;
 
324
    }
 
325
    else if (d->inFamily) {
 
326
        value = ch;
 
327
        type = ChemicalDataObject::family;
 
328
        d->inFamily = false;
 
329
    }
 
330
    else if (d->inGroup) {
 
331
        value = ch.toInt();
 
332
        type = ChemicalDataObject::group;
 
333
        d->inGroup = false;
 
334
    }
 
335
    else if (d->inElectronicconfiguration) {
 
336
        value = ch;
 
337
        type = ChemicalDataObject::electronicConfiguration;
 
338
        d->inElectronicconfiguration = false;
 
339
    }
 
340
    else if (d->inDangerSymbol) {
 
341
        value = ch;
 
342
        type = ChemicalDataObject::dangerSymbol;
 
343
        d->inDangerSymbol = false;
 
344
    }
 
345
    else if (d->inRPhrase) {
 
346
        value = ch;
 
347
        type = ChemicalDataObject::RPhrase;
 
348
        d->inRPhrase = false;
 
349
    }
 
350
    else if (d->inSPhrase) {
 
351
        value = ch;
 
352
        type = ChemicalDataObject::SPhrase;
 
353
        d->inSPhrase = false;
 
354
    }
 
355
    else if (d->inCountry) {
 
356
        if ( ch == "ancient" ) {
 
357
            value = 0;
 
358
            type = ChemicalDataObject::date;
 
359
        } else {
 
360
            value = ch;
 
361
            type = ChemicalDataObject::discoveryCountry;
 
362
        }
 
363
        d->inCountry = false;
 
364
    }
 
365
    else if (d->inOxidation) {
 
366
        value = ch;
 
367
        type = ChemicalDataObject::oxidation;
 
368
        d->inOxidation = false;
 
369
    }
 
370
    else//it is a non known value. Do not create a wrong object but return
 
371
        return true;
 
372
 
 
373
    d->currentDataObject.setData( value );
 
374
    d->currentDataObject.setType( type );
 
375
    d->currentDataObject.setUnit( d->currentUnit );
 
376
 
 
377
    if ( d->currentElement )
 
378
        d->currentElement->addData( d->currentDataObject );
 
379
 
 
380
    return true;
 
381
}
 
382
 
 
383
int ElementSaxParser::unit( const QString& unit ) const
 
384
{
 
385
    if ( unit == "siUnits:kelvin" )
 
386
        return KUnitConversion::Kelvin;
 
387
    else if ( unit == "units:ev" )
 
388
        return KUnitConversion::Electronvolt;
 
389
    else if ( unit == "units:ang" )
 
390
        return KUnitConversion::Angstrom;
 
391
    else if ( unit == "bo:noUnit" )
 
392
        return KUnitConversion::NoUnit;
 
393
    else
 
394
        return KUnitConversion::NoUnit;
 
395
}
 
396
 
 
397
QList<Element*> ElementSaxParser::getElements()
 
398
{
 
399
    return d->elements;
 
400
}