/* -*- 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 #include #include #include #include #include #include #include #include #include using namespace com::sun::star; std::map SwRDFHelper::getStatements(const css::uno::Reference& xModel, const OUString& rType, const css::uno::Reference& xSubject) { std::map aRet; uno::Reference xComponentContext(comphelper::getProcessComponentContext()); uno::Reference xType = rdf::URI::create(xComponentContext, rType); uno::Reference xDocumentMetadataAccess(xModel, uno::UNO_QUERY); uno::Sequence< uno::Reference > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType); if (!aGraphNames.hasElements()) return aRet; for (const uno::Reference& xGraphName : aGraphNames) { uno::Reference xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName); uno::Reference xStatements = xGraph->getStatements(xSubject, uno::Reference(), uno::Reference()); while (xStatements->hasMoreElements()) { rdf::Statement aStatement = xStatements->nextElement().get(); aRet[aStatement.Predicate->getStringValue()] = aStatement.Object->getStringValue(); } } return aRet; } void SwRDFHelper::addStatement(const css::uno::Reference& xModel, const OUString& rType, const OUString& rPath, const css::uno::Reference& xSubject, const OUString& rKey, const OUString& rValue) { uno::Reference xComponentContext(comphelper::getProcessComponentContext()); uno::Reference xType = rdf::URI::create(xComponentContext, rType); uno::Reference xDocumentMetadataAccess(xModel, uno::UNO_QUERY); uno::Sequence< uno::Reference > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType); uno::Reference xGraphName; if (aGraphNames.hasElements()) xGraphName = aGraphNames[0]; else { uno::Sequence< uno::Reference > xTypes = { xType }; xGraphName = xDocumentMetadataAccess->addMetadataFile(rPath, xTypes); } uno::Reference xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName); uno::Reference xKey = rdf::URI::create(xComponentContext, rKey); uno::Reference xValue = rdf::Literal::create(xComponentContext, rValue); xGraph->addStatement(xSubject, xKey, xValue); } bool SwRDFHelper::hasMetadataGraph(const css::uno::Reference& xModel, const OUString& rType) { uno::Reference xComponentContext(comphelper::getProcessComponentContext()); uno::Reference xType = rdf::URI::create(xComponentContext, rType); uno::Reference xDocumentMetadataAccess(xModel, uno::UNO_QUERY); uno::Sequence< uno::Reference > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType); return aGraphNames.hasElements(); } void SwRDFHelper::removeStatement(const css::uno::Reference& xModel, const OUString& rType, const css::uno::Reference& xSubject, const OUString& rKey, const OUString& rValue) { uno::Reference xComponentContext(comphelper::getProcessComponentContext()); uno::Reference xType = rdf::URI::create(xComponentContext, rType); uno::Reference xDocumentMetadataAccess(xModel, uno::UNO_QUERY); uno::Sequence< uno::Reference > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType); if (!aGraphNames.hasElements()) return; uno::Reference xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(aGraphNames[0]); uno::Reference xKey = rdf::URI::create(xComponentContext, rKey); uno::Reference xValue = rdf::Literal::create(xComponentContext, rValue); xGraph->removeStatements(xSubject, xKey, xValue); } void SwRDFHelper::clearStatements(const css::uno::Reference& xModel, const OUString& rType, const css::uno::Reference& xSubject) { uno::Reference xComponentContext(comphelper::getProcessComponentContext()); uno::Reference xType = rdf::URI::create(xComponentContext, rType); uno::Reference xDocumentMetadataAccess(xModel, uno::UNO_QUERY); uno::Sequence< uno::Reference > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType); if (!aGraphNames.hasElements()) return; for (const uno::Reference& xGraphName : aGraphNames) { uno::Reference xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName); uno::Reference xStatements = xGraph->getStatements(xSubject, uno::Reference(), uno::Reference()); while (xStatements->hasMoreElements()) { rdf::Statement aStatement = xStatements->nextElement().get(); uno::Reference xKey = rdf::URI::create(xComponentContext, aStatement.Predicate->getStringValue()); uno::Reference xValue = rdf::Literal::create(xComponentContext, aStatement.Object->getStringValue()); xGraph->removeStatements(xSubject, xKey, xValue); } } } void SwRDFHelper::cloneStatements(const css::uno::Reference& xSrcModel, const css::uno::Reference& xDstModel, const OUString& rType, const css::uno::Reference& xSrcSubject, const css::uno::Reference& xDstSubject) { uno::Reference xComponentContext(comphelper::getProcessComponentContext()); uno::Reference xType = rdf::URI::create(xComponentContext, rType); uno::Reference xDocumentMetadataAccess(xSrcModel, uno::UNO_QUERY); uno::Sequence< uno::Reference > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType); if (!aGraphNames.hasElements()) return; for (const uno::Reference& xGraphName : aGraphNames) { uno::Reference xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName); uno::Reference xStatements = xGraph->getStatements(xSrcSubject, uno::Reference(), uno::Reference()); while (xStatements->hasMoreElements()) { const rdf::Statement aStatement = xStatements->nextElement().get(); const OUString sKey = aStatement.Predicate->getStringValue(); const OUString sValue = aStatement.Object->getStringValue(); addStatement(xDstModel, rType, xGraphName->getLocalName(), xDstSubject, sKey, sValue); } } } std::map SwRDFHelper::getTextNodeStatements(const OUString& rType, SwTextNode& rTextNode) { uno::Reference xTextNode(SwXParagraph::CreateXParagraph(*rTextNode.GetDoc(), &rTextNode), uno::UNO_QUERY); return getStatements(rTextNode.GetDoc()->GetDocShell()->GetBaseModel(), rType, xTextNode); } void SwRDFHelper::addTextNodeStatement(const OUString& rType, const OUString& rPath, SwTextNode& rTextNode, const OUString& rKey, const OUString& rValue) { uno::Reference xSubject(SwXParagraph::CreateXParagraph(*rTextNode.GetDoc(), &rTextNode), uno::UNO_QUERY); addStatement(rTextNode.GetDoc()->GetDocShell()->GetBaseModel(), rType, rPath, xSubject, rKey, rValue); } void SwRDFHelper::removeTextNodeStatement(const OUString& rType, SwTextNode& rTextNode, const OUString& rKey, const OUString& rValue) { uno::Reference xComponentContext(comphelper::getProcessComponentContext()); uno::Reference xType = rdf::URI::create(xComponentContext, rType); uno::Reference xDocumentMetadataAccess(rTextNode.GetDoc()->GetDocShell()->GetBaseModel(), uno::UNO_QUERY); uno::Sequence< uno::Reference > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType); if (!aGraphNames.hasElements()) return; uno::Reference xGraphName = aGraphNames[0]; uno::Reference xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName); uno::Reference xSubject(SwXParagraph::CreateXParagraph(*rTextNode.GetDoc(), &rTextNode), uno::UNO_QUERY); uno::Reference xKey = rdf::URI::create(xComponentContext, rKey); uno::Reference xValue = rdf::Literal::create(xComponentContext, rValue); xGraph->removeStatements(xSubject, xKey, xValue); } void SwRDFHelper::updateTextNodeStatement(const OUString& rType, const OUString& rPath, SwTextNode& rTextNode, const OUString& rKey, const OUString& rOldValue, const OUString& rNewValue) { uno::Reference xComponentContext(comphelper::getProcessComponentContext()); uno::Reference xType = rdf::URI::create(xComponentContext, rType); uno::Reference xDocumentMetadataAccess(rTextNode.GetDoc()->GetDocShell()->GetBaseModel(), uno::UNO_QUERY); uno::Sequence< uno::Reference > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType); uno::Reference xGraphName; if (aGraphNames.hasElements()) { xGraphName = aGraphNames[0]; } else { uno::Sequence< uno::Reference > xTypes = { xType }; xGraphName = xDocumentMetadataAccess->addMetadataFile(rPath, xTypes); } uno::Reference xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName); uno::Reference xSubject(SwXParagraph::CreateXParagraph(*rTextNode.GetDoc(), &rTextNode), uno::UNO_QUERY); uno::Reference xKey = rdf::URI::create(xComponentContext, rKey); if (aGraphNames.hasElements()) { // Remove the old value. uno::Reference xOldValue = rdf::Literal::create(xComponentContext, rOldValue); xGraph->removeStatements(xSubject, xKey, xOldValue); } // Now add it with new value. uno::Reference xNewValue = rdf::Literal::create(xComponentContext, rNewValue); xGraph->addStatement(xSubject, xKey, xNewValue); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */