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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "RDFaExportHelper.hxx"
22 #include <xmloff/xmlnmspe.hxx>
24 #include <xmloff/xmlexp.hxx>
25 #include <xmloff/xmltoken.hxx>
27 #include <comphelper/stl_types.hxx>
28 #include <comphelper/processfactory.hxx>
30 #include <com/sun/star/uri/XUriReference.hpp>
31 #include <com/sun/star/uri/UriReferenceFactory.hpp>
32 #include <com/sun/star/rdf/Statement.hpp>
33 #include <com/sun/star/rdf/URIs.hpp>
34 #include <com/sun/star/rdf/URI.hpp>
35 #include <com/sun/star/rdf/XLiteral.hpp>
36 #include <com/sun/star/rdf/XRepositorySupplier.hpp>
37 #include <com/sun/star/rdf/XDocumentRepository.hpp>
39 #include <rtl/ustrbuf.hxx>
44 using namespace ::com::sun::star
;
49 makeCURIE(SvXMLExport
* i_pExport
,
50 uno::Reference
<rdf::XURI
> const & i_xURI
)
52 OSL_ENSURE(i_xURI
.is(), "makeCURIE: null URI");
53 if (!i_xURI
.is()) throw uno::RuntimeException();
55 const OUString
Namespace( i_xURI
->getNamespace() );
56 OSL_ENSURE(!Namespace
.isEmpty(), "makeCURIE: no namespace");
57 if (Namespace
.isEmpty()) throw uno::RuntimeException();
59 // N.B.: empty LocalName is valid!
60 return i_pExport
->EnsureNamespace(Namespace
) + ":" + i_xURI
->getLocalName();
63 // #i112473# SvXMLExport::GetRelativeReference() not right for RDF on SaveAs
64 // because the URIs in the repository are not rewritten on SaveAs, the
65 // URI of the loaded document has to be used, not the URI of the target doc.
67 getRelativeReference(SvXMLExport
const& rExport
, OUString
const& rURI
)
69 uno::Reference
< rdf::XURI
> const xModelURI(
70 rExport
.GetModel(), uno::UNO_QUERY_THROW
);
71 OUString
const baseURI( xModelURI
->getStringValue() );
73 uno::Reference
<uno::XComponentContext
> xContext( comphelper::getProcessComponentContext() );
74 uno::Reference
<uri::XUriReferenceFactory
> const xUriFactory
=
75 uri::UriReferenceFactory::create( xContext
);
77 uno::Reference
< uri::XUriReference
> const xBaseURI(
78 xUriFactory
->parse(baseURI
), uno::UNO_SET_THROW
);
79 uno::Reference
< uri::XUriReference
> const xAbsoluteURI(
80 xUriFactory
->parse(rURI
), uno::UNO_SET_THROW
);
81 uno::Reference
< uri::XUriReference
> const xRelativeURI(
82 xUriFactory
->makeRelative(xBaseURI
, xAbsoluteURI
, true, true, false),
84 OUString
const relativeURI(xRelativeURI
->getUriReference());
89 RDFaExportHelper::RDFaExportHelper(SvXMLExport
& i_rExport
)
90 : m_rExport(i_rExport
), m_xRepository(nullptr), m_Counter(0)
92 const uno::Reference
<rdf::XRepositorySupplier
> xRS( m_rExport
.GetModel(),
94 OSL_ENSURE(xRS
.is(), "AddRDFa: model is no rdf::XRepositorySupplier");
95 if (!xRS
.is()) throw uno::RuntimeException();
96 m_xRepository
.set(xRS
->getRDFRepository(), uno::UNO_QUERY_THROW
);
100 RDFaExportHelper::LookupBlankNode(
101 uno::Reference
<rdf::XBlankNode
> const & i_xBlankNode
)
103 OSL_ENSURE(i_xBlankNode
.is(), "null BlankNode?");
104 if (!i_xBlankNode
.is()) throw uno::RuntimeException();
106 m_BlankNodeMap
[ i_xBlankNode
->getStringValue() ] );
107 if (rEntry
.isEmpty())
109 rEntry
= "_:b" + OUString::number(++m_Counter
);
115 RDFaExportHelper::AddRDFa(
116 uno::Reference
<rdf::XMetadatable
> const & i_xMetadatable
)
120 beans::Pair
< uno::Sequence
<rdf::Statement
>, sal_Bool
> const
121 RDFaResult( m_xRepository
->getStatementRDFa(i_xMetadatable
) );
123 uno::Sequence
<rdf::Statement
> const & rStatements( RDFaResult
.First
);
125 if (0 == rStatements
.getLength())
130 // all stmts have the same subject, so we only handle first one
131 const uno::Reference
<rdf::XURI
> xSubjectURI(rStatements
[0].Subject
,
133 const uno::Reference
<rdf::XBlankNode
> xSubjectBNode(
134 rStatements
[0].Subject
, uno::UNO_QUERY
);
135 if (!xSubjectURI
.is() && !xSubjectBNode
.is())
137 throw uno::RuntimeException();
139 const OUString
about( xSubjectURI
.is()
140 ? getRelativeReference(m_rExport
, xSubjectURI
->getStringValue())
141 : "[" + LookupBlankNode(xSubjectBNode
) + "]"
144 const uno::Reference
<rdf::XLiteral
> xContent(
145 rStatements
[0].Object
, uno::UNO_QUERY_THROW
);
146 const uno::Reference
<rdf::XURI
> xDatatype(xContent
->getDatatype());
149 const OUString
datatype(
150 makeCURIE(&m_rExport
, xDatatype
) );
151 m_rExport
.AddAttribute(XML_NAMESPACE_XHTML
,
152 token::XML_DATATYPE
, datatype
);
154 if (RDFaResult
.Second
) // there is xhtml:content
156 m_rExport
.AddAttribute(XML_NAMESPACE_XHTML
, token::XML_CONTENT
,
157 xContent
->getValue());
160 ::std::vector
<::rtl::OUString
> curies
;
161 for (rdf::Statement
const& rStatement
: rStatements
)
163 curies
.push_back(makeCURIE(&m_rExport
, rStatement
.Predicate
));
165 OUStringBuffer property
;
166 ::comphelper::intersperse(curies
.begin(), curies
.end(),
167 ::comphelper::OUStringBufferAppender(property
),
170 m_rExport
.AddAttribute(XML_NAMESPACE_XHTML
, token::XML_PROPERTY
,
171 property
.makeStringAndClear());
173 m_rExport
.AddAttribute(XML_NAMESPACE_XHTML
, token::XML_ABOUT
, about
);
175 catch (uno::Exception
&)
177 OSL_FAIL("AddRDFa: exception");
181 } // namespace xmloff
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */