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/xmlnamespace.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/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>
38 #include <rtl/ustrbuf.hxx>
39 #include <comphelper/diagnose_ex.hxx>
43 using namespace ::com::sun::star
;
48 makeCURIE(SvXMLExport
* i_pExport
,
49 uno::Reference
<rdf::XURI
> const & i_xURI
)
51 OSL_ENSURE(i_xURI
.is(), "makeCURIE: null URI");
52 if (!i_xURI
.is()) throw uno::RuntimeException();
54 const OUString
Namespace( i_xURI
->getNamespace() );
55 OSL_ENSURE(!Namespace
.isEmpty(), "makeCURIE: no namespace");
56 if (Namespace
.isEmpty()) throw uno::RuntimeException();
58 // N.B.: empty LocalName is valid!
59 return i_pExport
->EnsureNamespace(Namespace
) + ":" + i_xURI
->getLocalName();
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.
66 getRelativeReference(SvXMLExport
const& rExport
, OUString
const& rURI
)
68 uno::Reference
< rdf::XURI
> const xModelURI(
69 rExport
.GetModel(), uno::UNO_QUERY_THROW
);
70 OUString
const baseURI( xModelURI
->getStringValue() );
72 const uno::Reference
<uno::XComponentContext
>& xContext( comphelper::getProcessComponentContext() );
73 uno::Reference
<uri::XUriReferenceFactory
> const xUriFactory
=
74 uri::UriReferenceFactory::create( xContext
);
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),
83 OUString
const relativeURI(xRelativeURI
->getUriReference());
88 RDFaExportHelper::RDFaExportHelper(SvXMLExport
& i_rExport
)
89 : m_rExport(i_rExport
), m_Counter(0)
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
);
97 RDFaExportHelper::LookupBlankNode(
98 uno::Reference
<rdf::XBlankNode
> const & i_xBlankNode
)
100 OSL_ENSURE(i_xBlankNode
.is(), "null BlankNode?");
101 if (!i_xBlankNode
.is()) throw uno::RuntimeException();
103 m_BlankNodeMap
[ i_xBlankNode
->getStringValue() ] );
104 if (rEntry
.isEmpty())
106 rEntry
= "_:b" + OUString::number(++m_Counter
);
112 RDFaExportHelper::AddRDFa(
113 uno::Reference
<rdf::XMetadatable
> const & i_xMetadatable
)
117 beans::Pair
< uno::Sequence
<rdf::Statement
>, sal_Bool
> const
118 RDFaResult( m_xRepository
->getStatementRDFa(i_xMetadatable
) );
120 uno::Sequence
<rdf::Statement
> const & rStatements( RDFaResult
.First
);
122 if (!rStatements
.hasElements())
127 // all stmts have the same subject, so we only handle first one
128 const uno::Reference
<rdf::XURI
> xSubjectURI(rStatements
[0].Subject
,
130 const uno::Reference
<rdf::XBlankNode
> xSubjectBNode(
131 rStatements
[0].Subject
, uno::UNO_QUERY
);
132 if (!xSubjectURI
.is() && !xSubjectBNode
.is())
134 throw uno::RuntimeException();
136 const OUString
about( xSubjectURI
.is()
137 ? getRelativeReference(m_rExport
, xSubjectURI
->getStringValue())
138 : "[" + LookupBlankNode(xSubjectBNode
) + "]"
141 const uno::Reference
<rdf::XLiteral
> xContent(
142 rStatements
[0].Object
, uno::UNO_QUERY_THROW
);
143 const uno::Reference
<rdf::XURI
> xDatatype(xContent
->getDatatype());
146 const OUString
datatype(
147 makeCURIE(&m_rExport
, xDatatype
) );
148 m_rExport
.AddAttribute(XML_NAMESPACE_XHTML
,
149 token::XML_DATATYPE
, datatype
);
151 if (RDFaResult
.Second
) // there is xhtml:content
153 m_rExport
.AddAttribute(XML_NAMESPACE_XHTML
, token::XML_CONTENT
,
154 xContent
->getValue());
157 ::std::vector
<OUString
> curies
;
158 for (rdf::Statement
const& rStatement
: rStatements
)
160 curies
.push_back(makeCURIE(&m_rExport
, rStatement
.Predicate
));
162 OUStringBuffer property
;
163 ::comphelper::intersperse(curies
.begin(), curies
.end(),
164 ::comphelper::OUStringBufferAppender(property
),
167 m_rExport
.AddAttribute(XML_NAMESPACE_XHTML
, token::XML_PROPERTY
,
168 property
.makeStringAndClear());
170 m_rExport
.AddAttribute(XML_NAMESPACE_XHTML
, token::XML_ABOUT
, about
);
172 catch (uno::Exception
&)
174 TOOLS_WARN_EXCEPTION("xmloff.core", "");
178 } // namespace xmloff
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */