PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 56ms app.codeStats 0ms

/thirdparty/liblastfm2/src/core/XmlQuery.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 78 lines | 33 code | 9 blank | 36 comment | 1 complexity | 3870d26e354f4c9cdd4f79f133c2524e MD5 | raw file
 1/*
 2   Copyright 2009 Last.fm Ltd. 
 3      - Primarily authored by Max Howell, Jono Cole and Doug Mansell
 4
 5   This file is part of liblastfm.
 6
 7   liblastfm is free software: you can redistribute it and/or modify
 8   it under the terms of the GNU General Public License as published by
 9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   liblastfm is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with liblastfm.  If not, see <http://www.gnu.org/licenses/>.
19*/
20#ifndef LASTFM_XMLQUERY_H
21#define LASTFM_XMLQUERY_H
22
23#include <lastfm/global.h>
24#include <lastfm/ws.h>
25#include <QDomDocument>
26#include <QDomElement>
27
28namespace lastfm
29{
30    /** Qt's XmlQuery implementation is totally unimpressive, so this is a 
31      * hack that feels like jQuery */
32    class LASTFM_DLLEXPORT XmlQuery
33    {
34        QDomDocument domdoc;
35        QDomElement e;
36
37    public:
38        /** we assume the bytearray is an XML document, this object will then
39          * represent the documentElement of that document, eg. if this is a
40          * Last.fm webservice response:
41          *
42          * XmlQuery xq = lastfm::ws::parse(response);
43          * qDebug() << xq["artist"].text()
44          *
45          * Notice the lfm node is not referenced, that is because it is the
46          * document-element of the XML document.
47          */
48        XmlQuery( const QByteArray& ) throw( lastfm::ws::ParseError );
49        
50        XmlQuery( const QDomElement& e, const char* name = "" ) : e( e )
51        {
52            if (e.isNull()) qWarning() << "Expected node absent:" << name;
53        }
54
55        /** Selects a DIRECT child element, you can specify attributes like so:
56          *
57          * e["element"]["element attribute=value"].text();
58          */
59        XmlQuery operator[]( const QString& name ) const;
60        QString text() const { return e.text(); }
61        QString attribute( const QString& name ) const{ return e.attribute( name ); }
62        
63        /** selects all children with specified name, recursively */
64        QList<XmlQuery> children( const QString& named ) const;
65
66        operator QDomElement() const { return e; }
67    };
68}
69
70inline QDebug operator<<( QDebug d, const lastfm::XmlQuery& xq )
71{
72    QString s;
73    QTextStream t( &s, QIODevice::WriteOnly );
74    QDomElement(xq).save( t, 2 );
75    return d << s;
76}
77
78#endif