syndication/rdf
resource.cpp
00001 /* 00002 * This file is part of the syndication library 00003 * 00004 * Copyright (C) 2006 Frank Osterfeld <osterfeld@kde.org> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 * 00021 */ 00022 00023 #include "resource.h" 00024 #include "model.h" 00025 #include "model_p.h" 00026 #include "nodevisitor.h" 00027 #include "property.h" 00028 #include "statement.h" 00029 00030 #include <krandom.h> 00031 00032 #include <QtCore/QList> 00033 #include <QtCore/QString> 00034 00035 #include <boost/weak_ptr.hpp> 00036 00037 using namespace boost; 00038 00039 namespace Syndication { 00040 namespace RDF { 00041 00042 class Resource::ResourcePrivate 00043 { 00044 public: 00045 00046 QString uri; 00047 weak_ptr<Model::ModelPrivate> model; 00048 bool isAnon; 00049 unsigned int id; 00050 00051 bool operator==(const ResourcePrivate& other) const 00052 { 00053 if (!isAnon && !other.isAnon) 00054 return uri == other.uri; 00055 else 00056 return id == other.id; 00057 } 00058 }; 00059 00060 Resource::Resource(const Resource& other) : Node(other) 00061 { 00062 *this = other; 00063 } 00064 00065 Resource::Resource() : d() 00066 { 00067 } 00068 00069 Resource::Resource(const QString& uri) : d(new ResourcePrivate) 00070 { 00071 if (uri.isNull()) 00072 { 00073 d->uri = KRandom::randomString(10); // TODO: ensure uniqueness 00074 d->isAnon = true; 00075 } 00076 else 00077 { 00078 d->uri = uri; 00079 d->isAnon = false; 00080 } 00081 00082 d->id = idCounter++; 00083 } 00084 00085 Resource::~Resource() 00086 { 00087 } 00088 00089 Resource& Resource::operator=(const Resource& other) 00090 { 00091 d = other.d; 00092 return *this; 00093 } 00094 00095 bool Resource::operator==(const Node& other) const 00096 { 00097 const Resource* o2 = dynamic_cast<const Resource*>(&other); 00098 if (!o2) 00099 return false; 00100 00101 if (!d || !o2->d) 00102 return d == o2->d; 00103 return *d == *(o2->d); 00104 } 00105 00106 bool Resource::hasProperty(PropertyPtr property) const 00107 { 00108 if (!d) 00109 return false; 00110 const shared_ptr<Model::ModelPrivate> m = d->model.lock(); 00111 if (!m) 00112 return false; 00113 return m->resourceHasProperty(this, property); 00114 } 00115 00116 StatementPtr Resource::property(PropertyPtr property) const 00117 { 00118 StatementPtr ptr(new Statement()); 00119 if (!d) 00120 return ptr; 00121 const shared_ptr<Model::ModelPrivate> m = d->model.lock(); 00122 if (!m) 00123 return ptr; 00124 return m->resourceProperty(this, property); 00125 } 00126 00127 QList<StatementPtr> Resource::properties(PropertyPtr property) const 00128 { 00129 if (!d) 00130 return QList<StatementPtr>(); 00131 const shared_ptr<Model::ModelPrivate> m = d->model.lock(); 00132 if (!m) 00133 return QList<StatementPtr>(); 00134 00135 return m->resourceProperties(this, property); 00136 } 00137 00138 Resource* Resource::clone() const 00139 { 00140 return new Resource(*this); 00141 } 00142 00143 void Resource::accept(NodeVisitor* visitor, NodePtr ptr) 00144 { 00145 ResourcePtr rptr = boost::static_pointer_cast<Resource>(ptr); 00146 if (!visitor->visitResource(rptr)) 00147 Node::accept(visitor, ptr); 00148 } 00149 00150 unsigned int Resource::id() const 00151 { 00152 return d ? d->id : 0; 00153 } 00154 00155 bool Resource::isNull() const 00156 { 00157 return !d; 00158 } 00159 00160 Model Resource::model() const 00161 { 00162 if (!d) 00163 return Model(); 00164 00165 const shared_ptr<Model::ModelPrivate> mp = d->model.lock(); 00166 00167 Model m; 00168 00169 if (mp) 00170 m.d = mp; 00171 00172 return m; 00173 } 00174 00175 bool Resource::isResource() const 00176 { 00177 return true; 00178 } 00179 00180 bool Resource::isProperty() const 00181 { 00182 return false; 00183 } 00184 00185 bool Resource::isLiteral() const 00186 { 00187 return false; 00188 } 00189 00190 bool Resource::isAnon() const 00191 { 00192 return d ? d->isAnon : false; 00193 } 00194 00195 bool Resource::isSequence() const 00196 { 00197 return false; 00198 } 00199 00200 void Resource::setModel(const Model& model) 00201 { 00202 if (d) 00203 d->model = model.d; 00204 } 00205 00206 void Resource::setId(unsigned int id) 00207 { 00208 if (d) 00209 d->id = id; 00210 } 00211 00212 QString Resource::text() const 00213 { 00214 return QString(); 00215 } 00216 00217 QString Resource::uri() const 00218 { 00219 return d ? d->uri : QString(); 00220 } 00221 00222 } // namespace RDF 00223 } // namespace Syndication