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