1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63bba73cSAndrew Rist  * distributed with this work for additional information
6*63bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*63bba73cSAndrew Rist  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist  *
13*63bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist  * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63bba73cSAndrew Rist  * specific language governing permissions and limitations
18*63bba73cSAndrew Rist  * under the License.
19*63bba73cSAndrew Rist  *
20*63bba73cSAndrew Rist  *************************************************************/
21*63bba73cSAndrew Rist 
22*63bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "RDFaExportHelper.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <xmloff/xmlexp.hxx>
31cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <comphelper/stlunosequence.hxx>
34cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <com/sun/star/uri/XUriReference.hpp>
37cdf0e10cSrcweir #include <com/sun/star/uri/XUriReferenceFactory.hpp>
38cdf0e10cSrcweir #include <com/sun/star/rdf/Statement.hpp>
39cdf0e10cSrcweir #include <com/sun/star/rdf/URIs.hpp>
40cdf0e10cSrcweir #include <com/sun/star/rdf/URI.hpp>
41cdf0e10cSrcweir #include <com/sun/star/rdf/XLiteral.hpp>
42cdf0e10cSrcweir #include <com/sun/star/rdf/XRepositorySupplier.hpp>
43cdf0e10cSrcweir #include <com/sun/star/rdf/XDocumentRepository.hpp>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <boost/bind.hpp>
48cdf0e10cSrcweir #include <boost/iterator_adaptors.hpp>
49cdf0e10cSrcweir #ifndef BOOST_ITERATOR_ADAPTOR_DWA053000_HPP_ // from iterator_adaptors.hpp
50cdf0e10cSrcweir // N.B.: the check for the header guard _of a specific version of boost_
51cdf0e10cSrcweir //       is here so this may work on different versions of boost,
52cdf0e10cSrcweir //       which sadly put the goods in different header files
53cdf0e10cSrcweir #include <boost/iterator/transform_iterator.hpp>
54cdf0e10cSrcweir #endif
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #include <functional>
57cdf0e10cSrcweir #include <algorithm>
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 
60cdf0e10cSrcweir using namespace ::com::sun::star;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir namespace xmloff {
63cdf0e10cSrcweir 
64cdf0e10cSrcweir static const char s_prefix [] = "_:b";
65cdf0e10cSrcweir 
66cdf0e10cSrcweir static ::rtl::OUString
makeCURIE(SvXMLExport * i_pExport,uno::Reference<rdf::XURI> const & i_xURI)67cdf0e10cSrcweir makeCURIE(SvXMLExport * i_pExport,
68cdf0e10cSrcweir     uno::Reference<rdf::XURI> const & i_xURI)
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     OSL_ENSURE(i_xURI.is(), "makeCURIE: null URI");
71cdf0e10cSrcweir     if (!i_xURI.is()) throw uno::RuntimeException();
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     const ::rtl::OUString Namespace( i_xURI->getNamespace() );
74cdf0e10cSrcweir     OSL_ENSURE(Namespace.getLength(), "makeCURIE: no namespace");
75cdf0e10cSrcweir     if (!Namespace.getLength()) throw uno::RuntimeException();
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     ::rtl::OUStringBuffer buf;
78cdf0e10cSrcweir     buf.append( i_pExport->EnsureNamespace(Namespace) );
79cdf0e10cSrcweir     buf.append( static_cast<sal_Unicode>(':') );
80cdf0e10cSrcweir     // N.B.: empty LocalName is valid!
81cdf0e10cSrcweir     buf.append( i_xURI->getLocalName() );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     return buf.makeStringAndClear();
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir // #i112473# SvXMLExport::GetRelativeReference() not right for RDF on SaveAs
87cdf0e10cSrcweir // because the URIs in the repository are not rewritten on SaveAs, the
88cdf0e10cSrcweir // URI of the loaded document has to be used, not the URI of the target doc.
89cdf0e10cSrcweir static ::rtl::OUString
getRelativeReference(SvXMLExport const & rExport,::rtl::OUString const & rURI)90cdf0e10cSrcweir getRelativeReference(SvXMLExport const& rExport, ::rtl::OUString const& rURI)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir     uno::Reference< rdf::XURI > const xModelURI(
93cdf0e10cSrcweir         rExport.GetModel(), uno::UNO_QUERY_THROW );
94cdf0e10cSrcweir     ::rtl::OUString const baseURI( xModelURI->getStringValue() );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     uno::Reference<uno::XComponentContext> const xContext(
97cdf0e10cSrcweir         rExport.GetComponentContext());
98cdf0e10cSrcweir     uno::Reference<lang::XMultiComponentFactory> const xServiceFactory(
99cdf0e10cSrcweir         xContext->getServiceManager(), uno::UNO_SET_THROW);
100cdf0e10cSrcweir     uno::Reference<uri::XUriReferenceFactory> const xUriFactory(
101cdf0e10cSrcweir         xServiceFactory->createInstanceWithContext(
102cdf0e10cSrcweir             ::rtl::OUString::createFromAscii(
103cdf0e10cSrcweir                 "com.sun.star.uri.UriReferenceFactory"), xContext),
104cdf0e10cSrcweir         uno::UNO_QUERY_THROW);
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     uno::Reference< uri::XUriReference > const xBaseURI(
107cdf0e10cSrcweir         xUriFactory->parse(baseURI), uno::UNO_SET_THROW );
108cdf0e10cSrcweir     uno::Reference< uri::XUriReference > const xAbsoluteURI(
109cdf0e10cSrcweir         xUriFactory->parse(rURI), uno::UNO_SET_THROW );
110cdf0e10cSrcweir     uno::Reference< uri::XUriReference > const xRelativeURI(
111cdf0e10cSrcweir         xUriFactory->makeRelative(xBaseURI, xAbsoluteURI, true, true, false),
112cdf0e10cSrcweir         uno::UNO_SET_THROW );
113cdf0e10cSrcweir     ::rtl::OUString const relativeURI(xRelativeURI->getUriReference());
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     return relativeURI;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 
119cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////
120cdf0e10cSrcweir 
RDFaExportHelper(SvXMLExport & i_rExport)121cdf0e10cSrcweir RDFaExportHelper::RDFaExportHelper(SvXMLExport & i_rExport)
122cdf0e10cSrcweir     : m_rExport(i_rExport), m_xRepository(0), m_Counter(0)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir     const uno::Reference<rdf::XRepositorySupplier> xRS( m_rExport.GetModel(),
125cdf0e10cSrcweir             uno::UNO_QUERY);
126cdf0e10cSrcweir     OSL_ENSURE(xRS.is(), "AddRDFa: model is no rdf::XRepositorySupplier");
127cdf0e10cSrcweir     if (!xRS.is()) throw uno::RuntimeException();
128cdf0e10cSrcweir     m_xRepository.set(xRS->getRDFRepository(), uno::UNO_QUERY_THROW);
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir ::rtl::OUString
LookupBlankNode(uno::Reference<rdf::XBlankNode> const & i_xBlankNode)132cdf0e10cSrcweir RDFaExportHelper::LookupBlankNode(
133cdf0e10cSrcweir     uno::Reference<rdf::XBlankNode> const & i_xBlankNode)
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     OSL_ENSURE(i_xBlankNode.is(), "null BlankNode?");
136cdf0e10cSrcweir     if (!i_xBlankNode.is()) throw uno::RuntimeException();
137cdf0e10cSrcweir     ::rtl::OUString & rEntry(
138cdf0e10cSrcweir         m_BlankNodeMap[ i_xBlankNode->getStringValue() ] );
139cdf0e10cSrcweir     if (!rEntry.getLength())
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         ::rtl::OUStringBuffer buf;
142cdf0e10cSrcweir         buf.appendAscii(s_prefix);
143cdf0e10cSrcweir         buf.append(++m_Counter);
144cdf0e10cSrcweir         rEntry = buf.makeStringAndClear();
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir     return rEntry;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////
150cdf0e10cSrcweir 
151cdf0e10cSrcweir void
AddRDFa(uno::Reference<rdf::XMetadatable> const & i_xMetadatable)152cdf0e10cSrcweir RDFaExportHelper::AddRDFa(
153cdf0e10cSrcweir     uno::Reference<rdf::XMetadatable> const & i_xMetadatable)
154cdf0e10cSrcweir {
155cdf0e10cSrcweir     try
156cdf0e10cSrcweir     {
157cdf0e10cSrcweir         beans::Pair< uno::Sequence<rdf::Statement>, sal_Bool > const
158cdf0e10cSrcweir             RDFaResult( m_xRepository->getStatementRDFa(i_xMetadatable) );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         uno::Sequence<rdf::Statement> const & rStatements( RDFaResult.First );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir         if (0 == rStatements.getLength())
163cdf0e10cSrcweir         {
164cdf0e10cSrcweir             return; // no RDFa
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         // all stmts have the same subject, so we only handle first one
168cdf0e10cSrcweir         const uno::Reference<rdf::XURI> xSubjectURI(rStatements[0].Subject,
169cdf0e10cSrcweir             uno::UNO_QUERY);
170cdf0e10cSrcweir         const uno::Reference<rdf::XBlankNode> xSubjectBNode(
171cdf0e10cSrcweir             rStatements[0].Subject, uno::UNO_QUERY);
172cdf0e10cSrcweir         if (!xSubjectURI.is() && !xSubjectBNode.is())
173cdf0e10cSrcweir         {
174cdf0e10cSrcweir             throw uno::RuntimeException();
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir         static const sal_Unicode s_OpenBracket ('[');
177cdf0e10cSrcweir         static const sal_Unicode s_CloseBracket(']');
178cdf0e10cSrcweir         const ::rtl::OUString about( xSubjectURI.is()
179cdf0e10cSrcweir             ?   getRelativeReference(m_rExport, xSubjectURI->getStringValue())
180cdf0e10cSrcweir             :   ::rtl::OUStringBuffer().append(s_OpenBracket).append(
181cdf0e10cSrcweir                         LookupBlankNode(xSubjectBNode)).append(s_CloseBracket)
182cdf0e10cSrcweir                     .makeStringAndClear()
183cdf0e10cSrcweir             );
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         const uno::Reference<rdf::XLiteral> xContent(
186cdf0e10cSrcweir             rStatements[0].Object, uno::UNO_QUERY_THROW );
187cdf0e10cSrcweir         const uno::Reference<rdf::XURI> xDatatype(xContent->getDatatype());
188cdf0e10cSrcweir         if (xDatatype.is())
189cdf0e10cSrcweir         {
190cdf0e10cSrcweir             const ::rtl::OUString datatype(
191cdf0e10cSrcweir                 makeCURIE(&m_rExport, xDatatype) );
192cdf0e10cSrcweir             m_rExport.AddAttribute(XML_NAMESPACE_XHTML,
193cdf0e10cSrcweir                 token::XML_DATATYPE, datatype);
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir         if (RDFaResult.Second) // there is xhtml:content
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir             m_rExport.AddAttribute(XML_NAMESPACE_XHTML, token::XML_CONTENT,
198cdf0e10cSrcweir                 xContent->getValue());
199cdf0e10cSrcweir         }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir         ::rtl::OUStringBuffer property;
202cdf0e10cSrcweir         ::comphelper::intersperse(
203cdf0e10cSrcweir             ::boost::make_transform_iterator(
204cdf0e10cSrcweir                 ::comphelper::stl_begin(rStatements),
205cdf0e10cSrcweir                 ::boost::bind(&makeCURIE, &m_rExport,
206cdf0e10cSrcweir                     ::boost::bind(&rdf::Statement::Predicate, _1))),
207cdf0e10cSrcweir             // argh, this must be the same type :(
208cdf0e10cSrcweir             ::boost::make_transform_iterator(
209cdf0e10cSrcweir                 ::comphelper::stl_end(rStatements),
210cdf0e10cSrcweir                 ::boost::bind(&makeCURIE, &m_rExport,
211cdf0e10cSrcweir                     ::boost::bind(&rdf::Statement::Predicate, _1))),
212cdf0e10cSrcweir             ::comphelper::OUStringBufferAppender(property),
213cdf0e10cSrcweir             ::rtl::OUString::createFromAscii(" "));
214cdf0e10cSrcweir 
215cdf0e10cSrcweir         m_rExport.AddAttribute(XML_NAMESPACE_XHTML, token::XML_PROPERTY,
216cdf0e10cSrcweir             property.makeStringAndClear());
217cdf0e10cSrcweir 
218cdf0e10cSrcweir         m_rExport.AddAttribute(XML_NAMESPACE_XHTML, token::XML_ABOUT, about);
219cdf0e10cSrcweir     }
220cdf0e10cSrcweir     catch (uno::Exception &)
221cdf0e10cSrcweir     {
222cdf0e10cSrcweir         OSL_ENSURE(false, "AddRDFa: exception");
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir } // namespace xmloff
227cdf0e10cSrcweir 
228