From 708483350fcc602c7c8193ef3df1181fe674995d Mon Sep 17 00:00:00 2001 From: Carsten Niehaus Date: Wed, 7 Jun 2006 11:27:00 +0000 Subject: [PATCH] * Adding the actual parser and a not yet working test svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=549082 --- libscience/cml/CMakeLists.txt | 2 +- libscience/cml/cmlclasses.h | 10 +-- libscience/cml/xml_cml.cpp | 117 ++++++++++++++++++++++++++++++++ libscience/cml/xml_cml.h | 71 +++++++++++++++++++ libscience/tests/CMakeLists.txt | 14 ++++ libscience/tests/cml2test.cpp | 56 +++++++++++++++ 6 files changed, 264 insertions(+), 6 deletions(-) create mode 100644 libscience/cml/xml_cml.cpp create mode 100644 libscience/cml/xml_cml.h create mode 100644 libscience/tests/cml2test.cpp diff --git a/libscience/cml/CMakeLists.txt b/libscience/cml/CMakeLists.txt index d9c4257..699e783 100644 --- a/libscience/cml/CMakeLists.txt +++ b/libscience/cml/CMakeLists.txt @@ -2,7 +2,7 @@ include_directories( ${CMAKE_SOURCE_DIR}/libkdeedu/libscience ) set(cml_LIB_SRCS cmlclasses.cpp -# xml_cml.cpp + xml_cml.cpp ) kde4_automoc(${cml_LIB_SRCS}) diff --git a/libscience/cml/cmlclasses.h b/libscience/cml/cmlclasses.h index e1f0603..d5be96a 100644 --- a/libscience/cml/cmlclasses.h +++ b/libscience/cml/cmlclasses.h @@ -46,23 +46,23 @@ class Atom m_elementType = et; } - void setiX2( int i ){ + void setX2( double i ){ coord_x2 = i; } - void setiX3( int i ){ + void setX3( double i ){ coord_x3 = i; } - void setiY2( int i ){ + void setY2( double i ){ coord_y2 = i; } - void setiY3( int i ){ + void setY3( double i ){ coord_y3 = i; } - void setiZ3( int i ){ + void setZ3( double i ){ coord_z3 = i; } diff --git a/libscience/cml/xml_cml.cpp b/libscience/cml/xml_cml.cpp new file mode 100644 index 0000000..e0731ee --- /dev/null +++ b/libscience/cml/xml_cml.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** + * ** Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved. + * ** Copyright ( C ) 2006 Carsten Niehaus + * ** + * ** This file is part of an example program for Qt. This example + * ** program may be used, distributed and modified without limitation. + * ** + * *****************************************************************************/ + +#include "xml_cml.h" + +#include +#include + +#include + +bool CMLParser::startDocument() +{ + qDebug() << "New CML parser started."; + return TRUE; +} + +bool CMLParser::startElement( const QString&, const QString&, + const QString& qName, + const QXmlAttributes& attr ) +{ + qDebug() << "Start:" << qName; + if ( qName.toUpper() == "ATOM" ) { + states = CML_ATOM; + tmp_atom = new CML::Atom(); + tmp_atom->setID( attr.value( "id" ) ); + qDebug() << "CML::Atom id=" << attr.value( "id" ); + } + if ( qName.toUpper() == "BOND" ) { + states = CML_BOND; + tmp_bond = new CML::Bond(); + atom1 = 0; atom2 = 0; + } +//X if ( qName.toUpper() == "FLOAT" ) { +//X last_builtin = attr.value( "builtin" ).toUpper(); +//X if ( last_builtin == "X3" ) last_builtin = "X2"; +//X if ( last_builtin == "X3" ) last_builtin = "Y2"; +//X } +//X if ( qName.toUpper() == "STRING" ) { +//X last_builtin = attr.value( "builtin" ).toUpper(); +//X } + return TRUE; +} + +bool CMLParser::endElement( const QString&, const QString&, + const QString& qName ) +{ + qDebug() << "End:" << qName; + if ( qName.toUpper() == "ATOM" ) { + localAtoms.append( tmp_atom ); + tmp_atom = 0; + states = CML_NONE; + qDebug() << "finished atom"; + } + if ( qName.toUpper() == "BOND" ) { + tmp_bond->setAtoms( atom1, atom2 ); + localBonds.append( tmp_bond ); + tmp_bond = 0; + states = CML_NONE; + qDebug() << "finished bond"; + } + return TRUE; +} + +bool CMLParser::characters( const QString &ch ) { + qDebug() << "char:" << ch << ":"; + if ( states == CML_ATOM ) + { + if ( last_builtin == "ELEMENTTYPE" ) + tmp_atom->setElementType( ch ); + + if ( last_builtin == "X2" ) + tmp_atom->setX2( ch.toDouble() ); + + if ( last_builtin == "Y2" ) + tmp_atom->setY2( ch.toDouble() ); + + if ( last_builtin == "X3" ) + tmp_atom->setX3( ch.toDouble() ); + + if ( last_builtin == "Y3" ) + tmp_atom->setY3( ch.toDouble() ); + + if ( last_builtin == "Z3" ) + tmp_atom->setZ3( ch.toDouble() ); + } + if ( states == CML_BOND ) { + + if ( last_builtin == "ATOMREFS2" ) + { + } + + if ( last_builtin == "ORDER" ) + tmp_bond->setOrder( ch.toInt() ); + +//X if ( last_builtin == "STEREO" ) { +//X if ( ch == "H" ) tmp_bond->setOrder( 7 ); +//X if ( ch == "W" ) tmp_bond->setOrder( 5 ); +//X } + } + return TRUE; +} + +bool CMLParser::ignorableWhitespace( const QString &ch ) { + qDebug() << "ignored:" << ch << ":"; + return TRUE; +} + +QList CMLParser::getAtoms() +{ + return localAtoms; +} diff --git a/libscience/cml/xml_cml.h b/libscience/cml/xml_cml.h new file mode 100644 index 0000000..f82fe1a --- /dev/null +++ b/libscience/cml/xml_cml.h @@ -0,0 +1,71 @@ +/**************************************************************************** + * ** $Id: xml_cml.h,v 1.2 2002/07/21 23:27:05 atenderholt Exp $ + * ** + * ** Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved. + * ** + * ** This file is part of an example program for Qt. This example + * ** program may be used, distributed and modified without limitation. + * ** + * *****************************************************************************/ + +#ifndef XML_CML_H +#define XML_CML_H + +#include +#include + +#include "cmlclasses.h" + +class QString; + +// possible states (CMLParser::states) +// // set in defs.h +// //#define CML_NONE 0 +// //#define CML_ATOM 1 +// //#define CML_BOND 2 +#define CML_MOLECULE 10 +#define CML_NONE 1 +#define CML_ATOM 2 +#define CML_BOND 3 +#define CML_ATOMARRAY 4 +#define CML_BONDARRAY 5 + + +class CMLParser : public QXmlDefaultHandler +{ + public: + CMLParser( ) + { + }; + + bool startDocument(); + + bool startElement( const QString&, const QString&, const QString& , + const QXmlAttributes& ); + + bool endElement( const QString&, const QString&, const QString& ); + + bool characters( const QString& ); + + bool ignorableWhitespace( const QString& ); + + QList getAtoms(); + + private: + QList localAtoms; + QList localBonds; + + CML::Bond *tmp_bond; + + //temporary atoms for the creation of a Bond + CML::Atom * atom1; + CML::Atom * atom2; + + CML::Atom * tmp_atom; + + QString indent, last_builtin; + + int states; +}; + +#endif diff --git a/libscience/tests/CMakeLists.txt b/libscience/tests/CMakeLists.txt index 0f380e4..6b33e39 100644 --- a/libscience/tests/CMakeLists.txt +++ b/libscience/tests/CMakeLists.txt @@ -30,6 +30,20 @@ target_link_libraries(isotopereadingtest ${KDE4_KDECORE_LIBS} science ) endif(KDE4_BUILD_TESTS) +########### next target ############### + +set(cml2test_SRCS cml2test.cpp ) + +kde4_automoc(${cml2test_SRCS}) + +if(KDE4_BUILD_TESTS) + +kde4_add_executable(cml2test ${cml2test_SRCS}) + +target_link_libraries(cml2test ${KDE4_KDECORE_LIBS} science ) + +endif(KDE4_BUILD_TESTS) + ########### install files ############### diff --git a/libscience/tests/cml2test.cpp b/libscience/tests/cml2test.cpp new file mode 100644 index 0000000..b9a89f8 --- /dev/null +++ b/libscience/tests/cml2test.cpp @@ -0,0 +1,56 @@ +/* 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 ){ + if ( e ) + { + QList list = e->data(); + + //Test: Check if the string-comparison works +//X if ( e->data( ChemicalDataObject::name ) == "Helium" ) +//X kDebug() << "Mass: " << e->dataAsString( ChemicalDataObject::mass ) << endl; + + //Test: Check if the double-comparison works +//X if ( e->data( ChemicalDataObject::mass ) == 4.002602 ) +//X kDebug() << "Correct mass found" << endl; + + //Test: Give me the name of the element +//X kDebug() << "Name: " << e->dataAsString( ChemicalDataObject::name ) << endl; + + //Test: give me all data available + foreach( ChemicalDataObject*o, list ){ + if ( o ) + { + QString unit = o->unitAsString(); + if ( unit == "bo:noUnit" ) + unit = ""; + kDebug() << "Name: " << o->dictRef() << " " << o->valueAsString() <<" " << unit << endl; + } + } + } + + } + + return 0; +} -- 2.47.3