bump product version to 4.1.6.2
[LibreOffice.git] / xmloff / source / core / RDFaImportHelper.cxx
blob3ff61d1b9055c99e7595514512175244e64cf723
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include "RDFaImportHelper.hxx"
23 #include <xmloff/xmlimp.hxx>
24 #include <xmloff/nmspmap.hxx>
26 #include <comphelper/sequenceasvector.hxx>
28 #include <com/sun/star/rdf/URI.hpp>
29 #include <com/sun/star/rdf/XDocumentMetadataAccess.hpp>
30 #include <com/sun/star/rdf/XDocumentRepository.hpp>
32 #include <rtl/ustring.hxx>
34 #include <boost/bind.hpp>
35 #include <boost/iterator_adaptors.hpp>
36 #ifndef BOOST_ITERATOR_ADAPTOR_DWA053000_HPP_ // from iterator_adaptors.hpp
37 // N.B.: the check for the header guard _of a specific version of boost_
38 // is here so this may work on different versions of boost,
39 // which sadly put the goods in different header files
40 #include <boost/iterator/transform_iterator.hpp>
41 #endif
43 #include <map>
44 #include <iterator>
45 #include <functional>
46 #include <algorithm>
49 using namespace ::com::sun::star;
51 namespace xmloff {
53 /** a bit of context for parsing RDFa attributes */
54 class SAL_DLLPRIVATE RDFaReader
56 const SvXMLImport & m_rImport;
58 const SvXMLImport & GetImport() const { return m_rImport; }
60 //FIXME: this is an ugly hack to workaround buggy SvXMLImport::GetAbsolute
61 OUString GetAbsoluteReference(OUString const & i_rURI) const
63 if (i_rURI.isEmpty() || i_rURI[0] == '#')
65 return GetImport().GetBaseURL() + i_rURI;
67 else
69 return GetImport().GetAbsoluteReference(i_rURI);
73 public:
74 RDFaReader(SvXMLImport const & i_rImport)
75 : m_rImport(i_rImport)
76 { }
78 // returns URI or blank node!
79 OUString ReadCURIE(OUString const & i_rCURIE) const;
81 std::vector< OUString >
82 ReadCURIEs(OUString const & i_rCURIEs) const;
84 OUString
85 ReadURIOrSafeCURIE( OUString const & i_rURIOrSafeCURIE) const;
88 /** helper to insert RDFa statements into the RDF repository */
89 class SAL_DLLPRIVATE RDFaInserter
91 const uno::Reference<uno::XComponentContext> m_xContext;
92 uno::Reference< rdf::XDocumentRepository > m_xRepository;
94 typedef ::std::map< OUString, uno::Reference< rdf::XBlankNode > >
95 BlankNodeMap_t;
97 BlankNodeMap_t m_BlankNodeMap;
99 public:
100 RDFaInserter(uno::Reference<uno::XComponentContext> const & i_xContext,
101 uno::Reference< rdf::XDocumentRepository > const & i_xRepository)
102 : m_xContext(i_xContext)
103 , m_xRepository(i_xRepository)
106 uno::Reference< rdf::XBlankNode >
107 LookupBlankNode(OUString const & i_rNodeId );
109 uno::Reference< rdf::XURI >
110 MakeURI( OUString const & i_rURI) const;
112 uno::Reference< rdf::XResource>
113 MakeResource( OUString const & i_rResource);
115 void InsertRDFaEntry(struct RDFaEntry const & i_rEntry);
118 /** store parsed RDFa attributes */
119 struct SAL_DLLPRIVATE ParsedRDFaAttributes
121 OUString m_About;
122 ::std::vector< OUString > m_Properties;
123 OUString m_Content;
124 OUString m_Datatype;
126 ParsedRDFaAttributes(
127 OUString const & i_rAbout,
128 ::std::vector< OUString > const & i_rProperties,
129 OUString const & i_rContent,
130 OUString const & i_rDatatype)
131 : m_About(i_rAbout)
132 , m_Properties(i_rProperties)
133 , m_Content(i_rContent)
134 , m_Datatype(i_rDatatype)
138 /** store metadatable object and its RDFa attributes */
139 struct SAL_DLLPRIVATE RDFaEntry
141 uno::Reference<rdf::XMetadatable> m_xObject;
142 ::boost::shared_ptr<ParsedRDFaAttributes> m_pRDFaAttributes;
144 RDFaEntry(uno::Reference<rdf::XMetadatable> const & i_xObject,
145 ::boost::shared_ptr<ParsedRDFaAttributes> const& i_pRDFaAttributes)
146 : m_xObject(i_xObject)
147 , m_pRDFaAttributes(i_pRDFaAttributes)
151 ////////////////////////////////////////////////////////////////////////////
154 static inline bool isWS(const sal_Unicode i_Char)
156 return ('\t' == i_Char) || ('\n' == i_Char) || ('\r' == i_Char)
157 || (' ' == i_Char);
160 static OUString splitAtWS(OUString & io_rString)
162 const sal_Int32 len( io_rString.getLength() );
163 sal_Int32 idxstt(0);
164 while ((idxstt < len) && ( isWS(io_rString[idxstt])))
165 ++idxstt; // skip leading ws
166 sal_Int32 idxend(idxstt);
167 while ((idxend < len) && (!isWS(io_rString[idxend])))
168 ++idxend; // the CURIE
169 const OUString ret(io_rString.copy(idxstt, idxend - idxstt));
170 io_rString = io_rString.copy(idxend); // rest
171 return ret;
174 OUString
175 RDFaReader::ReadCURIE(OUString const & i_rCURIE) const
177 // the RDFa spec says that a prefix is required (it may be empty: ":foo")
178 const sal_Int32 idx( i_rCURIE.indexOf(':') );
179 if (idx >= 0)
181 OUString Prefix;
182 OUString LocalName;
183 OUString Namespace;
184 sal_uInt16 nKey( GetImport().GetNamespaceMap()._GetKeyByAttrName(
185 i_rCURIE, &Prefix, &LocalName, &Namespace) );
186 if ( Prefix == "_" )
188 // eeek, it's a bnode!
189 // "_" is not a valid URI scheme => we can identify bnodes
190 return i_rCURIE;
192 else
194 SAL_WARN_IF(XML_NAMESPACE_NONE == nKey, "xmloff.core", "no namespace?");
195 if ((XML_NAMESPACE_UNKNOWN != nKey) &&
196 (XML_NAMESPACE_XMLNS != nKey))
198 // N.B.: empty LocalName is valid!
199 const OUString URI(Namespace + LocalName);
200 return GetAbsoluteReference(URI);
202 else
204 SAL_INFO("xmloff.core", "ReadCURIE: invalid CURIE: invalid prefix" );
205 return OUString();
209 SAL_INFO("xmloff.core", "ReadCURIE: invalid CURIE: no prefix" );
210 return OUString();
213 ::std::vector< OUString >
214 RDFaReader::ReadCURIEs(OUString const & i_rCURIEs) const
216 std::vector< OUString > vec;
217 OUString CURIEs(i_rCURIEs);
218 do {
219 OUString curie( splitAtWS(CURIEs) );
220 if (!curie.isEmpty())
222 const OUString uri(ReadCURIE(curie));
223 if (!uri.isEmpty())
225 vec.push_back(uri);
229 while (!CURIEs.isEmpty());
230 if (vec.empty())
232 SAL_INFO("xmloff.core", "ReadCURIEs: invalid CURIEs" );
234 return vec;
237 OUString
238 RDFaReader::ReadURIOrSafeCURIE(OUString const & i_rURIOrSafeCURIE) const
240 const sal_Int32 len(i_rURIOrSafeCURIE.getLength());
241 if (len && (i_rURIOrSafeCURIE[0] == '['))
243 if ((len >= 2) && (i_rURIOrSafeCURIE[len - 1] == ']'))
245 return ReadCURIE(i_rURIOrSafeCURIE.copy(1, len - 2));
247 else
249 SAL_INFO("xmloff.core", "ReadURIOrSafeCURIE: invalid SafeCURIE" );
250 return OUString();
253 else
255 if (i_rURIOrSafeCURIE.matchAsciiL("_:", 2)) // blank node
257 SAL_INFO("xmloff.core", "ReadURIOrSafeCURIE: invalid URI: scheme is _" );
258 return OUString();
260 else
262 return GetAbsoluteReference(i_rURIOrSafeCURIE);
267 ////////////////////////////////////////////////////////////////////////////
269 uno::Reference< rdf::XBlankNode >
270 RDFaInserter::LookupBlankNode(OUString const & i_rNodeId )
272 uno::Reference< rdf::XBlankNode > & rEntry( m_BlankNodeMap[ i_rNodeId ] );
273 if (!rEntry.is())
275 rEntry = m_xRepository->createBlankNode();
277 return rEntry;
280 uno::Reference< rdf::XURI >
281 RDFaInserter::MakeURI( OUString const & i_rURI) const
283 if (i_rURI.matchAsciiL("_:", 2)) // blank node
285 SAL_INFO("xmloff.core", "MakeURI: cannot create URI for blank node");
286 return 0;
288 else
292 return rdf::URI::create( m_xContext, i_rURI );
294 catch (uno::Exception &)
296 SAL_WARN("xmloff.core", "MakeURI: cannot create URI");
297 return 0;
302 uno::Reference< rdf::XResource>
303 RDFaInserter::MakeResource( OUString const & i_rResource)
305 if (i_rResource.matchAsciiL("_:", 2)) // blank node
307 // we cannot use the blank node label as-is: it must be distinct
308 // from labels in other graphs, so create fresh ones per XML stream
309 // N.B.: content.xml and styles.xml are distinct graphs
310 OUString name( i_rResource.copy(2) );
311 const uno::Reference< rdf::XBlankNode > xBNode( LookupBlankNode(name) );
312 SAL_WARN_IF(!xBNode.is(), "xmloff.core", "no blank node?");
313 return uno::Reference<rdf::XResource>( xBNode, uno::UNO_QUERY);
315 else
317 return uno::Reference<rdf::XResource>( MakeURI( i_rResource ),
318 uno::UNO_QUERY);
322 /** i wrote this because c++ implementations cannot agree on which variant
323 of boost::bind and std::mem_fun_ref applied to Reference::is compiles */
324 class ref_is_null :
325 public ::std::unary_function<sal_Bool, const uno::Reference<rdf::XURI> & >
327 public:
328 sal_Bool operator() (const uno::Reference<rdf::XURI> & i_rRef)
330 return !i_rRef.is();
334 void RDFaInserter::InsertRDFaEntry(
335 struct RDFaEntry const & i_rEntry)
337 SAL_WARN_IF(!i_rEntry.m_xObject.is(), "xmloff.core", "InsertRDFaEntry: invalid arg: null object");
338 if (!i_rEntry.m_xObject.is()) return;
340 const uno::Reference< rdf::XResource > xSubject(
341 MakeResource( i_rEntry.m_pRDFaAttributes->m_About ) );
342 if (!xSubject.is())
344 return; // invalid
347 ::comphelper::SequenceAsVector< uno::Reference< rdf::XURI > > predicates;
349 predicates.reserve(i_rEntry.m_pRDFaAttributes->m_Properties.size());
351 ::std::remove_copy_if(
352 ::boost::make_transform_iterator(
353 i_rEntry.m_pRDFaAttributes->m_Properties.begin(),
354 ::boost::bind(&RDFaInserter::MakeURI, this, _1)),
355 // argh, this must be the same type :(
356 ::boost::make_transform_iterator(
357 i_rEntry.m_pRDFaAttributes->m_Properties.end(),
358 ::boost::bind(&RDFaInserter::MakeURI, this, _1)),
359 ::std::back_inserter(predicates),
360 ref_is_null() );
361 // compiles only on wntmsci12
362 // ::boost::bind( ::std::logical_not<sal_Bool>(), ::boost::bind<sal_Bool>(&uno::Reference<rdf::XURI>::is, _1)));
363 // compiles on unxsoli4, wntsci12, but not unxlngi6
364 // ::boost::bind( ::std::logical_not<sal_Bool>(), ::boost::bind<sal_Bool, com::sun::star::uno::Reference<rdf::XURI> >(&uno::Reference<rdf::XURI>::is, _1)));
365 // compiles on unxsoli4, unxlngi6, but not wntsci12
366 // ::std::not1( ::std::mem_fun_ref(&uno::Reference<rdf::XURI>::is)) );
368 if (!predicates.size())
370 return; // invalid
373 uno::Reference<rdf::XURI> xDatatype;
374 if (!i_rEntry.m_pRDFaAttributes->m_Datatype.isEmpty())
376 xDatatype = MakeURI( i_rEntry.m_pRDFaAttributes->m_Datatype );
381 // N.B.: this will call xMeta->ensureMetadataReference, which is why
382 // this must be done _after_ importing the whole XML file,
383 // to prevent collision between generated ids and ids in the file
384 m_xRepository->setStatementRDFa(xSubject, predicates.getAsConstList(),
385 i_rEntry.m_xObject,
386 i_rEntry.m_pRDFaAttributes->m_Content, xDatatype);
388 catch (uno::Exception &)
390 SAL_WARN("xmloff.core", "InsertRDFaEntry: setStatementRDFa failed?");
394 ////////////////////////////////////////////////////////////////////////////
396 RDFaImportHelper::RDFaImportHelper(const SvXMLImport & i_rImport)
397 : m_rImport(i_rImport)
401 RDFaImportHelper::~RDFaImportHelper()
405 ::boost::shared_ptr<ParsedRDFaAttributes>
406 RDFaImportHelper::ParseRDFa(
407 OUString const & i_rAbout,
408 OUString const & i_rProperty,
409 OUString const & i_rContent,
410 OUString const & i_rDatatype)
412 if (i_rProperty.isEmpty())
414 SAL_INFO("xmloff.core", "AddRDFa: invalid input: xhtml:property empty");
415 return ::boost::shared_ptr<ParsedRDFaAttributes>();
417 // must parse CURIEs here: need namespace declaration context
418 RDFaReader reader(GetImport());
419 const OUString about( reader.ReadURIOrSafeCURIE(i_rAbout) );
420 if (about.isEmpty()) {
421 return ::boost::shared_ptr<ParsedRDFaAttributes>();
423 const ::std::vector< OUString > properties(
424 reader.ReadCURIEs(i_rProperty) );
425 if (!properties.size()) {
426 return ::boost::shared_ptr<ParsedRDFaAttributes>();
428 const OUString datatype( !i_rDatatype.isEmpty()
429 ? reader.ReadCURIE(i_rDatatype)
430 : OUString() );
431 return ::boost::shared_ptr<ParsedRDFaAttributes>(
432 new ParsedRDFaAttributes(about, properties, i_rContent, datatype));
435 void
436 RDFaImportHelper::AddRDFa(
437 uno::Reference<rdf::XMetadatable> const & i_xObject,
438 ::boost::shared_ptr<ParsedRDFaAttributes> & i_pRDFaAttributes)
440 if (!i_xObject.is())
442 SAL_WARN("xmloff.core", "AddRDFa: invalid arg: null textcontent");
443 return;
445 if (!i_pRDFaAttributes.get())
447 SAL_WARN("xmloff.core", "AddRDFa: invalid arg: null RDFa attributes");
448 return;
450 m_RDFaEntries.push_back(RDFaEntry(i_xObject, i_pRDFaAttributes));
453 void
454 RDFaImportHelper::ParseAndAddRDFa(
455 uno::Reference<rdf::XMetadatable> const & i_xObject,
456 OUString const & i_rAbout,
457 OUString const & i_rProperty,
458 OUString const & i_rContent,
459 OUString const & i_rDatatype)
461 ::boost::shared_ptr<ParsedRDFaAttributes> pAttributes(
462 ParseRDFa(i_rAbout, i_rProperty, i_rContent, i_rDatatype) );
463 if (pAttributes.get())
465 AddRDFa(i_xObject, pAttributes);
469 void RDFaImportHelper::InsertRDFa(
470 uno::Reference< rdf::XRepositorySupplier> const & i_xModel)
472 SAL_WARN_IF(!i_xModel.is(), "xmloff.core", "InsertRDFa: invalid arg: model null");
473 if (!i_xModel.is()) return;
474 const uno::Reference< rdf::XDocumentRepository > xRepository(
475 i_xModel->getRDFRepository(), uno::UNO_QUERY);
476 SAL_WARN_IF(!xRepository.is(), "xmloff.core", "InsertRDFa: no DocumentRepository?");
477 if (!xRepository.is()) return;
478 RDFaInserter inserter(GetImport().GetComponentContext(), xRepository);
479 ::std::for_each(m_RDFaEntries.begin(), m_RDFaEntries.end(),
480 ::boost::bind(&RDFaInserter::InsertRDFaEntry, &inserter, _1));
483 } // namespace xmloff
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */