2015-12-02 14:09:42 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rdfhelper.hxx>
|
|
|
|
|
2016-01-04 17:39:58 +01:00
|
|
|
#include <com/sun/star/rdf/Literal.hpp>
|
2015-12-02 14:09:42 +01:00
|
|
|
#include <com/sun/star/rdf/Statement.hpp>
|
|
|
|
#include <com/sun/star/rdf/URI.hpp>
|
|
|
|
#include <com/sun/star/rdf/XDocumentMetadataAccess.hpp>
|
|
|
|
|
|
|
|
#include <comphelper/processfactory.hxx>
|
|
|
|
|
|
|
|
#include <doc.hxx>
|
|
|
|
#include <docsh.hxx>
|
|
|
|
#include <ndtxt.hxx>
|
|
|
|
#include <unoparagraph.hxx>
|
|
|
|
|
|
|
|
using namespace com::sun::star;
|
|
|
|
|
2015-12-10 12:13:33 +01:00
|
|
|
std::map<OUString, OUString> SwRDFHelper::getTextNodeStatements(const OUString& rType, const SwTextNode& rNode)
|
2015-12-02 14:09:42 +01:00
|
|
|
{
|
|
|
|
std::map<OUString, OUString> aRet;
|
|
|
|
|
2015-12-10 12:13:33 +01:00
|
|
|
// We only read the node, but CreateXParagraph() needs a non-cost one.
|
2016-11-30 09:08:43 +01:00
|
|
|
auto& rTextNode = const_cast<SwTextNode&>(rNode);
|
2015-12-10 12:13:33 +01:00
|
|
|
|
2015-12-02 14:09:42 +01:00
|
|
|
uno::Reference<uno::XComponentContext> xComponentContext(comphelper::getProcessComponentContext());
|
|
|
|
uno::Reference<rdf::XURI> xType = rdf::URI::create(xComponentContext, rType);
|
|
|
|
uno::Reference<rdf::XDocumentMetadataAccess> xDocumentMetadataAccess(rTextNode.GetDoc()->GetDocShell()->GetBaseModel(), uno::UNO_QUERY);
|
|
|
|
uno::Sequence< uno::Reference<rdf::XURI> > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType);
|
|
|
|
if (!aGraphNames.hasElements())
|
|
|
|
return aRet;
|
|
|
|
|
|
|
|
uno::Reference<rdf::XResource> xTextNode(SwXParagraph::CreateXParagraph(*rTextNode.GetDoc(), &rTextNode), uno::UNO_QUERY);
|
|
|
|
for (const uno::Reference<rdf::XURI>& xGraphName : aGraphNames)
|
|
|
|
{
|
|
|
|
uno::Reference<rdf::XNamedGraph> xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName);
|
|
|
|
uno::Reference<container::XEnumeration> xStatements = xGraph->getStatements(xTextNode, uno::Reference<rdf::XURI>(), uno::Reference<rdf::XURI>());
|
|
|
|
while (xStatements->hasMoreElements())
|
|
|
|
{
|
|
|
|
rdf::Statement aStatement = xStatements->nextElement().get<rdf::Statement>();
|
|
|
|
aRet[aStatement.Predicate->getStringValue()] = aStatement.Object->getStringValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aRet;
|
|
|
|
}
|
|
|
|
|
2015-12-09 15:01:46 +01:00
|
|
|
void SwRDFHelper::addTextNodeStatement(const OUString& rType, const OUString& rPath, SwTextNode& rTextNode, const OUString& rKey, const OUString& rValue)
|
2015-12-09 12:33:36 +01:00
|
|
|
{
|
2015-12-09 15:01:46 +01:00
|
|
|
uno::Reference<uno::XComponentContext> xComponentContext(comphelper::getProcessComponentContext());
|
|
|
|
uno::Reference<rdf::XURI> xType = rdf::URI::create(xComponentContext, rType);
|
|
|
|
uno::Reference<rdf::XDocumentMetadataAccess> xDocumentMetadataAccess(rTextNode.GetDoc()->GetDocShell()->GetBaseModel(), uno::UNO_QUERY);
|
|
|
|
uno::Sequence< uno::Reference<rdf::XURI> > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType);
|
|
|
|
uno::Reference<rdf::XURI> xGraphName;
|
|
|
|
if (aGraphNames.hasElements())
|
|
|
|
xGraphName = aGraphNames[0];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uno::Sequence< uno::Reference<rdf::XURI> > xTypes = { xType };
|
|
|
|
xGraphName = xDocumentMetadataAccess->addMetadataFile(rPath, xTypes);
|
|
|
|
}
|
|
|
|
uno::Reference<rdf::XNamedGraph> xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName);
|
|
|
|
uno::Reference<rdf::XResource> xSubject(SwXParagraph::CreateXParagraph(*rTextNode.GetDoc(), &rTextNode), uno::UNO_QUERY);
|
|
|
|
uno::Reference<rdf::XURI> xKey = rdf::URI::create(xComponentContext, rKey);
|
2016-01-04 17:39:58 +01:00
|
|
|
uno::Reference<rdf::XLiteral> xValue = rdf::Literal::create(xComponentContext, rValue);
|
2015-12-09 15:01:46 +01:00
|
|
|
xGraph->addStatement(xSubject, xKey, xValue);
|
2015-12-09 12:33:36 +01:00
|
|
|
}
|
|
|
|
|
2017-08-02 22:19:00 -04:00
|
|
|
void SwRDFHelper::updateTextNodeStatement(const OUString& rType, const OUString& rPath, SwTextNode& rTextNode, const OUString& rKey, const OUString& rOldValue, const OUString& rNewValue)
|
|
|
|
{
|
|
|
|
uno::Reference<uno::XComponentContext> xComponentContext(comphelper::getProcessComponentContext());
|
|
|
|
uno::Reference<rdf::XURI> xType = rdf::URI::create(xComponentContext, rType);
|
|
|
|
uno::Reference<rdf::XDocumentMetadataAccess> xDocumentMetadataAccess(rTextNode.GetDoc()->GetDocShell()->GetBaseModel(), uno::UNO_QUERY);
|
|
|
|
uno::Sequence< uno::Reference<rdf::XURI> > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType);
|
|
|
|
uno::Reference<rdf::XURI> xGraphName;
|
|
|
|
if (aGraphNames.hasElements())
|
|
|
|
{
|
|
|
|
xGraphName = aGraphNames[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uno::Sequence< uno::Reference<rdf::XURI> > xTypes = { xType };
|
|
|
|
xGraphName = xDocumentMetadataAccess->addMetadataFile(rPath, xTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
uno::Reference<rdf::XNamedGraph> xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName);
|
|
|
|
uno::Reference<rdf::XResource> xSubject(SwXParagraph::CreateXParagraph(*rTextNode.GetDoc(), &rTextNode), uno::UNO_QUERY);
|
|
|
|
uno::Reference<rdf::XURI> xKey = rdf::URI::create(xComponentContext, rKey);
|
|
|
|
|
|
|
|
if (aGraphNames.hasElements())
|
|
|
|
{
|
|
|
|
// Remove the old value.
|
|
|
|
uno::Reference<rdf::XLiteral> xOldValue = rdf::Literal::create(xComponentContext, rOldValue);
|
|
|
|
xGraph->removeStatements(xSubject, xKey, xOldValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now add it with new value.
|
|
|
|
uno::Reference<rdf::XLiteral> xNewValue = rdf::Literal::create(xComponentContext, rNewValue);
|
|
|
|
xGraph->addStatement(xSubject, xKey, xNewValue);
|
|
|
|
}
|
|
|
|
|
2015-12-02 14:09:42 +01:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|