1 /******************************************************************************
2  *
3  *  utilxml.h -	definition of class that deal with xml constructs
4  *
5  * $Id: utilxml.h 2980 2013-09-14 21:51:47Z 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 
31 SWORD_NAMESPACE_START
32 
33 typedef std::map<SWBuf, SWBuf> StringPairMap;
34 typedef std::list<SWBuf> StringList;
35 
36 /** Simple XML helper class.
37 */
38 class SWDLLEXPORT XMLTag {
39 private:
40 	mutable char *buf;
41 	char *name;
42 	mutable bool parsed;
43 	mutable bool empty;
44 	mutable bool endTag;
45 	mutable StringPairMap attributes;
46 	mutable SWBuf junkBuf;
47 
48 	void parse() const;
49 	const char *getPart(const char *buf, int partNum = 0, char partSplit = '|') const;
50 
51 public:
52 	XMLTag(const char *tagString = 0);
53 	XMLTag(const XMLTag& tag);
54 	~XMLTag();
55 
56 	void setText(const char *tagString);
getName()57 	inline const char *getName() const { return (name) ? name : SWBuf::nullStr; }
58 
isEmpty()59 	inline bool isEmpty() const {
60 		if (!parsed)
61 			parse();
62 
63 		return empty;
64 	}
setEmpty(bool value)65 	inline void setEmpty(bool value) {
66 		if (!parsed)
67 			parse();
68 		empty = value;
69 		if (value)
70 			endTag = false;
71 	}
72 
73 	/***
74 	 * 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
75 	 * otherwise, we return if we're a simple XML end </tag>.
76 	 */
77 	bool isEndTag(const char *eID = 0) const;
78 
79 	const StringList getAttributeNames() const;
80 	int getAttributePartCount(const char *attribName, char partSplit = '|') const;
81 
82 	// return values should not be considered to persist beyond the return of the function.
83 	const char *getAttribute(const char *attribName, int partNum = -1, char partSplit = '|') const;
84 	const char *setAttribute(const char *attribName, const char *attribValue, int partNum = -1, char partSplit = '|');
85 	const char *toString() const;
86 	inline operator const char *() const { return toString(); }
87 	inline XMLTag & operator =(const char *tagString) { setText(tagString); return *this; }
88 	inline XMLTag & operator =(const XMLTag &other) { setText(other.toString()); return *this; }
89 };
90 
91 SWORD_NAMESPACE_END
92 #endif
93 
94