summaryrefslogtreecommitdiffstats
path: root/kmouth/phrasebook/phrasebookparser.cpp
blob: 9883e493b5d47be59326dd427610dffff7246d83 (plain)
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
/***************************************************************************
                          phrasebookparser.cpp  -  description
                             -------------------
    begin                : Don Sep 12 2002
    copyright            : (C) 2002 by Gunnar Schmi Dt
    email                : kmouth@schmi-dt.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "phrasebookparser.h"

PhraseBookParser::PhraseBookParser() {
}

PhraseBookParser::~PhraseBookParser() {
}

bool PhraseBookParser::warning (const QXmlParseException &) {
   return false;
}

bool PhraseBookParser::error (const QXmlParseException &) {
   return false;
}

bool PhraseBookParser::fatalError (const QXmlParseException &) {
   return false;
}

QString PhraseBookParser::errorString() {
   return "";
}

bool PhraseBookParser::startDocument() {
   list.clear ();
   isInPhrase = false;
   level = 0;
   offset = 0;
   starting = true;
   return true;
}

bool PhraseBookParser::startElement (const QString &, const QString &,
                                     const QString &name,
                                     const QXmlAttributes &attributes)
{
   if (name == "phrase") {
      if (isInPhrase)
         return false;

      phrase.phrase = "";
      phrase.shortcut = attributes.value("shortcut");
      isInPhrase = true;
   }
   else if (name == "phrasebook") {
      if (isInPhrase)
         return false;

      phrase.phrase = attributes.value("name");
      phrase.shortcut = "";
      if ((phrase.phrase.isNull() || phrase.phrase.isEmpty()) && starting)
         offset = -1;
      else {
         list += PhraseBookEntry (phrase, level, false);
         level++;
      }
      starting = false;
   }
   return true;
}

bool PhraseBookParser::characters (const QString &ch) {
   phrase.phrase += ch;
   return true;
}

bool PhraseBookParser::ignorableWhitespace (const QString &ch) {
   phrase.phrase += ch;
   return true;
}

bool PhraseBookParser::endElement (const QString &, const QString &,
                                   const QString &name)
{
   if (name == "phrase") {
      list += PhraseBookEntry (phrase, level, true);
      isInPhrase = false;
   }
   else if (name == "phrasebook") {
      if (level == offset)
         return false;

      level--;
   }
   return true;
}

bool PhraseBookParser::endDocument() {
   return (level == offset);
}

PhraseBookEntryList PhraseBookParser::getPhraseList() {
   return list;
}