utilxml.h

00001 /******************************************************************************
00002  *  utilxml.h   - definition of class that deal with xml constructs 
00003  *
00004  * $Id: utilxml.h 1952 2006-07-30 19:08:20Z scribe $
00005  *
00006  * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
00007  *      CrossWire Bible Society
00008  *      P. O. Box 2528
00009  *      Tempe, AZ  85280-2528
00010  *
00011  * This program is free software; you can redistribute it and/or modify it
00012  * under the terms of the GNU General Public License as published by the
00013  * Free Software Foundation version 2.
00014  *
00015  * This program is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * General Public License for more details.
00019  *
00020  */
00021 
00022 #ifndef UTILXML_H
00023 #define UTILXML_H
00024 
00025 #include <defs.h>
00026 #include <swbuf.h>
00027 #include <list>
00028 #include <map>
00029 
00030 SWORD_NAMESPACE_START
00031 
00032 typedef std::map<SWBuf, SWBuf> StringPairMap;
00033 typedef std::list<SWBuf> StringList;
00034 
00037 class SWDLLEXPORT XMLTag {
00038 private:
00039         mutable char *buf;
00040         char *name;
00041         mutable bool parsed;
00042         mutable bool empty;
00043         mutable bool endTag;
00044         mutable StringPairMap attributes;
00045         mutable SWBuf junkBuf;
00046         
00047         void parse() const;
00048         const char *getPart(const char *buf, int partNum = 0, char partSplit = '|') const;
00049         static const char *nullstr;
00050         
00051 public:
00052         XMLTag(const char *tagString = 0);
00053         ~XMLTag();
00054 
00055         void setText(const char *tagString);
00056         inline const char *getName() const { return (name)?name:nullstr; }
00057 
00058         inline bool isEmpty() const {
00059                 if (!parsed)
00060                         parse();
00061 
00062                 return empty;
00063         }
00064         inline void setEmpty(bool value) {
00065                 if (!parsed)
00066                         parse();
00067                 empty = value;
00068                 if (value)
00069                         endTag = false;
00070         }
00071 
00072         inline bool isEndTag() const { return endTag; }
00073 
00074         const StringList getAttributeNames() const;
00075         int getAttributePartCount(const char *attribName, char partSplit = '|') const;
00076 
00077         // return values should not be considered to persist beyond the return of the function.
00078         const char *getAttribute(const char *attribName, int partNum = -1, char partSplit = '|') const;
00079         const char *setAttribute(const char *attribName, const char *attribValue);
00080         const char *toString() const;
00081         inline operator const char *() const { return toString(); }
00082         inline XMLTag & operator =(const char *tagString) { setText(tagString); return *this; }
00083         inline XMLTag & operator =(const XMLTag &other) { setText(other.toString()); return *this; }
00084 };
00085 
00086 SWORD_NAMESPACE_END
00087 #endif
00088