Main Page   Class Hierarchy   Compound List   File List   Compound Members  

Dictionary.h

00001 // Copyright (C) 2001 Kevin Duffy <kevin@buhbird.com>
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License, or (at your option) any later version.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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

Generated at Wed May 2 00:24:41 2001 for API by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001