libmusicbrainz4
4.0.3
|
00001 /* -------------------------------------------------------------------------- 00002 00003 libmusicbrainz4 - Client library to access MusicBrainz 00004 00005 Copyright (C) 2011 Andrew Hawkins 00006 00007 This file is part of libmusicbrainz4. 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of v2 of the GNU Lesser General Public 00011 License as published by the Free Software Foundation. 00012 00013 libmusicbrainz4 is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this library. If not, see <http://www.gnu.org/licenses/>. 00020 00021 $Id$ 00022 00023 ----------------------------------------------------------------------------*/ 00024 00025 #ifndef _MUSICBRAINZ4_ENTITY_H 00026 #define _MUSICBRAINZ4_ENTITY_H 00027 00028 #include <iostream> 00029 #include <string> 00030 #include <sstream> 00031 #include <map> 00032 00033 #include "musicbrainz4/xmlParser.h" 00034 00035 namespace MusicBrainz4 00036 { 00037 class CEntityPrivate; 00038 00039 class CRelationListList; 00040 00041 class CEntity 00042 { 00043 public: 00044 CEntity(); 00045 CEntity(const CEntity& Other); 00046 CEntity& operator =(const CEntity& Other); 00047 virtual ~CEntity(); 00048 00049 virtual CEntity *Clone()=0; 00050 00051 void Parse(const XMLNode& Node); 00052 00053 std::map<std::string,std::string> ExtAttributes() const; 00054 std::map<std::string,std::string> ExtElements() const; 00055 00056 virtual std::ostream& Serialise(std::ostream& os) const; 00057 static std::string GetElementName(); 00058 00059 protected: 00060 void ProcessRelationList(const XMLNode& Node, CRelationListList* & RetVal); 00061 00062 template<typename T> 00063 void ProcessItem(const XMLNode& Node, T* & RetVal) 00064 { 00065 RetVal=new T(Node); 00066 } 00067 00068 template<class T> 00069 void ProcessItem(const XMLNode& Node, T& RetVal) 00070 { 00071 std::stringstream os; 00072 if (Node.getText()) 00073 os << (const char *)Node.getText(); 00074 00075 os >> RetVal; 00076 if (os.fail()) 00077 { 00078 std::cerr << "Error parsing value '"; 00079 if (Node.getText()) 00080 std::cerr << Node.getText(); 00081 std::cerr << "'" << std::endl; 00082 } 00083 } 00084 00085 template<typename T> 00086 void ProcessItem(const std::string& Text, T& RetVal) 00087 { 00088 std::stringstream os; 00089 os << Text; 00090 00091 os >> RetVal; 00092 if (os.fail()) 00093 { 00094 std::cerr << "Error parsing value '" << Text << "'" << std::endl; 00095 } 00096 } 00097 00098 void ProcessItem(const XMLNode& Node, std::string& RetVal) 00099 { 00100 if (Node.getText()) 00101 RetVal=Node.getText(); 00102 } 00103 00104 virtual void ParseAttribute(const std::string& Name, const std::string& Value)=0; 00105 virtual void ParseElement(const XMLNode& Node)=0; 00106 00107 private: 00108 CEntityPrivate *m_d; 00109 00110 void Cleanup(); 00111 }; 00112 } 00113 00114 std::ostream& operator << (std::ostream& os, const MusicBrainz4::CEntity& Entity); 00115 00116 #endif