]> Git trees. - libqmvoc.git/commitdiff
Cleanup. Work on the kvtml reader.
authorPeter Hedlund <peter@peterandlinda.com>
Thu, 22 Feb 2007 04:53:45 +0000 (04:53 +0000)
committerPeter Hedlund <peter@peterandlinda.com>
Thu, 22 Feb 2007 04:53:45 +0000 (04:53 +0000)
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=636141

kdeeducore/keduvocdocument.cpp
kdeeducore/keduvocdocument.h
kdeeducore/keduvockvtmlreader.cpp
kdeeducore/keduvockvtmlreader.h

index dd96fd4de95ba6ce31d2bf39a0cea32cbdbeb597..bda9ebc7f4eb2d5e112f49f69f7ad8d6cb0b25d2 100644 (file)
@@ -89,11 +89,6 @@ void KEduVocDocument::setVersion (const QString & vers)
 }
 
 
-void KEduVocDocument::version(int &, int &, int &)
-{
-}
-
-
 void KEduVocDocument::Init ()
 {
   m_lessonDescriptions.clear();
@@ -260,6 +255,10 @@ bool KEduVocDocument::open(const KUrl& url, bool /*append*/)
         if (result == KMessageBox::Cancel) {
           Init();
           return false;
+        } else {
+          Init();
+          if (!url.isEmpty())
+            m_url = url;
         }
       }
     }
index 3e15fe9e1700f12bba83ba9b7b52e0fdf788c8c4..f0fa75a5a3c6f87c2441ea3aa90fc1d035b6989c 100644 (file)
@@ -629,10 +629,20 @@ public:
    */
   void setFont(QFont *font);
 
+  /**
+   * Sets the generator of the file
+   */
+  inline void setGenerator(const QString & generator) { m_generator = generator; }
+
+  /**
+   * Gets the generator of the file
+   */
+  inline QString generator() const { return m_generator; }
+
   /**
    * Gets the version of the loaded file
    */
-  void version(int &major, int &minor, int &patch);
+  inline QString version() const { return m_version; }
 
   /**
    * @returns the current lesson index
@@ -719,15 +729,6 @@ public:
   void setLeitnerSystem( LeitnerSystem* system );
   LeitnerSystem* leitnerSystem();
 
-  bool unknownAttribute(int line, const QString &name, const QString &attr);
-  void unknownElement(int line, const QString &elem );
-  void errorKvtMl(int line, const QString &text );
-  void warningKvtMl(int line, const QString &text );
-
-  void errorLex(int line, const QString &text );
-
-  void errorCsv(int line, const QString &text );
-
   FileType detectFileType(const QString &fileName);
 
 signals:
@@ -753,8 +754,6 @@ private:
 
   // save these to document
   QStringList               m_identifiers;      //0= origin, 1,.. translations
-  int                       m_cols;
-  int                       m_lines;
   int                       m_currentLesson;
   QList<int>                m_extraSizeHints;
   QList<int>                m_sizeHints;
index e7a01800cc13ef3ec20bb7a20f5cadbaa0580cf8..beea81a47cd09b819c24479d44a9f4fb0a689bfe 100644 (file)
@@ -1,10 +1,10 @@
 /***************************************************************************
                      read a KEduVocDocument from a KVTML file
     -----------------------------------------------------------------------
-    copyright            : (C) 1999-2001 Ewald Arnold
-                           (C) 2001 The KDE-EDU team
-                           (C) 2005 Eric Pignet
-    email                : eric at erixpage.com
+    copyright           : (C) 1999-2001 Ewald Arnold
+                          (C) 2001 The KDE-EDU team
+                          (C) 2005 Eric Pignet <eric at erixpage.com>
+                          (C) 2007 Peter Hedlund <peter.hedlund@kdemail.net>
  ***************************************************************************/
 
 /***************************************************************************
@@ -38,19 +38,18 @@ KEduVocKvtmlReader::KEduVocKvtmlReader(QIODevice *file)
 bool KEduVocKvtmlReader::readDoc(KEduVocDocument *doc)
 {
   m_doc = doc;
-  m_doc->m_cols = 0;
-  m_doc->m_lines = 0;
+  m_cols = 0;
+  m_lines = 0;
 
-  QDomDocument domDoc("Kvtml");
+  QDomDocument domDoc("KEduVocDocument");
 
   if (!domDoc.setContent(m_inputFile, &m_errorMessage))
     return false;
 
   QDomElement domElementKvtml = domDoc.documentElement();
-  if( domElementKvtml.tagName() != KV_DOCTYPE )
+  if (domElementKvtml.tagName() != KV_DOCTYPE)
   {
-    domError(i18n("Tag <%1> was expected "
-            "but tag <%2> was read." , QString(KV_DOCTYPE), domElementKvtml.tagName()));
+    m_errorMessage = i18n("Tag <%1> was expected but tag <%2> was found.", QString(KV_DOCTYPE), domElementKvtml.tagName());
     return false;
   }
 
@@ -58,61 +57,46 @@ bool KEduVocKvtmlReader::readDoc(KEduVocDocument *doc)
   // Attributes
   //-------------------------------------------------------------------------
 
-  QDomAttr domAttrEncoding = domElementKvtml.attributeNode(KV_ENCODING);
-  if (!domAttrEncoding.isNull())
+  QDomAttr documentAttribute;
+  documentAttribute = domElementKvtml.attributeNode(KV_ENCODING);
+  if (!documentAttribute.isNull())
   {
     // TODO handle old encodings
     // Qt DOM API autodetects encoding, so is there anything to do ?
   }
 
-  QDomAttr domAttrTitle = domElementKvtml.attributeNode(KV_TITLE);
-  if (!domAttrTitle.isNull())
-  {
-    m_doc->m_title = domAttrTitle.value();
-  }
+  documentAttribute = domElementKvtml.attributeNode(KV_TITLE);
+  if (!documentAttribute.isNull())
+    m_doc->setTitle(documentAttribute.value());
 
-  QDomAttr domAttrAuthor = domElementKvtml.attributeNode(KV_AUTHOR);
-  if (!domAttrAuthor.isNull())
-  {
-    m_doc->m_author = domAttrAuthor.value();
-  }
+  documentAttribute = domElementKvtml.attributeNode(KV_AUTHOR);
+  if (!documentAttribute.isNull())
+    m_doc->setAuthor(documentAttribute.value());
 
-  QDomAttr domAttrLicence = domElementKvtml.attributeNode(KV_LICENSE);
-  if (!domAttrLicence.isNull())
-  {
-    m_doc->m_license = domAttrLicence.value();
-  }
+  documentAttribute = domElementKvtml.attributeNode(KV_LICENSE);
+  if (!documentAttribute.isNull())
+    m_doc->setLicense(documentAttribute.value());
 
-  QDomAttr domAttrRemark = domElementKvtml.attributeNode(KV_DOC_REM);
-  if (!domAttrRemark.isNull())
-  {
-    m_doc->m_remark = domAttrRemark.value();
-  }
+  documentAttribute = domElementKvtml.attributeNode(KV_DOC_REM);
+  if (!documentAttribute.isNull())
+    m_doc->setDocRemark(documentAttribute.value());
 
-  QDomAttr domAttrGenerator = domElementKvtml.attributeNode(KV_GENERATOR);
-  if (!domAttrGenerator.isNull())
+  documentAttribute = domElementKvtml.attributeNode(KV_GENERATOR);
+  if (!documentAttribute.isNull())
   {
-    m_doc->m_generator = domAttrGenerator.value();
-    int pos = m_doc->m_generator.lastIndexOf(KVD_VERS_PREFIX);
+    m_doc->setGenerator(documentAttribute.value());
+    int pos = m_doc->generator().lastIndexOf(KVD_VERS_PREFIX);
     if (pos >= 0)
-    {
-      m_doc->m_version = m_doc->m_generator;
-      m_doc->m_version.remove (0, pos+2);
-    }
+      m_doc->setVersion(m_doc->generator().remove(0, pos + 2));
   }
 
-  QDomAttr domAttrCols = domElementKvtml.attributeNode(KV_COLS);
-  if (!domAttrCols.isNull())
-  {
-    m_doc->m_cols = domAttrCols.value().toInt();
-  }
-
-  QDomAttr domAttrLines = domElementKvtml.attributeNode(KV_LINES);
-  if (!domAttrLines.isNull())
-  {
-    m_doc->m_lines = domAttrLines.value().toInt();
-  }
+  documentAttribute = domElementKvtml.attributeNode(KV_COLS);
+  if (!documentAttribute.isNull())
+    m_cols = documentAttribute.value().toInt(); ///currently not used anywhere
 
+  documentAttribute = domElementKvtml.attributeNode(KV_LINES);
+  if (!documentAttribute.isNull())
+    m_lines = documentAttribute.value().toInt();
 
   //-------------------------------------------------------------------------
   // Children
@@ -120,11 +104,150 @@ bool KEduVocKvtmlReader::readDoc(KEduVocDocument *doc)
 
   bool result = readBody(domElementKvtml);  // read vocabulary
 
-  // TODO EPT setModified (false);
   return result;
 }
 
 
+bool KEduVocKvtmlReader::readBody(QDomElement &domElementParent)
+{
+  bool lessgroup = false;
+  bool optgroup = false;
+  bool attrgroup = false;
+  bool tensegroup = false;
+  bool usagegroup = false;
+  bool articlegroup = false;
+  bool conjuggroup = false;
+
+  int ent_no = 0;
+  int ent_percent = (int) m_lines / 100;
+  float f_ent_percent = (int) m_lines / 100.0;
+/* TODO EPT
+if (lines != 0)
+    emit progressChanged(this, 0);
+*/
+  QDomElement currentElement;
+
+  currentElement = domElementParent.firstChildElement(KV_LESS_GRP);
+  if (!currentElement.isNull()) {
+    lessgroup = readLesson(currentElement);
+    if (!lessgroup)
+      return false;
+  }
+
+  QDomElement domElementChild = domElementParent.firstChild().toElement();
+
+  while (!domElementChild.isNull())
+  {
+    if (domElementChild.tagName() == KV_LESS_GRP)
+    {
+      /*if (lessgroup)
+      {
+        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
+        return false;
+      }
+      lessgroup = true;
+      if (!readLesson(domElementChild))
+        return false;*/
+    }
+
+    else if (domElementChild.tagName() == KV_ARTICLE_GRP)
+    {
+      if (articlegroup)
+      {
+        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
+        return false;
+      }
+      articlegroup = true;
+      if (!readArticle(domElementChild))
+        return false;
+    }
+
+    else if (domElementChild.tagName() == KV_CONJUG_GRP)
+    {
+      if (conjuggroup)
+      {
+        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
+        return false;
+      }
+      conjuggroup = true;
+      if (!readConjug(domElementChild, m_doc->m_conjugations, KV_CON_ENTRY))
+        return false;
+    }
+
+    else if (domElementChild.tagName() == KV_OPTION_GRP)
+    {
+      if (optgroup)
+      {
+        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
+        return false;
+      }
+      optgroup = true;
+      if (!readOptions(domElementChild))
+        return false;
+    }
+
+    else if (domElementChild.tagName() == KV_TYPE_GRP)
+    {
+      if (attrgroup)
+      {
+        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
+        return false;
+      }
+      attrgroup = true;
+      if (!readType(domElementChild))
+        return false;
+    }
+
+    else if (domElementChild.tagName() == KV_TENSE_GRP)
+    {
+      if (tensegroup)
+      {
+        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
+        return false;
+      }
+      tensegroup = true;
+      if (!readTense(domElementChild))
+        return false;
+    }
+
+    else if (domElementChild.tagName() == KV_USAGE_GRP)
+    {
+      if (usagegroup)
+      {
+        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
+        return false;
+      }
+      usagegroup = true;
+      if (!readUsage(domElementChild))
+        return false;
+    }
+
+    else if (domElementChild.tagName() == KV_EXPR)
+    {
+      /* TODO EPT
+      if (lines != 0)
+      {
+        ent_no++;
+        if (ent_percent != 0 && (ent_no % ent_percent) == 0 )
+          emit progressChanged(this, int(ent_no / f_ent_percent));
+      }*/
+      if (!readExpression(domElementChild))
+        return false;
+    }
+
+    else
+    {
+      domErrorUnknownElement(domElementChild.tagName());
+      return false;
+    }
+
+    domElementChild = domElementChild.nextSibling().toElement();
+  }
+
+  return true;
+}
+
+
 bool KEduVocKvtmlReader::readLesson(QDomElement &domElementParent)
 {
   QString s;
@@ -1508,139 +1631,6 @@ bool KEduVocKvtmlReader::readExpression(QDomElement &domElementParent)
   return true;
 }
 
-
-bool KEduVocKvtmlReader::readBody(QDomElement &domElementParent)
-{
-  bool lessgroup = false;
-  bool optgroup = false;
-  bool attrgroup = false;
-  bool tensegroup = false;
-  bool usagegroup = false;
-  bool articlegroup = false;
-  bool conjuggroup = false;
-
-  int ent_no = 0;
-  int ent_percent = (int) m_doc->m_lines / 100;
-  float f_ent_percent = (int) m_doc->m_lines / 100.0;
-/* TODO EPT
-if (lines != 0)
-    emit progressChanged(this, 0);
-*/
-
-  QDomElement domElementChild = domElementParent.firstChild().toElement();
-
-  while (!domElementChild.isNull())
-  {
-    if (domElementChild.tagName() == KV_LESS_GRP)
-    {
-      if (lessgroup)
-      {
-        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
-        return false;
-      }
-      lessgroup = true;
-      if (!readLesson(domElementChild))
-        return false;
-    }
-
-    else if (domElementChild.tagName() == KV_ARTICLE_GRP)
-    {
-      if (articlegroup)
-      {
-        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
-        return false;
-      }
-      articlegroup = true;
-      if (!readArticle(domElementChild))
-        return false;
-    }
-
-    else if (domElementChild.tagName() == KV_CONJUG_GRP)
-    {
-      if (conjuggroup)
-      {
-        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
-        return false;
-      }
-      conjuggroup = true;
-      if (!readConjug(domElementChild, m_doc->m_conjugations, KV_CON_ENTRY))
-        return false;
-    }
-
-    else if (domElementChild.tagName() == KV_OPTION_GRP)
-    {
-      if (optgroup)
-      {
-        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
-        return false;
-      }
-      optgroup = true;
-      if (!readOptions(domElementChild))
-        return false;
-    }
-
-    else if (domElementChild.tagName() == KV_TYPE_GRP)
-    {
-      if (attrgroup)
-      {
-        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
-        return false;
-      }
-      attrgroup = true;
-      if (!readType(domElementChild))
-        return false;
-    }
-
-    else if (domElementChild.tagName() == KV_TENSE_GRP)
-    {
-      if (tensegroup)
-      {
-        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
-        return false;
-      }
-      tensegroup = true;
-      if (!readTense(domElementChild))
-        return false;
-    }
-
-    else if (domElementChild.tagName() == KV_USAGE_GRP)
-    {
-      if (usagegroup)
-      {
-        domError(i18n("repeated occurrence of tag <%1>", domElementChild.tagName()));
-        return false;
-      }
-      usagegroup = true;
-      if (!readUsage(domElementChild))
-        return false;
-    }
-
-    else if (domElementChild.tagName() == KV_EXPR)
-    {
-      /* TODO EPT
-      if (lines != 0)
-      {
-        ent_no++;
-        if (ent_percent != 0 && (ent_no % ent_percent) == 0 )
-          emit progressChanged(this, int(ent_no / f_ent_percent));
-      }*/
-      if (!readExpression(domElementChild))
-        return false;
-    }
-
-    else
-    {
-      domErrorUnknownElement(domElementChild.tagName());
-      return false;
-    }
-
-    domElementChild = domElementChild.nextSibling().toElement();
-  }
-
-  return true;
-}
-
-
 void KEduVocKvtmlReader::domErrorUnknownElement(const QString &elem)
 {
   QString ln = i18n("File:\t%1\n", m_doc->URL().path());
index 618968a63cea5fead9c623d54aa7473735e872b2..94d7642b07366c73bae964ce8e2fe958c6a00ee3 100644 (file)
@@ -1,10 +1,10 @@
 /***************************************************************************
                      read a KEduVocDocument from a KVTML file
     -----------------------------------------------------------------------
-    copyright            : (C) 1999-2001 Ewald Arnold
-                           (C) 2001 The KDE-EDU team
-                           (C) 2005 Eric Pignet
-    email                : eric at erixpage.com
+    copyright           : (C) 1999-2001 Ewald Arnold
+                          (C) 2001 The KDE-EDU team
+                          (C) 2005 Eric Pignet <eric at erixpage.com>
+                          (C) 2007 Peter Hedlund <peter.hedlund@kdemail.net>
  ***************************************************************************/
 
 /***************************************************************************
@@ -117,6 +117,8 @@ private:
   QIODevice *m_inputFile;
   KEduVocDocument *m_doc;
   QString m_errorMessage;
+  int m_cols;
+  int m_lines;
 };
 
 #endif