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