From: Carsten Niehaus Date: Sun, 16 Oct 2005 12:15:51 +0000 (+0000) Subject: * Ok, Tsoots of Scribus fame convinced me that SAX is better than DOM for my usecase... X-Git-Tag: v3.80.2~292 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=07cbf399a16fd16773f8298d3b7cc97c79f6bf97;p=libqmvoc.git * Ok, Tsoots of Scribus fame convinced me that SAX is better than DOM for my usecase. He wrote the based code, I ported to read Qt-code. * This commit removea the QDom-implementation * Four datasets (number, mass, name and symbol) are now working CCMAIL:kalzium@kde.org svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=471100 --- diff --git a/libscience/Makefile.am b/libscience/Makefile.am index c7b815f..bd2a3d6 100644 --- a/libscience/Makefile.am +++ b/libscience/Makefile.am @@ -4,16 +4,13 @@ INCLUDES= $(all_includes) lib_LTLIBRARIES = libscience.la -bin_PROGRAMS = libscience - libscience_la_SOURCES = \ element.cpp \ spectrum.cpp \ isotope.cpp \ spectrumparser.cpp \ elementparser.cpp \ - tempunit.cpp \ - main.cpp + tempunit.cpp libscience_la_LDFLAGS = $(all_libraries) -no-undefined -version-info 4:0:0 libscience_la_LIBADD = $(LIB_KDEUI) diff --git a/libscience/elementparser.cpp b/libscience/elementparser.cpp index 411349a..b02d9f8 100644 --- a/libscience/elementparser.cpp +++ b/libscience/elementparser.cpp @@ -21,81 +21,13 @@ email : cniehaus@kde.org #include #include -QList ElementParser::loadAllElements( const QDomDocument& dataDocument ) -{ - QList elementList; - QStringList elementSymbols = loadElementSymbols(dataDocument); - - foreach(QString symbol, elementSymbols) - { - Element *e = loadElement( symbol, dataDocument ); - if ( e ) - elementList.append( e ); - } - - return elementList; -} - -QStringList ElementParser::loadElementSymbols( const QDomDocument& dataDocument ) -{ - QStringList symbolList; - - //xml-reading - QDomNodeList elementNodes = dataDocument.elementsByTagName( "elementType" ); - - const uint count = elementNodes.count(); - - for ( uint i = 0; i < count; ++i ) - { - QString symbol = elementNodes.item( i ).toElement().attribute("id"); - symbolList.append( symbol ); - } - - return symbolList; -} - -Element* ElementParser::loadElement( const QString& symbol, const QDomDocument& dataDocument ) -{ - QDomNodeList elementNodes = dataDocument.elementsByTagName( "elementType" ); - const uint count = elementNodes.count(); - - for ( uint i = 0; i < count; ++i ) - { - QDomElement currentElement = elementNodes.item( i ).toElement(); - QString currentSymbol = currentElement.attribute("id"); - - if ( currentSymbol == symbol ) - return loadElement( currentElement ); - } - - //the element was not found... - return 0; -} - -Element* ElementParser::loadElement( const QDomElement& element ) -{ - Element *e = new Element(); - - QString symbol = element.attribute( "id" ); - - QDomNodeList scalarList = element.elementsByTagName( "scalar" ); - - e->setSymbol( symbol ); - - kdDebug() << "scalarList::count of element " << e->symbol() << ": " << scalarList.count() << endl; - - return e; -} - - - - - -//////////// -// - ElementSaxParser::ElementSaxParser() -: QXmlDefaultHandler(), currentElement_(0), inElement_(false), inName_(false) +: QXmlDefaultHandler(), currentElement_(0), + inElement_(false), + inName_(false), + inMass_( false ), + inAtomicNumber_(false), + inSymbol_( false ) { } @@ -108,17 +40,24 @@ bool ElementSaxParser::startElement(const QString&, const QString &localName, co for (int i = 0; i < attrs.length(); ++i) { if (attrs.value(i) == "bo:name") inName_ = true; + if (attrs.value(i) == "bo:mass") + inMass_ = true; + if (attrs.value(i) == "bo:atomicNumber") + inAtomicNumber_ = true; + if (attrs.value(i) == "bo:symbol") + inSymbol_ = true; } } return true; } - -bool ElementSaxParser::endElement(const QString&, const QString &localName, const QString&, const QXmlAttributes&) + +bool ElementSaxParser::endElement ( const QString & namespaceURI, const QString & localName, const QString & qName ) { - if (localName == "elementType") { - inElement_ = false; + if ( localName == "elementType" ) + { elements_.append(currentElement_); currentElement_ = 0; + inElement_ = false; } return true; } @@ -126,10 +65,22 @@ bool ElementSaxParser::endElement(const QString&, const QString &localName, cons bool ElementSaxParser::characters(const QString &ch) { if (inName_) { - kdDebug() << "nimi: " << ch << endl; currentElement_->setName(ch); inName_ = false; } + if ( inMass_ ){ + currentElement_->setMass( ch.toDouble() ); + inMass_ = false; + } + if (inSymbol_) { + currentElement_->setSymbol(ch); + inSymbol_ = false; + } + if (inAtomicNumber_) { + currentElement_->setNumber(ch.toInt()); + inAtomicNumber_ = false; + } + return true; } diff --git a/libscience/elementparser.h b/libscience/elementparser.h index 497bcd8..d3123ad 100644 --- a/libscience/elementparser.h +++ b/libscience/elementparser.h @@ -22,40 +22,40 @@ class Element; -/** - * This class gives access to the elements which are listed in a CML-file - * @author Carsten Niehaus - */ -class ElementParser -{ - public: - /** - * @return the Element with the symbol symbol - * @param dataDocument the document to parse - * @param symbol the symbol of the Element which is looked for - */ - static Element* loadElement( const QString& symbol, const QDomDocument& dataDocument ); - - /** - * @return the element represented in the QDomeElement @p element - * @param element the XML-representation of the Element - */ - static Element* loadElement( const QDomElement& element ); - - /** - * @return all chemical elements in the xml-file - * @param dataDocument the document to parse - */ - static QList loadAllElements(const QDomDocument& dataDocument); - - private: - /** - * @return a QStringList with the symbols of all known elements - * @param dataDocument the document to parse - */ - static QStringList loadElementSymbols(const QDomDocument& dataDocument); - -}; +//X /** +//X * This class gives access to the elements which are listed in a CML-file +//X * @author Carsten Niehaus +//X */ +//X class ElementParser +//X { +//X public: +//X /** +//X * @return the Element with the symbol symbol +//X * @param dataDocument the document to parse +//X * @param symbol the symbol of the Element which is looked for +//X */ +//X static Element* loadElement( const QString& symbol, const QDomDocument& dataDocument ); +//X +//X /** +//X * @return the element represented in the QDomeElement @p element +//X * @param element the XML-representation of the Element +//X */ +//X static Element* loadElement( const QDomElement& element ); +//X +//X /** +//X * @return all chemical elements in the xml-file +//X * @param dataDocument the document to parse +//X */ +//X static QList loadAllElements(const QDomDocument& dataDocument); +//X +//X private: +//X /** +//X * @return a QStringList with the symbols of all known elements +//X * @param dataDocument the document to parse +//X */ +//X static QStringList loadElementSymbols(const QDomDocument& dataDocument); +//X +//X }; class ElementSaxParser : public QXmlDefaultHandler @@ -63,15 +63,20 @@ class ElementSaxParser : public QXmlDefaultHandler public: ElementSaxParser(); bool startElement(const QString&, const QString &localName, const QString&, const QXmlAttributes &attrs); - bool endElement(const QString&, const QString &localName, const QString&, const QXmlAttributes&); + + bool endElement ( const QString & namespaceURI, const QString & localName, const QString & qName ); + bool characters(const QString &ch); + QList getElements(); private: Element *currentElement_; QList elements_; bool inElement_; - bool inName_; - + bool inName_, + inMass_, + inSymbol_, + inAtomicNumber_; }; #endif // ELEMENTPARSER_H diff --git a/libscience/tests/xmlreadingtest.cpp b/libscience/tests/xmlreadingtest.cpp index 3a3f601..0badd10 100644 --- a/libscience/tests/xmlreadingtest.cpp +++ b/libscience/tests/xmlreadingtest.cpp @@ -22,7 +22,11 @@ int main(int argc, char *argv[]) QList v = parser->getElements(); foreach( Element* e, v ){ - kdDebug() << "Elementname: " << e->elementName() << ", mass: " << e->mass() << endl; + if ( e ) + kdDebug() << "(" << e->number() << ", " << + e->elementName() << ", " << e->symbol() << ") " << + ", mass: " << e->mass() << + endl; } return 0;