File: dom_document.cpp

package info (click to toggle)
clanlib 1.0~svn3827-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 24,632 kB
  • ctags: 16,580
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,743; ansic: 463; perl: 424; php: 247; sh: 53
file content (312 lines) | stat: -rw-r--r-- 8,710 bytes parent folder | download | duplicates (7)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
**  ClanLib SDK
**  Copyright (c) 1997-2005 The ClanLib Team
**
**  This software is provided 'as-is', without any express or implied
**  warranty.  In no event will the authors be held liable for any damages
**  arising from the use of this software.
**
**  Permission is granted to anyone to use this software for any purpose,
**  including commercial applications, and to alter it and redistribute it
**  freely, subject to the following restrictions:
**
**  1. The origin of this software must not be misrepresented; you must not
**     claim that you wrote the original software. If you use this software
**     in a product, an acknowledgment in the product documentation would be
**     appreciated but is not required.
**  2. Altered source versions must be plainly marked as such, and must not be
**     misrepresented as being the original software.
**  3. This notice may not be removed or altered from any source distribution.
**
**  Note: Some of the libraries ClanLib may link to may have additional
**  requirements or restrictions.
**
**  File Author(s):
**
**    Magnus Norddahl
**    (if your name is missing here, please add it)
*/

#include "Core/precomp.h"
#include "API/Core/XML/dom_document.h"
#include "API/Core/XML/dom_document_type.h"
#include "API/Core/XML/dom_implementation.h"
#include "API/Core/XML/dom_element.h"
#include "API/Core/XML/dom_document_fragment.h"
#include "API/Core/XML/dom_text.h"
#include "API/Core/XML/dom_comment.h"
#include "API/Core/XML/dom_cdata_section.h"
#include "API/Core/XML/dom_processing_instruction.h"
#include "API/Core/XML/dom_attr.h"
#include "API/Core/XML/dom_entity_reference.h"
#include "API/Core/XML/dom_node_list.h"
#include "API/Core/XML/xml_tokenizer.h"
#include "API/Core/XML/xml_writer.h"
#include "API/Core/XML/xml_token.h"
#include "dom_document_generic.h"
#include <stack>

#include "API/Core/XML/xml_token_load.h"
#include "API/Core/XML/xml_token_save.h"
#include "API/Core/XML/xml_token_string.h"

/////////////////////////////////////////////////////////////////////////////
// CL_DomDocument construction:

CL_DomDocument::CL_DomDocument() : CL_DomNode(CL_SharedPtr<CL_DomNode_Generic>(new CL_DomDocument_Generic))
{
}

CL_DomDocument::CL_DomDocument(CL_InputSource *input, bool delete_input, bool eat_whitespace)
: CL_DomNode(CL_SharedPtr<CL_DomNode_Generic>(new CL_DomDocument_Generic))
{
	load(input, delete_input, eat_whitespace);
}

CL_DomDocument::CL_DomDocument(const CL_SharedPtr<CL_DomNode_Generic> &impl) : CL_DomNode(impl)
{
}

CL_DomDocument::~CL_DomDocument()
{
}

/////////////////////////////////////////////////////////////////////////////
// CL_DomDocument attributes:

CL_DomDocumentType CL_DomDocument::get_doctype()
{
	return CL_DomDocumentType(*this);
}

CL_DomImplementation CL_DomDocument::get_implementation()
{
	return CL_DomImplementation(*this);
}

CL_DomElement CL_DomDocument::get_document_element()
{
	CL_DomNode cur(impl->first_child);
	while (!cur.is_null())
	{
		if (cur.is_element()) return cur.to_element();
		cur = cur.get_next_sibling();
	}
	return CL_DomElement();
}

/////////////////////////////////////////////////////////////////////////////
// CL_DomDocument operations:

CL_DomElement CL_DomDocument::create_element(const std::string &tag_name)
{
	return CL_DomElement(*this, tag_name);
}

CL_DomDocumentFragment CL_DomDocument::create_document_fragment()
{
	return CL_DomDocumentFragment(*this);
}

CL_DomText CL_DomDocument::create_text_node(const std::string &data)
{
	return CL_DomText(*this, data);
}

CL_DomComment CL_DomDocument::create_comment(const std::string &data)
{
	return CL_DomComment(*this, data);
}

CL_DomCDATASection CL_DomDocument::create_cdata_section(const std::string &data)
{
	return CL_DomCDATASection(*this, data);
}

CL_DomProcessingInstruction CL_DomDocument::create_processing_instruction(
	const std::string &target,
	const std::string &data)
{
	return CL_DomProcessingInstruction(*this, target, data);
}

CL_DomAttr CL_DomDocument::create_attribute(const std::string &name)
{
	return CL_DomAttr(*this, name);
}

CL_DomEntityReference CL_DomDocument::create_entity_reference(const std::string &name)
{
	return CL_DomEntityReference(*this, name);
}

CL_DomNodeList CL_DomDocument::get_elements_by_tag_name(const std::string &tag_name)
{
	return CL_DomNodeList(*this, tag_name);
}

std::vector<CL_DomNode> CL_DomDocument::load(
	CL_InputSource *input,
	bool delete_input,
	bool eat_whitespace,
	CL_DomNode insert_point)
{
	clear_all();

	CL_XMLTokenizer tokenizer(input, delete_input);
	tokenizer.set_eat_whitespace(eat_whitespace);

	if (insert_point.is_element() == false)
		insert_point = *this;
		
	std::stack<CL_DomNode> node_stack;
	node_stack.push(insert_point);

	std::vector<CL_DomNode> result;
	
	try
	{
		CL_XMLTokenLoad cur_token = tokenizer.next();
		while (cur_token.get_type() != CL_XMLToken::NULL_TOKEN)
		{
			switch (cur_token.get_type())
			{
			case CL_XMLToken::TEXT_TOKEN:
				node_stack.top().append_child(create_text_node(cur_token.get_value()));
				if (node_stack.top() == insert_point)
					result.push_back(node_stack.top().get_last_child());
				break;

			case CL_XMLToken::CDATA_SECTION_TOKEN:
				node_stack.top().append_child(create_cdata_section(cur_token.get_value()));
				if (node_stack.top() == insert_point)
					result.push_back(node_stack.top().get_last_child());
				break;

			case CL_XMLToken::ELEMENT_TOKEN:
				if (cur_token.get_variant() != CL_XMLToken::END)
				{
					CL_DomElement element = create_element(cur_token.get_name());
					node_stack.top().append_child(element);
					if (node_stack.top() == insert_point)
						result.push_back(node_stack.top().get_last_child());

					int size = cur_token.get_attributes_number();
					for (int i=0; i<size; i++)
					{
						std::pair<CL_XMLTokenString, CL_XMLTokenString> const & attribute = cur_token.get_attribute_fast(i);
						element.set_attribute(attribute.first.to_string(), attribute.second.to_string());
					}
				
					if (cur_token.get_variant() == CL_XMLToken::BEGIN)
						node_stack.push(element);
				}
				else
				{
					node_stack.pop();
					if (node_stack.empty()) throw CL_Error("Malformed XML tree!");
				}
				break;

			case CL_XMLToken::NULL_TOKEN: 
				break;

			case CL_XMLToken::ENTITY_REFERENCE_TOKEN: 
				break;

			case CL_XMLToken::ENTITY_TOKEN: 
				break;
			
			case CL_XMLToken::COMMENT_TOKEN:
				break;

			case CL_XMLToken::DOCUMENT_TYPE_TOKEN:
				break;

			case CL_XMLToken::NOTATION_TOKEN:
				break;

			case CL_XMLToken::PROCESSING_INSTRUCTION_TOKEN:
				break;
			}		

			cur_token = tokenizer.next();
		}
	}
	catch (CL_Error e)
	{
		for (std::vector<CL_DomNode>::size_type i = 0; i < result.size(); i++)
		{
			insert_point.remove_child(result[i]);
		}
		throw e;
	}
	return result;
}

void CL_DomDocument::save(CL_OutputSource *output, bool delete_output, bool insert_whitespace)
{
	CL_XMLWriter writer(output, delete_output);
	writer.set_insert_whitespace(insert_whitespace);

	std::stack<CL_DomNode> node_stack;
	CL_DomNode cur_node = get_first_child();
	while (!cur_node.is_null())
	{
		// Create opening node:
		CL_XMLTokenSave opening_node;
		opening_node.set_type((CL_XMLToken::TokenType) cur_node.get_node_type());
		opening_node.set_variant(cur_node.has_child_nodes() ? CL_XMLToken::BEGIN : CL_XMLToken::SINGLE);
		opening_node.set_name(cur_node.get_node_name());
		opening_node.set_value(cur_node.get_node_value());
		if (cur_node.is_element())
		{
			for (int i = 0; i < cur_node.impl->attributes.get_length(); ++i)
			{
				opening_node.set_attribute(cur_node.impl->attributes.item(i).to_attr().get_name(),
													cur_node.impl->attributes.item(i).to_attr().get_value());
			}
		}
		writer.write(opening_node);

		// Create any possible child nodes:
		if (cur_node.has_child_nodes())
		{
			node_stack.push(cur_node);
			cur_node = cur_node.get_first_child();
			continue;
		}

		// Create closing nodes until we reach next opening node in tree:
		while (true)
		{
			if (cur_node.has_child_nodes())
			{
				CL_XMLTokenSave closing_node;
				closing_node.set_type((CL_XMLToken::TokenType) cur_node.get_node_type());
				closing_node.set_name(cur_node.get_node_name());
				closing_node.set_variant(CL_XMLToken::END);
				writer.write(closing_node);
			}

			cur_node = cur_node.get_next_sibling();
			if (!cur_node.is_null()) break;
			if (node_stack.empty()) break;

			cur_node = node_stack.top();
			node_stack.pop();
		}
	}
}

void CL_DomDocument::clear_all()
{
	while (!get_first_child().is_null())
	{
		CL_DomNode node = get_first_child();
		remove_child(node);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CL_DomDocument implementation: