From: Carsten Niehaus Date: Mon, 31 Oct 2005 11:17:19 +0000 (+0000) Subject: * Add a test-file for xmlspec X-Git-Tag: v3.80.2~240 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=74972c8ca5d239d9c22d0d5615ec5989306e82bd;p=libqmvoc.git * Add a test-file for xmlspec * make the spectrumparser SAX svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=476034 --- diff --git a/libscience/spectrum.cpp b/libscience/spectrum.cpp index 66388a0..b73c8f0 100644 --- a/libscience/spectrum.cpp +++ b/libscience/spectrum.cpp @@ -25,11 +25,11 @@ #include -double Spectrum::minBand() +double Spectrum::minPeak() { - double value = ( *m_bandlist.begin() )->wavelength; - QList::const_iterator it = m_bandlist.begin(); - const QList::const_iterator itEnd = m_bandlist.end(); + double value = ( *m_peaklist.begin() )->wavelength; + QList::const_iterator it = m_peaklist.begin(); + const QList::const_iterator itEnd = m_peaklist.end(); for (;it!=itEnd;++it) { if ( value > ( *it )->wavelength ) @@ -38,11 +38,11 @@ double Spectrum::minBand() return value; } -double Spectrum::maxBand() +double Spectrum::maxPeak() { - double value = ( *m_bandlist.begin() )->wavelength; - QList::const_iterator it = m_bandlist.begin(); - const QList::const_iterator itEnd = m_bandlist.end(); + double value = ( *m_peaklist.begin() )->wavelength; + QList::const_iterator it = m_peaklist.begin(); + const QList::const_iterator itEnd = m_peaklist.end(); for (;it!=itEnd;++it) { if ( value < ( *it )->wavelength ) @@ -56,15 +56,15 @@ Spectrum* Spectrum::adjustToWavelength( double min, double max ) { Spectrum *spec = new Spectrum(); - QList::const_iterator it = m_bandlist.begin(); - const QList::const_iterator itEnd = m_bandlist.end(); + QList::const_iterator it = m_peaklist.begin(); + const QList::const_iterator itEnd = m_peaklist.end(); for ( ; it != itEnd; ++it ) { if ( ( *it )->wavelength < min || ( *it )->wavelength > max ) continue; - spec->addBand( *it ); + spec->addPeak( *it ); } spec->adjustMinMax(); @@ -75,8 +75,8 @@ Spectrum* Spectrum::adjustToWavelength( double min, double max ) void Spectrum::adjustIntensities() { int maxInt = 0; - QList::Iterator it = m_bandlist.begin(); - const QList::Iterator itEnd = m_bandlist.end(); + QList::Iterator it = m_peaklist.begin(); + const QList::Iterator itEnd = m_peaklist.end(); //find the highest intensity for ( ; it != itEnd; ++it ) @@ -91,7 +91,7 @@ void Spectrum::adjustIntensities() double max = ( double ) maxInt; //now adjust the intensities. - it = m_bandlist.begin(); + it = m_peaklist.begin(); for ( ; it != itEnd; ++it ) { double curInt = ( ( double )( *it )->intensity ); @@ -105,8 +105,8 @@ QList Spectrum::wavelengths( double min, double max ) { QList list; - QList::const_iterator it = m_bandlist.begin(); - const QList::const_iterator itEnd = m_bandlist.end(); + QList::const_iterator it = m_peaklist.begin(); + const QList::const_iterator itEnd = m_peaklist.end(); for ( ; it != itEnd; ++it ) { diff --git a/libscience/spectrum.h b/libscience/spectrum.h index 1092239..bc2bdfc 100644 --- a/libscience/spectrum.h +++ b/libscience/spectrum.h @@ -45,12 +45,12 @@ class Spectrum ~Spectrum(){}; /** - * a band is one line in the spectrum of an element + * a peak is one line in the spectrum of an element */ - class band + class peak { public: - band(){ + peak(){ wavelength = -1.0; aki = -1.0; energy1 = -1.0; @@ -83,12 +83,12 @@ class Spectrum }; /** - * adds the band @p b to the internal - * lists of bands + * adds the peak @p b to the internal + * lists of peaks */ - void addBand( Spectrum::band* b ){ + void addPeak( Spectrum::peak* b ){ if ( b ) - m_bandlist.append( b ); + m_peaklist.append( b ); } /** @@ -131,14 +131,14 @@ class Spectrum } /** - * @return the list of bands of the spectrum + * @return the list of peaks of the spectrum */ - QList bandlist(){ - return m_bandlist; + QList peaklist(){ + return m_peaklist; } - QList bandList(){ - return bandlist(); + QList peakList(){ + return peaklist(); } /** @@ -146,8 +146,8 @@ class Spectrum * smallest wavelenght */ void adjustMinMax(){ - m_min = minBand(); - m_max = maxBand(); + m_min = minPeak(); + m_max = maxPeak(); } /** @@ -161,17 +161,17 @@ class Spectrum /** * @return the smallest wavelength */ - double minBand(); + double minPeak(); /** * @return the bigest wavelength */ - double maxBand(); + double maxPeak(); /** * the internal dataset */ - QList m_bandlist; + QList m_peaklist; /** * the cached values of the highest and lowest wavelength diff --git a/libscience/spectrumparser.cpp b/libscience/spectrumparser.cpp index 12917a6..4128609 100644 --- a/libscience/spectrumparser.cpp +++ b/libscience/spectrumparser.cpp @@ -12,116 +12,114 @@ email : cniehaus@kde.org ***************************************************************************/ #include "spectrumparser.h" #include "spectrum.h" -#include "element.h" #include #include #include -#include -#include +#include #include -Spectrum* SpectrumParser::loadSpectrum( QFile*file, const QString& id ) +SpectrumParser::SpectrumParser() + : QXmlDefaultHandler(), + currentSpectrum_(0), + inAtomicNumber_(false), + inExactMass_(false), + inAbundance_(false) { - Q_UNUSED( file ); - Q_UNUSED( id ); - return 0; + currentElementSymbol_ = ""; } -void SpectrumParser::saveSpectrum( Spectrum *spectrum ) +bool SpectrumParser::startElement(const QString&, const QString &localName, const QString&, const QXmlAttributes &attrs) { - if ( !spectrum ) return; - - QDomDocument doc( "SpectrumDocument" ); - - QFile docfile( "/home/carsten/test.xml" ); - - QList blist = spectrum->bandlist(); + if (localName == "isotope") + { + currentSpectrum_ = new Spectrum(); + inSpectrum_ = true; - QDomElement cmlElement = doc.createElement( "cml" ); - QDomElement spectrumListElement = doc.createElement( "spectrumList" ); - QDomElement spectrumElement = doc.createElement( "spectrum" ); - QDomElement metadataListElement = doc.createElement( "metadataList" ); - - //metadata -//X QDomElement metadataElement = doc.createElement( "metadata" ); -//X if ( spectrum ) -//X { -//X metadataElement.setAttribute("content",spectrum->parentElement()->elname() ); -//X metadataElement.setAttribute("name","ElementName"); -//X } - - QDomElement metadataElement2 = doc.createElement( "metadata" ); - metadataElement2.setAttribute("content", "National Institute of Standards and Technology" ); - metadataElement2.setAttribute("name","OWNER"); - - QDomElement peakListElement = doc.createElement( "peakList" ); - - doc.appendChild( cmlElement ); - cmlElement.appendChild( spectrumListElement ); - spectrumListElement.appendChild( spectrumElement ); - spectrumElement.appendChild( metadataListElement ); -//X if ( spectrum ) -//X metadataListElement.appendChild( metadataElement ); - metadataListElement.appendChild( metadataElement2 ); - spectrumElement.appendChild( peakListElement ); - - foreach( Spectrum::band* band, blist ) + //now save the symbol of the current element + for (int i = 0; i < attrs.length(); ++i) + { + if ( attrs.localName( i ) == "elementType" ) + currentElementSymbol_ = attrs.value( i ); + } + } else if (inSpectrum_ && localName == "scalar") { - QDomElement peakElement = doc.createElement( "peak" ); - peakElement.setAttribute( "id" , "" ); - peakElement.setAttribute( "xUnits", "nm" ); - peakElement.setAttribute( "xValue", band->wavelength ); - peakElement.setAttribute( "yUnits", "Intensity relative to 1000" ); - peakElement.setAttribute( "yValue", band->intensity ); - - //now the scalar values - QDomElement scalar = doc.createElement( "scalar" ); - QDomElement scalar2 = doc.createElement( "scalar" ); - QDomElement scalar3 = doc.createElement( "scalar" ); - QDomElement scalar4 = doc.createElement( "scalar" ); - QDomElement scalar5 = doc.createElement( "scalar" ); - QDomElement scalar6 = doc.createElement( "scalar" ); - QDomElement scalar7 = doc.createElement( "scalar" ); - QDomElement scalar8 = doc.createElement( "scalar" ); - QDomElement scalar9 = doc.createElement( "scalar" ); + for (int i = 0; i < attrs.length(); ++i) + { + if ( attrs.localName( i ) == "errorValue" ) + { + currentErrorValue_ = QVariant( attrs.value( i ) ); + continue; + } + + if (attrs.value(i) == "bo:atomicNumber") + inAtomicNumber_ = true; + else if (attrs.value(i) == "bo:exactMass") + inExactMass_ = true; + } + } else if (inSpectrum_ && localName == "bo:relativeAbundance") { + kdDebug() << "bo:relativeAbundance" << endl; + inAbundance_ = true; + } + return true; +} - scalar.setAttribute( "title","aki" ); - scalar2.setAttribute( "title","energy1" ); - scalar3.setAttribute( "title","energy2" ); - scalar4.setAttribute( "title","term1" ); - scalar5.setAttribute( "title","term2" ); - scalar6.setAttribute( "title","electronconfig1" ); - scalar7.setAttribute( "title","electronconfig2" ); - scalar8.setAttribute( "title","J1" ); - scalar9.setAttribute( "title","J2" ); +bool SpectrumParser::endElement( const QString&, const QString& localName, const QString& ) +{ + if ( localName == "isotope" ) + { +//X currentSpectrum_->addData( new ChemicalDataObject( QVariant( currentElementSymbol_ ), ChemicalDataObject::symbol ) ); +//X isotopes_.append(currentSpectrum_); +//X +//X currentSpectrum_ = 0; +//X currentDataObject_ = 0; +//X inSpectrum_ = false; + } + else if ( localName == "scalar" ) + { +//X if ( currentDataObject_->type() == ChemicalDataObject::exactMass ){ +//X currentDataObject_->setErrorValue( currentErrorValue_ ); +//X } + } - scalar.setNodeValue( "test123" );//QString::number( band->aki ) ); + return true; +} -//X peakElement.setAttribute( "aki", band->aki ); -//X peakElement.setAttribute( "energy1", band->energy1 ); -//X peakElement.setAttribute( "energy2", band->energy2 ); -//X peakElement.setAttribute( "term1", band->term1 ); -//X peakElement.setAttribute( "term2", band->term2 ); -//X peakElement.setAttribute( "electronconfig1", band->electronconfig1 ); -//X peakElement.setAttribute( "electronconfig2", band->electronconfig2 ); -//X peakElement.setAttribute( "J1", band->J1 ); -//X peakElement.setAttribute( "J2", band->J2 ); +bool SpectrumParser::characters(const QString &ch) +{ + currentDataObject_ = new ChemicalDataObject(); + ChemicalDataObject::BlueObelisk type; + QVariant value; + + if ( inExactMass_ ){ + value = ch.toDouble(); + type = ChemicalDataObject::exactMass; + inExactMass_ = false; + } + else if (inAtomicNumber_) { + value = ch.toInt(); + type = ChemicalDataObject::atomicNumber; + inAtomicNumber_ = false; + } + else if ( inAbundance_ ){ + value = ch; + type = ChemicalDataObject::relativeAbundance; + inAbundance_ = false; + } + else//it is a non known value. Do not create a wrong object but return + return true; + currentDataObject_->setData( value ); + currentDataObject_->setType( type ); - peakElement.appendChild( scalar ); - peakElement.appendChild( scalar2 ); - peakElement.appendChild( scalar3 ); - peakElement.appendChild( scalar4 ); - peakElement.appendChild( scalar5 ); - peakElement.appendChild( scalar6 ); - peakElement.appendChild( scalar7 ); - peakElement.appendChild( scalar8 ); - peakElement.appendChild( scalar9 ); +//X if ( currentSpectrum_ ) +//X currentSpectrum_->addData( currentDataObject_ ); - peakListElement.appendChild( peakElement ); - } + return true; +} - kdDebug() << "Text: " << doc.toString() << endl; +QList SpectrumParser::getSpectrums() +{ + return spectra_; } diff --git a/libscience/spectrumparser.h b/libscience/spectrumparser.h index f5bbcfe..8f6b39f 100644 --- a/libscience/spectrumparser.h +++ b/libscience/spectrumparser.h @@ -14,27 +14,45 @@ ***************************************************************************/ #include +#include + +#include "chemicaldataobject.h" class Spectrum; /** * @author Carsten Niehaus */ -class SpectrumParser +class SpectrumParser : public QXmlDefaultHandler { public: /** - * Save the Spectrum @p spectrum - * @param spectrum the Spectrum to save + * Constructor */ - static void saveSpectrum( Spectrum *spectrum ); + SpectrumParser(); + bool startElement( const QString&, const QString &localName, const QString&, const QXmlAttributes &attrs ); - /** - * @return the Spectrum specified by the @p id - * @param file the file from which the data will be loaded - * @param id The id of the Spectrum - */ - static Spectrum* loadSpectrum( QFile *file, const QString& id ); + bool endElement( const QString& namespaceURI, const QString &localName, const QString& qName ); + + bool characters(const QString &ch); + + QList getSpectrums(); + + private: + ChemicalDataObject *currentDataObject_; + ChemicalDataObject::BlueObeliskUnit currentUnit_; + + QVariant currentErrorValue_; + + QString currentElementSymbol_; + + Spectrum* currentSpectrum_; + QList spectra_; + bool inSpectrum_; + bool inAtomicNumber_, + inExactMass_; + + bool inAbundance_; }; #endif // SPECTRUMPARSER_H diff --git a/libscience/tests/cmlspec_example.cml b/libscience/tests/cmlspec_example.cml new file mode 100644 index 0000000..e0c33cf --- /dev/null +++ b/libscience/tests/cmlspec_example.cml @@ -0,0 +1,191 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +