1
/***************************************************************************
2
copyright : (C) 2005-2008 by Carsten Niehaus
3
email : cniehaus@kde.org
4
***************************************************************************/
5
/***************************************************************************
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. *
12
***************************************************************************/
13
#include "isotopeparser.h"
15
#include "chemicaldataobject.h"
17
#include <kunitconversion/converter.h>
21
class IsotopeParser::Private
25
: currentUnit(KUnitConversion::NoUnit),
26
currentErrorValue(QVariant()),
27
currentElementSymbol(QString()),
31
inAtomicNumber(false),
36
inAlphaDecayLikeliness(false),
38
inBetaplusDecayLikeliness(false),
39
inBetaplusDecay(false),
40
inBetaminusDecayLikeliness(false),
41
inBetaminusDecay(false),
42
inECDecayLikeliness(false),
49
delete currentIsotope;
50
//qDeleteAll(isotopes);
53
ChemicalDataObject currentDataObject;
55
QVariant currentErrorValue;
56
QString currentElementSymbol;
57
Isotope* currentIsotope;
59
QList<Isotope*> isotopes;
68
bool inAlphaDecayLikeliness;
70
bool inBetaplusDecayLikeliness;
72
bool inBetaminusDecayLikeliness;
73
bool inBetaminusDecay;
74
bool inECDecayLikeliness;
79
IsotopeParser::IsotopeParser()
80
: QXmlDefaultHandler(), d( new Private )
84
IsotopeParser::~IsotopeParser()
89
bool IsotopeParser::startElement(const QString&, const QString &localName, const QString&, const QXmlAttributes &attrs)
91
if (localName == "isotopeList")
95
//now save the symbol of the current element
96
for (int i = 0; i < attrs.length(); ++i)
98
if ( attrs.localName( i ) == "id" )
99
d->currentElementSymbol = attrs.value( i );
102
} else if ( d->inElement && localName == "isotope")
104
d->currentIsotope = new Isotope();
105
d->currentIsotope->addData( ChemicalDataObject( QVariant( d->currentElementSymbol ), ChemicalDataObject::symbol ) );
107
for (int i = 0; i < attrs.length(); ++i)
109
if ( attrs.localName( i ) == "number" )
111
d->currentIsotope->setNucleons( attrs.value( i ).toInt() );
114
} else if (d->inIsotope && localName == "scalar")
116
for (int i = 0; i < attrs.length(); ++i)
118
if ( attrs.localName( i ) == "errorValue" )
120
d->currentErrorValue = QVariant( attrs.value( i ) );
124
if (attrs.value(i) == "bo:atomicNumber")
125
d->inAtomicNumber = true;
126
else if (attrs.value(i) == "bo:exactMass")
127
d->inExactMass = true;
128
else if (attrs.value(i) == "bo:halfLife"){
129
for (int i = 0; i < attrs.length(); ++i)
131
if (attrs.localName(i) == "units") {
132
if ( attrs.value(i) == "siUnits:s" ) {
133
d->currentUnit = KUnitConversion::Second;
134
} else if ( attrs.value(i) == "units:y" ) {
135
d->currentUnit = KUnitConversion::Year;
137
d->currentUnit = KUnitConversion::NoUnit;
142
d->currentDataObject.setUnit( d->currentUnit );
143
d->inHalfLife = true;
145
else if (attrs.value(i) == "bo:alphaDecay")
146
d->inAlphaDecay = true;
147
else if (attrs.value(i) == "bo:alphaDecayLikeliness")
148
d->inAlphaDecayLikeliness = true;
149
else if (attrs.value(i) == "bo:ecDecay")
151
else if (attrs.value(i) == "bo:ecDecayLikeliness")
152
d->inECDecayLikeliness = true;
153
else if (attrs.value(i) == "bo:betaminusDecay")
154
d->inBetaminusDecay = true;
155
else if (attrs.value(i) == "bo:betaminusDecayLikeliness")
156
d->inBetaminusDecayLikeliness = true;
157
else if (attrs.value(i) == "bo:betaplusDecay")
158
d->inBetaplusDecay = true;
159
else if (attrs.value(i) == "bo:betaplusDecayLikeliness")
160
d->inBetaplusDecayLikeliness = true;
161
else if (attrs.value(i) == "bo:spin")
163
else if (attrs.value(i) == "bo:magneticMoment")
164
d->inMagMoment = true;
165
else if (attrs.value(i) == "bo:relativeAbundance")
166
d->inAbundance = true;
172
bool IsotopeParser::endElement( const QString&, const QString& localName, const QString& )
174
if ( localName == "isotope" )
176
d->isotopes.append(d->currentIsotope);
178
d->currentIsotope = 0;
179
d->inIsotope = false;
181
else if ( localName == "isotopeList" )
182
{//a new list of isotopes start...
183
d->inElement = false;
189
bool IsotopeParser::characters(const QString &ch)
191
ChemicalDataObject::BlueObelisk type;
195
value = ch.toDouble();
196
type = ChemicalDataObject::exactMass;
197
d->inExactMass = false;
199
else if (d->inAtomicNumber) {
201
type = ChemicalDataObject::atomicNumber;
202
d->inAtomicNumber = false;
204
else if (d->inSpin) {
206
type = ChemicalDataObject::spin;
209
else if (d->inMagMoment) {
211
type = ChemicalDataObject::magneticMoment;
212
d->inMagMoment = false;
214
else if (d->inHalfLife) {
215
value = ch.toDouble();
216
type = ChemicalDataObject::halfLife;
217
d->inHalfLife = false;
219
else if (d->inAlphaDecay) {
220
value = ch.toDouble();
221
type = ChemicalDataObject::alphaDecay;
222
d->inAlphaDecay = false;
224
else if (d->inAlphaDecayLikeliness) {
225
value = ch.toDouble();
226
type = ChemicalDataObject::alphaDecayLikeliness;
227
d->inAlphaDecayLikeliness = false;
229
else if (d->inBetaplusDecay) {
230
value = ch.toDouble();
231
type = ChemicalDataObject::betaplusDecay;
232
d->inBetaplusDecay = false;
234
else if (d->inBetaplusDecayLikeliness) {
235
value = ch.toDouble();
236
type = ChemicalDataObject::betaplusDecayLikeliness;
237
d->inBetaplusDecayLikeliness = false;
239
else if (d->inBetaminusDecay) {
240
value = ch.toDouble();
241
type = ChemicalDataObject::betaminusDecay;
242
d->inBetaminusDecay = false;
244
else if (d->inBetaminusDecayLikeliness) {
245
value = ch.toDouble();
246
type = ChemicalDataObject::betaminusDecayLikeliness;
247
d->inBetaminusDecayLikeliness = false;
249
else if (d->inECDecayLikeliness) {
250
value = ch.toDouble();
251
type = ChemicalDataObject::ecDecayLikeliness;
252
d->inECDecayLikeliness = false;
254
else if (d->inECDecay) {
255
value = ch.toDouble();
256
type = ChemicalDataObject::ecDecay;
257
d->inECDecay = false;
259
else if (d->inAbundance){
261
type = ChemicalDataObject::relativeAbundance;
262
d->inAbundance = false;
264
else//it is a non known value. Do not create a wrong object but return
267
if ( type == ChemicalDataObject::exactMass )
269
d->currentDataObject.setErrorValue( d->currentErrorValue );
272
d->currentDataObject.setData( value );
273
d->currentDataObject.setType( type );
275
if ( d->currentIsotope )
277
d->currentIsotope->addData( d->currentDataObject );
283
QList<Isotope*> IsotopeParser::getIsotopes()