~ubuntu-branches/ubuntu/precise/me-tv/precise-proposed

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
/*
 * Copyright (C) 2008 Michael Lamothe
 *
 * This file is part of Me TV
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
 
#ifndef __XML_H__
#define __XML_H__

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/xmlsave.h>
#include "scope_log.h"
#include "string_utility.h"
#include "integer.h"
#include "exception.h"

class XmlNode
{
private:
	xmlNodePtr node;
	void create_node(xmlNodePtr n);
	
public:
	XmlNode(const XmlNode& n);
	XmlNode(xmlNodePtr node);
	
	xmlNodePtr create_child_node(const String& name);
	XmlNode& operator=(xmlNodePtr n);

	String get_attribute_value(const String& name) const;
	int get_int_attribute_value(const String& name) const;
	
	void set_content(const String& content, gboolean encode = true);
	void set_attribute(const String& name, const String& value);
	void set_attribute(const String& name, int value);
	void set_attribute(const String& name);
	void unlink();
	xmlNodePtr get_node();
	xmlNodePtr get_parent() const;
	String get_path() const;
	xmlDocPtr get_document();
};

class XmlDocument
{
private:
	xmlDocPtr document;
	String url;

public:
	XmlDocument();	
	XmlDocument(const XmlDocument& document);
	XmlDocument(const String& url, bool create_if_dne = true);
	~XmlDocument();
	
	xmlDocPtr get_document()	{ return document; }
	String get_url()			{ return url; }
	
	void load(const String& url, bool create_if_dne = true);
	void save(const String& to);
	void save();
	xmlNodePtr get_root_node();
	xmlNodePtr create_root_node(const String& name);
	
	String to_string(gboolean pretty = false) const;
};

class XPathResult
{
private:
	xmlXPathObjectPtr result;
public:
	XPathResult(const XPathResult& result);
	XPathResult(xmlXPathObjectPtr result);
	~XPathResult();
	
	int get_count();
	xmlNodePtr get_result(int index);
};

class XPath
{
private:			
	xmlXPathContextPtr context;

	void create(xmlDocPtr document);

public:
	XPath(XmlDocument& document);
	XPath(XmlNode& node);
	XPath(xmlNodePtr node);
	XPath(xmlDocPtr document);
	~XPath();
	
	xmlXPathObjectPtr evaluate_expression(const String& expression);
	xmlNodePtr evaluate_expression_single(const String& expression);
};

#endif