Warning, file /office/calligra/libs/rdf/KoRdfPrefixMapping.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KoRdfPrefixMapping.h"
0021 #include <KoTextRdfCore.h>
0022 #include <Soprano/Soprano>
0023 #include <kdebug.h>
0024 using namespace Soprano;
0025 
0026 class KoRdfPrefixMappingPrivate
0027 {
0028 public:
0029 
0030     KoDocumentRdf *m_rdf;
0031 
0032     /**
0033      * prefix -> uri map.
0034      * gives speed preference to prefix resolution over
0035      * turning a uri into a prefixed shorter string
0036          */
0037     QMap<QString, QString> m_mappings;
0038 
0039     KoRdfPrefixMappingPrivate(KoDocumentRdf *rdf)
0040         :
0041         m_rdf (rdf)
0042         {}
0043 };
0044 
0045 
0046 KoRdfPrefixMapping::KoRdfPrefixMapping(KoDocumentRdf *rdf)
0047     : d (new KoRdfPrefixMappingPrivate(rdf))
0048 {
0049     insert("pkg", "http://docs.oasis-open.org/ns/office/1.2/meta/pkg#");
0050     insert("odf", "http://docs.oasis-open.org/ns/office/1.2/meta/odf#");
0051     insert("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
0052     insert("xhtml", "http://www.w3.org/1999/xhtml");
0053     insert("xsd", "http://www.w3.org/2001/XMLSchema");
0054     insert("xsi", "http://www.w3.org/2001/XMLSchema-instance");
0055     insert("foaf", "http://xmlns.com/foaf/0.1/");
0056     insert("geo84", "http://www.w3.org/2003/01/geo/wgs84_pos#");
0057     insert("dcterms", "http://dublincore.org/documents/dcmi-terms/#");
0058     insert("cite", "http://docs.oasis-open.org/prototype/opendocument/citation#"); //FIXME: haven't found this in 1.2 specs
0059     insert("cal", "http://www.w3.org/2002/12/cal/icaltzd#");
0060 }
0061 
0062 KoRdfPrefixMapping::~KoRdfPrefixMapping()
0063 {
0064     delete d;
0065 }
0066 
0067 QString KoRdfPrefixMapping::canonPrefix(const QString &pname) const
0068 {
0069     QString name = pname;
0070     int idx = name.indexOf(':');
0071     if (idx >= 0) {
0072         name = name.left(idx + 1);
0073     }
0074     if (!name.endsWith(':')) {
0075         name += ':';
0076     }
0077     return name;
0078 }
0079 
0080 
0081 QString KoRdfPrefixMapping::URItoPrefexedLocalname(const QString &uri) const
0082 {
0083     for (QMap<QString,QString>::const_iterator mi = d->m_mappings.constBegin(); mi != d->m_mappings.constEnd(); ++mi) {
0084         if (uri.startsWith(mi.value())) {
0085             QString ret = mi.key() + uri.mid(mi.value().length());
0086             return ret;
0087         }
0088     }
0089     return uri;
0090 }
0091 
0092 QString KoRdfPrefixMapping::PrefexedLocalnameToURI(const QString &pname) const
0093 {
0094     QString pfx = canonPrefix(pname);
0095     if (pfx.isEmpty()) {
0096         return pname;
0097     }
0098     QMap<QString,QString>::const_iterator mi = d->m_mappings.constFind(pfx);
0099     if (mi == d->m_mappings.constEnd())
0100         return pname;
0101     return mi.value() + pname.mid(mi.key().length());
0102 }
0103 
0104 QString KoRdfPrefixMapping::prefexToURI(const QString &pfx) const
0105 {
0106     const QString prefix = canonPrefix(pfx);
0107     QMap<QString,QString>::const_iterator mi = d->m_mappings.constFind(prefix);
0108     if (mi == d->m_mappings.constEnd()) {
0109         return QString();
0110     }
0111     return mi.value();
0112 }
0113 
0114 void KoRdfPrefixMapping::dump() const
0115 {
0116     kDebug(30015) << d->m_mappings;
0117 }
0118 
0119 void KoRdfPrefixMapping::insert(const QString &prefix, const QString &url)
0120 {
0121     QString fixedPrefix = canonPrefix(prefix);
0122     //kDebug(30015) << " prefix:" << fixedPrefix << " url:" << url;
0123     d->m_mappings.insert(fixedPrefix, url);
0124 }
0125 
0126 void KoRdfPrefixMapping::remove(const QString &prefix)
0127 {
0128     QString fixedPrefix = canonPrefix(prefix);
0129     //kDebug(30015) << " prefix:" << fixedPrefix;
0130     d->m_mappings.remove(fixedPrefix);
0131 }
0132 
0133 void KoRdfPrefixMapping::load(QSharedPointer<Soprano::Model> model)
0134 {
0135     QString nodePrefix = "http://calligra.org/rdf/prefixmapping/";
0136     Node rdfNil = Node::createResourceNode(QUrl("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"));
0137     Node rdfFirst = Node::createResourceNode(QUrl("http://www.w3.org/1999/02/22-rdf-syntax-ns#first"));
0138     Node rdfRest = Node::createResourceNode(QUrl("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest"));
0139     Soprano::Node ListHeadSubject = Node::createResourceNode(QUrl(nodePrefix + "list"));
0140     QList<Statement> listNodes = KoTextRdfCore::loadList(model, ListHeadSubject);
0141     //kDebug(30015) << "found count:" << listNodes.size();
0142     foreach (const Soprano::Statement &s, listNodes) {
0143         Soprano::Node dataBNode = s.object();
0144         QString prefix = KoTextRdfCore::getObject(model, dataBNode,
0145                          Node::createResourceNode(QUrl(nodePrefix + "prefix"))).toString();
0146         QString url = KoTextRdfCore::getObject(model, dataBNode,
0147                                                Node::createResourceNode(QUrl(nodePrefix + "url"))).toString();
0148         //kDebug(30015) << "found prefix:" << prefix << " url:" << url;
0149         insert(prefix, url);
0150     }
0151 }
0152 
0153 void KoRdfPrefixMapping::save(QSharedPointer<Soprano::Model> model, Soprano::Node context) const
0154 {
0155     QString nodePrefix = "http://calligra.org/rdf/prefixmapping/";
0156     Node rdfNil = Node::createResourceNode(QUrl("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"));
0157     Node rdfFirst = Node::createResourceNode(QUrl("http://www.w3.org/1999/02/22-rdf-syntax-ns#first"));
0158     Node rdfRest = Node::createResourceNode(QUrl("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest"));
0159     Soprano::Node dataBNode = model->createBlankNode();
0160     QList< Soprano::Node > dataBNodeList;
0161     QMap<QString,QString>::const_iterator mi = d->m_mappings.constBegin();
0162     QMap<QString,QString>::const_iterator me = d->m_mappings.constEnd();
0163     for (; mi != me; ++mi) {
0164         //kDebug(30015) << "saving prefix:" << mi.key() << " url:" << mi.value();
0165         dataBNode = model->createBlankNode();
0166         model->addStatement(dataBNode,Node::createResourceNode(QUrl(nodePrefix + "prefix")),
0167             Node::createLiteralNode(mi.key()), context);
0168         model->addStatement(dataBNode, Node::createResourceNode(QUrl(nodePrefix + "url")),
0169             Node::createLiteralNode(mi.value()), context);
0170         dataBNodeList << dataBNode;
0171     }
0172     Soprano::Node ListHeadSubject = Node::createResourceNode(QUrl(nodePrefix + "list"));
0173     KoTextRdfCore::saveList(model, ListHeadSubject, dataBNodeList, context);
0174 }
0175 
0176 QMap<QString, QString> KoRdfPrefixMapping::mappings() const
0177 {
0178     return d->m_mappings;
0179 }
0180