File indexing completed on 2022-10-04 13:56:33

0001 /*
0002  * C++ Implementation: keduvocdeclension
0003  * SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "keduvocdeclension.h"
0007 
0008 #include "kvtml2defs.h"
0009 #include "keduvockvtml2writer.h"
0010 #include <QDomDocument>
0011 
0012 #include <QMap>
0013 
0014 class KEduVocDeclension::Private
0015 {
0016 public:
0017     QMap<KEduVocWordFlags, KEduVocText> m_declensions;
0018 };
0019 
0020 KEduVocDeclension::KEduVocDeclension()
0021     :d (new Private)
0022 {
0023 }
0024 
0025 KEduVocDeclension::KEduVocDeclension(const KEduVocDeclension & other)
0026     :d (new Private)
0027 {
0028     d->m_declensions = other.d->m_declensions;
0029 }
0030 
0031 KEduVocDeclension & KEduVocDeclension::operator =(const KEduVocDeclension & other)
0032 {
0033     d->m_declensions = other.d->m_declensions;
0034     return *this;
0035 }
0036 
0037 KEduVocDeclension::~KEduVocDeclension()
0038 {
0039     delete d;
0040 }
0041 
0042 KEduVocText& KEduVocDeclension::declension(KEduVocWordFlags flags)
0043 {
0044     return d->m_declensions[flags];
0045 }
0046 
0047 void KEduVocDeclension::setDeclension(const KEduVocText & declension, KEduVocWordFlags flags)
0048 {
0049     d->m_declensions[flags] = declension;
0050 }
0051 
0052 bool KEduVocDeclension::isEmpty()
0053 {
0054     return d->m_declensions.isEmpty();
0055 }
0056 
0057 void KEduVocDeclension::toKVTML2(QDomElement & parent)
0058 {
0059     if (isEmpty()) {
0060         return;
0061     }
0062     QDomDocument domDoc = parent.ownerDocument();
0063     QDomElement declensionElement = domDoc.createElement( KVTML_DECLENSION );
0064 
0065     QMap<int, KEduVocWordFlags> genders;
0066     genders[0] = KEduVocWordFlag::Masculine;
0067     genders[1] = KEduVocWordFlag::Feminine;
0068     genders[2] = KEduVocWordFlag::Neuter;
0069 
0070     QMap<int, KEduVocWordFlags> numbers;
0071     numbers[0] = KEduVocWordFlag::Singular;
0072     numbers[1] = KEduVocWordFlag::Dual;
0073     numbers[2] = KEduVocWordFlag::Plural;
0074 
0075     QMap<int, KEduVocWordFlags> cases;
0076     cases[0] = KEduVocWordFlag::Nominative;
0077     cases[1] = KEduVocWordFlag::Genitive;
0078     cases[2] = KEduVocWordFlag::Dative;
0079     cases[3] = KEduVocWordFlag::Accusative;
0080     cases[4] = KEduVocWordFlag::Ablative;
0081     cases[5] = KEduVocWordFlag::Locative;
0082     cases[6] = KEduVocWordFlag::Vocative;
0083 
0084 
0085     for ( int gen = 0; gen <= 2; ++gen)
0086     {
0087         QDomElement genderElement = domDoc.createElement( KVTML_GRAMMATICAL_GENDER[gen] );
0088         for ( int num = 0; num <= 2; ++num) {
0089             QDomElement numberElement = domDoc.createElement( KVTML_GRAMMATICAL_NUMBER[num] );
0090             for ( int dcase = 0; dcase <= 6; ++dcase ) {
0091                 QDomElement caseElement = domDoc.createElement( KVTML_DECLENSION_CASE[dcase] );
0092                 declension(genders[gen] | numbers[num] | cases[dcase]).toKVTML2(caseElement);
0093 
0094                 if (caseElement.hasChildNodes()) {
0095                     numberElement.appendChild(caseElement);
0096                 }
0097             }
0098             if (numberElement.hasChildNodes()) {
0099                 genderElement.appendChild(numberElement);
0100             }
0101         }
0102         if (genderElement.hasChildNodes()) {
0103             declensionElement.appendChild(genderElement);
0104         }
0105     }
0106     if (declensionElement.hasChildNodes()) {
0107         parent.appendChild(declensionElement);
0108     }
0109 }
0110 
0111 KEduVocDeclension* KEduVocDeclension::fromKVTML2(QDomElement & parent)
0112 {
0113     QDomElement declensionElement = parent.firstChildElement( KVTML_DECLENSION );
0114     // we don't create empty objects, if necessary, create later on demand.
0115     if (declensionElement.isNull()) {
0116         return 0;
0117     }
0118 
0119 
0120     QMap<int, KEduVocWordFlags> genders;
0121     genders[0] = KEduVocWordFlag::Masculine;
0122     genders[1] = KEduVocWordFlag::Feminine;
0123     genders[2] = KEduVocWordFlag::Neuter;
0124 
0125     QMap<int, KEduVocWordFlags> numbers;
0126     numbers[0] = KEduVocWordFlag::Singular;
0127     numbers[1] = KEduVocWordFlag::Dual;
0128     numbers[2] = KEduVocWordFlag::Plural;
0129 
0130     QMap<int, KEduVocWordFlags> cases;
0131     cases[0] = KEduVocWordFlag::Nominative;
0132     cases[1] = KEduVocWordFlag::Genitive;
0133     cases[2] = KEduVocWordFlag::Dative;
0134     cases[3] = KEduVocWordFlag::Accusative;
0135     cases[4] = KEduVocWordFlag::Ablative;
0136     cases[5] = KEduVocWordFlag::Locative;
0137     cases[6] = KEduVocWordFlag::Vocative;
0138 
0139 
0140     KEduVocDeclension* declension = new KEduVocDeclension;
0141 
0142     for ( int gen = 0; gen <= 2; ++gen ) {
0143         QDomElement genderElement = declensionElement.firstChildElement( KVTML_GRAMMATICAL_GENDER[gen] );
0144         if (!genderElement.isNull())
0145         {
0146             for ( int num = 0; num <= 2; ++num ) {
0147                 QDomElement numberElement = genderElement.firstChildElement( KVTML_GRAMMATICAL_NUMBER[num] );
0148                 if (!numberElement.isNull()) {
0149                     for ( int dcase = 0; dcase <= 6; ++dcase) {
0150                         QDomElement caseElement = numberElement.firstChildElement( KVTML_DECLENSION_CASE[dcase] );
0151                         if (!caseElement.isNull()) {
0152                             KEduVocText text;
0153                             text.fromKVTML2(caseElement);
0154                             declension->setDeclension(text, genders[gen] | numbers[num] | cases[dcase]);
0155                         }
0156                     }
0157                 }
0158             }
0159         }
0160     }
0161     return declension;
0162 }
0163 
0164 
0165