• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

libkdeedu/keduvocdocument

keduvockvtml2writer.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                    export a KEduVocDocument to a KVTML file
00003     -----------------------------------------------------------------------
00004     copyright           : (C) 2007 Jeremy Whiting <jeremy@scitools.com>
00005                           (C) 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
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 "keduvockvtml2writer.h"
00018 
00019 #include <QtCore/QTextStream>
00020 #include <QtCore/QFile>
00021 
00022 #include <KDebug>
00023 
00024 #include "keduvocdocument.h"
00025 #include "keduvocexpression.h"
00026 #include "keduvoclesson.h"
00027 #include "keduvocleitnerbox.h"
00028 #include "keduvocwordtype.h"
00029 #include "kvtml2defs.h"
00030 
00031 KEduVocKvtml2Writer::KEduVocKvtml2Writer( QFile *file )
00032 {
00033     // the file must be already open
00034     m_outputFile = file;
00035 }
00036 
00037 bool KEduVocKvtml2Writer::writeDoc( KEduVocDocument *doc, const QString &generator )
00038 {
00039     if (createXmlDocument(doc, generator)) {
00040         QTextStream ts( m_outputFile );
00041         m_domDoc.save( ts, 2 );
00042         return true;
00043     }
00044     return false;
00045 }
00046 
00047 QByteArray KEduVocKvtml2Writer::toByteArray(KEduVocDocument * doc, const QString & generator)
00048 {
00049     if (createXmlDocument(doc, generator)) {
00050         return m_domDoc.toByteArray();
00051     }
00052     return QByteArray();
00053 }
00054 
00055 bool KEduVocKvtml2Writer::createXmlDocument( KEduVocDocument *doc, const QString &generator )
00056 {
00057     m_doc = doc;
00058 
00059     m_domDoc = QDomDocument( "kvtml PUBLIC \"kvtml2.dtd\" \"http://edu.kde.org/kanagram/kvtml2.dtd\"" );
00060     m_domDoc.appendChild( m_domDoc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
00061     QDomElement domElementKvtml = m_domDoc.createElement( "kvtml" );
00062     m_domDoc.appendChild( domElementKvtml );
00063 
00064     domElementKvtml.setAttribute( KVTML_VERSION, ( QString ) "2.0" );
00065 
00066     // information group
00067     QDomElement currentElement = m_domDoc.createElement( KVTML_INFORMATION );
00068     writeInformation( currentElement, generator );
00069     domElementKvtml.appendChild( currentElement );
00070 
00071     // identifiers
00072     currentElement = m_domDoc.createElement( KVTML_IDENTIFIERS );
00073     writeIdentifiers( currentElement );
00074     domElementKvtml.appendChild( currentElement );
00075 
00076     // entries
00077     currentElement = m_domDoc.createElement( KVTML_ENTRIES );
00078     if ( !writeEntries( currentElement ) ) {
00079         // at least one entry is required!
00080         return false;
00081     }
00082     domElementKvtml.appendChild( currentElement );
00083 
00084     // lessons
00085     currentElement = m_domDoc.createElement( KVTML_LESSONS );
00086     writeLessons( m_doc->lesson(), currentElement );
00087     if ( currentElement.hasChildNodes() ) {
00088         domElementKvtml.appendChild( currentElement );
00089     }
00090 
00091     // types
00092     currentElement = m_domDoc.createElement( KVTML_WORDTYPES );
00093     writeWordTypes( currentElement, m_doc->wordTypeContainer() );
00094     if ( currentElement.hasChildNodes() ) {
00095         domElementKvtml.appendChild( currentElement );
00096     }
00097 
00098     // leitner boxes
00099     currentElement = m_domDoc.createElement( KVTML_LEITNERBOXES );
00100     writeLeitnerBoxes( currentElement, m_doc->leitnerContainer() );
00101     if ( currentElement.hasChildNodes() ) {
00102         domElementKvtml.appendChild( currentElement );
00103     }
00104 
00105     writeSynonymAntonymFalseFriend(domElementKvtml);
00106 
00107     m_domDoc.appendChild( domElementKvtml );
00108 
00109     return true;
00110 }
00111 
00112 bool KEduVocKvtml2Writer::writeInformation( QDomElement &informationElement, const QString &generator )
00113 {
00114     QDomElement currentElement;
00115     QDomText textNode;
00116 
00117     // generator
00118     informationElement.appendChild( newTextElement( KVTML_GENERATOR, generator ) );
00119 
00120     // title
00121     if ( !m_doc->title().isEmpty() ) {
00122         informationElement.appendChild( newTextElement( KVTML_TITLE, m_doc->title() ) );
00123     }
00124 
00125     // author
00126     if ( !m_doc->author().isEmpty() ) {
00127         informationElement.appendChild( newTextElement( KVTML_AUTHOR, m_doc->author() ) );
00128     }
00129 
00130     // author contact (mail/homepage)
00131     if ( !m_doc->authorContact().isEmpty() ) {
00132         informationElement.appendChild( newTextElement( KVTML_AUTHORCONTACT, m_doc->authorContact() ) );
00133     }
00134 
00135     // license
00136     if ( !m_doc->license().isEmpty() ) {
00137         informationElement.appendChild( newTextElement( KVTML_LICENSE, m_doc->license() ) );
00138     }
00139 
00140     // comment
00141     if ( !m_doc->documentComment().isEmpty() ) {
00142         informationElement.appendChild( newTextElement( KVTML_COMMENT, m_doc->documentComment() ) );
00143     }
00144 
00145     // category
00146     if ( !m_doc->category().isEmpty() ) {
00147         informationElement.appendChild( newTextElement( KVTML_CATEGORY, m_doc->category() ) );
00148     }
00149 
00150     return true;
00151 }
00152 
00153 
00154 bool KEduVocKvtml2Writer::writeIdentifiers( QDomElement &identifiersElement )
00155 {
00156     for ( int i = 0; i < m_doc->identifierCount(); ++i ) {
00157         // create the node
00158         QDomElement identifier = m_domDoc.createElement( KVTML_IDENTIFIER );
00159 
00160         // set the id
00161         identifier.setAttribute( KVTML_ID, QString::number( i ) );
00162 
00163         // record the identifier as the locale for now
00164         // TODO: when support for more parts of the identifier is in the document class (name, type, etc.) store those here as well
00165         identifier.appendChild( newTextElement( KVTML_NAME, m_doc->identifier( i ).name() ) );
00166 
00167         identifier.appendChild( newTextElement( KVTML_LOCALE, m_doc->identifier( i ).locale() ) );
00168 
00169         // record articles
00170         QDomElement article = m_domDoc.createElement( KVTML_ARTICLE );
00171         writeArticle( article, i );
00172         if ( article.hasChildNodes() ) {
00173             identifier.appendChild( article );
00174         }
00175 
00176         // record personalpronouns
00177         QDomElement personalpronouns = m_domDoc.createElement( KVTML_PERSONALPRONOUNS );
00178         writePersonalPronoun( personalpronouns, m_doc->identifier(i).personalPronouns() );
00179         if ( personalpronouns.hasChildNodes() ) {
00180             identifier.appendChild( personalpronouns );
00181         }
00182 
00183         // tenses
00184         foreach(const QString &tense, m_doc->identifier(i).tenseList() ) {
00185             if ( !( tense.isNull() ) ) {
00186                 identifier.appendChild( newTextElement( KVTML_TENSE, tense ) );
00187             }
00188         }
00189         // add this identifier to the group
00190         identifiersElement.appendChild( identifier );
00191     }
00192     return true;
00193 }
00194 
00195 bool KEduVocKvtml2Writer::writeLessons( KEduVocLesson *parentLesson, QDomElement &lessonsElement )
00196 {
00197     // iterate over child lessons.
00198     // the first time this is called with the root lesson which does not have a <lesson> entry.
00199     for( int i = 0; i < parentLesson->childContainerCount(); i++ ) {
00200         KEduVocLesson *lesson = static_cast<KEduVocLesson*>(parentLesson->childContainer(i));
00201         // make lesson element
00202         QDomElement thisLessonElement = m_domDoc.createElement( KVTML_CONTAINER );
00203 
00204         // add a name
00205         thisLessonElement.appendChild( newTextElement( KVTML_NAME, lesson->name() ) );
00206 
00207         // add a inquery tag
00208         if ( lesson->inPractice() ) {
00209             thisLessonElement.appendChild( newTextElement( KVTML_INPRACTICE, KVTML_TRUE ) );
00210         }
00211 
00212         // child lessons
00213         writeLessons(lesson, thisLessonElement);
00214 
00215         // child entries
00216         foreach(KEduVocExpression *entry, lesson->entries()) {
00217             QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
00218             entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(entry)) );
00219             thisLessonElement.appendChild(entryElement);
00220         }
00221         lessonsElement.appendChild( thisLessonElement );
00222     }
00223     return true;
00224 }
00225 
00226 
00227 
00228 void KEduVocKvtml2Writer::writeSynonymAntonymFalseFriend(QDomElement & parentElement)
00229 {
00230     QList< KEduVocTranslation* > currentList;
00231     QDomElement synonymElement;
00232     // synonym, antonym, false friend
00233     for(int type = KEduVocTranslation::Synonym; type <= KEduVocTranslation::FalseFriend; type++) {
00234         switch (type) {
00235             case KEduVocTranslation::Synonym:
00236                 synonymElement = m_domDoc.createElement( KVTML_SYNONYM );
00237                 currentList = m_synonyms;
00238                 break;
00239             case KEduVocTranslation::Antonym:
00240                 synonymElement = m_domDoc.createElement( KVTML_ANTONYM );
00241                 currentList = m_antonyms;
00242                 break;
00243             case KEduVocTranslation::FalseFriend:
00244                 synonymElement = m_domDoc.createElement( KVTML_FALSEFRIEND );
00245                 currentList = m_falseFriends;
00246                 break;
00247         }
00248 
00249         while (!currentList.isEmpty()) {
00250             // after writing a translation, remove it from the list
00251             KEduVocTranslation* translation = currentList.takeAt(0);
00252 
00253             // fill the entry element but only add later if it is valid
00254             QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
00255             entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(translation->entry())) );
00256             // find out which id that is... silly
00257             foreach(int index, translation->entry()->translationIndices()) {
00258                 if (translation->entry()->translation(index) == translation) {
00259                     // create <translation id="123">
00260                     QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION );
00261                     translationElement.setAttribute( KVTML_ID, QString::number(index) );
00262                     entryElement.appendChild(translationElement);
00263                     break;
00264                 }
00265             }
00266 
00267             QDomElement relatedElement;
00268             QList <KEduVocTranslation*> list;
00269             switch (type) {
00270             case KEduVocTranslation::Synonym:
00271                 list = translation->synonyms();
00272                 break;
00273             case KEduVocTranslation::Antonym:
00274                 list = translation->antonyms();
00275                 break;
00276             case KEduVocTranslation::FalseFriend:
00277                 list = translation->falseFriends();
00278                 break;
00279             }
00280             foreach (KEduVocTranslation* synonym, list) {
00281                 // if it is not in the list it has already been written and we can move on
00282                 if (currentList.contains(synonym)) {
00283                     relatedElement = m_domDoc.createElement( KVTML_PAIR );
00284                     synonymElement.appendChild(relatedElement);
00285                     relatedElement.appendChild(entryElement);
00286 
00287 
00288                     QDomElement partnerElement = m_domDoc.createElement( KVTML_ENTRY );
00289                     partnerElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(synonym->entry())) );
00290 
00291                     // find out which id that is
00292                     foreach(int index, synonym->entry()->translationIndices()) {
00293                         if (synonym->entry()->translation(index) == synonym) {
00294                             // create <translation id="123">
00295                             QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION );
00296                             translationElement.setAttribute( KVTML_ID, QString::number(index) );
00297                             partnerElement.appendChild(translationElement);
00298                             break;
00299                         }
00300                     }
00301                     relatedElement.appendChild( partnerElement );
00302                 }
00303             }
00304             if (relatedElement.hasChildNodes()) {
00305                 synonymElement.appendChild( relatedElement );
00306             }
00307         }
00308         if (synonymElement.hasChildNodes()) {
00309             parentElement.appendChild( synonymElement );
00310         }
00311     } // iterate over types
00312 }
00313 /*
00314 bool KEduVocKvtml2Writer::writeRelated(QDomElement & parentElement, QList< KEduVocTranslation * > relatedList)
00315 {
00316     foreach (KEduVocTranslation* synonym, translation->synonyms()) {
00317         QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
00318         entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(translation->entry())) );
00319 
00320         // find out which id that is... silly
00321         foreach(int index, translation->entry()->translationIndices()) {
00322             if (translation->entry()->translation(index) == translation) {
00323                 // create <translation id="123">
00324                 QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION );
00325                 translationElement.setAttribute( KVTML_ID, QString::number(index) );
00326                 entryElement.appendChild(translationElement);
00327             }
00328         }
00329         parentElement.appendChild( entryElement );
00330     }
00331 }*/
00332 
00333 bool KEduVocKvtml2Writer::writeArticle( QDomElement &articleElement, int language )
00334 {
00336     QMap<int, KEduVocWordFlag::Flags> numbers;
00337     numbers[0] = KEduVocWordFlag::Singular;
00338     numbers[1] = KEduVocWordFlag::Dual;
00339     numbers[2] = KEduVocWordFlag::Plural;
00340     QMap<int, KEduVocWordFlag::Flags> genders;
00341     genders[0] = KEduVocWordFlag::Masculine;
00342     genders[1] = KEduVocWordFlag::Feminine;
00343     genders[2] = KEduVocWordFlag::Neuter;
00344     QMap<int, KEduVocWordFlag::Flags> defs;
00345     defs[0] = KEduVocWordFlag::Definite;
00346     defs[1] = KEduVocWordFlag::Indefinite;
00347 
00348     for (int num = 0; num <= 2; num++)
00349     {
00350         QDomElement numberElement = m_domDoc.createElement( KVTML_GRAMMATICAL_NUMBER[num] );
00351 
00352         for (int def = 0; def <= 1; def++) {
00353             QDomElement defElement = m_domDoc.createElement( KVTML_GRAMMATICAL_DEFINITENESS[def] );
00354 
00355             for (int gen = 0; gen <= 2; gen++)
00356             {
00357                 QString articleString = m_doc->identifier(language).article().article(numbers[num] | genders[gen] | defs[def]);
00358                 if ( !articleString.isEmpty() ) {
00359                     defElement.appendChild( newTextElement( KVTML_GRAMMATICAL_GENDER[gen], articleString ) );
00360                 }
00361             }
00362             if ( defElement.hasChildNodes() ) {
00363                 numberElement.appendChild( defElement );
00364             }
00365         }
00366         if ( numberElement.hasChildNodes() ) {
00367             articleElement.appendChild( numberElement );
00368         }
00369     }
00370     return true;
00371 }
00372 
00373 
00374 bool KEduVocKvtml2Writer::writeWordTypes( QDomElement &typesElement, KEduVocWordType* parentContainer )
00375 {
00376     foreach( KEduVocContainer* container, parentContainer->childContainers() ) {
00377         KEduVocWordType* wordType = static_cast<KEduVocWordType*>(container);
00378 
00379         QDomElement typeDefinitionElement = m_domDoc.createElement( KVTML_CONTAINER );
00380         typeDefinitionElement.appendChild( newTextElement( KVTML_NAME, wordType->name() ) );
00381 
00382         if (wordType->wordType().testFlag(KEduVocWordFlag::Noun))
00383         {
00384             if (wordType->wordType().testFlag(KEduVocWordFlag::Masculine))
00385                 typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_NOUN_MALE ) );
00386 
00387             else if (wordType->wordType().testFlag(KEduVocWordFlag::Feminine))
00388                 typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_NOUN_FEMALE ) );
00389 
00390             else if (wordType->wordType().testFlag(KEduVocWordFlag::Neuter))
00391                 typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_NOUN_NEUTRAL ) );
00392             else
00393                 typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_NOUN ) );
00394         }
00395         else if (wordType->wordType().testFlag(KEduVocWordFlag::Verb))
00396             typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_VERB ) );
00397 
00398         else if (wordType->wordType().testFlag(KEduVocWordFlag::Adjective))
00399             typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_ADJECTIVE ) );
00400 
00401         else if (wordType->wordType().testFlag(KEduVocWordFlag::Adverb))
00402             typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_ADVERB ) );
00403 
00404 
00405 // child entries
00406 
00407         // child entries
00408         foreach(KEduVocExpression *entry, wordType->entries()) {
00409             QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
00410             entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(entry)) );
00411             for(int translation = 0; translation<m_doc->identifierCount(); translation++) {
00412                 if (entry->translation(translation)->wordType()== wordType) {
00413                     QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION );
00414                     // create <translation id="123">
00415                     translationElement.setAttribute( KVTML_ID, QString::number(translation) );
00416                     // append both
00417                     entryElement.appendChild(translationElement);
00418                 }
00419             }
00420             typeDefinitionElement.appendChild( entryElement );
00421         }
00422 
00423         writeWordTypes( typeDefinitionElement, wordType );
00424 
00425         typesElement.appendChild( typeDefinitionElement );
00426     }
00427     return true;
00428 }
00429 
00430 bool KEduVocKvtml2Writer::writeLeitnerBoxes( QDomElement &leitnerParentElement, KEduVocLeitnerBox* parentContainer )
00431 {
00432     foreach( KEduVocContainer* container, parentContainer->childContainers() ) {
00433         KEduVocLeitnerBox* leitnerBox = static_cast<KEduVocLeitnerBox*>(container);
00434 
00435         QDomElement containerElement = m_domDoc.createElement( KVTML_CONTAINER );
00436         containerElement.appendChild( newTextElement( KVTML_NAME, leitnerBox->name() ) );
00437 
00438         // child entries
00439         foreach(KEduVocExpression *entry, leitnerBox->entries()) {
00440             QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
00441             entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(entry)) );
00442             for(int translation = 0; translation<m_doc->identifierCount(); translation++) {
00443                 if (entry->translation(translation)->leitnerBox()== leitnerBox) {
00444                     QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION );
00445                     // create <translation id="123">
00446                     translationElement.setAttribute( KVTML_ID, QString::number(translation) );
00447                     // append both
00448                     entryElement.appendChild(translationElement);
00449                 }
00450             }
00451             containerElement.appendChild( entryElement );
00452         }
00453 
00454         leitnerParentElement.appendChild( containerElement );
00455     }
00456     return true;
00457 }
00458 
00459 bool KEduVocKvtml2Writer::writeEntries( QDomElement &entriesElement )
00460 {
00461     m_allEntries = m_doc->lesson()->entries(KEduVocLesson::Recursive);
00462 
00463     // loop through entries
00464     for ( int i = 0; i < m_allEntries.count(); ++i ) {
00465         KEduVocExpression *thisEntry = m_allEntries.value(i);
00466 
00467         // write entry tag
00468         QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
00469 
00470         // add id
00471         entryElement.setAttribute( KVTML_ID, QString::number( i ) );
00472 
00473         // write deactivated
00474         if(!thisEntry->isActive()) {
00475             entryElement.appendChild( newTextElement( KVTML_DEACTIVATED, KVTML_TRUE ) );
00476         }
00477 
00478         // loop through translations
00479         foreach( int trans, thisEntry->translationIndices() ) {
00480             // write translations
00481             QDomElement translation = m_domDoc.createElement( KVTML_TRANSLATION );
00482             translation.setAttribute( KVTML_ID, QString::number( trans ) );
00483             writeTranslation( translation, thisEntry->translation( trans ) );
00484             entryElement.appendChild( translation );
00485         }
00486         // add this entry to the entriesElement
00487         entriesElement.appendChild( entryElement );
00488     }
00489     return true;
00490 }
00491 
00492 
00493 bool KEduVocKvtml2Writer::writeTranslation( QDomElement &translationElement, KEduVocTranslation* translation )
00494 {
00495     // so far only for KEduVocWord - text and grades
00496     translation->toKVTML2(translationElement);
00497 
00498     // comparison
00499     if ( !(translation->comparative().isEmpty() || translation->comparative().isEmpty())) {
00500         QDomElement comparisonElement = m_domDoc.createElement( KVTML_COMPARISON );
00501         writeComparison( comparisonElement, translation );
00502         translationElement.appendChild( comparisonElement );
00503     }
00504 
00505     // multiplechoice
00506     if ( !translation->multipleChoice().isEmpty() ) {
00507         QDomElement multipleChoiceElement = m_domDoc.createElement( KVTML_MULTIPLECHOICE );
00508         writeMultipleChoice( multipleChoiceElement, translation );
00509         translationElement.appendChild( multipleChoiceElement );
00510     }
00511 
00512     // image
00513     if ( !translation->imageUrl().isEmpty() ) {
00514         QString urlString;
00515         if ( translation->imageUrl().url().startsWith(m_doc->url().upUrl().url()) ) {
00516             // try to save as relative url
00517             urlString = KUrl::relativeUrl( m_doc->url() , translation->imageUrl() );
00518         } else {
00519             urlString =  translation->imageUrl().url();
00520         }
00521         translationElement.appendChild( newTextElement( KVTML_IMAGE, urlString ) );
00522     }
00523 
00524     // sound
00525     if ( !translation->soundUrl().isEmpty() ) {
00526         QString urlString;
00527         if ( translation->soundUrl().url().startsWith(m_doc->url().upUrl().url()) ) {
00528             // try to save as relative url
00529             urlString = KUrl::relativeUrl( m_doc->url() , translation->soundUrl() );
00530         } else {
00531             urlString =  translation->soundUrl().url();
00532         }
00533         translationElement.appendChild( newTextElement( KVTML_SOUND, urlString ) );
00534     }
00535 
00536 
00537     // synonym, antonym, false friend
00538     // add to the list if it has any, write later since we want them separate
00539     if (!translation->synonyms().isEmpty()) {
00540         m_synonyms.append(translation);
00541     }
00542     if (!translation->antonyms().isEmpty()) {
00543         m_antonyms.append(translation);
00544     }
00545     if (!translation->falseFriends().isEmpty()) {
00546         m_falseFriends.append(translation);
00547     }
00548     return true;
00549 }
00550 
00551 
00552 
00554     // <falsefriend fromid="0"></falsefriend>
00555     // loop through the identifiers
00556 //     for ( int i = 0; i < m_doc->identifierCount(); ++i ) {
00557 //         // see if this identifier has a falsefriend in this translation
00558 //         QString thisFriend = translation->falseFriend( i );
00559 //         if ( !thisFriend.isEmpty() ) {
00560 //             // if so, create it, and set the fromid to i
00561 //             QDomElement thisFriendElement = newTextElement( KVTML_FALSEFRIEND, thisFriend );
00562 //             thisFriendElement.setAttribute( KVTML_FROMID, QString::number( i ) );
00563 //             translationElement.appendChild( thisFriendElement );
00564 //         }
00565 //     }
00566 
00567 
00568 
00569 
00570 
00571 
00572 bool KEduVocKvtml2Writer::writeComparison( QDomElement &comparisonElement, KEduVocTranslation* translation )
00573 /*
00574  <comparison>
00575    <absolute>good</absolute>
00576    <comparative>better</comparative>
00577    <superlative>best</superlative>
00578  </comparison>
00579 */
00580 {
00581     comparisonElement.appendChild( newTextElement( KVTML_COMPARATIVE, translation->comparative() ) );
00582     comparisonElement.appendChild( newTextElement( KVTML_SUPERLATIVE, translation->superlative() ) );
00583 
00584     return true;
00585 }
00586 
00587 
00588 bool KEduVocKvtml2Writer::writeMultipleChoice( QDomElement &multipleChoiceElement, KEduVocTranslation* translation )
00589 /*
00590  <multiplechoice>
00591    <choice>good</choice>
00592    <choice>better</choice>
00593    <choice>best</choice>
00594    <choice>best 2</choice>
00595    <choice>best 3</choice>
00596  </multiplechoice>
00597 */
00598 {
00599     foreach ( const QString &choice, translation->multipleChoice() ) {
00600         multipleChoiceElement.appendChild( newTextElement( KVTML_CHOICE, choice ) );
00601     }
00602     return true;
00603 }
00604 
00605 QDomElement KEduVocKvtml2Writer::newTextElement( const QString &elementName, const QString &text )
00606 {
00607     kDebug() << "append: " << elementName << text;
00608     QDomElement retval = m_domDoc.createElement( elementName );
00609     QDomText textNode = m_domDoc.createTextNode( text );
00610     retval.appendChild( textNode );
00611     return retval;
00612 }
00613 
00614 bool KEduVocKvtml2Writer::writePersonalPronoun(QDomElement & pronounElement, const KEduVocPersonalPronoun & pronoun)
00615 {
00616     // general pronoun properties
00617     if ( pronoun.maleFemaleDifferent() ) {
00618         pronounElement.appendChild( m_domDoc.createElement( KVTML_THIRD_PERSON_MALE_FEMALE_DIFFERENT ) );
00619     }
00620     if ( pronoun.neutralExists() ) {
00621         pronounElement.appendChild( m_domDoc.createElement( KVTML_THIRD_PERSON_NEUTRAL_EXISTS ) );
00622     }
00623     if ( pronoun.dualExists() ) {
00624         pronounElement.appendChild( m_domDoc.createElement( KVTML_DUAL_EXISTS ) );
00625     }
00626 
00627 
00628     QMap<int, KEduVocWordFlag::Flags> numbers;
00629     numbers[0] = KEduVocWordFlag::Singular;
00630     numbers[1] = KEduVocWordFlag::Dual;
00631     numbers[2] = KEduVocWordFlag::Plural;
00632     QMap<int, KEduVocWordFlag::Flags> persons;
00633     persons[0] = KEduVocWordFlag::First;
00634     persons[1] = KEduVocWordFlag::Second;
00635     persons[2] = (KEduVocWordFlag::Flags)((int)KEduVocWordFlag::Third | (int)KEduVocWordFlag::Masculine);
00636     persons[3] = (KEduVocWordFlag::Flags)((int)KEduVocWordFlag::Third | (int)KEduVocWordFlag::Feminine);
00637     persons[4] = (KEduVocWordFlag::Flags)((int)KEduVocWordFlag::Third | (int)KEduVocWordFlag::Neuter);
00638 
00639 
00640 
00641     // the actual pronouns
00642     for ( int num = 0; num < 3; num++ ) {
00643         QDomElement numberElement = m_domDoc.createElement( KVTML_GRAMMATICAL_NUMBER[num] );
00644         for ( int person = 0; person < 5; person++ ) {
00645             QString pronounString = pronoun.personalPronoun(numbers[num] | persons[person]);
00646             if (!pronounString.isEmpty()) {
00647                 numberElement.appendChild( newTextElement( KVTML_GRAMMATICAL_PERSON[person], pronounString ));
00648             }
00649         }
00650         if (numberElement.hasChildNodes()) {
00651             pronounElement.appendChild( numberElement );
00652         }
00653     }
00654     return true;
00655 }
00656 
00657 void KEduVocKvtml2Writer::appendTextElement(QDomElement & parent, const QString & elementName, const QString & text)
00658 {
00659     // empty will never be written
00660     if (text.isEmpty()) {
00661         return;
00662     }
00663 
00664     QDomDocument domDoc = parent.ownerDocument();
00665     QDomElement element = domDoc.createElement( elementName );
00666     parent.appendChild( element );
00667     QDomText textNode = domDoc.createTextNode( text );
00668     element.appendChild( textNode );
00669 }
00670 
00671 

libkdeedu/keduvocdocument

Skip menu "libkdeedu/keduvocdocument"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal