]> Git trees. - libqmvoc.git/commitdiff
* Adding the actual parser and a not yet working test
authorCarsten Niehaus <cniehaus@gmx.de>
Wed, 7 Jun 2006 11:27:00 +0000 (11:27 +0000)
committerCarsten Niehaus <cniehaus@gmx.de>
Wed, 7 Jun 2006 11:27:00 +0000 (11:27 +0000)
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=549082

libscience/cml/CMakeLists.txt
libscience/cml/cmlclasses.h
libscience/cml/xml_cml.cpp [new file with mode: 0644]
libscience/cml/xml_cml.h [new file with mode: 0644]
libscience/tests/CMakeLists.txt
libscience/tests/cml2test.cpp [new file with mode: 0644]

index d9c425763b90d57fae4a9143de1793308131afb6..699e783c19a4fa184f4b0c66e7feb6ec26e5d7fa 100644 (file)
@@ -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})
index e1f0603c289778db15157357f405fb5fa507a4b1..d5be96acdaf200d9f6f2a0e497d296df93ae1d01 100644 (file)
@@ -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 (file)
index 0000000..e0731ee
--- /dev/null
@@ -0,0 +1,117 @@
+/****************************************************************************
+ * ** Copyright ( C ) 1992-2000 Trolltech AS.  All rights reserved.
+ * ** Copyright ( C ) 2006 Carsten Niehaus <cniehaus@kde.org>
+ * **
+ * ** 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 <QFile>
+#include <QString>
+
+#include <QtDebug>
+
+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<CML::Atom*> CMLParser::getAtoms()
+{ 
+       return localAtoms; 
+}
diff --git a/libscience/cml/xml_cml.h b/libscience/cml/xml_cml.h
new file mode 100644 (file)
index 0000000..f82fe1a
--- /dev/null
@@ -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 <qxml.h>
+#include <QList>
+
+#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<CML::Atom*> getAtoms();
+
+       private:
+               QList<CML::Atom*> localAtoms;
+               QList<CML::Bond*> 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
index 0f380e42af711c947950dd93b68623e6c9e8e323..6b33e394ab93fd480b8217e0052dbebc7438259f 100644 (file)
@@ -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 (file)
index 0000000..b9a89f8
--- /dev/null
@@ -0,0 +1,56 @@
+/* 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 ){
+               if ( e )
+               {
+                       QList<ChemicalDataObject*> 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;
+}