1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
|
/***************************************************************************
export a KEduVocDocument to a KVTML file
-----------------------------------------------------------------------
copyright : (C) 2007 Jeremy Whiting <jpwhiting@kde.org>
Copyright 2007-2010 Frederik Gladhorn <gladhorn@kde.org>
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "keduvockvtml2writer.h"
#include <QtCore/QTextStream>
#include <QtCore/QFile>
#include <QDebug>
#include "keduvocdocument.h"
#include "keduvocexpression.h"
#include "keduvoclesson.h"
#include "keduvocleitnerbox.h"
#include "keduvocwordtype.h"
#include "kvtml2defs.h"
#include <kio/global.h>
KEduVocKvtml2Writer::KEduVocKvtml2Writer( QFile *file )
{
// the file must be already open
m_outputFile = file;
}
bool KEduVocKvtml2Writer::writeDoc( KEduVocDocument *doc, const QString &generator )
{
if (createXmlDocument(doc, generator)) {
QTextStream ts( m_outputFile );
m_domDoc.save( ts, 2 );
return true;
}
return false;
}
QByteArray KEduVocKvtml2Writer::toByteArray(KEduVocDocument * doc, const QString & generator)
{
if (createXmlDocument(doc, generator)) {
return m_domDoc.toByteArray();
}
return QByteArray();
}
bool KEduVocKvtml2Writer::createXmlDocument( KEduVocDocument *doc, const QString &generator )
{
m_doc = doc;
m_domDoc = QDomDocument( "kvtml PUBLIC \"kvtml2.dtd\" \"http://edu.kde.org/kvtml/kvtml2.dtd\"" );
m_domDoc.appendChild( m_domDoc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
QDomElement domElementKvtml = m_domDoc.createElement( "kvtml" );
m_domDoc.appendChild( domElementKvtml );
domElementKvtml.setAttribute( KVTML_VERSION, ( QString ) "2.0" );
// information group
QDomElement currentElement = m_domDoc.createElement( KVTML_INFORMATION );
writeInformation( currentElement, generator );
domElementKvtml.appendChild( currentElement );
// identifiers
currentElement = m_domDoc.createElement( KVTML_IDENTIFIERS );
writeIdentifiers( currentElement );
domElementKvtml.appendChild( currentElement );
// entries
currentElement = m_domDoc.createElement( KVTML_ENTRIES );
if ( !writeEntries( currentElement ) ) {
// at least one entry is required!
return false;
}
domElementKvtml.appendChild( currentElement );
// lessons
currentElement = m_domDoc.createElement( KVTML_LESSONS );
writeLessons( m_doc->lesson(), currentElement );
if ( currentElement.hasChildNodes() ) {
domElementKvtml.appendChild( currentElement );
}
// types
currentElement = m_domDoc.createElement( KVTML_WORDTYPES );
writeWordTypes( currentElement, m_doc->wordTypeContainer() );
if ( currentElement.hasChildNodes() ) {
domElementKvtml.appendChild( currentElement );
}
// leitner boxes
currentElement = m_domDoc.createElement( KVTML_LEITNERBOXES );
writeLeitnerBoxes( currentElement, m_doc->leitnerContainer() );
if ( currentElement.hasChildNodes() ) {
domElementKvtml.appendChild( currentElement );
}
writeSynonymAntonymFalseFriend(domElementKvtml);
m_domDoc.appendChild( domElementKvtml );
return true;
}
bool KEduVocKvtml2Writer::writeInformation( QDomElement &informationElement, const QString &generator )
{
QDomElement currentElement;
QDomText textNode;
// generator
informationElement.appendChild( newTextElement( KVTML_GENERATOR, generator ) );
// title
if ( !m_doc->title().isEmpty() ) {
informationElement.appendChild( newTextElement( KVTML_TITLE, m_doc->title() ) );
}
// author
if ( !m_doc->author().isEmpty() ) {
informationElement.appendChild( newTextElement( KVTML_AUTHOR, m_doc->author() ) );
}
// author contact (mail/homepage)
if ( !m_doc->authorContact().isEmpty() ) {
informationElement.appendChild( newTextElement( KVTML_AUTHORCONTACT, m_doc->authorContact() ) );
}
// license
if ( !m_doc->license().isEmpty() ) {
informationElement.appendChild( newTextElement( KVTML_LICENSE, m_doc->license() ) );
}
// comment
if ( !m_doc->documentComment().isEmpty() ) {
informationElement.appendChild( newTextElement( KVTML_COMMENT, m_doc->documentComment() ) );
}
QDate today = QDate::currentDate();
informationElement.appendChild( newTextElement( KVTML_DATE, today.toString(QLatin1String("yyyy-MM-dd")) ) );
// category
if ( !m_doc->category().isEmpty() ) {
informationElement.appendChild( newTextElement( KVTML_CATEGORY, m_doc->category() ) );
}
return true;
}
bool KEduVocKvtml2Writer::writeIdentifiers( QDomElement &identifiersElement )
{
for ( int i = 0; i < m_doc->identifierCount(); ++i ) {
// create the node
QDomElement identifier = m_domDoc.createElement( KVTML_IDENTIFIER );
// set the id
identifier.setAttribute( KVTML_ID, QString::number( i ) );
// record the identifier as the locale for now
// TODO: when support for more parts of the identifier is in the document class (name, type, etc.) store those here as well
identifier.appendChild( newTextElement( KVTML_NAME, m_doc->identifier( i ).name() ) );
identifier.appendChild( newTextElement( KVTML_LOCALE, m_doc->identifier( i ).locale() ) );
// record articles
QDomElement article = m_domDoc.createElement( KVTML_ARTICLE );
writeArticle( article, i );
if ( article.hasChildNodes() ) {
identifier.appendChild( article );
}
// record personalpronouns
QDomElement personalpronouns = m_domDoc.createElement( KVTML_PERSONALPRONOUNS );
writePersonalPronoun( personalpronouns, m_doc->identifier(i).personalPronouns() );
if ( personalpronouns.hasChildNodes() ) {
identifier.appendChild( personalpronouns );
}
// tenses
foreach(const QString &tense, m_doc->identifier(i).tenseList() ) {
if ( !( tense.isNull() ) ) {
identifier.appendChild( newTextElement( KVTML_TENSE, tense ) );
}
}
// add this identifier to the group
identifiersElement.appendChild( identifier );
}
return true;
}
bool KEduVocKvtml2Writer::writeLessons( KEduVocLesson *parentLesson, QDomElement &lessonsElement )
{
// iterate over child lessons.
// the first time this is called with the root lesson which does not have a <lesson> entry.
for( int i = 0; i < parentLesson->childContainerCount(); i++ ) {
KEduVocLesson *lesson = static_cast<KEduVocLesson*>(parentLesson->childContainer(i));
// make lesson element
QDomElement thisLessonElement = m_domDoc.createElement( KVTML_CONTAINER );
// add a name
thisLessonElement.appendChild( newTextElement( KVTML_NAME, lesson->name() ) );
// add a inquery tag
if ( lesson->inPractice() ) {
thisLessonElement.appendChild( newTextElement( KVTML_INPRACTICE, KVTML_TRUE ) );
}
// child lessons
writeLessons(lesson, thisLessonElement);
// child entries
foreach(KEduVocExpression *entry, lesson->entries()) {
QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(entry)) );
thisLessonElement.appendChild(entryElement);
}
lessonsElement.appendChild( thisLessonElement );
}
return true;
}
void KEduVocKvtml2Writer::writeSynonymAntonymFalseFriend(QDomElement & parentElement)
{
QList< KEduVocTranslation* > currentList;
QDomElement synonymElement;
// synonym, antonym, false friend
for(int type = KEduVocTranslation::Synonym; type <= KEduVocTranslation::FalseFriend; type++) {
switch (type) {
case KEduVocTranslation::Synonym:
synonymElement = m_domDoc.createElement( KVTML_SYNONYM );
currentList = m_synonyms;
break;
case KEduVocTranslation::Antonym:
synonymElement = m_domDoc.createElement( KVTML_ANTONYM );
currentList = m_antonyms;
break;
case KEduVocTranslation::FalseFriend:
synonymElement = m_domDoc.createElement( KVTML_FALSEFRIEND );
currentList = m_falseFriends;
break;
}
while (!currentList.isEmpty()) {
// after writing a translation, remove it from the list
KEduVocTranslation* translation = currentList.takeFirst();
QDomElement relatedElement;
QList <KEduVocTranslation*> list;
switch (type) {
case KEduVocTranslation::Synonym:
list = translation->synonyms();
break;
case KEduVocTranslation::Antonym:
list = translation->antonyms();
break;
case KEduVocTranslation::FalseFriend:
list = translation->falseFriends();
break;
}
foreach (KEduVocTranslation* synonym, list) {
// if it is not in the list it has already been written and we can move on
if (currentList.contains(synonym)) {
relatedElement = m_domDoc.createElement( KVTML_PAIR );
// fill the entry element but only add later if it is valid
QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(translation->entry())) );
// find out which id that is... silly
foreach(int index, translation->entry()->translationIndices()) {
if (translation->entry()->translation(index) == translation) {
// create <translation id="123">
QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION );
translationElement.setAttribute( KVTML_ID, QString::number(index) );
entryElement.appendChild(translationElement);
break;
}
}
relatedElement.appendChild(entryElement);
QDomElement partnerElement = m_domDoc.createElement( KVTML_ENTRY );
partnerElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(synonym->entry())) );
// find out which id that is
foreach(int index, synonym->entry()->translationIndices()) {
if (synonym->entry()->translation(index) == synonym) {
// create <translation id="123">
QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION );
translationElement.setAttribute( KVTML_ID, QString::number(index) );
partnerElement.appendChild(translationElement);
break;
}
}
relatedElement.appendChild( partnerElement );
synonymElement.appendChild(relatedElement);
}
}
}
if (synonymElement.hasChildNodes()) {
parentElement.appendChild( synonymElement );
}
} // iterate over types
}
/*
bool KEduVocKvtml2Writer::writeRelated(QDomElement & parentElement, QList< KEduVocTranslation * > relatedList)
{
foreach (KEduVocTranslation* synonym, translation->synonyms()) {
QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(translation->entry())) );
// find out which id that is... silly
foreach(int index, translation->entry()->translationIndices()) {
if (translation->entry()->translation(index) == translation) {
// create <translation id="123">
QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION );
translationElement.setAttribute( KVTML_ID, QString::number(index) );
entryElement.appendChild(translationElement);
}
}
parentElement.appendChild( entryElement );
}
}*/
bool KEduVocKvtml2Writer::writeArticle( QDomElement &articleElement, int language )
{
///@todo only write if not empty
QMap<int, KEduVocWordFlag::Flags> numbers;
numbers[0] = KEduVocWordFlag::Singular;
numbers[1] = KEduVocWordFlag::Dual;
numbers[2] = KEduVocWordFlag::Plural;
QMap<int, KEduVocWordFlag::Flags> genders;
genders[0] = KEduVocWordFlag::Masculine;
genders[1] = KEduVocWordFlag::Feminine;
genders[2] = KEduVocWordFlag::Neuter;
QMap<int, KEduVocWordFlag::Flags> defs;
defs[0] = KEduVocWordFlag::Definite;
defs[1] = KEduVocWordFlag::Indefinite;
for (int num = 0; num <= 2; num++)
{
QDomElement numberElement = m_domDoc.createElement( KVTML_GRAMMATICAL_NUMBER[num] );
for (int def = 0; def <= 1; def++) {
QDomElement defElement = m_domDoc.createElement( KVTML_GRAMMATICAL_DEFINITENESS[def] );
for (int gen = 0; gen <= 2; gen++)
{
QString articleString = m_doc->identifier(language).article().article(numbers[num] | genders[gen] | defs[def]);
if ( !articleString.isEmpty() ) {
defElement.appendChild( newTextElement( KVTML_GRAMMATICAL_GENDER[gen], articleString ) );
}
}
if ( defElement.hasChildNodes() ) {
numberElement.appendChild( defElement );
}
}
if ( numberElement.hasChildNodes() ) {
articleElement.appendChild( numberElement );
}
}
return true;
}
bool KEduVocKvtml2Writer::writeWordTypes( QDomElement &typesElement, KEduVocWordType* parentContainer )
{
foreach( KEduVocContainer* container, parentContainer->childContainers() ) {
KEduVocWordType* wordType = static_cast<KEduVocWordType*>(container);
QDomElement typeDefinitionElement = m_domDoc.createElement( KVTML_CONTAINER );
typeDefinitionElement.appendChild( newTextElement( KVTML_NAME, wordType->name() ) );
if (wordType->wordType().testFlag(KEduVocWordFlag::Noun))
{
if (wordType->wordType().testFlag(KEduVocWordFlag::Masculine))
typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_NOUN_MALE ) );
else if (wordType->wordType().testFlag(KEduVocWordFlag::Feminine))
typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_NOUN_FEMALE ) );
else if (wordType->wordType().testFlag(KEduVocWordFlag::Neuter))
typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_NOUN_NEUTRAL ) );
else
typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_NOUN ) );
}
else if (wordType->wordType().testFlag(KEduVocWordFlag::Verb))
typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_VERB ) );
else if (wordType->wordType().testFlag(KEduVocWordFlag::Adjective))
typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_ADJECTIVE ) );
else if (wordType->wordType().testFlag(KEduVocWordFlag::Adverb))
typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_ADVERB ) );
else if (wordType->wordType().testFlag(KEduVocWordFlag::Conjunction))
typeDefinitionElement.appendChild( newTextElement( KVTML_SPECIALWORDTYPE, KVTML_SPECIALWORDTYPE_CONJUNCTION ) );
// child entries
// child entries
foreach(KEduVocExpression *entry, wordType->entries()) {
QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(entry)) );
for(int translation = 0; translation<m_doc->identifierCount(); translation++) {
if (entry->translation(translation)->wordType()== wordType) {
QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION );
// create <translation id="123">
translationElement.setAttribute( KVTML_ID, QString::number(translation) );
// append both
entryElement.appendChild(translationElement);
}
}
typeDefinitionElement.appendChild( entryElement );
}
writeWordTypes( typeDefinitionElement, wordType );
typesElement.appendChild( typeDefinitionElement );
}
return true;
}
bool KEduVocKvtml2Writer::writeLeitnerBoxes( QDomElement &leitnerParentElement, KEduVocLeitnerBox* parentContainer )
{
foreach( KEduVocContainer* container, parentContainer->childContainers() ) {
KEduVocLeitnerBox* leitnerBox = static_cast<KEduVocLeitnerBox*>(container);
QDomElement containerElement = m_domDoc.createElement( KVTML_CONTAINER );
containerElement.appendChild( newTextElement( KVTML_NAME, leitnerBox->name() ) );
// child entries
foreach(KEduVocExpression *entry, leitnerBox->entries()) {
QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
entryElement.setAttribute( KVTML_ID, QString::number(m_allEntries.indexOf(entry)) );
for(int translation = 0; translation<m_doc->identifierCount(); translation++) {
if (entry->translation(translation)->leitnerBox()== leitnerBox) {
QDomElement translationElement = m_domDoc.createElement( KVTML_TRANSLATION );
// create <translation id="123">
translationElement.setAttribute( KVTML_ID, QString::number(translation) );
// append both
entryElement.appendChild(translationElement);
}
}
containerElement.appendChild( entryElement );
}
leitnerParentElement.appendChild( containerElement );
}
return true;
}
bool KEduVocKvtml2Writer::writeEntries( QDomElement &entriesElement )
{
m_allEntries = m_doc->lesson()->entries(KEduVocLesson::Recursive);
// loop through entries
for ( int i = 0; i < m_allEntries.count(); ++i ) {
KEduVocExpression *thisEntry = m_allEntries.value(i);
// write entry tag
QDomElement entryElement = m_domDoc.createElement( KVTML_ENTRY );
// add id
entryElement.setAttribute( KVTML_ID, QString::number( i ) );
// write deactivated
if(!thisEntry->isActive()) {
entryElement.appendChild( newTextElement( KVTML_DEACTIVATED, KVTML_TRUE ) );
}
// loop through translations
foreach( int trans, thisEntry->translationIndices() ) {
// write translations
QDomElement translation = m_domDoc.createElement( KVTML_TRANSLATION );
translation.setAttribute( KVTML_ID, QString::number( trans ) );
writeTranslation( translation, thisEntry->translation( trans ) );
entryElement.appendChild( translation );
}
// add this entry to the entriesElement
entriesElement.appendChild( entryElement );
}
return true;
}
bool KEduVocKvtml2Writer::writeTranslation( QDomElement &translationElement, KEduVocTranslation* translation )
{
// so far only for KEduVocWord - text and grades
translation->toKVTML2(translationElement);
// comparison
if ( !(translation->comparativeForm().text().isEmpty() || translation->superlativeForm().text().isEmpty())) {
qDebug() << "Write comp";
QDomElement comparisonElement = m_domDoc.createElement( KVTML_COMPARISON );
translationElement.appendChild(comparisonElement);
QDomElement comparativeElement = m_domDoc.createElement( KVTML_COMPARATIVE );
comparisonElement.appendChild(comparativeElement);
translation->comparativeForm().toKVTML2(comparativeElement);
QDomElement superlativeElement = m_domDoc.createElement( KVTML_SUPERLATIVE );
comparisonElement.appendChild(superlativeElement);
translation->superlativeForm().toKVTML2(superlativeElement);
}
if (translation->article().practiceCount() != 0) {
QDomElement articleElement = m_domDoc.createElement( KVTML_ARTICLE );
translation->article().toKVTML2(articleElement);
translationElement.appendChild(articleElement);
}
// multiplechoice
if (!translation->getMultipleChoice().isEmpty()) {
QDomElement multipleChoiceElement = m_domDoc.createElement( KVTML_MULTIPLECHOICE );
writeMultipleChoice( multipleChoiceElement, translation );
translationElement.appendChild( multipleChoiceElement );
}
// image
if ( !translation->imageUrl().isEmpty() ) {
QString urlString;
if ( KIO::upUrl(m_doc->url()).isParentOf( translation->imageUrl()) ) {
// try to save as relative url
urlString = m_doc->url().toString() + '/' + translation->imageUrl().toString();
} else {
urlString = translation->imageUrl().url();
}
translationElement.appendChild( newTextElement( KVTML_IMAGE, urlString ) );
}
// sound
if ( !translation->soundUrl().isEmpty() ) {
QString urlString;
if ( KIO::upUrl(m_doc->url().adjusted(QUrl::RemoveFilename)).isParentOf( translation->soundUrl()) ) {
// try to save as relative url
urlString = m_doc->url().toString(QUrl::RemoveFilename) + '/' + translation->soundUrl().toString();
} else {
urlString = translation->soundUrl().url();
}
translationElement.appendChild( newTextElement( KVTML_SOUND, urlString ) );
}
// synonym, antonym, false friend
// add to the list if it has any, write later since we want them separate
if (!translation->synonyms().isEmpty()) {
m_synonyms.append(translation);
}
if (!translation->antonyms().isEmpty()) {
m_antonyms.append(translation);
}
if (!translation->falseFriends().isEmpty()) {
m_falseFriends.append(translation);
}
return true;
}
///@todo write false friends
// <falsefriend fromid="0"></falsefriend>
// loop through the identifiers
// for ( int i = 0; i < m_doc->identifierCount(); ++i ) {
// // see if this identifier has a falsefriend in this translation
// QString thisFriend = translation->falseFriend( i );
// if ( !thisFriend.isEmpty() ) {
// // if so, create it, and set the fromid to i
// QDomElement thisFriendElement = newTextElement( KVTML_FALSEFRIEND, thisFriend );
// thisFriendElement.setAttribute( KVTML_FROMID, QString::number( i ) );
// translationElement.appendChild( thisFriendElement );
// }
// }
bool KEduVocKvtml2Writer::writeMultipleChoice( QDomElement &multipleChoiceElement, KEduVocTranslation* translation )
/*
<multiplechoice>
<choice>good</choice>
<choice>better</choice>
<choice>best</choice>
<choice>best 2</choice>
<choice>best 3</choice>
</multiplechoice>
*/
{
foreach (const QString &choice, translation->getMultipleChoice()) {
multipleChoiceElement.appendChild( newTextElement( KVTML_CHOICE, choice ) );
}
return true;
}
QDomElement KEduVocKvtml2Writer::newTextElement( const QString &elementName, const QString &text )
{
QDomElement retval = m_domDoc.createElement( elementName );
QDomText textNode = m_domDoc.createTextNode( text );
retval.appendChild( textNode );
return retval;
}
bool KEduVocKvtml2Writer::writePersonalPronoun(QDomElement & pronounElement, const KEduVocPersonalPronoun & pronoun)
{
// general pronoun properties
if ( pronoun.maleFemaleDifferent() ) {
pronounElement.appendChild( m_domDoc.createElement( KVTML_THIRD_PERSON_MALE_FEMALE_DIFFERENT ) );
}
if ( pronoun.neutralExists() ) {
pronounElement.appendChild( m_domDoc.createElement( KVTML_THIRD_PERSON_NEUTRAL_EXISTS ) );
}
if ( pronoun.dualExists() ) {
pronounElement.appendChild( m_domDoc.createElement( KVTML_DUAL_EXISTS ) );
}
QMap<int, KEduVocWordFlag::Flags> numbers;
numbers[0] = KEduVocWordFlag::Singular;
numbers[1] = KEduVocWordFlag::Dual;
numbers[2] = KEduVocWordFlag::Plural;
QMap<int, KEduVocWordFlag::Flags> persons;
persons[0] = KEduVocWordFlag::First;
persons[1] = KEduVocWordFlag::Second;
persons[2] = (KEduVocWordFlag::Flags)((int)KEduVocWordFlag::Third | (int)KEduVocWordFlag::Masculine);
persons[3] = (KEduVocWordFlag::Flags)((int)KEduVocWordFlag::Third | (int)KEduVocWordFlag::Feminine);
persons[4] = (KEduVocWordFlag::Flags)((int)KEduVocWordFlag::Third | (int)KEduVocWordFlag::Neuter);
// the actual pronouns
for ( int num = 0; num < 3; num++ ) {
QDomElement numberElement = m_domDoc.createElement( KVTML_GRAMMATICAL_NUMBER[num] );
for ( int person = 0; person < 5; person++ ) {
QString pronounString = pronoun.personalPronoun(numbers[num] | persons[person]);
if (!pronounString.isEmpty()) {
numberElement.appendChild( newTextElement( KVTML_GRAMMATICAL_PERSON[person], pronounString ));
}
}
if (numberElement.hasChildNodes()) {
pronounElement.appendChild( numberElement );
}
}
return true;
}
void KEduVocKvtml2Writer::appendTextElement(QDomElement & parent, const QString & elementName, const QString & text)
{
// empty will never be written
if (text.isEmpty()) {
return;
}
QDomDocument domDoc = parent.ownerDocument();
QDomElement element = domDoc.createElement( elementName );
parent.appendChild( element );
QDomText textNode = domDoc.createTextNode( text );
element.appendChild( textNode );
}
|