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 "nodevisitor.h" 00026 #include "property.h" 00027 #include "statement.h" 00028 00029 #include <krandom.h> 00030 00031 #include <QtCore/QList> 00032 #include <QtCore/QString> 00033 00034 namespace Syndication { 00035 namespace RDF { 00036 00037 class Resource::ResourcePrivate 00038 { 00039 public: 00040 00041 QString uri; 00042 Model model; 00043 bool isAnon; 00044 unsigned int id; 00045 00046 bool operator==(const ResourcePrivate& other) const 00047 { 00048 if (!isAnon && !other.isAnon) 00049 return uri == other.uri; 00050 else 00051 return id == other.id; 00052 } 00053 }; 00054 00055 Resource::Resource(const Resource& other) : Node(other) 00056 { 00057 *this = other; 00058 } 00059 00060 Resource::Resource() : d() 00061 { 00062 } 00063 00064 Resource::Resource(const QString& uri) : d(new ResourcePrivate) 00065 { 00066 if (uri.isNull()) 00067 { 00068 d->uri = KRandom::randomString(10); // TODO: ensure uniqueness 00069 d->isAnon = true; 00070 } 00071 else 00072 { 00073 d->uri = uri; 00074 d->isAnon = false; 00075 } 00076 00077 d->id = idCounter++; 00078 } 00079 00080 Resource::~Resource() 00081 { 00082 } 00083 00084 Resource& Resource::operator=(const Resource& other) 00085 { 00086 d = other.d; 00087 return *this; 00088 } 00089 00090 bool Resource::operator==(const Node& other) const 00091 { 00092 const Resource* o2 = dynamic_cast<const Resource*>(&other); 00093 if (!o2) 00094 return false; 00095 00096 if (!d || !o2->d) 00097 return d == o2->d; 00098 return *d == *(o2->d); 00099 } 00100 00101 bool Resource::hasProperty(PropertyPtr property) const 00102 { 00103 return d ? d->model.resourceHasProperty(this, property) : false; 00104 } 00105 00106 StatementPtr Resource::property(PropertyPtr property) const 00107 { 00108 StatementPtr ptr(new Statement()); 00109 if (d) 00110 ptr = d->model.resourceProperty(this, property); 00111 return ptr; 00112 } 00113 00114 QList<StatementPtr> Resource::properties(PropertyPtr property) const 00115 { 00116 if (d) 00117 return d->model.resourceProperties(this, property); 00118 return QList<StatementPtr>(); 00119 } 00120 00121 Resource* Resource::clone() const 00122 { 00123 return new Resource(*this); 00124 } 00125 00126 void Resource::accept(NodeVisitor* visitor, NodePtr ptr) 00127 { 00128 ResourcePtr rptr = boost::static_pointer_cast<Resource>(ptr); 00129 if (!visitor->visitResource(rptr)) 00130 Node::accept(visitor, ptr); 00131 } 00132 00133 unsigned int Resource::id() const 00134 { 00135 return d ? d->id : 0; 00136 } 00137 00138 bool Resource::isNull() const 00139 { 00140 return !d; 00141 } 00142 00143 Model Resource::model() const 00144 { 00145 return d ? d->model : Model(); 00146 } 00147 00148 bool Resource::isResource() const 00149 { 00150 return true; 00151 } 00152 00153 bool Resource::isProperty() const 00154 { 00155 return false; 00156 } 00157 00158 bool Resource::isLiteral() const 00159 { 00160 return false; 00161 } 00162 00163 bool Resource::isAnon() const 00164 { 00165 return d ? d->isAnon : false; 00166 } 00167 00168 bool Resource::isSequence() const 00169 { 00170 return false; 00171 } 00172 00173 void Resource::setModel(const Model& model) 00174 { 00175 if (d) 00176 d->model = model; 00177 } 00178 00179 void Resource::setId(unsigned int id) 00180 { 00181 if (d) 00182 d->id = id; 00183 } 00184 00185 QString Resource::text() const 00186 { 00187 return QString(); 00188 } 00189 00190 QString Resource::uri() const 00191 { 00192 return d ? d->uri : QString(); 00193 } 00194 00195 } // namespace RDF 00196 } // namespace Syndication