Line data Source code
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 :
21 : #include "RDFaExportHelper.hxx"
22 :
23 : #include "xmloff/xmlnmspe.hxx"
24 :
25 : #include <xmloff/xmlexp.hxx>
26 : #include <xmloff/xmltoken.hxx>
27 :
28 : #include <comphelper/stlunosequence.hxx>
29 : #include <comphelper/stl_types.hxx>
30 :
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/URIs.hpp>
35 : #include <com/sun/star/rdf/URI.hpp>
36 : #include <com/sun/star/rdf/XLiteral.hpp>
37 : #include <com/sun/star/rdf/XRepositorySupplier.hpp>
38 : #include <com/sun/star/rdf/XDocumentRepository.hpp>
39 :
40 : #include <rtl/ustrbuf.hxx>
41 :
42 : #include <boost/bind.hpp>
43 : #include <boost/iterator_adaptors.hpp>
44 : #ifndef BOOST_ITERATOR_ADAPTOR_DWA053000_HPP_ // from iterator_adaptors.hpp
45 : // N.B.: the check for the header guard _of a specific version of boost_
46 : // is here so this may work on different versions of boost,
47 : // which sadly put the goods in different header files
48 : #include <boost/iterator/transform_iterator.hpp>
49 : #endif
50 :
51 : #include <functional>
52 : #include <algorithm>
53 :
54 :
55 : using namespace ::com::sun::star;
56 :
57 : namespace xmloff {
58 :
59 : static ::rtl::OUString
60 0 : makeCURIE(SvXMLExport * i_pExport,
61 : uno::Reference<rdf::XURI> const & i_xURI)
62 : {
63 : OSL_ENSURE(i_xURI.is(), "makeCURIE: null URI");
64 0 : if (!i_xURI.is()) throw uno::RuntimeException();
65 :
66 0 : const ::rtl::OUString Namespace( i_xURI->getNamespace() );
67 : OSL_ENSURE(!Namespace.isEmpty(), "makeCURIE: no namespace");
68 0 : if (Namespace.isEmpty()) throw uno::RuntimeException();
69 :
70 0 : ::rtl::OUStringBuffer buf;
71 0 : buf.append( i_pExport->EnsureNamespace(Namespace) );
72 0 : buf.append( static_cast<sal_Unicode>(':') );
73 : // N.B.: empty LocalName is valid!
74 0 : buf.append( i_xURI->getLocalName() );
75 :
76 0 : return buf.makeStringAndClear();
77 : }
78 :
79 : // #i112473# SvXMLExport::GetRelativeReference() not right for RDF on SaveAs
80 : // because the URIs in the repository are not rewritten on SaveAs, the
81 : // URI of the loaded document has to be used, not the URI of the target doc.
82 : static ::rtl::OUString
83 0 : getRelativeReference(SvXMLExport const& rExport, ::rtl::OUString const& rURI)
84 : {
85 : uno::Reference< rdf::XURI > const xModelURI(
86 0 : rExport.GetModel(), uno::UNO_QUERY_THROW );
87 0 : ::rtl::OUString const baseURI( xModelURI->getStringValue() );
88 :
89 : uno::Reference<uno::XComponentContext> const xContext(
90 0 : rExport.GetComponentContext());
91 : uno::Reference<uri::XUriReferenceFactory> const xUriFactory =
92 0 : uri::UriReferenceFactory::create( xContext );
93 :
94 : uno::Reference< uri::XUriReference > const xBaseURI(
95 0 : xUriFactory->parse(baseURI), uno::UNO_SET_THROW );
96 : uno::Reference< uri::XUriReference > const xAbsoluteURI(
97 0 : xUriFactory->parse(rURI), uno::UNO_SET_THROW );
98 : uno::Reference< uri::XUriReference > const xRelativeURI(
99 0 : xUriFactory->makeRelative(xBaseURI, xAbsoluteURI, true, true, false),
100 0 : uno::UNO_SET_THROW );
101 0 : ::rtl::OUString const relativeURI(xRelativeURI->getUriReference());
102 :
103 0 : return relativeURI;
104 : }
105 :
106 :
107 : ////////////////////////////////////////////////////////////////////////////
108 :
109 0 : RDFaExportHelper::RDFaExportHelper(SvXMLExport & i_rExport)
110 0 : : m_rExport(i_rExport), m_xRepository(0), m_Counter(0)
111 : {
112 0 : const uno::Reference<rdf::XRepositorySupplier> xRS( m_rExport.GetModel(),
113 0 : uno::UNO_QUERY);
114 : OSL_ENSURE(xRS.is(), "AddRDFa: model is no rdf::XRepositorySupplier");
115 0 : if (!xRS.is()) throw uno::RuntimeException();
116 0 : m_xRepository.set(xRS->getRDFRepository(), uno::UNO_QUERY_THROW);
117 0 : }
118 :
119 : ::rtl::OUString
120 0 : RDFaExportHelper::LookupBlankNode(
121 : uno::Reference<rdf::XBlankNode> const & i_xBlankNode)
122 : {
123 : OSL_ENSURE(i_xBlankNode.is(), "null BlankNode?");
124 0 : if (!i_xBlankNode.is()) throw uno::RuntimeException();
125 : ::rtl::OUString & rEntry(
126 0 : m_BlankNodeMap[ i_xBlankNode->getStringValue() ] );
127 0 : if (rEntry.isEmpty())
128 : {
129 0 : rEntry = "_:b" + OUString::valueOf(++m_Counter);
130 : }
131 0 : return rEntry;
132 : }
133 :
134 : ////////////////////////////////////////////////////////////////////////////
135 :
136 : void
137 0 : RDFaExportHelper::AddRDFa(
138 : uno::Reference<rdf::XMetadatable> const & i_xMetadatable)
139 : {
140 : try
141 : {
142 : beans::Pair< uno::Sequence<rdf::Statement>, sal_Bool > const
143 0 : RDFaResult( m_xRepository->getStatementRDFa(i_xMetadatable) );
144 :
145 0 : uno::Sequence<rdf::Statement> const & rStatements( RDFaResult.First );
146 :
147 0 : if (0 == rStatements.getLength())
148 : {
149 0 : return; // no RDFa
150 : }
151 :
152 : // all stmts have the same subject, so we only handle first one
153 0 : const uno::Reference<rdf::XURI> xSubjectURI(rStatements[0].Subject,
154 0 : uno::UNO_QUERY);
155 : const uno::Reference<rdf::XBlankNode> xSubjectBNode(
156 0 : rStatements[0].Subject, uno::UNO_QUERY);
157 0 : if (!xSubjectURI.is() && !xSubjectBNode.is())
158 : {
159 0 : throw uno::RuntimeException();
160 : }
161 : static const sal_Unicode s_OpenBracket ('[');
162 : static const sal_Unicode s_CloseBracket(']');
163 0 : const ::rtl::OUString about( xSubjectURI.is()
164 0 : ? getRelativeReference(m_rExport, xSubjectURI->getStringValue())
165 0 : : ::rtl::OUStringBuffer().append(s_OpenBracket).append(
166 0 : LookupBlankNode(xSubjectBNode)).append(s_CloseBracket)
167 : .makeStringAndClear()
168 0 : );
169 :
170 : const uno::Reference<rdf::XLiteral> xContent(
171 0 : rStatements[0].Object, uno::UNO_QUERY_THROW );
172 0 : const uno::Reference<rdf::XURI> xDatatype(xContent->getDatatype());
173 0 : if (xDatatype.is())
174 : {
175 : const ::rtl::OUString datatype(
176 0 : makeCURIE(&m_rExport, xDatatype) );
177 : m_rExport.AddAttribute(XML_NAMESPACE_XHTML,
178 0 : token::XML_DATATYPE, datatype);
179 : }
180 0 : if (RDFaResult.Second) // there is xhtml:content
181 : {
182 : m_rExport.AddAttribute(XML_NAMESPACE_XHTML, token::XML_CONTENT,
183 0 : xContent->getValue());
184 : }
185 :
186 0 : ::rtl::OUStringBuffer property;
187 : ::comphelper::intersperse(
188 : ::boost::make_transform_iterator(
189 : ::comphelper::stl_begin(rStatements),
190 : ::boost::bind(&makeCURIE, &m_rExport,
191 : ::boost::bind(&rdf::Statement::Predicate, _1))),
192 : // argh, this must be the same type :(
193 : ::boost::make_transform_iterator(
194 : ::comphelper::stl_end(rStatements),
195 : ::boost::bind(&makeCURIE, &m_rExport,
196 : ::boost::bind(&rdf::Statement::Predicate, _1))),
197 : ::comphelper::OUStringBufferAppender(property),
198 0 : ::rtl::OUString(" "));
199 :
200 : m_rExport.AddAttribute(XML_NAMESPACE_XHTML, token::XML_PROPERTY,
201 0 : property.makeStringAndClear());
202 :
203 0 : m_rExport.AddAttribute(XML_NAMESPACE_XHTML, token::XML_ABOUT, about);
204 : }
205 0 : catch (uno::Exception &)
206 : {
207 : OSL_FAIL("AddRDFa: exception");
208 : }
209 : }
210 :
211 72 : } // namespace xmloff
212 :
213 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|