libkdeedu/keduvocdocument
keduvocxdxfreader.cpp
Go to the documentation of this file.00001 /*************************************************************************** 00002 create a KEduVocDocument from a XDXF file 00003 ----------------------------------------------------------------------- 00004 copyright : (C) 2007 Peter Hedlund <peter.hedlund@kdemail.net> 00005 00006 ***************************************************************************/ 00007 00008 /*************************************************************************** 00009 * * 00010 * This program is free software; you can redistribute it and/or modify * 00011 * it under the terms of the GNU General Public License as published by * 00012 * the Free Software Foundation; either version 2 of the License, or * 00013 * (at your option) any later version. * 00014 * * 00015 ***************************************************************************/ 00016 00017 #include "keduvocxdxfreader.h" 00018 00019 #include <QIODevice> 00020 00021 #include <KLocale> 00022 00023 #include "keduvocexpression.h" 00024 #include "keduvocdocument.h" 00025 00026 KEduVocXdxfReader::KEduVocXdxfReader( KEduVocDocument *doc ) 00027 { 00028 m_doc = doc; 00029 } 00030 00031 00032 bool KEduVocXdxfReader::read( QIODevice *device ) 00033 { 00034 setDevice( device ); 00035 00036 while ( !atEnd() ) { 00037 readNext(); 00038 00039 if ( isStartElement() ) { 00040 if ( name() == "xdxf" ) 00041 readXdxf(); 00042 else 00043 raiseError( i18n( "This is not a XDXF document" ) ); 00044 } 00045 } 00046 00047 return !error(); 00048 } 00049 00050 00051 void KEduVocXdxfReader::readUnknownElement() 00052 { 00053 while ( !atEnd() ) { 00054 readNext(); 00055 00056 if ( isEndElement() ) 00057 break; 00058 00059 if ( isStartElement() ) 00060 readUnknownElement(); 00061 } 00062 } 00063 00064 00065 void KEduVocXdxfReader::readXdxf() 00066 { 00068 QStringRef id1 = attributes().value( "lang_from" ); 00069 m_doc->appendIdentifier(); 00070 if ( !id1.isNull() ) { 00071 m_doc->identifier(0).setLocale( id1.toString().toLower() ); 00072 m_doc->identifier(0).setName( id1.toString().toLower() ); 00073 } 00074 QStringRef id2 = attributes().value( "lang_to" ); 00075 m_doc->appendIdentifier(); 00076 if ( !id2.isNull() ) { 00077 m_doc->identifier(1).setLocale( id2.toString().toLower() ); 00078 m_doc->identifier(1).setName( id2.toString().toLower() ); 00079 } 00080 00081 while ( !atEnd() ) { 00082 readNext(); 00083 00084 if ( isEndElement() ) 00085 break; 00086 00087 if ( isStartElement() ) { 00088 if ( name() == "description" ) 00089 m_doc->setDocumentComment( readElementText() ); 00090 else if ( name() == "full_name" ) 00091 m_doc->setTitle( readElementText() ); 00092 else if ( name() == "ar" ) 00093 readEntry(); 00094 else 00095 readUnknownElement(); 00096 } 00097 } 00098 00099 m_doc->setAuthor( "http://xdxf.sf.net" ); 00100 } 00101 00102 00103 void KEduVocXdxfReader::readEntry() 00104 { 00105 QString front; 00106 QString back; 00107 00108 while ( !( isEndElement() && name() == "ar" ) ) { 00109 readNext(); 00110 if ( isStartElement() && name() == "k" ) 00111 front = readElementText(); 00112 else if ( isCharacters() || isEntityReference() ) 00113 back.append( text().toString() ); 00114 } 00115 00116 KEduVocExpression expr = KEduVocExpression( front ); 00117 expr.setTranslation( 1, back ); 00118 m_doc->lesson()->appendEntry( &expr ); 00119 }
KDE 4.2 API Reference