if ( !xmlFile.open( IO_ReadOnly ) )
return false;
- QTextStream t1( &xmlFile );
-
- QXmlInputSource source;
-// source.setData( t1 );
-
+ QXmlInputSource source( xmlFile );
QXmlSimpleReader xmlReader;
xmlReader.setContentHandler( handler );
-
xmlReader.parse( source );
+
+ QList<CML::Atom*> parsedAtoms;
+ parsedAtoms = handler->getAtoms();
+ QList<CML::Bond*> parsedBonds;
+ parsedBonds = handler->getBonds();
+
+ kDebug() << "Found " << parsedAtoms.count() << " Atoms and " << parsedBonds.count() << " Bonds!" << endl;
-//X QList<Bond*> parsedBonds;
-//X Bond *tbond;
-//X parsedBonds = handler.getBonds();
-//X
+ foreach( CML::Atom* a, parsedAtoms )
+ kDebug() << a->debug() << endl;
+ foreach( CML::Bond* b, parsedBonds )
+ kDebug() << b->debug() << endl;
return 0;
}
bool CMLParser::startDocument()
{
- qDebug() << "New CML parser started.";
return TRUE;
}
const QString& qName,
const QXmlAttributes& attr )
{
- qDebug() << "Start:" << qName;
if ( qName.toUpper() == "ATOM" ) {
- states = CML_ATOM;
tmp_atom = new CML::Atom();
+
+ QString x2 = attr.value( "x2");
+ if ( x2 != QString( "" ) )
+ tmp_atom->setX2( x2.toDouble() );
+
+ QString x3 = attr.value( "x3");
+ if ( x3 != QString( "" ) )
+ tmp_atom->setX3( x3.toDouble() );
+
+ QString y2 = attr.value( "y2");
+ if ( y2 != QString( "" ) )
+ tmp_atom->setY2( y2.toDouble() );
+
+ QString y3 = attr.value( "y3");
+ if ( y3 != QString( "" ) )
+ tmp_atom->setY3( y3.toDouble() );
+
+ QString z3 = attr.value( "z3");
+ if ( z3 != QString( "" ) )
+ tmp_atom->setZ3( z3.toDouble() );
+
tmp_atom->setID( attr.value( "id" ) );
- qDebug() << "CML::Atom id=" << attr.value( "id" );
}
- if ( qName.toUpper() == "BOND" ) {
- states = CML_BOND;
+ else if ( qName.toUpper() == "BOND" ) {
tmp_bond = new CML::Bond();
- atom1 = 0; atom2 = 0;
+
+ QString order = attr.value( "order");
+ if ( order != QString( "" ) )
+ tmp_bond->setOrder( order.toInt() );
+
+ QString atomRef = attr.value( "atomRefs2");
+ if ( atomRef != QString( "" ) )
+ {
+ QStringList Split = atomRef.split( " " );
+ tmp_bond->setAtoms( Split.at( 0 ), Split.at( 1 ) );
+ }
}
-//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 }
- }
+bool CMLParser::characters( const QString &ch )
+{
return TRUE;
}
-bool CMLParser::ignorableWhitespace( const QString &ch ) {
- qDebug() << "ignored:" << ch << ":";
+bool CMLParser::ignorableWhitespace( const QString &ch )
+{
return TRUE;
}
{
return localAtoms;
}
+
+QList<CML::Bond*> CMLParser::getBonds()
+{
+ return localBonds;
+}
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
+// set in defs.h
+//X #define CML_NONE 0
+//X #define CML_ATOM 1
+//X #define CML_BOND 2
+//X #define CML_MOLECULE 10
+//X #define CML_NONE 1
+//X #define CML_ATOM 2
+//X #define CML_BOND 3
+//X #define CML_ATOMARRAY 4
+//X #define CML_BONDARRAY 5
class EDUSCIENCE_EXPORT CMLParser : public QXmlDefaultHandler
bool startDocument();
- bool startElement( const QString&, const QString&, const QString& ,
+ bool startElement( const QString&,
+ const QString&,
+ const QString& ,
const QXmlAttributes& );
bool endElement( const QString&, const QString&, const QString& );
bool ignorableWhitespace( const QString& );
QList<CML::Atom*> getAtoms();
+ QList<CML::Bond*> getBonds();
private:
QList<CML::Atom*> localAtoms;
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