[Libreoffice-commits] core.git: sw/inc sw/source

Ashod Nakashian ashod.nakashian at collabora.co.uk
Sat Aug 26 21:48:30 UTC 2017


 sw/inc/rdfhelper.hxx             |   38 ++++++++++++++++
 sw/source/core/doc/rdfhelper.cxx |   89 +++++++++++++++++++++++++++++++++++----
 2 files changed, 120 insertions(+), 7 deletions(-)

New commits:
commit 714282573cd6fe8c9d95d3072c15d1e6cb94dc73
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Fri Aug 25 13:32:04 2017 -0400

    sw: new RDF helpers
    
    These are to help with adding paragraph
    signature metadata.
    
    Change-Id: Icac32c9e7937696f755ff3c99d619e97d56fc2e8
    Reviewed-on: https://gerrit.libreoffice.org/41590
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/sw/inc/rdfhelper.hxx b/sw/inc/rdfhelper.hxx
index c47ba0b5c9a6..dccd152d026f 100644
--- a/sw/inc/rdfhelper.hxx
+++ b/sw/inc/rdfhelper.hxx
@@ -16,20 +16,58 @@
 
 #include <swdllapi.h>
 
+#include <com/sun/star/uno/Reference.hxx>
+
 class SwTextNode;
 
+namespace com { namespace sun { namespace star {
+    namespace frame {
+        class XModel;
+    }
+    namespace rdf {
+        class XResource;
+    }
+}}}
+
 /// Provides access to RDF metadata on core objects.
 class SW_DLLPUBLIC SwRDFHelper
 {
 public:
+
+    /// Gets all (XResource, key, value) statements in RDF graphs of type rType.
+    static std::map<OUString, OUString> getStatements(const css::uno::Reference<css::frame::XModel>& xModel,
+                                                      const OUString& rType,
+                                                      const css::uno::Reference<css::rdf::XResource>& xSubject);
+
+    /// Add an (XResource, key, value) statement in the graph of type rType -- or if it does not exist, create a graph at rPath first.
+    static void addStatement(const css::uno::Reference<css::frame::XModel>& xModel,
+                             const OUString& rType, const OUString& rPath,
+                             const css::uno::Reference<css::rdf::XResource>& xSubject,
+                             const OUString& rKey, const OUString& rValue);
+
+    /// Remove an (XResource, key, value) statement in the graph of type rType, if it exists.
+    static void removeStatement(const css::uno::Reference<css::frame::XModel>& xModel,
+                                const OUString& rType,
+                                const css::uno::Reference<css::rdf::XResource>& xSubject,
+                                const OUString& rKey, const OUString& rValue);
+
+    /// Remove all statements in the graph of type rType, if any exists.
+    static void clearStatements(const css::uno::Reference<css::frame::XModel>& xModel,
+                                const OUString& rType,
+                                const css::uno::Reference<css::rdf::XResource>& xSubject);
+
     /// Gets all (rTextNode, key, value) statements in RDF graphs of type rType.
     static std::map<OUString, OUString> getTextNodeStatements(const OUString& rType, SwTextNode& rTextNode);
 
     /// Add an (rTextNode, key, value) statement in the graph of type rType -- or if it does not exist, create a graph at rPath first.
     static void addTextNodeStatement(const OUString& rType, const OUString& rPath, SwTextNode& rTextNode, const OUString& rKey, const OUString& rValue);
 
+    /// Remove an (rTextNode, key, value) statement in the graph of type rType.
+    static void removeTextNodeStatement(const OUString& rType, SwTextNode& rTextNode, const OUString& rKey, const OUString& rValue);
+
     /// Update an (rTextNode, key, value) statement in the graph of type rType from old value to new. Creates the graph at rPath if doesn't exist.
     static void updateTextNodeStatement(const OUString& rType, const OUString& rPath, SwTextNode& rTextNode, const OUString& rKey, const OUString& rOldValue, const OUString& rNewValue);
+
 };
 
 #endif // INCLUDED_SW_INC_RDFHELPER_HXX
diff --git a/sw/source/core/doc/rdfhelper.cxx b/sw/source/core/doc/rdfhelper.cxx
index 390e17ba7d79..3e146c80db39 100644
--- a/sw/source/core/doc/rdfhelper.cxx
+++ b/sw/source/core/doc/rdfhelper.cxx
@@ -23,22 +23,23 @@
 
 using namespace com::sun::star;
 
-std::map<OUString, OUString> SwRDFHelper::getTextNodeStatements(const OUString& rType, SwTextNode& rTextNode)
+std::map<OUString, OUString> SwRDFHelper::getStatements(const css::uno::Reference<css::frame::XModel>& xModel,
+                                                        const OUString& rType,
+                                                        const css::uno::Reference<css::rdf::XResource>& xSubject)
 {
     std::map<OUString, OUString> aRet;
 
     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::Reference<rdf::XDocumentMetadataAccess> xDocumentMetadataAccess(xModel, 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>());
+        uno::Reference<container::XEnumeration> xStatements = xGraph->getStatements(xSubject, uno::Reference<rdf::XURI>(), uno::Reference<rdf::XURI>());
         while (xStatements->hasMoreElements())
         {
             rdf::Statement aStatement = xStatements->nextElement().get<rdf::Statement>();
@@ -49,11 +50,14 @@ std::map<OUString, OUString> SwRDFHelper::getTextNodeStatements(const OUString&
     return aRet;
 }
 
-void SwRDFHelper::addTextNodeStatement(const OUString& rType, const OUString& rPath, SwTextNode& rTextNode, const OUString& rKey, const OUString& rValue)
+void SwRDFHelper::addStatement(const css::uno::Reference<css::frame::XModel>& xModel,
+                               const OUString& rType, const OUString& rPath,
+                               const css::uno::Reference<css::rdf::XResource>& xSubject,
+                               const OUString& rKey, const OUString& rValue)
 {
     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::Reference<rdf::XDocumentMetadataAccess> xDocumentMetadataAccess(xModel, uno::UNO_QUERY);
     uno::Sequence< uno::Reference<rdf::XURI> > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType);
     uno::Reference<rdf::XURI> xGraphName;
     if (aGraphNames.hasElements())
@@ -64,12 +68,83 @@ void SwRDFHelper::addTextNodeStatement(const OUString& rType, const OUString& rP
         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);
     uno::Reference<rdf::XLiteral> xValue = rdf::Literal::create(xComponentContext, rValue);
     xGraph->addStatement(xSubject, xKey, xValue);
 }
 
+void SwRDFHelper::removeStatement(const css::uno::Reference<css::frame::XModel>& xModel,
+                                  const OUString& rType,
+                                  const css::uno::Reference<css::rdf::XResource>& xSubject,
+                                  const OUString& rKey, const OUString& rValue)
+{
+    uno::Reference<uno::XComponentContext> xComponentContext(comphelper::getProcessComponentContext());
+    uno::Reference<rdf::XURI> xType = rdf::URI::create(xComponentContext, rType);
+    uno::Reference<rdf::XDocumentMetadataAccess> xDocumentMetadataAccess(xModel, uno::UNO_QUERY);
+    uno::Sequence< uno::Reference<rdf::XURI> > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType);
+    if (!aGraphNames.hasElements())
+        return;
+
+    uno::Reference<rdf::XNamedGraph> xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(aGraphNames[0]);
+    uno::Reference<rdf::XURI> xKey = rdf::URI::create(xComponentContext, rKey);
+    uno::Reference<rdf::XLiteral> xValue = rdf::Literal::create(xComponentContext, rValue);
+    xGraph->removeStatements(xSubject, xKey, xValue);
+}
+
+void SwRDFHelper::clearStatements(const css::uno::Reference<css::frame::XModel>& xModel,
+                                  const OUString& rType,
+                                  const css::uno::Reference<css::rdf::XResource>& xSubject)
+{
+    uno::Reference<uno::XComponentContext> xComponentContext(comphelper::getProcessComponentContext());
+    uno::Reference<rdf::XURI> xType = rdf::URI::create(xComponentContext, rType);
+    uno::Reference<rdf::XDocumentMetadataAccess> xDocumentMetadataAccess(xModel, uno::UNO_QUERY);
+    uno::Sequence< uno::Reference<rdf::XURI> > aGraphNames = xDocumentMetadataAccess->getMetadataGraphsWithType(xType);
+    if (!aGraphNames.hasElements())
+        return;
+
+    for (const uno::Reference<rdf::XURI>& xGraphName : aGraphNames)
+    {
+        uno::Reference<rdf::XNamedGraph> xGraph = xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName);
+        uno::Reference<container::XEnumeration> xStatements = xGraph->getStatements(xSubject, uno::Reference<rdf::XURI>(), uno::Reference<rdf::XURI>());
+        while (xStatements->hasMoreElements())
+        {
+            rdf::Statement aStatement = xStatements->nextElement().get<rdf::Statement>();
+            uno::Reference<rdf::XURI> xKey = rdf::URI::create(xComponentContext, aStatement.Predicate->getStringValue());
+            uno::Reference<rdf::XLiteral> xValue = rdf::Literal::create(xComponentContext, aStatement.Object->getStringValue());
+            xGraph->removeStatements(xSubject, xKey, xValue);
+        }
+    }
+}
+
+std::map<OUString, OUString> SwRDFHelper::getTextNodeStatements(const OUString& rType, SwTextNode& rTextNode)
+{
+    uno::Reference<rdf::XResource> 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<rdf::XResource> 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<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;
+
+    uno::Reference<rdf::XURI> xGraphName = aGraphNames[0];
+    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);
+    uno::Reference<rdf::XLiteral> 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<uno::XComponentContext> xComponentContext(comphelper::getProcessComponentContext());


More information about the Libreoffice-commits mailing list