1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <rdfhelper.hxx>
12 #include <com/sun/star/frame/XModel.hpp>
13 #include <com/sun/star/rdf/Literal.hpp>
14 #include <com/sun/star/rdf/Statement.hpp>
15 #include <com/sun/star/rdf/URI.hpp>
16 #include <com/sun/star/rdf/XDocumentMetadataAccess.hpp>
18 #include <comphelper/processfactory.hxx>
23 #include <unoparagraph.hxx>
25 using namespace com::sun::star
;
27 css::uno::Sequence
<css::uno::Reference
<css::rdf::XURI
>> SwRDFHelper::getGraphNames(
28 const css::uno::Reference
<rdf::XDocumentMetadataAccess
>& xDocumentMetadataAccess
,
29 const css::uno::Reference
<rdf::XURI
>& xType
)
33 return xDocumentMetadataAccess
->getMetadataGraphsWithType(xType
);
35 catch (const uno::RuntimeException
&)
37 return uno::Sequence
<uno::Reference
<rdf::XURI
>>();
41 css::uno::Sequence
<uno::Reference
<css::rdf::XURI
>>
42 SwRDFHelper::getGraphNames(const css::uno::Reference
<css::frame::XModel
>& xModel
,
43 const OUString
& rType
)
47 uno::Reference
<uno::XComponentContext
> xComponentContext(
48 comphelper::getProcessComponentContext());
49 // rdf::URI::create may fail with type: com.sun.star.uno.DeploymentException
50 // message: component context fails to supply service com.sun.star.rdf.URI of type com.sun.star.rdf.XURI
51 // context: cppu::ComponentContext
52 uno::Reference
<rdf::XURI
> xType
= rdf::URI::create(xComponentContext
, rType
);
53 uno::Reference
<rdf::XDocumentMetadataAccess
> xDocumentMetadataAccess(xModel
,
55 return getGraphNames(xDocumentMetadataAccess
, xType
);
57 catch (const ::css::uno::Exception
&)
59 return uno::Sequence
<uno::Reference
<rdf::XURI
>>();
63 std::map
<OUString
, OUString
>
64 SwRDFHelper::getStatements(const css::uno::Reference
<css::frame::XModel
>& xModel
,
65 const uno::Sequence
<uno::Reference
<css::rdf::XURI
>>& rGraphNames
,
66 const css::uno::Reference
<css::rdf::XResource
>& xSubject
)
68 std::map
<OUString
, OUString
> aRet
;
69 if (!rGraphNames
.hasElements())
72 uno::Reference
<rdf::XDocumentMetadataAccess
> xDocumentMetadataAccess(xModel
, uno::UNO_QUERY
);
73 const uno::Reference
<rdf::XRepository
>& xRepo
= xDocumentMetadataAccess
->getRDFRepository();
74 for (const uno::Reference
<rdf::XURI
>& xGraphName
: rGraphNames
)
76 uno::Reference
<rdf::XNamedGraph
> xGraph
= xRepo
->getGraph(xGraphName
);
80 uno::Reference
<container::XEnumeration
> xStatements
= xGraph
->getStatements(
81 xSubject
, uno::Reference
<rdf::XURI
>(), uno::Reference
<rdf::XURI
>());
82 while (xStatements
->hasMoreElements())
84 const rdf::Statement aStatement
= xStatements
->nextElement().get
<rdf::Statement
>();
85 aRet
[aStatement
.Predicate
->getStringValue()] = aStatement
.Object
->getStringValue();
92 std::map
<OUString
, OUString
>
93 SwRDFHelper::getStatements(const css::uno::Reference
<css::frame::XModel
>& xModel
,
94 const OUString
& rType
,
95 const css::uno::Reference
<css::rdf::XResource
>& xSubject
)
97 return getStatements(xModel
, getGraphNames(xModel
, rType
), xSubject
);
100 void SwRDFHelper::addStatement(const css::uno::Reference
<css::frame::XModel
>& xModel
,
101 const OUString
& rType
, const OUString
& rPath
,
102 const css::uno::Reference
<css::rdf::XResource
>& xSubject
,
103 const OUString
& rKey
, const OUString
& rValue
)
105 uno::Reference
<uno::XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
106 uno::Reference
<rdf::XURI
> xType
= rdf::URI::create(xComponentContext
, rType
);
107 uno::Reference
<rdf::XDocumentMetadataAccess
> xDocumentMetadataAccess(xModel
, uno::UNO_QUERY
);
108 const uno::Sequence
< uno::Reference
<rdf::XURI
> > aGraphNames
= getGraphNames(xDocumentMetadataAccess
, xType
);
109 uno::Reference
<rdf::XURI
> xGraphName
;
110 if (aGraphNames
.hasElements())
111 xGraphName
= aGraphNames
[0];
114 uno::Sequence
< uno::Reference
<rdf::XURI
> > xTypes
= { xType
};
115 xGraphName
= xDocumentMetadataAccess
->addMetadataFile(rPath
, xTypes
);
117 uno::Reference
<rdf::XNamedGraph
> xGraph
= xDocumentMetadataAccess
->getRDFRepository()->getGraph(xGraphName
);
118 uno::Reference
<rdf::XURI
> xKey
= rdf::URI::create(xComponentContext
, rKey
);
119 uno::Reference
<rdf::XLiteral
> xValue
= rdf::Literal::create(xComponentContext
, rValue
);
120 xGraph
->addStatement(xSubject
, xKey
, xValue
);
123 bool SwRDFHelper::hasMetadataGraph(const css::uno::Reference
<css::frame::XModel
>& xModel
, const OUString
& rType
)
125 uno::Reference
<uno::XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
126 uno::Reference
<rdf::XURI
> xType
= rdf::URI::create(xComponentContext
, rType
);
127 uno::Reference
<rdf::XDocumentMetadataAccess
> xDocumentMetadataAccess(xModel
, uno::UNO_QUERY
);
128 return getGraphNames(xDocumentMetadataAccess
, xType
).hasElements();
131 void SwRDFHelper::removeStatement(const css::uno::Reference
<css::frame::XModel
>& xModel
,
132 const OUString
& rType
,
133 const css::uno::Reference
<css::rdf::XResource
>& xSubject
,
134 const OUString
& rKey
, const OUString
& rValue
)
136 uno::Reference
<uno::XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
137 uno::Reference
<rdf::XURI
> xType
= rdf::URI::create(xComponentContext
, rType
);
138 uno::Reference
<rdf::XDocumentMetadataAccess
> xDocumentMetadataAccess(xModel
, uno::UNO_QUERY
);
139 const uno::Sequence
< uno::Reference
<rdf::XURI
> > aGraphNames
= getGraphNames(xDocumentMetadataAccess
, xType
);
140 if (!aGraphNames
.hasElements())
143 uno::Reference
<rdf::XNamedGraph
> xGraph
= xDocumentMetadataAccess
->getRDFRepository()->getGraph(aGraphNames
[0]);
144 uno::Reference
<rdf::XURI
> xKey
= rdf::URI::create(xComponentContext
, rKey
);
145 uno::Reference
<rdf::XLiteral
> xValue
= rdf::Literal::create(xComponentContext
, rValue
);
146 xGraph
->removeStatements(xSubject
, xKey
, xValue
);
149 void SwRDFHelper::clearStatements(const css::uno::Reference
<css::frame::XModel
>& xModel
,
150 const OUString
& rType
,
151 const css::uno::Reference
<css::rdf::XResource
>& xSubject
)
153 uno::Reference
<uno::XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
154 uno::Reference
<rdf::XURI
> xType
= rdf::URI::create(xComponentContext
, rType
);
155 uno::Reference
<rdf::XDocumentMetadataAccess
> xDocumentMetadataAccess(xModel
, uno::UNO_QUERY
);
156 const uno::Sequence
< uno::Reference
<rdf::XURI
> > aGraphNames
= getGraphNames(xDocumentMetadataAccess
, xType
);
157 if (!aGraphNames
.hasElements())
160 for (const uno::Reference
<rdf::XURI
>& xGraphName
: aGraphNames
)
162 uno::Reference
<rdf::XNamedGraph
> xGraph
= xDocumentMetadataAccess
->getRDFRepository()->getGraph(xGraphName
);
163 uno::Reference
<container::XEnumeration
> xStatements
= xGraph
->getStatements(xSubject
, uno::Reference
<rdf::XURI
>(), uno::Reference
<rdf::XURI
>());
164 while (xStatements
->hasMoreElements())
166 rdf::Statement aStatement
= xStatements
->nextElement().get
<rdf::Statement
>();
167 uno::Reference
<rdf::XURI
> xKey
= rdf::URI::create(xComponentContext
, aStatement
.Predicate
->getStringValue());
168 uno::Reference
<rdf::XLiteral
> xValue
= rdf::Literal::create(xComponentContext
, aStatement
.Object
->getStringValue());
169 xGraph
->removeStatements(xSubject
, xKey
, xValue
);
174 void SwRDFHelper::cloneStatements(const css::uno::Reference
<css::frame::XModel
>& xSrcModel
,
175 const css::uno::Reference
<css::frame::XModel
>& xDstModel
,
176 const OUString
& rType
,
177 const css::uno::Reference
<css::rdf::XResource
>& xSrcSubject
,
178 const css::uno::Reference
<css::rdf::XResource
>& xDstSubject
)
180 uno::Reference
<uno::XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
181 uno::Reference
<rdf::XURI
> xType
= rdf::URI::create(xComponentContext
, rType
);
182 uno::Reference
<rdf::XDocumentMetadataAccess
> xDocumentMetadataAccess(xSrcModel
, uno::UNO_QUERY
);
183 const uno::Sequence
< uno::Reference
<rdf::XURI
> > aGraphNames
= getGraphNames(xDocumentMetadataAccess
, xType
);
184 if (!aGraphNames
.hasElements())
187 for (const uno::Reference
<rdf::XURI
>& xGraphName
: aGraphNames
)
189 uno::Reference
<rdf::XNamedGraph
> xGraph
= xDocumentMetadataAccess
->getRDFRepository()->getGraph(xGraphName
);
190 uno::Reference
<container::XEnumeration
> xStatements
= xGraph
->getStatements(xSrcSubject
, uno::Reference
<rdf::XURI
>(), uno::Reference
<rdf::XURI
>());
191 while (xStatements
->hasMoreElements())
193 const rdf::Statement aStatement
= xStatements
->nextElement().get
<rdf::Statement
>();
195 const OUString sKey
= aStatement
.Predicate
->getStringValue();
196 const OUString sValue
= aStatement
.Object
->getStringValue();
197 addStatement(xDstModel
, rType
, xGraphName
->getLocalName(), xDstSubject
, sKey
, sValue
);
202 std::map
<OUString
, OUString
> SwRDFHelper::getTextNodeStatements(const OUString
& rType
, SwTextNode
& rTextNode
)
204 uno::Reference
<rdf::XResource
> xTextNode(SwXParagraph::CreateXParagraph(rTextNode
.GetDoc(), &rTextNode
));
205 return getStatements(rTextNode
.GetDoc().GetDocShell()->GetBaseModel(), rType
, xTextNode
);
208 void SwRDFHelper::addTextNodeStatement(const OUString
& rType
, const OUString
& rPath
, SwTextNode
& rTextNode
, const OUString
& rKey
, const OUString
& rValue
)
210 uno::Reference
<rdf::XResource
> xSubject(SwXParagraph::CreateXParagraph(rTextNode
.GetDoc(), &rTextNode
));
211 addStatement(rTextNode
.GetDoc().GetDocShell()->GetBaseModel(), rType
, rPath
, xSubject
, rKey
, rValue
);
214 void SwRDFHelper::removeTextNodeStatement(const OUString
& rType
, SwTextNode
& rTextNode
, const OUString
& rKey
, const OUString
& rValue
)
216 uno::Reference
<uno::XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
217 uno::Reference
<rdf::XURI
> xType
= rdf::URI::create(xComponentContext
, rType
);
218 uno::Reference
<rdf::XDocumentMetadataAccess
> xDocumentMetadataAccess(rTextNode
.GetDoc().GetDocShell()->GetBaseModel(), uno::UNO_QUERY
);
219 const uno::Sequence
< uno::Reference
<rdf::XURI
> > aGraphNames
= getGraphNames(xDocumentMetadataAccess
, xType
);
220 if (!aGraphNames
.hasElements())
223 uno::Reference
<rdf::XURI
> xGraphName
= aGraphNames
[0];
224 uno::Reference
<rdf::XNamedGraph
> xGraph
= xDocumentMetadataAccess
->getRDFRepository()->getGraph(xGraphName
);
225 uno::Reference
<rdf::XResource
> xSubject(SwXParagraph::CreateXParagraph(rTextNode
.GetDoc(), &rTextNode
));
226 uno::Reference
<rdf::XURI
> xKey
= rdf::URI::create(xComponentContext
, rKey
);
227 uno::Reference
<rdf::XLiteral
> xValue
= rdf::Literal::create(xComponentContext
, rValue
);
228 xGraph
->removeStatements(xSubject
, xKey
, xValue
);
231 void SwRDFHelper::updateTextNodeStatement(const OUString
& rType
, const OUString
& rPath
, SwTextNode
& rTextNode
, const OUString
& rKey
, const OUString
& rOldValue
, const OUString
& rNewValue
)
233 uno::Reference
<uno::XComponentContext
> xComponentContext(comphelper::getProcessComponentContext());
234 uno::Reference
<rdf::XURI
> xType
= rdf::URI::create(xComponentContext
, rType
);
235 uno::Reference
<rdf::XDocumentMetadataAccess
> xDocumentMetadataAccess(rTextNode
.GetDoc().GetDocShell()->GetBaseModel(), uno::UNO_QUERY
);
236 const uno::Sequence
< uno::Reference
<rdf::XURI
> > aGraphNames
= getGraphNames(xDocumentMetadataAccess
, xType
);
237 uno::Reference
<rdf::XURI
> xGraphName
;
238 if (aGraphNames
.hasElements())
240 xGraphName
= aGraphNames
[0];
244 uno::Sequence
< uno::Reference
<rdf::XURI
> > xTypes
= { xType
};
245 xGraphName
= xDocumentMetadataAccess
->addMetadataFile(rPath
, xTypes
);
248 uno::Reference
<rdf::XNamedGraph
> xGraph
= xDocumentMetadataAccess
->getRDFRepository()->getGraph(xGraphName
);
249 uno::Reference
<rdf::XResource
> xSubject(SwXParagraph::CreateXParagraph(rTextNode
.GetDoc(), &rTextNode
));
250 uno::Reference
<rdf::XURI
> xKey
= rdf::URI::create(xComponentContext
, rKey
);
252 if (aGraphNames
.hasElements())
254 // Remove the old value.
255 uno::Reference
<rdf::XLiteral
> xOldValue
= rdf::Literal::create(xComponentContext
, rOldValue
);
256 xGraph
->removeStatements(xSubject
, xKey
, xOldValue
);
259 // Now add it with new value.
260 uno::Reference
<rdf::XLiteral
> xNewValue
= rdf::Literal::create(xComponentContext
, rNewValue
);
261 xGraph
->addStatement(xSubject
, xKey
, xNewValue
);
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */