00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef DICTIONARY_H
00019 #define DICTIONARY_H
00020
00021 #include "FIFOMap.h"
00022 #include "Serializable.h"
00023 #include <libXMLTree.h>
00024 #include <XMLParser.h>
00025 #include <XMLDocument.h>
00026 #include <CDataChild.h>
00027 #include <CDataTag.h>
00028 #include <ElementChild.h>
00029
00030 using namespace libXMLTree;
00031
00033
00038 template <class Word,
00039 class Definition,
00040 class WordList = vector<Word>,
00041 class DefList = vector<Definition>
00042 >
00043
00044 class Dictionary : virtual public Serializable,
00045 virtual public FIFOMap<Word, Definition, WordList, DefList>
00046 {
00047 public:
00049
00050
00051 virtual void Read(istream& is);
00052
00054 virtual string ToString() const;
00056 };
00057
00058 template <class Word, class Definition,class WordList,class DefList>
00059 void
00060
00061 Dictionary<Word, Definition, WordList, DefList>::Read (istream& is)
00062 {
00063 clear();
00064
00065 XMLParser xp;
00066 xp.Read(is);
00067
00068 XMLement root = xp.Parse();
00069
00070 ElementList words = GetChildrenByName(&root, "Word");
00071 ElementList defs = GetChildrenByName(&root, "Definition");
00072
00073 ElementList::iterator p, q;
00074 for (p = words.begin(), q = defs.begin(); p != words.end(); ++p, ++q)
00075 Add(GetText(*p), (q != defs.end()) ? GetText(*q) : Definition());
00076 }
00077
00078 template <class Word, class Definition, class WordList, class DefList>
00079 string
00080
00081 Dictionary<Word, Definition, WordList, DefList>::ToString() const
00082 {
00083 XMLProcInst prolog("xml");
00084 prolog["version"] = "1.0";
00085 prolog["encoding"] = "UTF-8";
00086 prolog["standalone"] = "yes";
00087
00088 SpecialTag dtd("DOCTYPE Dictionary");
00089
00090 XMLement* root = new XMLement("Dictionary");
00091
00092 for (const_iterator p = begin(); p != end(); ++p)
00093 {
00094 Element* word = new Element("Word");
00095 word->Add(dynamic_cast<XMLChild*>(new CDataChild(CDataTag((string)p->first))));
00096 root->Add(dynamic_cast<XMLChild*>(new ElementChild(word)));
00097
00098 Element* def = new Element("Definition");
00099 def->Add(dynamic_cast<XMLChild*>(new CDataChild(CDataTag((string)p->second))));
00100 root->Add(dynamic_cast<XMLChild*>(new ElementChild(def)));
00101 }
00102
00103 return XMLDocument(prolog, dtd, root).ToString();
00104 }
00105
00106 #endif