File: xml.cpp

package info (click to toggle)
tqsllib 2.0-8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,460 kB
  • ctags: 1,081
  • sloc: sh: 8,309; cpp: 7,980; xml: 4,068; makefile: 102
file content (191 lines) | stat: -rw-r--r-- 4,873 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/***************************************************************************
                          xml.cpp  -  description
                             -------------------
    begin                : Fri Aug 9 2002
    copyright            : (C) 2002 by ARRL
    author               : Jon Bloom
    email                : jbloom@arrl.org
    revision             : $Id: xml.cpp,v 1.7 2005/02/18 20:21:01 ke3z Exp $
 ***************************************************************************/

#include "xml.h"
#include "expat.h"
#include <stack>
#include <fstream>
#include <zlib.h>

using namespace std;

namespace tqsllib {

pair<string,bool>
XMLElement::getAttribute(const string& key) {
	string s;
	XMLElementAttributeList::iterator pos;
	pos = _attributes.find(key);
	pair<string,bool> rval;
	if (pos == _attributes.end())
		rval.second = false;
	else {
		rval.first = pos->second;
		rval.second = true;
	}
	return rval;
}

void
XMLElement::xml_start(void *data, const XML_Char *name, const XML_Char **atts) {
	XMLElement *el = (XMLElement *)data;
	XMLElement new_el(name);
//cout << "Element: " << name << endl;
	for (int i = 0; atts[i]; i += 2) {
		new_el.setAttribute(atts[i], atts[i+1]);
	}
	if (el->_parsingStack.empty()) {
		el->_parsingStack.push_back(el->addElement(new_el));
//cout << "Empty: " << new_el.getElementName() << endl;
	} else {
//cout << "Adding: " << el->_parsingStack.back()->second.getElementName() << endl;
		new_el.setPretext(el->_parsingStack.back()->second.getText());
		el->_parsingStack.back()->second.setText("");
		el->_parsingStack.push_back(el->_parsingStack.back()->second.addElement(new_el));
	}
}

void
XMLElement::xml_end(void *data, const XML_Char *name) {
	XMLElement *el = (XMLElement *)data;
	if (!(el->_parsingStack.empty()))
		el->_parsingStack.pop_back();
}

void
XMLElement::xml_text(void *data, const XML_Char *text, int len) {
	XMLElement *el = (XMLElement *)data;
	el->_parsingStack.back()->second._text.append(text, len);
}

/*
bool
XMLElement::parseFile(const char *filename) {
	ifstream in;

	in.open(filename);
	if (!in)
		return false;	// Failed to open file
	char buf[256];
	XML_Parser xp = XML_ParserCreate(0);
	XML_SetUserData(xp, (void *)this);
	XML_SetStartElementHandler(xp, &XMLElement::xml_start);
	XML_SetEndElementHandler(xp, &XMLElement::xml_end);
	XML_SetCharacterDataHandler(xp, &XMLElement::xml_text);

	_parsingStack.clear();	
	while (in.get(buf, sizeof buf, 0).good()) {
		// Process the XML
		if (XML_Parse(xp, buf, strlen(buf), 0) == 0) {
			XML_ParserFree(xp);
			return false;
		}
	}

	bool rval = !in.bad();
	if (!rval)
		rval = (XML_Parse(xp, "", 0, 1) != 0);
	XML_ParserFree(xp);
	return rval;
}
*/

bool
XMLElement::parseFile(const char *filename) {
	gzFile in = gzopen(filename, "rb");

	if (!in)
		return false;	// Failed to open file
	char buf[256];
	XML_Parser xp = XML_ParserCreate(0);
	XML_SetUserData(xp, (void *)this);
	XML_SetStartElementHandler(xp, &XMLElement::xml_start);
	XML_SetEndElementHandler(xp, &XMLElement::xml_end);
	XML_SetCharacterDataHandler(xp, &XMLElement::xml_text);

	_parsingStack.clear();
	int rcount;
	while ((rcount = gzread(in, buf, sizeof buf)) > 0) {
		// Process the XML
		if (XML_Parse(xp, buf, rcount, 0) == 0) {
			gzclose(in);
			XML_ParserFree(xp);
			return false;
		}
	}
	gzclose(in);
	bool rval = (rcount == 0);
	if (rval)
		rval = (XML_Parse(xp, "", 0, 1) != 0);
	XML_ParserFree(xp);
	return rval;
}


static struct {
	char c;
	char *ent;
} xml_entity_table[] = {
	{ '"', "&quot;" },
	{ '\'', "&apos;" },
	{ '>', "&gt;" },
	{ '<', "&lt;" }
};

static string
xml_entities(const string& s) {
	string ns = s;
	string::size_type idx = 0;
	while ((idx = ns.find('&', idx)) != string::npos) {
		ns.replace(idx, 1, "&amp;");
		idx++;
	}
	for (int i = 0; i < int(sizeof xml_entity_table / sizeof xml_entity_table[0]); i++) {
		while ((idx = ns.find(xml_entity_table[i].c)) != string::npos)
			ns.replace(idx, 1, xml_entity_table[i].ent);
	}
	return ns;
}

/* Stream out an XMLElement as XML text */
ostream&
operator<< (ostream& stream, XMLElement& el) {
	bool ok;
	XMLElement subel;
 	if (el.getElementName() != "") {
	 	stream << "<" << el.getElementName();
 		string key, val;
	 	bool ok = el.getFirstAttribute(key, val);
 		while (ok) {
 			stream << " " << key << "=\"" << xml_entities(val) << "\"";
	 		ok = el.getNextAttribute(key, val);
 		}
	 	if (el.getText() == "" && !el.getFirstElement(subel)) {
 			stream << " />";
 			return stream;
	 	} else
			stream << ">";
	}
	ok = el.getFirstElement(subel);
	while (ok) {
		string s = subel.getPretext();
		if (s != "")
			stream << xml_entities(s);
		stream << subel;
		ok = el.getNextElement(subel);
	}
	if (el.getText() != "")
		stream << xml_entities(el.getText());
	if (el.getElementName() != "")
		stream << "</" << el.getElementName() << ">";
	return stream;
}

}	// namespace tqsllib