Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

CommonPkdParser.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                     (C) 2002 SuSE AG |
00011 \----------------------------------------------------------------------/
00012 
00013    File:       CommonPkdParser.h
00014    Purpose:    Declare Tag and TagSet as interface to the TagParser
00015    Author:     Ludwig Nussel <lnussel@suse.de>
00016    Maintainer: Ludwig Nussel <lnussel@suse.de>
00017 
00018 /-*/
00019 
00020 #ifndef CommonPkdParser_h
00021 #define CommonPkdParser_h
00022 #include <iostream>
00023 #include <string>
00024 #include <map>
00025 #include <list>
00026 #include <y2util/TagParser.h>
00027 
00028 namespace CommonPkdParser
00029 {
00030 
00035 class Tag
00036 {
00037     public:
00038         enum assignstatus { ACCEPTED, REJECTED_NOMATCH, REJECTED_FULL, REJECTED_NOENDTAG };
00048         enum assigntype { ACCEPTONCE, ACCEPTPREFERREDLOCALE, ACCEPTLOCALEONLY };
00053         enum datatype { SINGLE, MULTI };
00057         enum endtagtype { ENDTAG_NORMAL, ENDTAG_COMPLETELYREVERSED };
00058         enum encodingtype { LATIN1, LATIN2, UTF8 };
00059     private:
00061         std::string _name;
00063         std::string _endtag;
00065         std::streamoff _startpos;
00067         std::streamoff _endpos;
00069         std::string _data;
00071         std::list<std::string> _multidata;
00073         std::string _prefmainlocale;
00075         std::string _prefsublocale;
00077         std::string _lastmainlocale;
00078         std::string _lastsublocale;
00079         assigntype _type;
00081         std::string _defaultlocale;
00082         datatype _datatype;
00083         endtagtype _endtagtype;
00084         encodingtype _encodingtype;
00085 
00086         static char* const global_defaultlocale;
00087 
00089         bool comparebeforedot(const std::string& str2);
00090     public:
00095         Tag(const std::string& name, assigntype type = ACCEPTONCE )
00096             : _name(name), _type(type), _endtagtype(ENDTAG_NORMAL), _encodingtype(LATIN1)
00097         {
00098             if ( _type == ACCEPTLOCALEONLY )
00099             {
00100                 _prefmainlocale = _defaultlocale;
00101             }
00102         }
00103         const std::string& Name() const
00104         {
00105             return _name;
00106         }
00107         const std::string& Data() const
00108         {
00109             return _data;
00110         }
00111         const std::list<std::string>& MultiData() const
00112         {
00113             return _multidata;
00114         }
00115         bool operator==(const Tag& t2 )
00116         {
00117             return comparebeforedot(t2._name);
00118         }
00121         void setType(assigntype type)
00122         {
00123             _type = type;
00124             if( _type == ACCEPTLOCALEONLY )
00125             {
00126                 _defaultlocale = global_defaultlocale;
00127             }
00128         }
00132         // TODO make use of encoding
00133         void setEncoding(encodingtype etype)
00134         {
00135             _encodingtype = etype;
00136         }
00144         void setDefaultLocale(const std::string& defaultlocale)
00145         {
00146             _defaultlocale = defaultlocale;
00147         }
00152         void setPreferredLocale(const std::string& preflocale);
00159         void setEndTag(const std::string& endtag, endtagtype etype = ENDTAG_NORMAL );
00162         void clear()
00163         {
00164             _startpos = 0;
00165             _endpos = 0;
00166             _lastmainlocale = _lastsublocale = _data.erase();
00167             _multidata.clear ();
00168         }
00172         assignstatus assign(const std::string& starttag, TagParser& parser, std::istream& istr);
00175         std::streamoff posDataStart() { return _startpos; }
00178         std::streamoff posDataEnd() { return _endpos; }
00179         void print(std::ostream& os)
00180         {
00181             os << "Tag: " << _name;
00182             os << " ( " << _startpos << "," <<  _endpos << " ): "
00183                << _data << std::endl;
00184             if(_datatype == MULTI )
00185                 os << "EndTag: " << _endtag << std::endl;
00186         }
00187 };
00188 
00192 class TagSet
00193 {
00194     public:
00195         typedef std::vector<CommonPkdParser::Tag*> tagvectortype;
00196 
00197     protected:
00203         void insert(const std::string key, Tag* tag)
00204         {
00205             _tags[key]=tag;
00206         }
00207 
00209         std::vector<Tag*> _localetags;
00210         typedef std::map<std::string, Tag*> tagmaptype;
00212         tagmaptype _tags;
00214         tagvectortype _tagv;
00215 
00216     public:
00217         TagSet()
00218         {
00219         }
00220         virtual ~TagSet();
00225         void addTag(Tag* tag)
00226         {
00227             _tags[tag->Name()]=tag;
00228         }
00229 
00236         Tag::assignstatus assign(const std::string& starttag, TagParser& parser, std::istream& istr);
00237 
00239         void print(std::ostream& os)
00240         {
00241             tagmaptype::iterator it;
00242             for(it=_tags.begin();it!=_tags.end();++it)
00243             {
00244                 it->second->print(os);
00245             }
00246         }
00250         void clear()
00251         {
00252             tagmaptype::iterator it;
00253             for(it=_tags.begin();it!=_tags.end();++it)
00254             {
00255                 it->second->clear();
00256             }
00257         }
00262         virtual void setEncoding(Tag::encodingtype etype)
00263         {
00264             std::vector<Tag*>::iterator it;
00265             for(it=_localetags.begin();it!=_localetags.end();++it)
00266                 (*it)->setEncoding(etype);
00267         }
00268 
00274         CommonPkdParser::Tag* getTagByIndex(unsigned int idx)
00275         {
00276             if(idx<_tagv.size())
00277                 return _tagv[idx];
00278             else
00279                 return NULL;
00280         }
00281 
00287         void addTagByIndex(unsigned int idx, CommonPkdParser::Tag* t)
00288         {
00289             if(_tagv.size()<=idx) { _tagv.resize(idx+2,NULL); }
00290             _tagv[idx]=t;
00291         }
00292 };
00293 
00294 }
00295 
00296 #endif // CommonPkdParser_h
00297 
00298 // vim:sw=4

Generated on Thu Feb 23 23:56:10 2006 for liby2util by doxygen 1.3.6