File indexing completed on 2024-05-26 16:15:57

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 #ifndef KO_TEXT_RDF_CORE_H
0021 #define KO_TEXT_RDF_CORE_H
0022 
0023 #include "kotext_export.h"
0024 
0025 #include <QSharedPointer>
0026 
0027 // this file can only be used by code that is built
0028 // with soprano enabled.
0029 #include <Soprano/Soprano>
0030 
0031 class KoStore;
0032 class KoXmlWriter;
0033 
0034 /**
0035  * @short Basic low level methods that are available to KoText objects
0036  *
0037  * Low level functionality such as streaming a Soprano::Model to and
0038  * from an ODF container is provided here so that both KoDocumentRdf
0039  * and other code in libs/kotext can share it.
0040  *
0041  * @author Ben Martin
0042  * @see KoDocumentRdf
0043  */
0044 namespace KoTextRdfCore
0045 {
0046 /**
0047  * Save the RDF selected triples from model to the store with the
0048  * given RDF/XML filename
0049  */
0050 bool saveRdf( QSharedPointer<Soprano::Model> model, Soprano::StatementIterator triples,
0051               KoStore *store, KoXmlWriter *manifestWriter, const QString &fileName);
0052 
0053 /**
0054  * Save the given RDF model to the manifest.rdf file. The idmap is used
0055  * to maintain xml:id links from the model so they will be valid with
0056  * the content.xml that generated the idmap.
0057  */
0058 bool createAndSaveManifest(QSharedPointer<Soprano::Model> model,
0059                            const QMap<QString, QString> &idmap, KoStore *store, KoXmlWriter *manifestWriter);
0060 
0061 /**
0062  * Load the manifest.rdf file from the ODF container store
0063  * into the model provided.
0064  */
0065 bool loadManifest(KoStore *store, QSharedPointer<Soprano::Model> model);
0066 
0067 /**
0068  * For debugging, dump the model to debugText along with the
0069  * given header message for identification
0070  */
0071 void dumpModel(const QString &message, QSharedPointer<Soprano::Model> model);
0072 
0073 /**
0074  * Load an Rdf linked list of statements. See saveList() for the
0075  * details. The return value of loadList() is the equivalent of
0076  * dataBNodeList in saveList().
0077  *
0078  * @see saveList()
0079  */
0080 QList<Soprano::Statement> KOTEXT_EXPORT loadList(QSharedPointer<Soprano::Model> model, Soprano::Node ListHeadSubject);
0081 
0082 /**
0083  * Save an Rdf List of data nodes into the model. Rdf defines a
0084  * linked list format in the
0085  * http://www.w3.org/1999/02/22-rdf-syntax-ns URI namespace using
0086  * first/rest to link the current element with the "rest" of the
0087  * list. A scheme that will be familiar to many lisp programmers
0088  * car/cdr. Unfortunately dealing with such lists directly is
0089  * clumsy so this and loadList() let you store a list of data
0090  * nodes and these methods create all the boilerplate Rdf triples
0091  * to store/read a simple QList of nodes to Rdf properly. You
0092  * supply the list header node ListHeadSubject which is normally
0093  * the subject that you want the list associated with in Rdf. The
0094  * other nodes used in the internal structure of the Rdf list are
0095  * just random bnodes as shown below. If you have a previous,
0096  * existing list then this method will remove those nodes first so
0097  * that the Rdf model does not grow with disgarded list nodes over
0098  * time.
0099  *
0100  * The old list nodes are removed if they exist, and a new list is
0101  * created starting at ListHeadSubject, and linking all the nodes
0102  * in dataBNodeList using the supplied rdf context. Use the
0103  * loadList() method to get the list dataBNodeList back from the
0104  * model again.
0105  *
0106  * The result will be like:
0107  * ListHeadSubject 22-rdf-syntax-ns\#first dataBNodeList[0]
0108  * ListHeadSubject 22-rdf-syntax-ns\#rest  bnodeA
0109  * bnodeA          22-rdf-syntax-ns\#first dataBNodeList[1]
0110  * bnodeA          22-rdf-syntax-ns\#rest  bnodeB
0111  * ...
0112  * bnodeZ          22-rdf-syntax-ns\#first dataBNodeList[N]
0113  * bnodeZ          22-rdf-syntax-ns\#rest  nil
0114  *
0115  */
0116 void KOTEXT_EXPORT saveList(QSharedPointer<Soprano::Model> model, Soprano::Node ListHeadSubject,
0117         QList<Soprano::Node> &dataBNodeList, Soprano::Node context);
0118 
0119 /**
0120  * Using model->removeStatements() will fail if the statement does not
0121  * exist in the model. This method is a bit sloppier in that it ignores
0122  * attempts to remove statements twice, or ones that no longer exist
0123  * in the model. This is handy for set based remove/add bulk updates
0124  * because you don't have to ensure that a statement is added only once
0125  * to the remove list.
0126  */
0127 void KOTEXT_EXPORT removeStatementsIfTheyExist( QSharedPointer<Soprano::Model> model,
0128                                                 const QList<Soprano::Statement> &removeList);
0129 
0130 /**
0131  * Given the Subj+Pred get the Object for the triple. If there are
0132  * more than one object, a random one from the possible candidates is
0133  * returned. This is mainly useful when you *know* there is only zero
0134  * or one object.
0135  */
0136 Soprano::Node KOTEXT_EXPORT getObject(QSharedPointer<Soprano::Model> model, Soprano::Node s, Soprano::Node p);
0137 
0138 QString KOTEXT_EXPORT getProperty(QSharedPointer<Soprano::Model> m,
0139                                   Soprano::Node subj,
0140                                   Soprano::Node pred,
0141                                   const QString &defval);
0142 QString KOTEXT_EXPORT optionalBindingAsString(Soprano::QueryResultIterator& it,
0143                                               const QString &bindingName,
0144                                               const QString &def = QString());
0145 QByteArray KOTEXT_EXPORT fileToByteArray(const QString &fileName);
0146 
0147 }
0148 #endif
0149