]> Git trees. - libqmvoc.git/commitdiff
* Ok, Tsoots of Scribus fame convinced me that SAX is better than DOM for my usecase...
authorCarsten Niehaus <cniehaus@gmx.de>
Sun, 16 Oct 2005 12:15:51 +0000 (12:15 +0000)
committerCarsten Niehaus <cniehaus@gmx.de>
Sun, 16 Oct 2005 12:15:51 +0000 (12:15 +0000)
* 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

libscience/Makefile.am
libscience/elementparser.cpp
libscience/elementparser.h
libscience/tests/xmlreadingtest.cpp

index c7b815fffca79d67cf39dc8e62a8ce9a2763bed5..bd2a3d6431091c8128551d6f81639fb30fd8d977 100644 (file)
@@ -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) 
index 411349ac75fbee00a4e69a5226279f4c9af711c7..b02d9f8ff8f9fb9224a91c6f2ce3a21ee68d6fa5 100644 (file)
@@ -21,81 +21,13 @@ email                : cniehaus@kde.org
 #include <kdebug.h>
 #include <kurl.h>
 
-QList<Element*> ElementParser::loadAllElements( const QDomDocument& dataDocument )
-{
-       QList<Element*> 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;
 }
 
index 497bcd8c1a947142f580cce20bcc8a6855918f34..d3123addc9aa52af4e97d621417d26af49d88dd1 100644 (file)
 
 class Element;
 
-/**
- * This class gives access to the elements which are listed in a CML-file
- * @author Carsten Niehaus <cniehaus@kde.org>
- */
-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<Element*> 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 <cniehaus@kde.org>
+//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<Element*> 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<Element*> getElements();
 
        private:
                Element *currentElement_;
                QList<Element*> elements_;
                bool inElement_;
-               bool inName_;
-
+               bool inName_,
+                        inMass_,
+                        inSymbol_,
+                        inAtomicNumber_;
 };
 #endif // ELEMENTPARSER_H
index 3a3f6010693c4fda993b7de8f2aadbda97698c11..0badd105ef0cb957e1d8ac7877372114e50f9c58 100644 (file)
@@ -22,7 +22,11 @@ int main(int argc, char *argv[])
        QList<Element*> 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;