VocabParser.cpp
Go to the documentation of this file.00001 #include "VocabParser.h"
00002
00003 VocabParser::VocabParser( Vocabulary& vocab, const QStringList& languages ) : vocabulary( vocab ), languages( languages ), isVocabFile( false ) {
00004 }
00005
00006 bool VocabParser::startDocument() {
00007 return( true );
00008 }
00009
00010 bool VocabParser::startElement( const QString&, const QString&, const QString& qname, const QXmlAttributes& attribs ) {
00011 if( qname == QString( "glossary" ) ) {
00012 vocabulary.setTitle( attribs.value( QString( "name" ) ) );
00013 vocabulary.setAuthor( attribs.value( QString( "author" ) ) );
00014 desc = QString();
00015 isVocabFile = true;
00016 }
00017 else if( qname == QString( "word" ) || qname == QString( "alt" ) || qname == QString( "comment" ) ) {
00018 mustKeepText = true;
00019 tempCh = QString();
00020 if( qname == QString( "comment" ) ) {
00021 commentKey = attribs.value( QString( "languages" ) );
00022 comment = QString();
00023 }
00024 }
00025 else if( qname == QString( "trans" ) ) {
00026 word = QString();
00027 alt = QString();
00028 lang = attribs.value( QString( "lang" ) );
00029 }
00030 else if( qname == QString( "term" ) ) {
00031 int id = atoi( attribs.value( QString( "id" ) ).latin1() );
00032 QString imagePath = attribs.value( QString( "imagePath" ) );
00033 term = Term( id, vocabulary.getId() );
00034 if( !imagePath.isNull() )
00035 term.setImagePath( imagePath );
00036 }
00037 else if( qname == QString( "desc" ) ) {
00038 mustKeepText = true;
00039 tempCh = QString();
00040 }
00041 return( true );
00042 }
00043
00044 bool VocabParser::characters( const QString& characters ) {
00045 if( mustKeepText ) {
00046 tempCh = characters.stripWhiteSpace();
00047 mustKeepText = false;
00048 }
00049 return( true );
00050 }
00051
00052 bool VocabParser::endElement( const QString&, const QString&, const QString& qname ) {
00053 if( qname == QString( "word" ) )
00054 word = tempCh;
00055 else if( qname == QString( "comment" ) ) {
00056 comment = tempCh;
00057 if( languages.count() == 0 || ( languages.contains( commentKey.getFirstLanguage() ) && languages.contains( commentKey.getSecondLanguage() ) ) )
00058 term.addComment( commentKey, comment );
00059 }
00060 else if( qname == QString( "alt" ) )
00061 alt = tempCh;
00062 else if( qname == QString( "trans" ) ) {
00063 if( languages.count() == 0 || languages.contains( lang ) ) {
00064 Translation translation( lang, word, alt );
00065 term.addTranslation( translation );
00066 }
00067 }
00068 else if( qname == QString( "term" ) ) {
00069 if( term.getTranslationCount() > 0 )
00070 vocabulary.addTerm( term );
00071 }
00072 else if( qname == QString( "desc" ) ) {
00073 desc = tempCh;
00074 vocabulary.setDescription( desc );
00075 }
00076
00077 return( true );
00078 }
00079
00080 bool VocabParser::endDocument() {
00081 return( true );
00082 }
00083
00084 bool VocabParser::fatalError( const QXmlParseException& exception ) {
00085 QMessageBox::warning( 0, QObject::tr( "Error" ),
00086 QObject::tr( "ParseError" )
00087 .arg( exception.lineNumber() )
00088 .arg( exception.columnNumber() )
00089 .arg( exception.message() ) );
00090 return( false );
00091 }
00092
00093 bool VocabParser::isVocabularyFile() {
00094 return( isVocabFile );
00095 }