~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-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
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
/*
 * This file is part of the syndication library
 *
 * Copyright (C) 2006 Frank Osterfeld <osterfeld@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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 Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */
#ifndef SYNDICATION_ELEMENTWRAPPER_H
#define SYNDICATION_ELEMENTWRAPPER_H

#include <QtCore/QString>

#include <boost/shared_ptr.hpp>

#include "ksyndication_export.h"

class QDomElement;
template <class T> class QList;

namespace Syndication {

/**
 * A wrapper for XML elements. This is the base class for the (lazy) wrappers
 * used in the RSS2 and Atom parsers. The wrapped element can be accessed
 * via element(). It also contains several helper functions for XML processing.
 *
 * @author Frank Osterfeld
 */
class SYNDICATION_EXPORT ElementWrapper
{
    public:

        /**
         * creates a element wrapper wrapping a null element.
         * isNull() will return @c true for these instances.
         */
        ElementWrapper();

        /**
         * Copy constructor.The instances share the same element.
         * @param other the element wrapper to copy
         */
        ElementWrapper(const ElementWrapper& other);

        /**
         * Creates an element wrapper wrapping the DOM element @c element
         * @param element the element to wrap
         */
        ElementWrapper(const QDomElement& element); // implicit

        /**
         * destructor
         */
        virtual ~ElementWrapper();

        /**
         * Assigns another element wrapper to this one. Both instances
         * share the same wrapped element instance.
         *
         * @param other the element wrapper to assign
         * @return reference to this instance
         */
        ElementWrapper& operator=(const ElementWrapper& other);

        /**
         * compares two wrappers. Two wrappers are equal if and only if
         * the wrapped elements are equal.
         * @param other another element wrapper to compare to
         */
        bool operator==(const ElementWrapper& other) const;

        /**
         * returns the wrapped resource.
         */
        const QDomElement& element() const;

        /**
         * returns whether the wrapped element is a null element
         * @return @c true if isNull() is true for the wrapped element,
         * @c false otherwise
         */
        bool isNull() const;

        /**
         * returns the xml:base value to be used for the wrapped element.
         * The xml:base attribute establishes the base URI for resolving any
         * relative references found in its scope (its own element and all
         * descendants). (See also completeURI())
         *
         * @return the xml:base value, or a null string if not set
         */
        QString xmlBase() const;

        /**
         * returns the xml:lang value to be used for the wrapped element.
         * The xml:lang attribute indicates the natural language for its element
         * and all descendants.
         *
         * @return the xml:lang value, or a null string if not set
         */
        QString xmlLang() const;

        /**
         * completes relative URIs with a prefix specified via xml:base.
         *
         * Example:
         * @code
         * xml:base="http://www.foo.org/", uri="announcements/bar.html"
         * @endcode
         *
         * is completed to @c http://www.foo.org/announcements/bar.html
         *
         * See also xmlBase().
         *
         * @param uri a possibly relative URI
         * @return the resolved, absolute URI (using xml:base), if @c uri is
         * a relative, valid URI. If @c uri is not valid, absolute, or no
         * xml:base is set in the scope of this element, @c uri is returned
         * unmodified.
         */
        QString completeURI(const QString& uri) const;

        /**
         * extracts the text from a child element, respecting namespaces. If
         * there is more than one child with the same tag name, the first one is
         * processed.
         * For instance, when the wrapped element is @c &lt;hisElement>:
         * @code
         * <thisElement>
         *     <atom:title>Hi there</atom:title>
         * </thisElement>
         * @endcode
         * @code
         * extractElementText("http://www.w3.org/2005/Atom", "title")
         * @endcode
         * will return the text content of @c atom:title, "Hi there".
         * (Assuming that "atom" is defined as "http://www.w3.org/2005/Atom")
         *
         * @param namespaceURI the namespace URI of the element to extract
         * @param localName the local name (local within its namespace) of the
         * element to extract
         * @return the (trimmed) text content of @c localName, or a null string
         * if there is no such tag
         */

        QString extractElementTextNS(const QString& namespaceURI,
                                     const QString& localName) const;

        /**
         * extracts the text from a child element, ignoring namespaces. For
         * instance, when the wrapped element is @c &lt;thisElement>:
         * @code
         * <thisElement>
         *     <title>Hi there</title>
         * </thisElement>
         * @endcode
         * @c extractElementText("title") will return the text content
         * of @c title, "Hi there".
         *
         * @param tagName the name of the element to extract
         * @return the (trimmed) text content of @c tagName, or a null string if
         * there is no such tag
         */
        QString extractElementText(const QString& tagName) const;

        /**
         * returns all child elements with tag name @c tagName
         * Contrary to QDomElement::elementsByTagName() only direct descendents
         * are returned.
         *
         * @param tagName the tag name of the elements to extract
         * @return a list of child elements with the given tag name
         */
        QList<QDomElement> elementsByTagName(const QString& tagName) const;

        /**
         * returns the child nodes of the wrapped element as XML.
         *
         * See childNodesAsXML(const QDomElement& parent) for details
         * @return XML serialization of the wrapped element's children
         */
        QString childNodesAsXML() const;

        /**
         * concatenates the XML representations of all children. Example: If
         * @c parent is an @c xhtml:body element like
         * @code
         * <xhtml:body><p>foo</p><blockquote>bar</blockquote></xhtml:body>
         * @endcode
         * this function returns
         * @code
         * <p>foo</p><blockquote>bar</blockquote>
         * @endcode
         *
         * namespace and xml:base information are preserved.
         *
         * @param parent the DOM element whose children should be returned as
         * XML
         * @return XML serialization of parent's children
         */
        static QString childNodesAsXML(const QDomElement& parent);

        /**
         * returns all child elements with tag name @c tagname
         * and namespace URI @c nsURI.
         * Contrary to QDomElement::elementsByTagNameNS() only direct
         * descendents are returned
         *
         * @param nsURI the namespace URI
         * @param tagName the local name (local within its namespace) of the
         * element to search for
         * @return a list of child elements with the given namespace URI
         * and tag name
         */
        QList<QDomElement> elementsByTagNameNS(const QString& nsURI,
                                               const QString& tagName) const;

        /**
         * searches the direct children of the wrapped element for an element
         * with a given namespace and tag name.
         *
         * @param nsURI the namespace URI
         * @param tagName the local name (local within its namespace) of the
         * element to search for
         * @return the first child element with the given namespace URI and tag
         * name, or a null element if no such element was found.
         */
        QDomElement firstElementByTagNameNS(const QString& nsURI,
                                            const QString& tagName) const;

        /**
         * Returns the wrapped element's text or an empty string.
         * For more information, see QDomElement::text();
         */
        QString text() const;

        /**
         * Returns the attribute called name. If the attribute does not exist
         * defValue is returned.
         * (which is a null string by default).
         *
         * @param name tag name
         * @param defValue the default value
         */
        QString attribute(const QString& name,
                          const QString& defValue=QString()) const;

        /**
         * Returns the attribute with the local @c name localName and the
         * namespace URI @c nsURI.
         * If the attribute does not exist @c defValue is returned (which is a
         * null string by default).
         *
         * @param nsURI namespace URI
         * @param localName local tag name
         * @param defValue the default value
         */
        QString attributeNS(const QString& nsURI, const QString& localName,
                            const QString& defValue=QString()) const;


        /**
         * Returns true if this element has an attribute called @c name;
         * otherwise returns @c false.
         *
         * @param name the attribute name (without namespace)
         */
        bool hasAttribute(const QString& name) const;

        /**
         * Returns true if this element has an attribute with the local name
         * localName and the namespace URI nsURI; otherwise returns false.
         *
         * @param nsURI namespace URI
         * @param localName local attribute name
         */
        bool hasAttributeNS(const QString& nsURI, const QString& localName) const;

    private:

        class ElementWrapperPrivate;
        boost::shared_ptr<ElementWrapperPrivate> d;
};

} // namespace Syndication

#endif // SYNDICATION_ELEMENTWRAPPER_H