Make reading and writing of conjugations much more elegant by using keduvoctext.
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=786741
#include "keduvocconjugation.h"
#include "keduvoccommon_p.h"
+#include "kvtml2defs.h"
+
#include <KLocalizedString>
#include <KDebug>
+#include <QtXml/QDomDocument>
class KEduVocConjugation::Private
{
public:
- QMap<int, QString> m_conjugations;
+ QMap<int, KEduVocText> m_conjugations;
};
delete d;
}
-
KEduVocConjugation& KEduVocConjugation::operator = ( const KEduVocConjugation& other )
{
d->m_conjugations = other.d->m_conjugations;
}
-QString KEduVocConjugation::conjugation(int index) const
+KEduVocText& KEduVocConjugation::conjugation(int index)
{
- if ( d->m_conjugations.contains(index) ) {
- return d->m_conjugations.value(index);
- }
- return QString();
+ return d->m_conjugations[index];
}
-QString KEduVocConjugation::conjugation(ConjugationPerson person, ConjugationNumber number) const
+KEduVocText& KEduVocConjugation::conjugation(ConjugationPerson person, ConjugationNumber number)
{
return conjugation(indexOf(person, number));
}
-void KEduVocConjugation::setConjugation(const QString & conjugation, ConjugationPerson person, ConjugationNumber number)
+void KEduVocConjugation::setConjugation(const KEduVocText& conjugation, ConjugationPerson person, ConjugationNumber number)
{
setConjugation(conjugation, indexOf(person, number));
}
-void KEduVocConjugation::setConjugation(const QString & conjugation, int index)
+void KEduVocConjugation::setConjugation(const KEduVocText& conjugation, int index)
{
- if ( !conjugation.isEmpty() ) {
- d->m_conjugations[index] = conjugation;
- } else {
- // if we received an empty string, remove the element.
- if ( d->m_conjugations.contains(index) ) {
- d->m_conjugations.remove(index);
- }
- }
+ d->m_conjugations[index] = conjugation;
}
int KEduVocConjugation::indexOf(ConjugationPerson person, ConjugationNumber number)
{
- return person + PersonMAX * number;
+ return person + (ThirdNeutralCommon+1) * number;
}
+void KEduVocConjugation::toKVTML2(QDomElement & parent, const QString &tense)
+{
+ if (isEmpty()) {
+ return;
+ }
+
+ // write the tense tag
+ QDomDocument domDoc = parent.ownerDocument();
+ QDomElement tenseElement = domDoc.createElement( KVTML_TENSE );
+ tenseElement.appendChild( domDoc.createTextNode(tense) );
+ parent.appendChild(tenseElement);
+
+ for ( KEduVocConjugation::ConjugationNumber num = KEduVocConjugation::Singular; num <= KEduVocConjugation::Plural; num = KEduVocConjugation::ConjugationNumber(num +1) ) {
+
+ QDomElement numberElement = domDoc.createElement( KVTML_GRAMMATICAL_NUMBER[num] );
+ for ( KEduVocConjugation::ConjugationPerson person = KEduVocConjugation::First; person <= KEduVocConjugation::ThirdNeutralCommon; person = KEduVocConjugation::ConjugationPerson(person +1) ) {
+
+ if (!conjugation(indexOf(person, num)).isEmpty()) {
+ QDomElement personElement = domDoc.createElement( KVTML_GRAMMATICAL_PERSON[person] );
+ numberElement.appendChild(personElement);
+ conjugation(indexOf(person, num)).toKVTML2(personElement);
+ }
+ }
+ if (numberElement.hasChildNodes()) {
+ parent.appendChild( numberElement );
+ }
+ }
+}
+
+/*
+
+ QString first = conjugation.conjugation(
+ KEduVocConjugation::First, num );
+ QString second = conjugation.conjugation(
+ KEduVocConjugation::Second, num );
+ QString third_male = conjugation.conjugation(
+ KEduVocConjugation::ThirdMale, num );
+ QString third_female = conjugation.conjugation(
+ KEduVocConjugation::ThirdFemale, num );
+ QString third_neutral = conjugation.conjugation(
+ KEduVocConjugation::ThirdNeutralCommon, num );
+
+ if ( !first.isEmpty() || !second.isEmpty() || !third_female.isEmpty() ||
+ !third_male.isEmpty() || !third_neutral.isEmpty() ) {
+ QDomElement number;
+ switch (num) {
+ case KEduVocConjugation::Singular:
+ number = m_domDoc.createElement( KVTML_SINGULAR );
+ break;
+ case KEduVocConjugation::Dual:
+ number = m_domDoc.createElement( KVTML_DUAL );
+ break;
+ case KEduVocConjugation::Plural:
+ number = m_domDoc.createElement( KVTML_PLURAL );
+ break;
+ }
+
+ number.appendChild( newTextElement( KVTML_1STPERSON, first ) );
+ number.appendChild( newTextElement( KVTML_2NDPERSON, second ) );
+ number.appendChild( newTextElement( KVTML_THIRD_MALE, third_male ) );
+ number.appendChild( newTextElement( KVTML_THIRD_FEMALE, third_female ) );
+ number.appendChild( newTextElement( KVTML_THIRD_NEUTRAL_COMMON, third_neutral ) );
+
+ conjugationElement.appendChild( number );
+ }
+ }*/
+
+
+ /*
+ for ( KEduVocDeclension::DeclensionNumber num = KEduVocDeclension::Singular; num <= KEduVocDeclension::Plural; num = KEduVocDeclension::DeclensionNumber(num +1) ) {
+ QDomElement numberElement = domDoc.createElement( KVTML_GRAMMATICAL_NUMBER[num] );
+ for ( KEduVocDeclension::DeclensionCase dcase = KEduVocDeclension::Nominative; dcase < KEduVocDeclension::DeclensionCaseMAX; dcase = KEduVocDeclension::DeclensionCase(dcase +1) ) {
+ QDomElement caseElement = domDoc.createElement( KVTML_DECLENSION_CASE[dcase] );
+ declension(num, dcase).toKVTML2(caseElement);
+
+ if (caseElement.hasChildNodes()) {
+ numberElement.appendChild(caseElement);
+ }
+ }
+ if (numberElement.hasChildNodes()) {
+ declensionElement.appendChild(numberElement);
+ }
+ }
+
+ */
+
+
+
+KEduVocConjugation* KEduVocConjugation::fromKVTML2(QDomElement & parent)
+{
+ // sanity check
+ if (parent.isNull()) {
+ return 0;
+ }
+
+ KEduVocConjugation* conjugation = new KEduVocConjugation;
+
+ for ( int num = KEduVocConjugation::Singular; num <= KEduVocConjugation::Plural; num++ ) {
+ QDomElement numberElement = parent.firstChildElement( KVTML_GRAMMATICAL_NUMBER[num] );
+
+ if (numberElement.hasChildNodes()) {
+ for (int person = KEduVocConjugation::First; person <= KEduVocConjugation::ThirdNeutralCommon; person++) {
+ QDomElement personElement = numberElement.firstChildElement( KVTML_GRAMMATICAL_PERSON[person] );
+ if (!personElement.isNull()) {
+ KEduVocText text;
+ text.fromKVTML2(personElement);
+ if (text.text().isEmpty()) {
+ // compatibility for kde 4.0. There the text was directly below the person, not enabling grades per conjugation form.
+ text.setText(personElement.text());
+ }
+ conjugation->setConjugation(text, ConjugationPerson(person), KEduVocConjugation::ConjugationNumber(num));
+ }
+ }
+ }
+ }
+ return conjugation;
+}
+
+
enum ConjugationNumber {
Singular,
Dual,
- Plural,
- NumberMAX
+ Plural
};
// store third person neutral/common in the same sttr
Second,
ThirdMale,
ThirdFemale,
- ThirdNeutralCommon,
- PersonMAX
+ ThirdNeutralCommon
};
KEduVocConjugation& operator = ( const KEduVocConjugation& a );
bool operator == ( const KEduVocConjugation& a ) const;
- QString conjugation(ConjugationPerson person, ConjugationNumber number) const;
- QString conjugation(int index) const;
- void setConjugation(const QString& conjugation, ConjugationPerson person, ConjugationNumber number);
- void setConjugation(const QString& conjugation, int index);
+ KEduVocText& conjugation(ConjugationPerson person, ConjugationNumber number);
+ KEduVocText& conjugation(int index);
+ void setConjugation(const KEduVocText& conjugation, ConjugationPerson person, ConjugationNumber number);
+ void setConjugation(const KEduVocText& conjugation, int index);
QList<int> keys();
bool isEmpty();
+ /**
+ * Create xml for this declension
+ * @param parent
+ */
+ void toKVTML2(QDomElement& parent, const QString &tense);
+
+ /**
+ * Reads a declension from xml, returns 0 if it is empty
+ * @param parent
+ * @return
+ */
+ static KEduVocConjugation* fromKVTML2(QDomElement& parent);
+
static int indexOf(ConjugationPerson person, ConjugationNumber number);
private:
kDebug() << entry->translation(translation)->text() <<
entry->translation(translation)->grade();
}
-kDebug() << "translation: " << translation << "sum: " << sum;
// make that a percentage
return (sum * 100.0/7.0)/entryCount(NotRecursive);
}
int KEduVocDeclension::indexOf(DeclensionNumber number, DeclensionCase decCase)
{
- return number * DeclensionCaseMAX + decCase;
+ return number * (Vocative+1) + decCase;
}
bool KEduVocDeclension::isEmpty()
for ( KEduVocDeclension::DeclensionNumber num = KEduVocDeclension::Singular; num <= KEduVocDeclension::Plural; num = KEduVocDeclension::DeclensionNumber(num +1) ) {
QDomElement numberElement = domDoc.createElement( KVTML_GRAMMATICAL_NUMBER[num] );
- for ( KEduVocDeclension::DeclensionCase dcase = KEduVocDeclension::Nominative; dcase < KEduVocDeclension::DeclensionCaseMAX; dcase = KEduVocDeclension::DeclensionCase(dcase +1) ) {
+ for ( KEduVocDeclension::DeclensionCase dcase = KEduVocDeclension::Nominative; dcase < (KEduVocDeclension::Vocative+1); dcase = KEduVocDeclension::DeclensionCase(dcase +1) ) {
QDomElement caseElement = domDoc.createElement( KVTML_DECLENSION_CASE[dcase] );
declension(num, dcase).toKVTML2(caseElement);
for ( KEduVocDeclension::DeclensionNumber num = KEduVocDeclension::Singular; num <= KEduVocDeclension::Plural; num = KEduVocDeclension::DeclensionNumber(num +1) ) {
QDomElement numberElement = declensionElement.firstChildElement( KVTML_GRAMMATICAL_NUMBER[num] );
if (!numberElement.isNull()) {
- for ( KEduVocDeclension::DeclensionCase dcase = KEduVocDeclension::Nominative; dcase < KEduVocDeclension::DeclensionCaseMAX; dcase = KEduVocDeclension::DeclensionCase(dcase +1) ) {
+ for ( KEduVocDeclension::DeclensionCase dcase = KEduVocDeclension::Nominative; dcase <= KEduVocDeclension::Vocative; dcase = DeclensionCase(dcase +1) ) {
QDomElement caseElement = numberElement.firstChildElement( KVTML_DECLENSION_CASE[dcase] );
if (!caseElement.isNull()) {
KEduVocText text;
Accusative,
Ablative,
Locative,
- Vocative,
- DeclensionCaseMAX
+ Vocative
};
/**
bool isEmpty();
+ /**
+ * Create xml for this declension
+ * @param parent
+ */
void toKVTML2(QDomElement& parent);
/**
void KEduVocArticle::setArticle(const QString & article, ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender)
{
- kDebug() << article << "#" << number << "def" << definite << "indef" << gender << "index" << indexOf(number, definite, gender);
- d->m_articles[indexOf(number, definite, gender)] = article;
+ setArticle(article, indexOf(number, definite, gender));
+}
+
+void KEduVocArticle::setArticle(const QString & article, int index)
+{
+ d->m_articles[index] = article;
}
int KEduVocArticle::indexOf(ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender)
{
- return number + (definite * NumberMAX) + (gender * NumberMAX * DefinitenessMAX);
+ return number + (definite * (Plural+1)) + (gender * (Plural+1) * (Indefinite+1));
}
bool KEduVocArticle::isArticle(const QString & article) const
return d->m_articles.isEmpty();
}
+
+
enum ArticleNumber {
Singular,
Dual,
- Plural,
- NumberMAX
+ Plural
};
enum ArticleGender {
Masculine,
Feminine,
- Neutral,
- GenderMAX
+ Neutral
};
enum ArticleDefiniteness {
Definite,
- Indefinite,
- DefinitenessMAX
+ Indefinite
};
*/
~KEduVocArticle();
-
/**
* assignment operator for d-pointer copying
*/
void setArticle(const QString& article, ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender);
+ void setArticle(const QString& article, int index);
+
bool isArticle(const QString& article) const;
bool isEmpty();
-private:
- int indexOf(ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender);
+ static int indexOf(ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender);
class Private;
Private * const d;
bool KEduVocKvtml2Reader::readTranslation( QDomElement &translationElement,
KEduVocExpression *expr, int index )
{
+ // read the text, grade, declension and conjugation
expr->translation(index)->fromKVTML2(translationElement);
//<falsefriend fromid="1"></falsefriend>
expr->translation(index)->setFalseFriend( fromid, currentElement.text() );
}
- // conjugations
- currentElement = translationElement.firstChildElement( KVTML_CONJUGATION );
- while ( !currentElement.isNull() ) {
- // read any conjugations (NOTE: this will overwrite any conjugations of the same type for this
- // translation, as the type is used as the key
- QDomElement tenseElement = currentElement.firstChildElement( KVTML_TENSE );
- QString tense = tenseElement.text();
-
- readConjugation( currentElement, expr->translation(index)->conjugation(tense) );
- currentElement = currentElement.nextSiblingElement( KVTML_CONJUGATION );
- }
-
// comparisons
currentElement = translationElement.firstChildElement( KVTML_COMPARISON );
if ( !currentElement.isNull() ) {
bool KEduVocKvtml2Reader::readArticle( QDomElement &articleElement, int identifierNum )
/*
<article>
- <definite>
- <male>der</male>
- <female>die</female>
- <neutral>das</neutral>
- </definite>
- <indefinite>
- <male>ein</male>
- <female>eine</female>
- <neutral>ein</neutral>
- </indefinite>
+ <singlular>
+ <definite>
+ <male>der</male>
+ <female>die</female>
+ <neutral>das</neutral>
+ </definite>
+ <indefinite>
+ <male>ein</male>
+ <female>eine</female>
+ <neutral>ein</neutral>
+ </indefinite>
+ </singular>
+ <dual>
+ </dual>
</article>
*/
{
- QString fem_def = "";
- QString mal_def = "";
- QString nat_def = "";
- QString fem_indef = "";
- QString mal_indef = "";
- QString nat_indef = "";
-
- QDomElement currentElement = articleElement.firstChildElement( KVTML_SINGULAR ).firstChildElement( KVTML_DEFINITE );
- if ( !currentElement.isNull() )
- {
- QDomElement subElement = currentElement.firstChildElement( KVTML_MALE );
- if ( !subElement.isNull() ) {
- mal_def = subElement.text();
- }
-
- subElement = currentElement.firstChildElement( KVTML_FEMALE );
- if ( !subElement.isNull() ) {
- fem_def = subElement.text();
- }
-
- subElement = currentElement.firstChildElement( KVTML_NEUTRAL );
- if ( !subElement.isNull() ) {
- nat_def = subElement.text();
- }
- }
-
- currentElement = articleElement.firstChildElement( KVTML_SINGULAR ).firstChildElement( KVTML_INDEFINITE );
- if ( !currentElement.isNull() )
- {
- QDomElement subElement = currentElement.firstChildElement( KVTML_MALE );
- if ( !subElement.isNull() ) {
- mal_indef = subElement.text();
- }
-
- subElement = currentElement.firstChildElement( KVTML_FEMALE );
- if ( !subElement.isNull() ) {
- fem_indef = subElement.text();
- }
-
- subElement = currentElement.firstChildElement( KVTML_NEUTRAL );
- if ( !subElement.isNull() ) {
- nat_indef = subElement.text();
+ // singular
+ for ( KEduVocArticle::ArticleNumber num = KEduVocArticle::Singular; num <= KEduVocArticle::Plural; num = KEduVocArticle::ArticleNumber(num+1) ) {
+ QDomElement numberElement = articleElement.firstChildElement( KVTML_GRAMMATICAL_NUMBER[num] );
+ if (!numberElement.isNull()) {
+ // definite
+ for ( KEduVocArticle::ArticleDefiniteness def = KEduVocArticle::Definite; def <= KEduVocArticle::Indefinite; def = KEduVocArticle::ArticleDefiniteness(def+1) ) {
+ QDomElement defElement = numberElement.firstChildElement( KVTML_GRAMMATICAL_DEFINITENESS[def] );
+ if (!defElement.isNull()) {
+ // male
+ for ( KEduVocArticle::ArticleGender gen = KEduVocArticle::Masculine; gen <= KEduVocArticle::Neutral; gen = KEduVocArticle::ArticleGender(gen+1) ) {
+ QDomElement genderElement = defElement.firstChildElement( KVTML_GRAMMATICAL_GENDER[gen] );
+ if (!genderElement.isNull()) {
+ m_doc->identifier(identifierNum).article().setArticle( genderElement.text(), KEduVocArticle::indexOf(num, def, gen) );
+ }
+ }
+ }
+ }
}
}
- m_doc->identifier(identifierNum).setArticle( KEduVocArticle( fem_def, fem_indef, mal_def, mal_indef, nat_def, nat_indef ) );
return true;
}
return true;
}
-bool KEduVocKvtml2Reader::readConjugation( QDomElement &conjugElement, KEduVocConjugation &conjugation )
-/*
- <conjugation>
- <tense>Futurepastperfekt:)</tense>
- <singular>
- <firstperson></firstperson>
- <secondperson></secondperson>
- <thirdperson>
- <male></male>
- <female></female>
- <neutral></neutral>
- </thirdperson>
- </singular>
- <plural>
- <firstperson></firstperson>
- <secondperson></secondperson>
- <thirdsperson>
- <common></common>
- </third person>
- </plural>
- </conjugation>
-*/
-{
- QDomElement personElement = conjugElement.firstChildElement( KVTML_SINGULAR );
- if ( !personElement.isNull() )
- {
- readConjugationPerson( personElement, conjugation, KEduVocConjugation::Singular );
- }
-
- personElement = conjugElement.firstChildElement( KVTML_DUAL );
- if ( !personElement.isNull() )
- {
- readConjugationPerson( personElement, conjugation, KEduVocConjugation::Dual );
- }
-
- personElement = conjugElement.firstChildElement( KVTML_PLURAL );
- if ( !personElement.isNull() )
- {
- readConjugationPerson( personElement, conjugation, KEduVocConjugation::Plural );
- }
-
- return true;
-}
-
-
-bool KEduVocKvtml2Reader::readConjugationPerson(QDomElement & personElement, KEduVocConjugation & conjugation, KEduVocConjugation::ConjugationNumber number)
-{
- QDomElement currentElement = personElement.firstChildElement( KVTML_1STPERSON );
- conjugation.setConjugation( currentElement.text(),
- KEduVocConjugation::First, number );
-
- currentElement = personElement.firstChildElement( KVTML_2NDPERSON );
- conjugation.setConjugation( currentElement.text(),
- KEduVocConjugation::Second, number );
-
- currentElement = personElement.firstChildElement( KVTML_THIRD_MALE );
- conjugation.setConjugation( currentElement.text(),
- KEduVocConjugation::ThirdMale, number );
-
- currentElement = personElement.firstChildElement( KVTML_THIRD_FEMALE );
- conjugation.setConjugation( currentElement.text(),
- KEduVocConjugation::ThirdFemale, number );
-
- currentElement = personElement.firstChildElement( KVTML_THIRD_NEUTRAL_COMMON );
- conjugation.setConjugation( currentElement.text(),
- KEduVocConjugation::ThirdNeutralCommon, number );
- return true;
-}
-
bool KEduVocKvtml2Reader::readPersonalPronoun(QDomElement & pronounElement, KEduVocPersonalPronoun & pronoun)
{
pronoun.setDualExists( !pronounElement.firstChildElement(
KVTML_DUAL_EXISTS).isNull() );
- QDomElement personElement = pronounElement.firstChildElement( KVTML_SINGULAR );
+ QDomElement personElement = pronounElement.firstChildElement( KVTML_GRAMMATICAL_NUMBER[0] );
if ( !personElement.isNull() ) {
readPersonalPronounChild( personElement, pronoun, KEduVocConjugation::Singular );
}
- personElement = pronounElement.firstChildElement( KVTML_DUAL );
+ personElement = pronounElement.firstChildElement( KVTML_GRAMMATICAL_NUMBER[1] );
if ( !personElement.isNull() ) {
readPersonalPronounChild( personElement, pronoun, KEduVocConjugation::Dual );
}
- personElement = pronounElement.firstChildElement( KVTML_PLURAL );
+ personElement = pronounElement.firstChildElement( KVTML_GRAMMATICAL_NUMBER[2] );
if ( !personElement.isNull() ) {
readPersonalPronounChild( personElement, pronoun, KEduVocConjugation::Plural );
}
bool KEduVocKvtml2Reader::readPersonalPronounChild(QDomElement & personElement, KEduVocPersonalPronoun & pronoun, KEduVocConjugation::ConjugationNumber number)
{
- QDomElement currentElement = personElement.firstChildElement( KVTML_1STPERSON );
- pronoun.setPersonalPronoun( currentElement.text(),
- KEduVocConjugation::First, number );
-
- currentElement = personElement.firstChildElement( KVTML_2NDPERSON );
- pronoun.setPersonalPronoun( currentElement.text(),
- KEduVocConjugation::Second, number );
-
- currentElement = personElement.firstChildElement( KVTML_THIRD_MALE );
- pronoun.setPersonalPronoun( currentElement.text(),
- KEduVocConjugation::ThirdMale, number );
-
- currentElement = personElement.firstChildElement( KVTML_THIRD_FEMALE );
- pronoun.setPersonalPronoun( currentElement.text(),
- KEduVocConjugation::ThirdFemale, number );
+ for (int person = KEduVocConjugation::First; person <= KEduVocConjugation::ThirdNeutralCommon; person++) {
+ QDomElement currentElement = personElement.firstChildElement( KVTML_GRAMMATICAL_PERSON[person] );
+ pronoun.setPersonalPronoun( currentElement.text(), KEduVocConjugation::ConjugationPerson(person), number );
+ }
- currentElement = personElement.firstChildElement( KVTML_THIRD_NEUTRAL_COMMON );
- pronoun.setPersonalPronoun( currentElement.text(),
- KEduVocConjugation::ThirdNeutralCommon, number );
return true;
}
}
-bool KEduVocKvtml2Writer::writeArticle( QDomElement &articleElement, int article )
+bool KEduVocKvtml2Writer::writeArticle( QDomElement &articleElement, int language )
{
- QDomElement number;
- QString def;
- QString indef;
-
- for( int i= KEduVocArticle::Singular; i <= KEduVocArticle::Plural; ++i)
+ ///@todo only write if not empty
+ for (int num = KEduVocArticle::Singular; num <= KEduVocArticle::Plural; num++)
{
- QDomElement definite = m_domDoc.createElement( KVTML_DEFINITE );
- QDomElement indefinite = m_domDoc.createElement( KVTML_INDEFINITE );
+ QDomElement numberElement = m_domDoc.createElement( KVTML_GRAMMATICAL_NUMBER[num] );
- switch(i) {
- case KEduVocArticle::Singular:
- number = m_domDoc.createElement( KVTML_SINGULAR );
- break;
- case KEduVocArticle::Plural:
- number = m_domDoc.createElement( KVTML_PLURAL);
- break;
- case KEduVocArticle::Dual:
- number = m_domDoc.createElement( KVTML_DUAL );
- break;
- }
+ for (int def = KEduVocArticle::Definite; def <= KEduVocArticle::Indefinite; def++) {
+ QDomElement defElement = m_domDoc.createElement( KVTML_GRAMMATICAL_DEFINITENESS[def] );
- QString articleString;
- articleString = m_doc->identifier(article).article().article( KEduVocArticle::ArticleNumber(i), KEduVocArticle::Definite, KEduVocArticle::Masculine );
- if ( !articleString.isEmpty() ) {
- definite.appendChild( newTextElement( KVTML_MALE, articleString ) );
- }
- articleString = m_doc->identifier(article).article().article(KEduVocArticle::ArticleNumber(i), KEduVocArticle::Indefinite, KEduVocArticle::Masculine );
- if ( !articleString.isEmpty() ) {
- indefinite.appendChild( newTextElement( KVTML_MALE, articleString ) );
- }
-
- // female
- articleString = m_doc->identifier(article).article().article( KEduVocArticle::ArticleNumber(i), KEduVocArticle::Definite, KEduVocArticle::Feminine );
- if ( !articleString.isEmpty() ) {
- definite.appendChild( newTextElement( KVTML_FEMALE, articleString ) );
- }
- articleString = m_doc->identifier(article).article().article( KEduVocArticle::ArticleNumber(i), KEduVocArticle::Indefinite, KEduVocArticle::Feminine );
- if ( !articleString.isEmpty() ) {
- indefinite.appendChild( newTextElement( KVTML_FEMALE, articleString ) );
- }
-
- // neutral
- articleString = m_doc->identifier(article).article().article( KEduVocArticle::ArticleNumber(i), KEduVocArticle::Definite, KEduVocArticle::Neutral );
- if ( !articleString.isEmpty() ) {
- definite.appendChild( newTextElement( KVTML_NEUTRAL, articleString ) );
- }
- articleString = m_doc->identifier(article).article().article( KEduVocArticle::ArticleNumber(i), KEduVocArticle::Indefinite, KEduVocArticle::Neutral );
- if ( !articleString.isEmpty() ) {
- indefinite.appendChild( newTextElement( KVTML_NEUTRAL, articleString ) );
- }
-
-
- if ( definite.hasChildNodes() ) {
- number.appendChild( definite );
- }
- if ( indefinite.hasChildNodes() ) {
- number.appendChild( indefinite );
+ for (int gen = KEduVocArticle::Masculine; gen <= KEduVocArticle::Neutral; gen++)
+ {
+ QString articleString = m_doc->identifier(language).article().article( KEduVocArticle::ArticleNumber(num), KEduVocArticle::ArticleDefiniteness(def), KEduVocArticle::ArticleGender(gen) );
+ if ( !articleString.isEmpty() ) {
+ defElement.appendChild( newTextElement( KVTML_GRAMMATICAL_GENDER[gen], articleString ) );
+ }
+ }
+ if ( defElement.hasChildNodes() ) {
+ numberElement.appendChild( defElement );
+ }
}
- if ( number.hasChildNodes() ) {
- articleElement.appendChild( number );
+ if ( numberElement.hasChildNodes() ) {
+ articleElement.appendChild( numberElement );
}
}
return true;
}
}
- // conjugation
- foreach ( const QString &tense, translation->conjugationTenses() ) {
- QDomElement thisElement = m_domDoc.createElement( KVTML_CONJUGATION );
- writeConjugation( thisElement, translation->conjugation(tense), tense );
- translationElement.appendChild( thisElement );
- }
-
// comparison
if ( !(translation->comparative().isEmpty() || translation->comparative().isEmpty())) {
QDomElement comparisonElement = m_domDoc.createElement( KVTML_COMPARISON );
return true;
}
-bool KEduVocKvtml2Writer::writeConjugation( QDomElement &conjugationElement,
- const KEduVocConjugation &conjugation, const QString &tense )
-{
- // write the tense tag
- conjugationElement.appendChild( newTextElement(KVTML_TENSE, tense) );
-
- for ( KEduVocConjugation::ConjugationNumber num = KEduVocConjugation::Singular; num < KEduVocConjugation::NumberMAX; num = KEduVocConjugation::ConjugationNumber(num +1) ) {
- QString first = conjugation.conjugation(
- KEduVocConjugation::First, num );
- QString second = conjugation.conjugation(
- KEduVocConjugation::Second, num );
- QString third_male = conjugation.conjugation(
- KEduVocConjugation::ThirdMale, num );
- QString third_female = conjugation.conjugation(
- KEduVocConjugation::ThirdFemale, num );
- QString third_neutral = conjugation.conjugation(
- KEduVocConjugation::ThirdNeutralCommon, num );
-
- if ( !first.isEmpty() || !second.isEmpty() || !third_female.isEmpty() ||
- !third_male.isEmpty() || !third_neutral.isEmpty() ) {
- QDomElement number;
- switch (num) {
- case KEduVocConjugation::Singular:
- number = m_domDoc.createElement( KVTML_SINGULAR );
- break;
- case KEduVocConjugation::Dual:
- number = m_domDoc.createElement( KVTML_DUAL );
- break;
- case KEduVocConjugation::Plural:
- number = m_domDoc.createElement( KVTML_PLURAL );
- break;
- }
-
- number.appendChild( newTextElement( KVTML_1STPERSON, first ) );
- number.appendChild( newTextElement( KVTML_2NDPERSON, second ) );
- number.appendChild( newTextElement( KVTML_THIRD_MALE, third_male ) );
- number.appendChild( newTextElement( KVTML_THIRD_FEMALE, third_female ) );
- number.appendChild( newTextElement( KVTML_THIRD_NEUTRAL_COMMON, third_neutral ) );
-
- conjugationElement.appendChild( number );
- }
- }
-
- return true;
-}
-
QDomElement KEduVocKvtml2Writer::newTextElement( const QString &elementName, const QString &text )
{
+ kDebug() << "append: " << elementName << text;
QDomElement retval = m_domDoc.createElement( elementName );
QDomText textNode = m_domDoc.createTextNode( text );
retval.appendChild( textNode );
pronounElement.appendChild( m_domDoc.createElement( KVTML_DUAL_EXISTS ) );
}
- for ( KEduVocConjugation::ConjugationNumber num = KEduVocConjugation::Singular; num < KEduVocConjugation::NumberMAX; num = KEduVocConjugation::ConjugationNumber(num +1) ) {
- QString first = pronoun.personalPronoun(
- KEduVocConjugation::First, num );
- QString second = pronoun.personalPronoun(
- KEduVocConjugation::Second, num );
- QString third_male = pronoun.personalPronoun(
- KEduVocConjugation::ThirdMale, num );
- QString third_female = pronoun.personalPronoun(
- KEduVocConjugation::ThirdFemale, num );
- QString third_neutral = pronoun.personalPronoun(
- KEduVocConjugation::ThirdNeutralCommon, num );
-
- if ( !first.isEmpty() || !second.isEmpty() || !third_female.isEmpty() ||
- !third_male.isEmpty() || !third_neutral.isEmpty() ) {
- QDomElement number;
- switch (num) {
- case KEduVocConjugation::Singular:
- number = m_domDoc.createElement( KVTML_SINGULAR );
- break;
- case KEduVocConjugation::Dual:
- number = m_domDoc.createElement( KVTML_DUAL );
- break;
- case KEduVocConjugation::Plural:
- number = m_domDoc.createElement( KVTML_PLURAL );
- break;
- }
-
- number.appendChild( newTextElement( KVTML_1STPERSON, first ) );
- number.appendChild( newTextElement( KVTML_2NDPERSON, second ) );
- number.appendChild( newTextElement( KVTML_THIRD_MALE, third_male ) );
- number.appendChild( newTextElement( KVTML_THIRD_FEMALE, third_female ) );
- number.appendChild( newTextElement( KVTML_THIRD_NEUTRAL_COMMON, third_neutral ) );
+ // the actual pronouns
+ for ( int num = KEduVocConjugation::Singular; num <= KEduVocConjugation::Plural; num++ ) {
+ QDomElement numberElement = m_domDoc.createElement( KVTML_GRAMMATICAL_NUMBER[num] );
+ for ( int person = KEduVocConjugation::First; person <= KEduVocConjugation::ThirdNeutralCommon; person++ ) {
- if ( pronoun.maleFemaleDifferent() ) {
- number.appendChild( m_domDoc.createElement( KVTML_THIRD_PERSON_MALE_FEMALE_DIFFERENT ) );
- }
- if ( pronoun.neutralExists() ) {
- number.appendChild( m_domDoc.createElement( KVTML_THIRD_PERSON_NEUTRAL_EXISTS ) );
- }
- pronounElement.appendChild( number );
+ numberElement.appendChild( newTextElement( KVTML_GRAMMATICAL_PERSON[person], pronoun.personalPronoun(KEduVocConjugation::ConjugationPerson(person), KEduVocConjugation::ConjugationNumber(num))) );
}
+ pronounElement.appendChild( numberElement );
}
return true;
}
*/
bool writeArticle( QDomElement &articleElement, int article );
- /** write conjugation
- * @param conjugationElement QDomElement conjugation or personalpronouns to write to
- * @param conjugation object to write
- * @param type conjugation type
- */
- bool writeConjugation( QDomElement &conjugationElement, const KEduVocConjugation &conjugation,
- const QString &tense );
-
bool writePersonalPronoun( QDomElement &pronounElement, const KEduVocPersonalPronoun &pronoun);
/** write types
* @param typesElement QDomElement types to write to
int KEduVocPersonalPronoun::indexOf(KEduVocConjugation::ConjugationPerson person, KEduVocConjugation::ConjugationNumber number) const
{
- return person + KEduVocConjugation::PersonMAX * number;
+ return person + (KEduVocConjugation::ThirdNeutralCommon+1) * number;
}
bool KEduVocPersonalPronoun::maleFemaleDifferent() const
}
}
}
+
+bool KEduVocText::isEmpty()
+{
+ return d->m_text.isEmpty();
+}
*/
void setPracticeDate( const QDateTime & date );
+ /**
+ * If the string inside is empty this returns true.
+ * @return
+ */
+ bool isEmpty();
+
void fromKVTML2(QDomElement& parent);
void toKVTML2(QDomElement& parent);
void KEduVocTranslation::toKVTML2(QDomElement & parent)
{
+ // text and grade
KEduVocText::toKVTML2(parent);
+
+ // declension
if (d->m_declension) {
d->m_declension->toKVTML2(parent);
}
+ // conjugation
+ QDomElement conjugationElement = parent.ownerDocument().createElement( KVTML_CONJUGATION );
+ foreach ( const QString &tense, conjugationTenses() ) {
+ conjugation(tense).toKVTML2(conjugationElement, tense);
+ }
+ if (conjugationElement.hasChildNodes()) {
+ parent.appendChild( conjugationElement );
+ }
+
// <comment>
KEduVocKvtml2Writer::appendTextElement( parent, KVTML_COMMENT, comment() );
//<paraphrase></paraphrase>
setParaphrase( parent.firstChildElement( KVTML_PARAPHRASE ).text() );
+
+ // conjugations
+ QDomElement conjugationElement = parent.firstChildElement( KVTML_CONJUGATION );
+ while ( !conjugationElement.isNull() ) {
+ QDomElement tenseElement = conjugationElement.firstChildElement( KVTML_TENSE );
+ QString tense = tenseElement.text();
+ KEduVocConjugation *conjugation = KEduVocConjugation::fromKVTML2(conjugationElement);
+ setConjugation(tense, *conjugation);
+ delete conjugation;
+ conjugationElement = conjugationElement.nextSiblingElement( KVTML_CONJUGATION );
+ }
+
}
// articles
#define KVTML_ARTICLE "article"
-#define KVTML_DEFINITE "definite"
-#define KVTML_INDEFINITE "indefinite"
-// declension
+// conjugation, declension and personal pronouns
+#define KVTML_CONJUGATION "conjugation"
+#define KVTML_CONJUGATIONTYPE "conjugationtype"
+#define KVTML_PERSONALPRONOUNS "personalpronouns"
#define KVTML_DECLENSION "declension"
+
static const QString KVTML_DECLENSION_CASE[] = {
"nominative",
"genitive",
"accusative",
"ablative",
"locative",
- "vocative" };
+ "vocative"
+};
static const QString KVTML_GRAMMATICAL_NUMBER[] = {
"singular",
- "dual",
- "plural" };
-
-// conjugation and personal pronouns
-#define KVTML_CONJUGATION "conjugation"
-#define KVTML_CONJUGATIONTYPE "conjugationtype"
-#define KVTML_PERSONALPRONOUNS "personalpronouns"
+ "dual", // dual is the case where there's a special form for exactly two
+ "plural"
+};
+
+static const QString KVTML_GRAMMATICAL_GENDER[] = {
+ "male",
+ "female",
+ "neutral"
+};
+
+static const QString KVTML_GRAMMATICAL_PERSON[] = {
+ "firstperson",
+ "secondperson",
+ "thirdpersonmale",
+ "thirdpersonfemale",
+ "thirdpersonneutralcommon"
+};
+
+static const QString KVTML_GRAMMATICAL_DEFINITENESS[] = {
+ "definite",
+ "indefinite"
+};
-#define KVTML_MALE "male"
-#define KVTML_FEMALE "female"
-#define KVTML_NEUTRAL "neutral"
-#define KVTML_SINGULAR "singular"
-// dual is the case where there's a special form for exactly two
-#define KVTML_DUAL "dual"
-#define KVTML_PLURAL "plural"
-#define KVTML_1STPERSON "firstperson"
-#define KVTML_2NDPERSON "secondperson"
-#define KVTML_THIRD_MALE "thirdpersonmale"
-#define KVTML_THIRD_FEMALE "thirdpersonfemale"
-#define KVTML_THIRD_NEUTRAL_COMMON "thirdpersonneutralcommon"
// for the personal pronuns:
// if this tag exists, in a conjugation male/female are different
#include <qobject.h>
#include <qvalidator.h>
+#include <QtXml/QDomDocument>
class KEduVocDocumentValidatorTest
: public QObject
void testLessons();
void testWordTypes();
void testTranslations();
+ void testConjugations();
void testDeclensions();
};
QCOMPARE(translation.declension(), declension);
}
+void KEduVocDocumentValidatorTest::testConjugations()
+{
+ KEduVocConjugation conjugation;
+ conjugation.setConjugation(KEduVocText("first-singular"), KEduVocConjugation::First, KEduVocConjugation::Singular);
+ QCOMPARE(conjugation.conjugation(KEduVocConjugation::First, KEduVocConjugation::Singular).text(), QString("first-singular"));
+
+ QDomDocument doc = QDomDocument("test doc");
+ QDomElement root = doc.createElement( "kvtml" );
+ doc.appendChild(root);
+ conjugation.toKVTML2(root, "tense");
+
+ qDebug() << root.text();
+
+ KEduVocConjugation *con2 = KEduVocConjugation::fromKVTML2(root);
+
+ QCOMPARE(conjugation.conjugation(KEduVocConjugation::First, KEduVocConjugation::Singular).text(), con2->conjugation(KEduVocConjugation::First, KEduVocConjugation::Singular).text());
+ delete con2;
+}
QTEST_KDEMAIN_CORE( KEduVocDocumentValidatorTest )