LibreOffice Module xmloff (master) 1
RDFaExportHelper.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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <RDFaExportHelper.hxx>
21
23
24#include <xmloff/xmlexp.hxx>
25#include <xmloff/xmltoken.hxx>
26
29
30#include <com/sun/star/frame/XModel.hpp>
31#include <com/sun/star/uri/XUriReference.hpp>
32#include <com/sun/star/uri/UriReferenceFactory.hpp>
33#include <com/sun/star/rdf/Statement.hpp>
34#include <com/sun/star/rdf/XLiteral.hpp>
35#include <com/sun/star/rdf/XRepositorySupplier.hpp>
36#include <com/sun/star/rdf/XDocumentRepository.hpp>
37
38#include <rtl/ustrbuf.hxx>
40
41#include <algorithm>
42
43using namespace ::com::sun::star;
44
45namespace xmloff {
46
47static OUString
49 uno::Reference<rdf::XURI> const & i_xURI)
50{
51 OSL_ENSURE(i_xURI.is(), "makeCURIE: null URI");
52 if (!i_xURI.is()) throw uno::RuntimeException();
53
54 const OUString Namespace( i_xURI->getNamespace() );
55 OSL_ENSURE(!Namespace.isEmpty(), "makeCURIE: no namespace");
56 if (Namespace.isEmpty()) throw uno::RuntimeException();
57
58 // N.B.: empty LocalName is valid!
59 return i_pExport->EnsureNamespace(Namespace) + ":" + i_xURI->getLocalName();
60}
61
62// #i112473# SvXMLExport::GetRelativeReference() not right for RDF on SaveAs
63// because the URIs in the repository are not rewritten on SaveAs, the
64// URI of the loaded document has to be used, not the URI of the target doc.
65static OUString
66getRelativeReference(SvXMLExport const& rExport, OUString const& rURI)
67{
68 uno::Reference< rdf::XURI > const xModelURI(
69 rExport.GetModel(), uno::UNO_QUERY_THROW );
70 OUString const baseURI( xModelURI->getStringValue() );
71
72 uno::Reference<uno::XComponentContext> xContext( comphelper::getProcessComponentContext() );
73 uno::Reference<uri::XUriReferenceFactory> const xUriFactory =
74 uri::UriReferenceFactory::create( xContext );
75
76 uno::Reference< uri::XUriReference > const xBaseURI(
77 xUriFactory->parse(baseURI), uno::UNO_SET_THROW );
78 uno::Reference< uri::XUriReference > const xAbsoluteURI(
79 xUriFactory->parse(rURI), uno::UNO_SET_THROW );
80 uno::Reference< uri::XUriReference > const xRelativeURI(
81 xUriFactory->makeRelative(xBaseURI, xAbsoluteURI, true, true, false),
82 uno::UNO_SET_THROW );
83 OUString const relativeURI(xRelativeURI->getUriReference());
84
85 return relativeURI;
86}
87
89 : m_rExport(i_rExport), m_Counter(0)
90{
91 const uno::Reference<rdf::XRepositorySupplier> xRS( m_rExport.GetModel(),
92 uno::UNO_QUERY_THROW);
93 m_xRepository.set(xRS->getRDFRepository(), uno::UNO_QUERY_THROW);
94}
95
96OUString
98 uno::Reference<rdf::XBlankNode> const & i_xBlankNode)
99{
100 OSL_ENSURE(i_xBlankNode.is(), "null BlankNode?");
101 if (!i_xBlankNode.is()) throw uno::RuntimeException();
102 OUString & rEntry(
103 m_BlankNodeMap[ i_xBlankNode->getStringValue() ] );
104 if (rEntry.isEmpty())
105 {
106 rEntry = "_:b" + OUString::number(++m_Counter);
107 }
108 return rEntry;
109}
110
111void
113 uno::Reference<rdf::XMetadatable> const & i_xMetadatable)
114{
115 try
116 {
117 beans::Pair< uno::Sequence<rdf::Statement>, sal_Bool > const
118 RDFaResult( m_xRepository->getStatementRDFa(i_xMetadatable) );
119
120 uno::Sequence<rdf::Statement> const & rStatements( RDFaResult.First );
121
122 if (!rStatements.hasElements())
123 {
124 return; // no RDFa
125 }
126
127 // all stmts have the same subject, so we only handle first one
128 const uno::Reference<rdf::XURI> xSubjectURI(rStatements[0].Subject,
129 uno::UNO_QUERY);
130 const uno::Reference<rdf::XBlankNode> xSubjectBNode(
131 rStatements[0].Subject, uno::UNO_QUERY);
132 if (!xSubjectURI.is() && !xSubjectBNode.is())
133 {
134 throw uno::RuntimeException();
135 }
136 const OUString about( xSubjectURI.is()
137 ? getRelativeReference(m_rExport, xSubjectURI->getStringValue())
138 : "[" + LookupBlankNode(xSubjectBNode) + "]"
139 );
140
141 const uno::Reference<rdf::XLiteral> xContent(
142 rStatements[0].Object, uno::UNO_QUERY_THROW );
143 const uno::Reference<rdf::XURI> xDatatype(xContent->getDatatype());
144 if (xDatatype.is())
145 {
146 const OUString datatype(
147 makeCURIE(&m_rExport, xDatatype) );
149 token::XML_DATATYPE, datatype);
150 }
151 if (RDFaResult.Second) // there is xhtml:content
152 {
154 xContent->getValue());
155 }
156
157 ::std::vector<OUString> curies;
158 for (rdf::Statement const& rStatement : rStatements)
159 {
160 curies.push_back(makeCURIE(&m_rExport, rStatement.Predicate));
161 }
162 OUStringBuffer property;
163 ::comphelper::intersperse(curies.begin(), curies.end(),
165 OUString(" "));
166
168 property.makeStringAndClear());
169
171 }
172 catch (uno::Exception &)
173 {
174 TOOLS_WARN_EXCEPTION("xmloff.core", "");
175 }
176}
177
178} // namespace xmloff
179
180/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString EnsureNamespace(OUString const &i_rNamespace)
ensures that the given namespace is in scope at the next started element.
Definition: xmlexp.cxx:867
void AddAttribute(sal_uInt16 nPrefix, const OUString &rName, const OUString &rValue)
Definition: xmlexp.cxx:907
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlexp.hxx:411
OUString LookupBlankNode(css::uno::Reference< css::rdf::XBlankNode > const &i_xBlankNode)
RDFaExportHelper(SvXMLExport &i_rExport)
void AddRDFa(css::uno::Reference< css::rdf::XMetadatable > const &i_xMetadatable)
css::uno::Reference< css::rdf::XDocumentRepository > m_xRepository
#define TOOLS_WARN_EXCEPTION(area, stream)
Reference< XComponentContext > getProcessComponentContext()
static OUString getRelativeReference(SvXMLExport const &rExport, OUString const &rURI)
static OUString makeCURIE(SvXMLExport *i_pExport, uno::Reference< rdf::XURI > const &i_xURI)
unsigned char sal_Bool
constexpr sal_uInt16 XML_NAMESPACE_XHTML