--- /dev/null
+/****************************************************************************
+ * ** 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;
+}
--- /dev/null
+/****************************************************************************
+ * ** $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
--- /dev/null
+/* 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;
+}