XMLTag Class Reference

#include <utilxml.h>

Collaboration diagram for XMLTag:
Collaboration graph
[legend]

List of all members.

Public Member Functions

const char * getAttribute (const char *attribName, int partNum=-1, char partSplit= '|') const
const StringList getAttributeNames () const
int getAttributePartCount (const char *attribName, char partSplit= '|') const
const char * getName () const
bool isEmpty () const
bool isEndTag (const char *eID=0) const
 operator const char * () const
XMLTagoperator= (const XMLTag &other)
XMLTagoperator= (const char *tagString)
const char * setAttribute (const char *attribName, const char *attribValue, int partNum=-1, char partSplit= '|')
void setEmpty (bool value)
void setText (const char *tagString)
const char * toString () const
 XMLTag (const XMLTag &tag)
 XMLTag (const char *tagString=0)
 ~XMLTag ()

Private Member Functions

const char * getPart (const char *buf, int partNum=0, char partSplit= '|') const
void parse () const

Private Attributes

StringPairMap attributes
char * buf
bool empty
bool endTag
SWBuf junkBuf
char * name
bool parsed

Static Private Attributes

static const char * nullstr = ""

Detailed Description

Simple XML helper class.

Definition at line 37 of file utilxml.h.


Constructor & Destructor Documentation

XMLTag::XMLTag ( const char *  tagString = 0  ) 

Definition at line 117 of file utilxml.cpp.

00117                                     {
00118 
00119     name   = 0;
00120     buf    = 0;
00121     setText(tagString);
00122 }

XMLTag::XMLTag ( const XMLTag tag  ) 

Definition at line 124 of file utilxml.cpp.

00124                               : attributes(t.attributes)  {
00125     parsed = t.parsed;
00126     empty = t.empty;
00127     endTag = t.endTag;
00128     if (t.buf) {
00129         int len = strlen(t.buf);
00130         buf = new char[len + 1];
00131         memcpy(buf, t.buf, len + 1);
00132     }
00133     if (t.name) {
00134         int len = strlen(t.name);
00135         name = new char[len + 1];
00136         memcpy(name, t.name, len + 1);
00137     }
00138 }

XMLTag::~XMLTag (  ) 

Definition at line 176 of file utilxml.cpp.

00176                 {
00177     if (buf)
00178         delete [] buf;
00179     if (name)
00180         delete [] name;
00181 }


Member Function Documentation

const char * XMLTag::getAttribute ( const char *  attribName,
int  partNum = -1,
char  partSplit = '|' 
) const

Definition at line 225 of file utilxml.cpp.

00225                                                                                           {
00226 
00227     if (!parsed)
00228         parse();
00229 
00230     StringPairMap::const_iterator it = attributes.find(attribName);
00231 
00232     const char *retVal = 0;
00233     if (it != attributes.end()) 
00234         retVal = it->second.c_str();        
00235         
00236     if ((retVal) && (partNum > -1))
00237         retVal = getPart(retVal, partNum, partSplit);
00238 
00239     return retVal;
00240 }

const StringList XMLTag::getAttributeNames (  )  const

Definition at line 183 of file utilxml.cpp.

00183                                                  {
00184     StringList retVal;
00185 
00186     if (!parsed)
00187         parse();
00188 
00189     for (StringPairMap::const_iterator it = attributes.begin(); it != attributes.end(); it++)
00190         retVal.push_back(it->first.c_str());
00191 
00192     return retVal;
00193 }

int XMLTag::getAttributePartCount ( const char *  attribName,
char  partSplit = '|' 
) const

Definition at line 213 of file utilxml.cpp.

00213                                                                               {
00214     int count;
00215     const char *buf = getAttribute(attribName);
00216     for (count = 0; buf; count++) {
00217         buf = strchr(buf, partSplit);
00218         if (buf)
00219             buf++;
00220     }
00221     return count;
00222 }

const char* XMLTag::getName (  )  const [inline]

Definition at line 57 of file utilxml.h.

00057 { return (name)?name:nullstr; }

const char * XMLTag::getPart ( const char *  buf,
int  partNum = 0,
char  partSplit = '|' 
) const [private]

Definition at line 196 of file utilxml.cpp.

00196                                                                               {
00197     for (; (buf && partNum); partNum--) {
00198         buf = strchr(buf, partSplit);
00199         if (buf)
00200             buf++;
00201     }
00202     if (buf) {
00203         const char *end = strchr(buf, partSplit);
00204         junkBuf = buf;
00205         if (end)
00206             junkBuf.setSize(end - buf);
00207         return junkBuf.c_str();
00208     }
00209     return 0;
00210 }

bool XMLTag::isEmpty (  )  const [inline]

Definition at line 59 of file utilxml.h.

00059                                 {
00060         if (!parsed)
00061             parse();
00062 
00063         return empty;
00064     }

bool XMLTag::isEndTag ( const char *  eID = 0  )  const

Definition at line 314 of file utilxml.cpp.

00314                                            {
00315     if (eID) {
00316         return (SWBuf(eID) == getAttribute("eID"));
00317     }
00318     return endTag;
00319 }

XMLTag::operator const char * (  )  const [inline]

Definition at line 86 of file utilxml.h.

00086 { return toString(); }

XMLTag& XMLTag::operator= ( const XMLTag other  )  [inline]

Definition at line 88 of file utilxml.h.

00088 { setText(other.toString()); return *this; }

XMLTag& XMLTag::operator= ( const char *  tagString  )  [inline]

Definition at line 87 of file utilxml.h.

00087 { setText(tagString); return *this; }

void XMLTag::parse (  )  const [private]

Definition at line 29 of file utilxml.cpp.

00029                          {
00030     int i;
00031     int start;
00032     char *name = 0;
00033     char *value = 0;
00034     attributes.clear();
00035     
00036     if (!buf)
00037         return;
00038     for (i = 0; ((buf[i]) && (!isalpha(buf[i]))); i++);
00039     for (; buf[i]; i++) {
00040         if (strchr("\t\r\n ", buf[i])) {
00041                         // Convert newlines, carriage returns and tabs to spaces
00042             buf[i] = ' ';
00043 
00044             for (; ((buf[i]) && (!isalpha(buf[i]))); i++);
00045             if (buf[i]) {       // we have an attribute name
00046                 start = i;
00047                 // Deprecated: check for following whitespacee
00048                 // Should be: for (; (buf[i] && buf[i] != '='; i++);
00049                 for (; ((buf[i]) && (!strchr(" =", buf[i]))); i++);
00050 
00051                 if (i-start) {
00052                     if (name)
00053                         delete [] name;
00054                     name = new char [ (i-start) + 1 ];
00055                     strncpy(name, buf+start, i-start);
00056                     name[i-start] = 0;
00057                 }
00058 
00059                 // The following does not allow for empty attributes
00060                 //for (; ((buf[i]) && (strchr(" =\"\'", buf[i]))); i++);
00061 
00062                 // skip space preceding the = sign
00063                 // Deprecated: this is not part of the xml spec
00064                 for (; buf[i] == ' '; i++) ;
00065 
00066                 // skip the = sign
00067                 if (buf[i])
00068                     i++;
00069 
00070                 // skip space following the = sign
00071                 // Deprecated: this is not part of the xml spec
00072                 for (; buf[i] == ' '; i++) ;
00073 
00074                 // remember and skip the quote sign
00075                 char quoteChar = buf[i];
00076                 if (quoteChar)
00077                     i++;
00078 
00079                 if (buf[i]) {   // we have attribute value
00080                     start = i;
00081                     // Skip until matching quote character
00082                     for (; ((buf[i]) && (buf[i] != quoteChar)); i++);
00083 
00084                     // Allow for empty quotes
00085                     //if (i-start) {
00086                         if (value)
00087                             delete [] value;
00088                         value = new char [ (i-start) + 1 ];
00089                         if (i-start) {
00090                             strncpy(value, buf+start, i-start);
00091                         }
00092                         value[i-start] = 0;
00093                         attributes[name] = value;
00094                     //}
00095                 }
00096             }
00097         }
00098 
00099         // if there are no more characters left then quit
00100         if (!buf[i])
00101             break;
00102 
00103     }
00104     for (;i;i--) {
00105         if (buf[i] == '/')
00106             empty = true;
00107         if (!strchr(" \t\r\n>\t", buf[i]))
00108             break;
00109     }
00110         
00111     parsed = true;
00112     if (name) delete [] name;
00113     if (value) delete [] value;
00114 }

const char * XMLTag::setAttribute ( const char *  attribName,
const char *  attribValue,
int  partNum = -1,
char  partSplit = '|' 
)

Definition at line 243 of file utilxml.cpp.

00243                                                                                                              {
00244     if (!parsed)
00245         parse();
00246 
00247     SWBuf newVal = "";
00248     // set part of an attribute
00249     if (partNum > -1) {
00250         const char *wholeAttr = getAttribute(attribName);
00251         int attrCount = getAttributePartCount(attribName, partSplit);
00252         for (int i = 0; i < attrCount; i++) {
00253             if (i == partNum) {
00254                 if (attribValue) {
00255                     newVal += attribValue;
00256                     newVal += partSplit;
00257                 }
00258                 else {
00259                     // discard this part per null attribValue
00260                 }
00261             }
00262             else {
00263                 newVal += getPart(wholeAttr, i, partSplit);
00264                 newVal += partSplit;
00265             }
00266         }
00267         if (newVal.length()) newVal--;  // discard the last partSplit
00268         attribValue = (!attribValue && !newVal.length()) ? 0 : newVal.c_str();
00269     }
00270 
00271     // perform the actual set
00272     if (attribValue)
00273         attributes[attribName] = attribValue;
00274     else    attributes.erase(attribName);
00275 
00276     return attribValue;
00277 }

void XMLTag::setEmpty ( bool  value  )  [inline]

Definition at line 65 of file utilxml.h.

00065                                      {
00066         if (!parsed)
00067             parse();
00068         empty = value;
00069         if (value)
00070             endTag = false;
00071     }

void XMLTag::setText ( const char *  tagString  ) 

Definition at line 140 of file utilxml.cpp.

00140                                           {
00141     parsed = false;
00142     empty  = false;
00143     endTag = false;
00144 
00145     if (buf) {
00146         delete [] buf;
00147         buf = 0;
00148     }
00149 
00150     if (!tagString)     // assert tagString before proceeding
00151         return;
00152 
00153     stdstr(&buf, tagString);
00154 
00155     int start = 0;
00156     int i;
00157 
00158     // skip beginning silliness
00159     for (i = 0; ((tagString[i]) && (!isalpha(tagString[i]))); i++) {
00160         if (tagString[i] == '/')
00161             endTag = true;
00162     }
00163     start = i;
00164     for (; ((tagString[i]) && (!strchr("\t\r\n />", tagString[i]))); i++);
00165     if (i-start) {
00166         if (name)
00167             delete [] name;
00168         name = new char [ (i-start) + 1 ];
00169         strncpy(name, tagString+start, i-start);
00170         name[i-start] = 0;
00171         if (tagString[i] == '/')
00172             empty = true;
00173     }
00174 }

const char * XMLTag::toString (  )  const

Definition at line 279 of file utilxml.cpp.

00279                                    {
00280     SWBuf tag = "<";
00281     if (!parsed)
00282         parse();
00283 
00284     if (isEndTag())
00285         tag.append('/');
00286 
00287     tag.append(getName());
00288     for (StringPairMap::iterator it = attributes.begin(); it != attributes.end(); it++) {
00289         //tag.appendFormatted(" %s=\"%s\"", it->first.c_str(), it->second.c_str());
00290         tag.append(' ');
00291         tag.append(it->first.c_str());
00292         tag.append((strchr(it->second.c_str(), '\"')) ? "=\'" : "=\"");
00293         tag.append(it->second.c_str());
00294         tag.append((strchr(it->second.c_str(), '\"'))? '\'' : '\"');
00295     }
00296 
00297     if (isEmpty())
00298         tag.append('/');
00299 
00300     tag.append('>');
00301 
00302 
00303     if (buf)
00304         delete [] buf;
00305     buf = new char [ tag.length() + 1 ];
00306     strcpy(buf, tag.c_str());
00307 
00308     return buf;
00309 }


Member Data Documentation

StringPairMap XMLTag::attributes [mutable, private]

Definition at line 44 of file utilxml.h.

char* XMLTag::buf [mutable, private]

Definition at line 39 of file utilxml.h.

bool XMLTag::empty [mutable, private]

Definition at line 42 of file utilxml.h.

bool XMLTag::endTag [mutable, private]

Definition at line 43 of file utilxml.h.

SWBuf XMLTag::junkBuf [mutable, private]

Definition at line 45 of file utilxml.h.

char* XMLTag::name [private]

Definition at line 40 of file utilxml.h.

SWORD_NAMESPACE_START const char * XMLTag::nullstr = "" [static, private]

Definition at line 49 of file utilxml.h.

bool XMLTag::parsed [mutable, private]

Definition at line 41 of file utilxml.h.


The documentation for this class was generated from the following files:

Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1