summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/htmlexport/xmlutils.h
blob: 8f066ae071ca4363f7a4e8a63279ce50db366565 (plain)
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
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
/*
A KIPI plugin to generate HTML image galleries
Copyright 2006 by Aurelien Gateau <aurelien dot gateau at free.fr>

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 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, Cambridge, MA 02110-1301, USA.

*/
#ifndef XMLUTILS_H
#define XMLUTILS_H

#include <kdebug.h>

#include <libxml/xmlwriter.h>

namespace KIPIHTMLExport {


/**
 * A simple wrapper for a C structure pointed to by @Ptr, which must be freed
 * with @freeFcn
 */
template <class Ptr, void(*freeFcn)(Ptr)>
class CWrapper {
public:
	CWrapper() : mPtr(0) {}
	CWrapper(Ptr ptr)
	: mPtr(ptr) {}

	~CWrapper() {
		freeFcn(mPtr);
	}

	operator Ptr() const {
		return mPtr;
	}

	bool operator!() const {
		return !mPtr;
	}

	void assign(Ptr ptr) {
		if (mPtr) freeFcn(mPtr);
		mPtr=ptr;
	}

private:
	Ptr mPtr;
};


/**
 * Simple wrapper around xmlTextWriter
 */
class XMLWriter {
public:
	bool open(const TQString& name) {
		xmlTextWriterPtr ptr=xmlNewTextWriterFilename(name.local8Bit(), 0);
		if (!ptr) return false;
		mWriter.assign(ptr);

		int rc=xmlTextWriterStartDocument(ptr, NULL, "UTF-8", NULL);
		if (rc<0) {
			mWriter.assign(0);
			return false;
		}

		xmlTextWriterSetIndent(ptr, 1);

		return true;
	}

	operator xmlTextWriterPtr() const {
		return mWriter;
	}

	void writeElement(const char* element, const TQString& value) {
		xmlTextWriterWriteElement(mWriter, BAD_CAST element, BAD_CAST value.utf8().data());
	}

	void writeElement(const char* element, int value) {
		writeElement(element, TQString::number(value));
	}

private:
	CWrapper<xmlTextWriterPtr,xmlFreeTextWriter> mWriter;
};


/**
 * A list of attributes for an XML element. To be used with @ref XMLElement
 */
class XMLAttributeList {
	typedef TQMap<TQString, TQString> Map;
public:
	void write(XMLWriter& writer) const {
		Map::const_iterator it=mMap.begin();
		Map::const_iterator end=mMap.end();
		for (; it!=end; ++it) {
			xmlTextWriterWriteAttribute(writer,
				BAD_CAST it.key().ascii(),
				BAD_CAST it.data().utf8().data());
		}
	}
	
	void append(const TQString& key, const TQString& value) {
		mMap[key]=value;
	}
	
	void append(const TQString& key, int value) {
		mMap[key]=TQString::number(value);
	}

private:
	Map mMap;
};


/**
 * A class to generate an XML element
 */
class XMLElement {
public:
	XMLElement(XMLWriter& writer, const TQString& element, const XMLAttributeList* attributeList=0)
	: mWriter(writer)
	{
		xmlTextWriterStartElement(writer, BAD_CAST element.ascii());
		if (attributeList) {
			attributeList->write(writer);
		}
	}

	~XMLElement() {
		xmlTextWriterEndElement(mWriter);
	}

private:
	XMLWriter& mWriter;
};


} // namespace

#endif /* XMLUTILS_H */