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