[Kalzium] KDE/kdeedu/libkdeedu/libscience

Carsten Niehaus cniehaus at gmx.de
Sun Oct 16 14:15:53 CEST 2005


SVN commit 471100 by cniehaus:

* Ok, Tsoots of Scribus fame convinced me that SAX is better than DOM for my usecase. He wrote the based code, I ported to read Qt-code.
* This commit removea the QDom-implementation
* Four datasets (number, mass, name and symbol) are now working
CCMAIL:kalzium at kde.org


 M  +1 -4      Makefile.am  
 M  +30 -79    elementparser.cpp  
 M  +42 -37    elementparser.h  
 M  +5 -1      tests/xmlreadingtest.cpp  


--- trunk/KDE/kdeedu/libkdeedu/libscience/Makefile.am #471099:471100
@@ -4,16 +4,13 @@
 
 lib_LTLIBRARIES = libscience.la
 
-bin_PROGRAMS = libscience
-
 libscience_la_SOURCES = \
 	element.cpp \
 	spectrum.cpp \
 	isotope.cpp \
 	spectrumparser.cpp \
 	elementparser.cpp \
-	tempunit.cpp \
-	main.cpp
+	tempunit.cpp
 
 libscience_la_LDFLAGS = $(all_libraries) -no-undefined -version-info 4:0:0
 libscience_la_LIBADD = $(LIB_KDEUI) 
--- trunk/KDE/kdeedu/libkdeedu/libscience/elementparser.cpp #471099:471100
@@ -21,81 +21,13 @@
 #include <kdebug.h>
 #include <kurl.h>
 
-QList<Element*> ElementParser::loadAllElements( const QDomDocument& dataDocument )
-{
-	QList<Element*> elementList;
-	QStringList elementSymbols = loadElementSymbols(dataDocument);
-
-	foreach(QString symbol, elementSymbols)
-	{
-		Element *e = loadElement( symbol, dataDocument );
-		if ( e )
-			elementList.append( e );
-	}
-
-	return elementList;
-}
-
-QStringList ElementParser::loadElementSymbols( const QDomDocument& dataDocument )
-{
-	QStringList symbolList;
-
-	//xml-reading
-	QDomNodeList elementNodes = dataDocument.elementsByTagName( "elementType" );
-
-	const uint count = elementNodes.count();
-
-	for ( uint i = 0; i < count; ++i )
-	{
-		QString symbol = elementNodes.item( i ).toElement().attribute("id");
-		symbolList.append( symbol );
-	}
-
-	return symbolList;
-}
-
-Element* ElementParser::loadElement( const QString& symbol, const QDomDocument& dataDocument )
-{
-	QDomNodeList elementNodes = dataDocument.elementsByTagName( "elementType" );
-	const uint count = elementNodes.count();
-
-	for ( uint i = 0; i < count; ++i )
-	{
-		QDomElement currentElement = elementNodes.item( i ).toElement();
-		QString currentSymbol = currentElement.attribute("id");
-
-		if ( currentSymbol == symbol )
-			return loadElement( currentElement );
-	}
-
-	//the element was not found...
-	return 0;
-}
-
-Element* ElementParser::loadElement( const QDomElement& element )
-{
-	Element *e = new Element();
-
-	QString symbol = element.attribute( "id" );
-	
-	QDomNodeList scalarList = element.elementsByTagName( "scalar" );
-
-	e->setSymbol( symbol );
-
-	kdDebug() << "scalarList::count of element " << e->symbol() << ": " << scalarList.count() << endl;
-	
-	return e;
-}
-
-
-
-
-
-////////////
-//
-
 ElementSaxParser::ElementSaxParser()
-: QXmlDefaultHandler(), currentElement_(0), inElement_(false), inName_(false)
+: QXmlDefaultHandler(), currentElement_(0), 
+	inElement_(false), 
+	inName_(false), 
+	inMass_( false ),
+	inAtomicNumber_(false), 
+	inSymbol_( false )
 {
 }
 
@@ -108,17 +40,24 @@
 		for (int i = 0; i < attrs.length(); ++i) {
 			if (attrs.value(i) == "bo:name")
 				inName_ = true;
+			if (attrs.value(i) == "bo:mass")
+				inMass_ = true;
+			if (attrs.value(i) == "bo:atomicNumber")
+				inAtomicNumber_ = true;
+			if (attrs.value(i) == "bo:symbol")
+				inSymbol_ = true;
 		}
 	}
 	return true;
 }
-
-bool ElementSaxParser::endElement(const QString&, const QString &localName, const QString&, const QXmlAttributes&)
+		
+bool ElementSaxParser::endElement (  const QString & namespaceURI, const QString & localName, const QString & qName )
 {
-	if (localName == "elementType") {
-		inElement_ = false;
+	if ( localName == "elementType" )
+	{
 		elements_.append(currentElement_);
 		currentElement_ = 0;
+		inElement_ = false;
 	}
 	return true;
 }
@@ -126,10 +65,22 @@
 bool ElementSaxParser::characters(const QString &ch)
 {
 	if (inName_) {
-		kdDebug() << "nimi: " << ch << endl;
 		currentElement_->setName(ch);
 		inName_ = false;
 	}
+	if ( inMass_ ){
+		currentElement_->setMass( ch.toDouble() );
+		inMass_ = false;
+	}
+	if (inSymbol_) {
+		currentElement_->setSymbol(ch);
+		inSymbol_ = false;
+	}
+	if (inAtomicNumber_) {
+		currentElement_->setNumber(ch.toInt());
+		inAtomicNumber_ = false;
+	}
+
 	return true;
 }
 
--- trunk/KDE/kdeedu/libkdeedu/libscience/elementparser.h #471099:471100
@@ -22,56 +22,61 @@
 
 class Element;
 
-/**
- * This class gives access to the elements which are listed in a CML-file
- * @author Carsten Niehaus <cniehaus at kde.org>
- */
-class ElementParser
-{
-	public:
-		/**
-		 * @return the Element with the symbol symbol
-		 * @param dataDocument the document to parse
-		 * @param symbol the symbol of the Element which is looked for
-		 */
-		static Element* loadElement( const QString& symbol, const QDomDocument& dataDocument );
-		
-		/**
-		 * @return the element represented in the QDomeElement @p element
-		 * @param element the XML-representation of the Element
-		 */
-		static Element* loadElement( const QDomElement& element );
+//X /**
+//X  * This class gives access to the elements which are listed in a CML-file
+//X  * @author Carsten Niehaus <cniehaus at kde.org>
+//X  */
+//X class ElementParser
+//X {
+//X 	public:
+//X 		/**
+//X 		 * @return the Element with the symbol symbol
+//X 		 * @param dataDocument the document to parse
+//X 		 * @param symbol the symbol of the Element which is looked for
+//X 		 */
+//X 		static Element* loadElement( const QString& symbol, const QDomDocument& dataDocument );
+//X 		
+//X 		/**
+//X 		 * @return the element represented in the QDomeElement @p element
+//X 		 * @param element the XML-representation of the Element
+//X 		 */
+//X 		static Element* loadElement( const QDomElement& element );
+//X 
+//X 		/**
+//X 		 * @return all chemical elements in the xml-file
+//X 		 * @param dataDocument the document to parse
+//X 		 */
+//X 		static QList<Element*> loadAllElements(const QDomDocument& dataDocument);
+//X 		
+//X 	private:
+//X 		/**
+//X 		 * @return a QStringList with the symbols of all known elements
+//X 		 * @param dataDocument the document to parse
+//X 		 */
+//X 		static QStringList loadElementSymbols(const QDomDocument& dataDocument);
+//X 
+//X };
 
-		/**
-		 * @return all chemical elements in the xml-file
-		 * @param dataDocument the document to parse
-		 */
-		static QList<Element*> loadAllElements(const QDomDocument& dataDocument);
-		
-	private:
-		/**
-		 * @return a QStringList with the symbols of all known elements
-		 * @param dataDocument the document to parse
-		 */
-		static QStringList loadElementSymbols(const QDomDocument& dataDocument);
 
-};
-
-
 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 endElement (  const QString & namespaceURI, const QString & localName, const QString & qName );
+		
 		bool characters(const QString &ch);
+
 		QList<Element*> getElements();
 
 	private:
 		Element *currentElement_;
 		QList<Element*> elements_;
 		bool inElement_;
-		bool inName_;
-
+		bool inName_,
+			 inMass_,
+			 inSymbol_,
+			 inAtomicNumber_;
 };
 #endif // ELEMENTPARSER_H
--- trunk/KDE/kdeedu/libkdeedu/libscience/tests/xmlreadingtest.cpp #471099:471100
@@ -22,7 +22,11 @@
 	QList<Element*> v = parser->getElements();
 
 	foreach( Element* e, v ){
-		kdDebug() << "Elementname: " << e->elementName() << ", mass: " << e->mass() << endl;
+		if ( e )
+			kdDebug() << "(" << e->number() << ", " <<
+				       e->elementName() << ", " << e->symbol() << ") " << 
+				", mass: " << e->mass() << 
+				endl;
 	}
 
 	return 0;


More information about the Kalzium mailing list