LibreOffice Module writerfilter (master) 1
SmartTagHandler.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 */
9
10#include "SmartTagHandler.hxx"
11
12#include <com/sun/star/rdf/Literal.hpp>
13#include <com/sun/star/rdf/URI.hpp>
14#include <com/sun/star/rdf/XDocumentMetadataAccess.hpp>
15#include <com/sun/star/text/XTextDocument.hpp>
16#include <com/sun/star/text/XTextRange.hpp>
17
18#include <ooxml/resourceids.hxx>
19
20#include <sal/log.hxx>
21
22namespace
23{
24OUString lcl_getTypePath(OUString& rType)
25{
26 OUString aRet;
27 if (rType.startsWith("urn:bails"))
28 {
29 rType = "urn:bails";
30 aRet = "tscp/bails.rdf";
31 }
32 return aRet;
33}
34}
35
37{
38using namespace ::com::sun::star;
39
41 const uno::Reference<text::XTextDocument>& xTextDocument)
42 : LoggedProperties("SmartTagHandler")
43 , m_xComponentContext(std::move(xComponentContext))
44 , m_xDocumentMetadataAccess(xTextDocument, uno::UNO_QUERY)
45{
46}
47
49
51{
52 switch (nId)
53 {
54 case NS_ooxml::LN_CT_Attr_name:
55 m_aAttributes.emplace_back(rValue.getString(), OUString());
56 break;
57 case NS_ooxml::LN_CT_Attr_val:
58 if (!m_aAttributes.empty())
59 m_aAttributes.back().second = rValue.getString();
60 break;
61 default:
62 SAL_WARN("writerfilter", "SmartTagHandler::lcl_attribute: unhandled attribute "
63 << nId << " (string value: '" << rValue.getString()
64 << "')");
65 break;
66 }
67}
68
70{
71 switch (rSprm.getId())
72 {
73 case NS_ooxml::LN_CT_SmartTagPr_attr:
74 {
76 if (pProperties)
77 pProperties->resolve(*this);
78 break;
79 }
80 }
81}
82
83void SmartTagHandler::setURI(const OUString& rURI) { m_aURI = rURI; }
84
85void SmartTagHandler::setElement(const OUString& rElement) { m_aElement = rElement; }
86
88{
89 if (m_aURI.isEmpty() || m_aElement.isEmpty() || m_aAttributes.empty())
90 return;
91
92 uno::Reference<rdf::XResource> xSubject(xParagraph, uno::UNO_QUERY);
93
94 for (const std::pair<OUString, OUString>& rAttribute : m_aAttributes)
95 {
96 OUString aTypeNS = rAttribute.first;
97 OUString aMetadataFilePath = lcl_getTypePath(aTypeNS);
98 if (aMetadataFilePath.isEmpty())
99 continue;
100
101 uno::Reference<rdf::XURI> xType = rdf::URI::create(m_xComponentContext, aTypeNS);
103 = m_xDocumentMetadataAccess->getMetadataGraphsWithType(xType);
104 uno::Reference<rdf::XURI> xGraphName;
105 if (aGraphNames.hasElements())
106 xGraphName = aGraphNames[0];
107 else
108 {
109 uno::Sequence<uno::Reference<rdf::XURI>> xTypes = { xType };
110 xGraphName = m_xDocumentMetadataAccess->addMetadataFile(aMetadataFilePath, xTypes);
111 }
113 = m_xDocumentMetadataAccess->getRDFRepository()->getGraph(xGraphName);
114 uno::Reference<rdf::XURI> xKey = rdf::URI::create(m_xComponentContext, rAttribute.first);
116 = rdf::Literal::create(m_xComponentContext, rAttribute.second);
117 xGraph->addStatement(xSubject, xKey, xValue);
118 }
119
120 m_aURI.clear();
121 m_aElement.clear();
122 m_aAttributes.clear();
123}
124
125} // namespace writerfilter::dmapper
126
127/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
An SPRM: Section, Paragraph and Run Modifier.
virtual sal_uInt32 getId() const =0
Returns id of the SPRM.
virtual writerfilter::Reference< Properties >::Pointer_t getProps()=0
Returns reference to properties contained in the SPRM.
virtual OUString getString() const =0
Returns string representation of the value.
void lcl_sprm(Sprm &rSprm) override
SmartTagHandler(css::uno::Reference< css::uno::XComponentContext > xComponentContext, const css::uno::Reference< css::text::XTextDocument > &xTextDocument)
void setElement(const OUString &rElement)
std::vector< std::pair< OUString, OUString > > m_aAttributes
css::uno::Reference< css::uno::XComponentContext > m_xComponentContext
void lcl_attribute(Id nId, Value &rValue) override
void handle(const css::uno::Reference< css::text::XTextRange > &xParagraph)
Set m_aAttributes as RDF statements on xParagraph.
css::uno::Reference< css::rdf::XDocumentMetadataAccess > m_xDocumentMetadataAccess
Reference< XComponentContext > const m_xComponentContext
#define SAL_WARN(area, stream)
sal_Int16 nId
sal_uInt32 Id