The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utilxml.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * utilxml.h - class XMLTag: utility class which helps work with XML tags
4  *
5  * $Id: utilxml.h 3786 2020-08-30 11:35:14Z scribe $
6  *
7  * Copyright 2003-2013 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 #ifndef UTILXML_H
24 #define UTILXML_H
25 
26 #include <defs.h>
27 #include <swbuf.h>
28 #include <list>
29 #include <map>
30 
32 
33 typedef std::map<SWBuf, SWBuf> StringPairMap;
34 typedef std::list<SWBuf> StringList;
35 
39 
40 private:
41  mutable char *buf;
42  char *name;
43  mutable bool parsed;
44  mutable bool empty;
45  mutable bool endTag;
47  mutable SWBuf junkBuf;
48 
49  void parse() const;
50  const char *getPart(const char *buf, int partNum = 0, char partSplit = '|') const;
51 
52 public:
53  XMLTag(const char *tagString = 0);
54  XMLTag(const XMLTag& tag);
55  ~XMLTag();
56 
57  void setText(const char *tagString);
58  inline const char *getName() const { return (name) ? name : SWBuf::nullStr; }
59 
60  inline bool isEmpty() const {
61  if (!parsed)
62  parse();
63 
64  return empty;
65  }
66  inline void setEmpty(bool value) {
67  if (!parsed)
68  parse();
69  empty = value;
70  if (value)
71  endTag = false;
72  }
73 
74  /***
75  * if an eID is provided, then we check to be sure we have an attribute <tag eID="xxx"/> value xxx equiv to what is given us
76  * otherwise, we return if we're a simple XML end </tag>.
77  */
78  bool isEndTag(const char *eID = 0) const;
79  inline void setEndTag(bool value) {
80  if (!parsed)
81  parse();
82  endTag = value;
83  if (value)
84  empty = false;
85  }
86 
87  const StringList getAttributeNames() const;
88  int getAttributePartCount(const char *attribName, char partSplit = '|') const;
89 
90  // return values should not be considered to persist beyond the return of the function.
91  const char *getAttribute(const char *attribName, int partNum = -1, char partSplit = '|') const;
92  const char *setAttribute(const char *attribName, const char *attribValue, int partNum = -1, char partSplit = '|');
93  const char *toString() const;
94  inline operator const char *() const { return toString(); }
95  inline XMLTag & operator =(const char *tagString) { setText(tagString); return *this; }
96  inline XMLTag & operator =(const XMLTag &other) { setText(other.toString()); return *this; }
97 };
98 
100 #endif
101 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Definition: swbuf.h:47
#define SWDLLEXPORT
Definition: defs.h:171
void setEmpty(bool value)
Definition: utilxml.h:66
bool parsed
Definition: utilxml.h:43
const char * getName() const
Definition: utilxml.h:58
Definition: utilxml.h:38
const char * toString() const
Definition: utilxml.cpp:285
SWORD_NAMESPACE_START typedef std::map< SWBuf, SWBuf > StringPairMap
Definition: utilxml.h:33
StringPairMap attributes
Definition: utilxml.h:46
bool isEmpty() const
Definition: utilxml.h:60
char * name
Definition: utilxml.h:42
std::list< SWBuf > StringList
Definition: swmodule.cpp:91
void setEndTag(bool value)
Definition: utilxml.h:79
SWBuf junkBuf
Definition: utilxml.h:47
bool empty
Definition: utilxml.h:44
char * buf
Definition: utilxml.h:41
#define SWORD_NAMESPACE_END
Definition: defs.h:40
static char * nullStr
Definition: swbuf.h:87
bool endTag
Definition: utilxml.h:45