From 693fb6fbc9bfc08f9d2b50b3b71c72c6df6e3821 Mon Sep 17 00:00:00 2001 From: Carsten Niehaus Date: Sun, 16 Oct 2005 11:04:07 +0000 Subject: [PATCH] Adding the SAX2-parser Adding a testdirectory with a test of my code works svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=471084 --- libscience/Makefile.am | 5 ++- libscience/elementparser.cpp | 51 +++++++++++++++++++++++++++++ libscience/elementparser.h | 22 +++++++++++++ libscience/tests/Makefile.am | 11 +++++++ libscience/tests/xmlreadingtest.cpp | 29 ++++++++++++++++ 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 libscience/tests/Makefile.am create mode 100644 libscience/tests/xmlreadingtest.cpp diff --git a/libscience/Makefile.am b/libscience/Makefile.am index bd2a3d6..c7b815f 100644 --- a/libscience/Makefile.am +++ b/libscience/Makefile.am @@ -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) diff --git a/libscience/elementparser.cpp b/libscience/elementparser.cpp index dae083e..411349a 100644 --- a/libscience/elementparser.cpp +++ b/libscience/elementparser.cpp @@ -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 ElementSaxParser::getElements() +{ + return elements_; +} diff --git a/libscience/elementparser.h b/libscience/elementparser.h index dcffb82..497bcd8 100644 --- a/libscience/elementparser.h +++ b/libscience/elementparser.h @@ -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 #include +#include + 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 getElements(); + + private: + Element *currentElement_; + QList elements_; + bool inElement_; + bool inName_; + +}; +#endif // ELEMENTPARSER_H diff --git a/libscience/tests/Makefile.am b/libscience/tests/Makefile.am new file mode 100644 index 0000000..2dba645 --- /dev/null +++ b/libscience/tests/Makefile.am @@ -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 index 0000000..3a3f601 --- /dev/null +++ b/libscience/tests/xmlreadingtest.cpp @@ -0,0 +1,29 @@ +/* Sample parsing with QT's SAX2 by Riku Leino */ + +#include "../elementparser.h" +#include "../element.h" +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc < 2 || argc > 2) { + std::cout << "Usage: elements \n"; + return 1; + } + + ElementSaxParser * parser = new ElementSaxParser(); + QFile xmlFile(argv[1]); + QXmlInputSource source(xmlFile); + QXmlSimpleReader reader; + reader.setContentHandler(parser); + reader.parse(source); + + QList v = parser->getElements(); + + foreach( Element* e, v ){ + kdDebug() << "Elementname: " << e->elementName() << ", mass: " << e->mass() << endl; + } + + return 0; +} -- 2.47.3