]> Git trees. - libqmvoc.git/commitdiff
Adding the SAX2-parser
authorCarsten Niehaus <cniehaus@gmx.de>
Sun, 16 Oct 2005 11:04:07 +0000 (11:04 +0000)
committerCarsten Niehaus <cniehaus@gmx.de>
Sun, 16 Oct 2005 11:04:07 +0000 (11:04 +0000)
Adding a testdirectory with a test of my code works

svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=471084

libscience/Makefile.am
libscience/elementparser.cpp
libscience/elementparser.h
libscience/tests/Makefile.am [new file with mode: 0644]
libscience/tests/xmlreadingtest.cpp [new file with mode: 0644]

index bd2a3d6431091c8128551d6f81639fb30fd8d977..c7b815fffca79d67cf39dc8e62a8ce9a2763bed5 100644 (file)
@@ -4,13 +4,16 @@ 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
+       tempunit.cpp \
+       main.cpp
 
 libscience_la_LDFLAGS = $(all_libraries) -no-undefined -version-info 4:0:0
 libscience_la_LIBADD = $(LIB_KDEUI) 
index dae083e0fb3d47f2bdd4061c79b12f9f7eedc0dd..411349ac75fbee00a4e69a5226279f4c9af711c7 100644 (file)
@@ -86,3 +86,54 @@ Element* ElementParser::loadElement( const QDomElement& element )
        
        return e;
 }
+
+
+
+
+
+////////////
+//
+
+ElementSaxParser::ElementSaxParser()
+: QXmlDefaultHandler(), currentElement_(0), inElement_(false), inName_(false)
+{
+}
+
+bool ElementSaxParser::startElement(const QString&, const QString &localName, const QString&, const QXmlAttributes &attrs)
+{
+       if (localName == "elementType") {
+               currentElement_ = new Element();
+               inElement_ = true;
+       } else if (inElement_ && localName == "scalar") {
+               for (int i = 0; i < attrs.length(); ++i) {
+                       if (attrs.value(i) == "bo:name")
+                               inName_ = true;
+               }
+       }
+       return true;
+}
+
+bool ElementSaxParser::endElement(const QString&, const QString &localName, const QString&, const QXmlAttributes&)
+{
+       if (localName == "elementType") {
+               inElement_ = false;
+               elements_.append(currentElement_);
+               currentElement_ = 0;
+       }
+       return true;
+}
+
+bool ElementSaxParser::characters(const QString &ch)
+{
+       if (inName_) {
+               kdDebug() << "nimi: " << ch << endl;
+               currentElement_->setName(ch);
+               inName_ = false;
+       }
+       return true;
+}
+
+QList<Element*> ElementSaxParser::getElements()
+{
+       return elements_;
+}
index dcffb825d86cf59eb6f0b2a67deb37fb570bdba3..497bcd8c1a947142f580cce20bcc8a6855918f34 100644 (file)
@@ -1,3 +1,5 @@
+#ifndef ELEMENTPARSER_H
+#define ELEMENTPARSER_H
 /***************************************************************************
     copyright            : (C) 2005 by Carsten Niehaus
     email                : cniehaus@kde.org
@@ -16,6 +18,8 @@
 #include <QStringList>
 #include <qdom.h>
 
+#include <qxml.h>
+
 class Element;
 
 /**
@@ -53,3 +57,21 @@ class ElementParser
 
 };
 
+
+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 characters(const QString &ch);
+               QList<Element*> getElements();
+
+       private:
+               Element *currentElement_;
+               QList<Element*> elements_;
+               bool inElement_;
+               bool inName_;
+
+};
+#endif // ELEMENTPARSER_H
diff --git a/libscience/tests/Makefile.am b/libscience/tests/Makefile.am
new file mode 100644 (file)
index 0000000..2dba645
--- /dev/null
@@ -0,0 +1,11 @@
+INCLUDES = -I$(top_srcdir)/libkdeedu/libscience $(all_includes)
+
+AM_LDFLAGS = $(QT_LDFLAGS) $(X_LDFLAGS) $(KDE_RPATH)
+
+check_PROGRAMS = xmlreadingtest
+
+xmlreadingtest_SOURCES = xmlreadingtest.cpp
+xmlreadingtest_LDFLAGS = $(all_libraries)
+xmlreadingtest_LDADD = ../libscience.la
+
+METASOURCES = AUTO
diff --git a/libscience/tests/xmlreadingtest.cpp b/libscience/tests/xmlreadingtest.cpp
new file mode 100644 (file)
index 0000000..3a3f601
--- /dev/null
@@ -0,0 +1,29 @@
+/* Sample parsing with QT's SAX2 by Riku Leino <tsoots@gmail.com> */
+
+#include "../elementparser.h"
+#include "../element.h"
+#include <kdebug.h>
+#include <iostream>
+
+int main(int argc, char *argv[])
+{
+       if (argc < 2 || argc > 2) {
+               std::cout << "Usage: elements <XML_FILE>\n";
+               return 1;
+       }
+
+       ElementSaxParser * parser = new ElementSaxParser();
+       QFile xmlFile(argv[1]);
+       QXmlInputSource source(xmlFile);
+       QXmlSimpleReader reader;
+       reader.setContentHandler(parser);
+       reader.parse(source);
+
+       QList<Element*> v = parser->getElements();
+
+       foreach( Element* e, v ){
+               kdDebug() << "Elementname: " << e->elementName() << ", mass: " << e->mass() << endl;
+       }
+
+       return 0;
+}