File: XMLWriter.cpp

package info (click to toggle)
libtuxcap 1.4.0.dfsg2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,176 kB
  • sloc: cpp: 43,203; ansic: 3,095; python: 774; objc: 242; makefile: 100; xml: 87
file content (299 lines) | stat: -rw-r--r-- 5,974 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// XMLWriter.cpp: implementation of the XMLWriter class by James Poag
//
//////////////////////////////////////////////////////////////////////

#include "XMLWriter.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
using namespace Sexy;

XMLWriter::XMLWriter()
{
	mFile = NULL;
	mLineNum = 0;
	mAllowComments = false;
	mOpenAttributes = false;
}

XMLWriter::~XMLWriter()
{
	if (mFile != NULL)
		fclose(mFile);
}

void XMLWriter::Fail(const std::string& theErrorText)
{
	mHasFailed = true;
	mErrorText = theErrorText;
}

void XMLWriter::Warn(const std::string &theWarning)
{
	mWarningStack.push("WARNING: " + theWarning);
}

void XMLWriter::Comment(const std::string &theComment)
{
	mWarningStack.push(theComment);
}

void XMLWriter::Init()
{
	mLineNum = 1;
	mHasFailed = false;
	mErrorText = "";	
}

bool XMLWriter::OpenFile(const std::string& theFileName)
{		
	mFile = fopen(theFileName.c_str(), "w");

	if (mFile == NULL)
	{
		mLineNum = 0;
		Fail("Unable to open file " + theFileName);		
		return false;
	}

	mFileName = theFileName.c_str();
	Init();

	//write header
	fprintf(mFile, "<?xml version=\"1.0\" ?>\n");
	mLineNum++;

	return true;
}

bool XMLWriter::HasFailed()
{
	return mHasFailed;
}

std::string XMLWriter::GetErrorText()
{
	return mErrorText;
}

int XMLWriter::GetCurrentLineNum()
{
	return mLineNum;
}

std::string XMLWriter::GetFileName()
{
	return mFileName;
}

/*
	Used as a tool to add attributes to XMLElement Nodes.
*/
bool XMLWriter::AddAttribute(XMLElement* theElement, const std::string& theAttributeKey, const std::string& theAttributeValue)
{
	std::pair<XMLParamMap::iterator,bool> aRet;

	aRet = theElement->mAttributes.insert(XMLParamMap::value_type(theAttributeKey, theAttributeValue));
	if (!aRet.second)
		aRet.first->second = theAttributeValue;

	if (theAttributeKey != "/")
		theElement->mAttributeIteratorList.push_back(aRet.first);

	return aRet.second;
}

/*
	Pushes the Element onto the section stack and creates 
	a new Node with an Attributes section
*/
bool XMLWriter::StartElement(const std::string &theElementName)
{
	CheckFileOpen();
	if(mHasFailed) return false;

	if(mOpenAttributes)
	{
		// Close Previous Element Attribute section
		fprintf(mFile, ">\n");
		mLineNum++;
	}

	if(!ValidateElementNodeName(theElementName))
	{
		Warn(theElementName + " is an Invalid Node Name.");
	}

	while(!mWarningStack.empty())
	{
		fprintf(mFile, "<!--  %s -->\n", mWarningStack.top().c_str());
		mWarningStack.pop();
	}

	mSectionStack.push(theElementName);
	
	for(unsigned int i = 1; i < mSectionStack.size(); i++)
	{
		fprintf(mFile, "\t");
	}
	
	fprintf(mFile, "<%s", theElementName.c_str());
	
	mOpenAttributes = true;
	return true;
}

/*
	Pushes theElement->Value onto the stack and adds all of 
	theElements->mAttributes to the Attribute section
*/
bool XMLWriter::StartElement(XMLElement *theElement)
{
	if(StartElement(theElement->mValue.c_str()) == false)
		return false;

	std::map<std::string, std::string>::iterator map_itr;
	map_itr = theElement->mAttributes.begin();

	for( ; map_itr != theElement->mAttributes.end(); map_itr++)
	{
		if(!WriteAttribute(map_itr->first, map_itr->second))
			return false;
	}
	
	return true;
}

/*
	Closes the previously open Node and pops it from the stack.
	Also Closes the Attributes Writing until StartElement is called.
*/
bool XMLWriter::StopElement()
{
	CheckFileOpen();
	if(mHasFailed) return false;

	if(mSectionStack.empty())
	{
		Fail("Stop Element Calls do not match StartElement Calls.");
		return false;
	}

	const std::string aNodeName = mSectionStack.top();
	mSectionStack.pop();

	if(mOpenAttributes)
	{
		// Close Previous Element Attribute section
		fprintf(mFile, "/>\n");
		mLineNum++;
	}
	else
	{
		// Otherwise close element section
		for(unsigned int i = 0; i < mSectionStack.size(); i++)
		{
			fprintf(mFile, "\t");
		}

		fprintf(mFile, "</%s>\n", aNodeName.c_str());
	}

	mOpenAttributes = false;

	while(!mWarningStack.empty())
	{
		fprintf(mFile, "<!--  %s -->\n", mWarningStack.top().c_str());
		mWarningStack.pop();
	}

	return true;
}

/*
	Adds an attribute to the Current Element.  If No element is open, then it returns
	false.
*/
bool XMLWriter::WriteAttribute(const std::string& aAttributeKey, const std::string& aAttributeValue)
{
	CheckFileOpen();
	if(mHasFailed) return false;

	if(mOpenAttributes)
	{
		if(!ValidateElementNodeName(aAttributeKey))
		{
			Warn(aAttributeKey + " is an invalid Attribute Name.");
		}
		
		fprintf(mFile, " %s=\"%s\"", aAttributeKey.c_str(), XMLEncodeString(aAttributeValue).c_str());
		return true;
	}

	if(mSectionStack.size())
		Fail("Attributes Section already closed for " + mSectionStack.top());
	else
		Fail("No Element Nodes Open for Writing Attributes.");

	return false;
}

bool XMLWriter::WriteAttribute(const std::string &aAttributeKey, const int &aAttributeValue)
{
	return WriteAttribute(aAttributeKey, StrFormat("%d", aAttributeValue));
}

bool XMLWriter::WriteAttribute(const std::string &aAttributeKey, const float &aAttributeValue)
{
	return WriteAttribute(aAttributeKey, StrFormat("%f", aAttributeValue));
}

bool XMLWriter::CloseFile()
{
	while(!mSectionStack.empty())
	{
		StopElement();
	}

	if (mFile != NULL)
	{
		fclose(mFile);
                mFile = NULL;
		return true;
	}

	Fail("File not Open");
	return false;
}

bool XMLWriter::ValidateElementNodeName(const std::string &theNodeName)
{
	const char* aNodeName = theNodeName.c_str();

	for(unsigned int i = 0; i < theNodeName.size(); i++)
	{
		if( aNodeName[i] < '0'
			|| (aNodeName[i] > '9' && aNodeName[i] < 'A')
			|| (aNodeName[i] > 'Z' && aNodeName[i] < '_')
			|| (aNodeName[i] > '_' && aNodeName[i] < 'a')
			|| aNodeName[i] > 'z')
		{
			return false;
		}
	}
	return true;
}

bool XMLWriter::CheckFileOpen()
{
	if (mFile != NULL)
	{
		return true;
	}

	Fail("No File Opened for writing");
	return false;
}