Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "RDFaImportHelper.hxx"
31 : :
32 : : #include <xmloff/xmlimp.hxx>
33 : : #include <xmloff/nmspmap.hxx>
34 : :
35 : : #include <comphelper/sequenceasvector.hxx>
36 : :
37 : : #include <com/sun/star/rdf/URI.hpp>
38 : : #include <com/sun/star/rdf/XDocumentMetadataAccess.hpp>
39 : : #include <com/sun/star/rdf/XDocumentRepository.hpp>
40 : :
41 : : #include <rtl/ustring.hxx>
42 : :
43 : : #include <boost/bind.hpp>
44 : : #include <boost/iterator_adaptors.hpp>
45 : : #ifndef BOOST_ITERATOR_ADAPTOR_DWA053000_HPP_ // from iterator_adaptors.hpp
46 : : // N.B.: the check for the header guard _of a specific version of boost_
47 : : // is here so this may work on different versions of boost,
48 : : // which sadly put the goods in different header files
49 : : #include <boost/iterator/transform_iterator.hpp>
50 : : #endif
51 : :
52 : : #include <map>
53 : : #include <iterator>
54 : : #include <functional>
55 : : #include <algorithm>
56 : :
57 : :
58 : : using namespace ::com::sun::star;
59 : :
60 : : namespace xmloff {
61 : :
62 : : /** a bit of context for parsing RDFa attributes */
63 : : class SAL_DLLPRIVATE RDFaReader
64 : : {
65 : : const SvXMLImport & m_rImport;
66 : :
67 : 280 : const SvXMLImport & GetImport() const { return m_rImport; }
68 : :
69 : : //FIXME: this is an ugly hack to workaround buggy SvXMLImport::GetAbsolute
70 : 158 : ::rtl::OUString GetAbsoluteReference(::rtl::OUString const & i_rURI) const
71 : : {
72 [ + + ][ - + ]: 158 : if (i_rURI.isEmpty() || i_rURI[0] == '#')
[ + + ]
73 : : {
74 : 2 : return GetImport().GetBaseURL() + i_rURI;
75 : : }
76 : : else
77 : : {
78 : 158 : return GetImport().GetAbsoluteReference(i_rURI);
79 : : }
80 : : }
81 : :
82 : : public:
83 : 76 : RDFaReader(SvXMLImport const & i_rImport)
84 : 76 : : m_rImport(i_rImport)
85 : 76 : { }
86 : :
87 : : // returns URI or blank node!
88 : : ::rtl::OUString ReadCURIE(::rtl::OUString const & i_rCURIE) const;
89 : :
90 : : std::vector< ::rtl::OUString >
91 : : ReadCURIEs(::rtl::OUString const & i_rCURIEs) const;
92 : :
93 : : ::rtl::OUString
94 : : ReadURIOrSafeCURIE( ::rtl::OUString const & i_rURIOrSafeCURIE) const;
95 : : };
96 : :
97 : : /** helper to insert RDFa statements into the RDF repository */
98 : 12 : class SAL_DLLPRIVATE RDFaInserter
99 : : {
100 : : const uno::Reference<uno::XComponentContext> m_xContext;
101 : : uno::Reference< rdf::XDocumentRepository > m_xRepository;
102 : :
103 : : typedef ::std::map< ::rtl::OUString, uno::Reference< rdf::XBlankNode > >
104 : : BlankNodeMap_t;
105 : :
106 : : BlankNodeMap_t m_BlankNodeMap;
107 : :
108 : : public:
109 : 12 : RDFaInserter(uno::Reference<uno::XComponentContext> const & i_xContext,
110 : : uno::Reference< rdf::XDocumentRepository > const & i_xRepository)
111 : : : m_xContext(i_xContext)
112 [ + - ]: 12 : , m_xRepository(i_xRepository)
113 : 12 : {}
114 : :
115 : : uno::Reference< rdf::XBlankNode >
116 : : LookupBlankNode(::rtl::OUString const & i_rNodeId );
117 : :
118 : : uno::Reference< rdf::XURI >
119 : : MakeURI( ::rtl::OUString const & i_rURI) const;
120 : :
121 : : uno::Reference< rdf::XResource>
122 : : MakeResource( ::rtl::OUString const & i_rResource);
123 : :
124 : : void InsertRDFaEntry(struct RDFaEntry const & i_rEntry);
125 : : };
126 : :
127 : : /** store parsed RDFa attributes */
128 : 70 : struct SAL_DLLPRIVATE ParsedRDFaAttributes
129 : : {
130 : : ::rtl::OUString m_About;
131 : : ::std::vector< ::rtl::OUString > m_Properties;
132 : : ::rtl::OUString m_Content;
133 : : ::rtl::OUString m_Datatype;
134 : :
135 : 70 : ParsedRDFaAttributes(
136 : : ::rtl::OUString const & i_rAbout,
137 : : ::std::vector< ::rtl::OUString > const & i_rProperties,
138 : : ::rtl::OUString const & i_rContent,
139 : : ::rtl::OUString const & i_rDatatype)
140 : : : m_About(i_rAbout)
141 : : , m_Properties(i_rProperties)
142 : : , m_Content(i_rContent)
143 [ + - ]: 70 : , m_Datatype(i_rDatatype)
144 : 70 : { }
145 : : };
146 : :
147 : : /** store metadatable object and its RDFa attributes */
148 [ + - ][ + - ]: 338 : struct SAL_DLLPRIVATE RDFaEntry
149 : : {
150 : : uno::Reference<rdf::XMetadatable> m_xObject;
151 : : ::boost::shared_ptr<ParsedRDFaAttributes> m_pRDFaAttributes;
152 : :
153 : 70 : RDFaEntry(uno::Reference<rdf::XMetadatable> const & i_xObject,
154 : : ::boost::shared_ptr<ParsedRDFaAttributes> const& i_pRDFaAttributes)
155 : : : m_xObject(i_xObject)
156 [ + - ]: 70 : , m_pRDFaAttributes(i_pRDFaAttributes)
157 : 70 : { }
158 : : };
159 : :
160 : : ////////////////////////////////////////////////////////////////////////////
161 : :
162 : :
163 : 688 : static inline bool isWS(const sal_Unicode i_Char)
164 : : {
165 : : return ('\t' == i_Char) || ('\n' == i_Char) || ('\r' == i_Char)
166 [ + - ][ + - ]: 688 : || (' ' == i_Char);
[ + - ][ + + ]
167 : : }
168 : :
169 : 84 : static ::rtl::OUString splitAtWS(::rtl::OUString & io_rString)
170 : : {
171 : 84 : const sal_Int32 len( io_rString.getLength() );
172 : 84 : sal_Int32 idxstt(0);
173 [ + + ][ + + ]: 106 : while ((idxstt < len) && ( isWS(io_rString[idxstt])))
[ + + ]
174 : 22 : ++idxstt; // skip leading ws
175 : 84 : sal_Int32 idxend(idxstt);
176 [ + + ][ + + ]: 654 : while ((idxend < len) && (!isWS(io_rString[idxend])))
[ + + ]
177 : 570 : ++idxend; // the CURIE
178 : 84 : const ::rtl::OUString ret(io_rString.copy(idxstt, idxend - idxstt));
179 : 84 : io_rString = io_rString.copy(idxend); // rest
180 : 84 : return ret;
181 : : }
182 : :
183 : : ::rtl::OUString
184 : 124 : RDFaReader::ReadCURIE(::rtl::OUString const & i_rCURIE) const
185 : : {
186 : : // the RDFa spec says that a prefix is required (it may be empty: ":foo")
187 : 124 : const sal_Int32 idx( i_rCURIE.indexOf(':') );
188 [ + + ]: 124 : if (idx >= 0)
189 : : {
190 : 122 : ::rtl::OUString Prefix;
191 : 122 : ::rtl::OUString LocalName;
192 : 122 : ::rtl::OUString Namespace;
193 : 122 : sal_uInt16 nKey( GetImport().GetNamespaceMap()._GetKeyByAttrName(
194 [ + - ]: 122 : i_rCURIE, &Prefix, &LocalName, &Namespace) );
195 [ + + ]: 122 : if ( Prefix == "_" )
196 : : {
197 : : // eeek, it's a bnode!
198 : : // "_" is not a valid URI scheme => we can identify bnodes
199 : 16 : return i_rCURIE;
200 : : }
201 : : else
202 : : {
203 : : OSL_ENSURE(XML_NAMESPACE_NONE != nKey, "no namespace?");
204 [ + + ][ + - ]: 106 : if ((XML_NAMESPACE_UNKNOWN != nKey) &&
205 : : (XML_NAMESPACE_XMLNS != nKey))
206 : : {
207 : : // N.B.: empty LocalName is valid!
208 : 102 : const ::rtl::OUString URI(Namespace + LocalName);
209 [ + - ]: 102 : return GetAbsoluteReference(URI);
210 : : }
211 : : else
212 : : {
213 : : OSL_TRACE( "ReadCURIE: invalid CURIE: invalid prefix" );
214 : 4 : return ::rtl::OUString();
215 : : }
216 : 122 : }
217 : : }
218 : : OSL_TRACE( "ReadCURIE: invalid CURIE: no prefix" );
219 : 124 : return ::rtl::OUString();
220 : : }
221 : :
222 : : ::std::vector< ::rtl::OUString >
223 : 70 : RDFaReader::ReadCURIEs(::rtl::OUString const & i_rCURIEs) const
224 : : {
225 [ + - ]: 70 : std::vector< ::rtl::OUString > vec;
226 : 70 : ::rtl::OUString CURIEs(i_rCURIEs);
227 [ + + ]: 84 : do {
228 : 84 : ::rtl::OUString curie( splitAtWS(CURIEs) );
229 [ + + ]: 84 : if (!curie.isEmpty())
230 : : {
231 [ + - ]: 82 : const ::rtl::OUString uri(ReadCURIE(curie));
232 [ + - ]: 82 : if (!uri.isEmpty())
233 : : {
234 [ + - ]: 82 : vec.push_back(uri);
235 : 82 : }
236 : 84 : }
237 : : }
238 : 84 : while (!CURIEs.isEmpty());
239 : 70 : if (vec.empty())
240 : : {
241 : : OSL_TRACE( "ReadCURIEs: invalid CURIEs" );
242 : : }
243 : 70 : return vec;
244 : : }
245 : :
246 : : ::rtl::OUString
247 : 76 : RDFaReader::ReadURIOrSafeCURIE(::rtl::OUString const & i_rURIOrSafeCURIE) const
248 : : {
249 : 76 : const sal_Int32 len(i_rURIOrSafeCURIE.getLength());
250 [ + + ][ + + ]: 76 : if (len && (i_rURIOrSafeCURIE[0] == '['))
[ + + ]
251 : : {
252 [ + - ][ + - ]: 18 : if ((len >= 2) && (i_rURIOrSafeCURIE[len - 1] == ']'))
[ + - ]
253 : : {
254 [ + - ]: 18 : return ReadCURIE(i_rURIOrSafeCURIE.copy(1, len - 2));
255 : : }
256 : : else
257 : : {
258 : : OSL_TRACE( "ReadURIOrSafeCURIE: invalid SafeCURIE" );
259 : 0 : return ::rtl::OUString();
260 : : }
261 : : }
262 : : else
263 : : {
264 [ + + ]: 58 : if (i_rURIOrSafeCURIE.matchAsciiL("_:", 2)) // blank node
265 : : {
266 : : OSL_TRACE( "ReadURIOrSafeCURIE: invalid URI: scheme is _" );
267 : 2 : return ::rtl::OUString();
268 : : }
269 : : else
270 : : {
271 : 76 : return GetAbsoluteReference(i_rURIOrSafeCURIE);
272 : : }
273 : : }
274 : : }
275 : :
276 : : ////////////////////////////////////////////////////////////////////////////
277 : :
278 : : uno::Reference< rdf::XBlankNode >
279 : 12 : RDFaInserter::LookupBlankNode(::rtl::OUString const & i_rNodeId )
280 : : {
281 : 12 : uno::Reference< rdf::XBlankNode > & rEntry( m_BlankNodeMap[ i_rNodeId ] );
282 [ + + ]: 12 : if (!rEntry.is())
283 : : {
284 [ + - ]: 8 : rEntry = m_xRepository->createBlankNode();
285 : : }
286 : 12 : return rEntry;
287 : : }
288 : :
289 : : uno::Reference< rdf::XURI >
290 : 242 : RDFaInserter::MakeURI( ::rtl::OUString const & i_rURI) const
291 : : {
292 [ + + ]: 242 : if (i_rURI.matchAsciiL("_:", 2)) // blank node
293 : : {
294 : : OSL_TRACE("MakeURI: cannot create URI for blank node");
295 : 4 : return 0;
296 : : }
297 : : else
298 : : {
299 : : try
300 : : {
301 [ + - ]: 242 : return rdf::URI::create( m_xContext, i_rURI );
302 : : }
303 [ # # ]: 0 : catch (uno::Exception &)
304 : : {
305 : : OSL_FAIL("MakeURI: cannot create URI");
306 [ # # ]: 0 : return 0;
307 : : }
308 : : }
309 : : }
310 : :
311 : : uno::Reference< rdf::XResource>
312 : 70 : RDFaInserter::MakeResource( ::rtl::OUString const & i_rResource)
313 : : {
314 [ + + ]: 70 : if (i_rResource.matchAsciiL("_:", 2)) // blank node
315 : : {
316 : : // we cannot use the blank node label as-is: it must be distinct
317 : : // from labels in other graphs, so create fresh ones per XML stream
318 : : // N.B.: content.xml and styles.xml are distinct graphs
319 : 12 : ::rtl::OUString name( i_rResource.copy(2) );
320 [ + - ]: 12 : const uno::Reference< rdf::XBlankNode > xBNode( LookupBlankNode(name) );
321 : : OSL_ENSURE(xBNode.is(), "no blank node?");
322 [ + - ]: 12 : return uno::Reference<rdf::XResource>( xBNode, uno::UNO_QUERY);
323 : : }
324 : : else
325 : : {
326 : : return uno::Reference<rdf::XResource>( MakeURI( i_rResource ),
327 [ + - ]: 70 : uno::UNO_QUERY);
328 : : }
329 : : }
330 : :
331 : : /** i wrote this because c++ implementations cannot agree on which variant
332 : : of boost::bind and std::mem_fun_ref applied to Reference::is compiles */
333 : : class ref_is_null :
334 : : public ::std::unary_function<sal_Bool, const uno::Reference<rdf::XURI> & >
335 : : {
336 : : public:
337 : 82 : sal_Bool operator() (const uno::Reference<rdf::XURI> & i_rRef)
338 : : {
339 : 82 : return !i_rRef.is();
340 : : }
341 : : };
342 : :
343 : 70 : void RDFaInserter::InsertRDFaEntry(
344 : : struct RDFaEntry const & i_rEntry)
345 : : {
346 : : OSL_ENSURE(i_rEntry.m_xObject.is(),
347 : : "InsertRDFaEntry: invalid arg: null object");
348 [ + - ]: 70 : if (!i_rEntry.m_xObject.is()) return;
349 : :
350 : : const uno::Reference< rdf::XResource > xSubject(
351 [ + - ]: 70 : MakeResource( i_rEntry.m_pRDFaAttributes->m_About ) );
352 [ - + ]: 70 : if (!xSubject.is())
353 : : {
354 : : return; // invalid
355 : : }
356 : :
357 [ + - ]: 70 : ::comphelper::SequenceAsVector< uno::Reference< rdf::XURI > > predicates;
358 : :
359 [ + - ]: 70 : predicates.reserve(i_rEntry.m_pRDFaAttributes->m_Properties.size());
360 : :
361 : : ::std::remove_copy_if(
362 : : ::boost::make_transform_iterator(
363 : 70 : i_rEntry.m_pRDFaAttributes->m_Properties.begin(),
364 : : ::boost::bind(&RDFaInserter::MakeURI, this, _1)),
365 : : // argh, this must be the same type :(
366 : : ::boost::make_transform_iterator(
367 : 70 : i_rEntry.m_pRDFaAttributes->m_Properties.end(),
368 : : ::boost::bind(&RDFaInserter::MakeURI, this, _1)),
369 : : ::std::back_inserter(predicates),
370 [ + - ]: 210 : ref_is_null() );
[ + - + - ]
[ + - + - ]
[ + - ]
371 : : // compiles only on wntmsci12
372 : : // ::boost::bind( ::std::logical_not<sal_Bool>(), ::boost::bind<sal_Bool>(&uno::Reference<rdf::XURI>::is, _1)));
373 : : // compiles on unxsoli4, wntsci12, but not unxlngi6
374 : : // ::boost::bind( ::std::logical_not<sal_Bool>(), ::boost::bind<sal_Bool, com::sun::star::uno::Reference<rdf::XURI> >(&uno::Reference<rdf::XURI>::is, _1)));
375 : : // compiles on unxsoli4, unxlngi6, but not wntsci12
376 : : // ::std::not1( ::std::mem_fun_ref(&uno::Reference<rdf::XURI>::is)) );
377 : :
378 [ + + ]: 70 : if (!predicates.size())
379 : : {
380 : : return; // invalid
381 : : }
382 : :
383 : 68 : uno::Reference<rdf::XURI> xDatatype;
384 [ + + ]: 68 : if (!i_rEntry.m_pRDFaAttributes->m_Datatype.isEmpty())
385 : : {
386 [ + - ][ + - ]: 22 : xDatatype = MakeURI( i_rEntry.m_pRDFaAttributes->m_Datatype );
387 : : }
388 : :
389 : : try
390 : : {
391 : : // N.B.: this will call xMeta->ensureMetadataReference, which is why
392 : : // this must be done _after_ importing the whole XML file,
393 : : // to prevent collision between generated ids and ids in the file
394 [ + - ]: 68 : m_xRepository->setStatementRDFa(xSubject, predicates.getAsConstList(),
395 : : i_rEntry.m_xObject,
396 [ + - ][ + - ]: 68 : i_rEntry.m_pRDFaAttributes->m_Content, xDatatype);
[ + - ][ # # ]
397 : : }
398 [ # # ]: 0 : catch (uno::Exception &)
399 : : {
400 : : OSL_FAIL("InsertRDFaEntry: setStatementRDFa failed?");
401 [ + + ][ + + ]: 70 : }
402 : : }
403 : :
404 : : ////////////////////////////////////////////////////////////////////////////
405 : :
406 : 12 : RDFaImportHelper::RDFaImportHelper(const SvXMLImport & i_rImport)
407 : 12 : : m_rImport(i_rImport)
408 : : {
409 : 12 : }
410 : :
411 : 12 : RDFaImportHelper::~RDFaImportHelper()
412 : : {
413 : 12 : }
414 : :
415 : : ::boost::shared_ptr<ParsedRDFaAttributes>
416 : 80 : RDFaImportHelper::ParseRDFa(
417 : : ::rtl::OUString const & i_rAbout,
418 : : ::rtl::OUString const & i_rProperty,
419 : : ::rtl::OUString const & i_rContent,
420 : : ::rtl::OUString const & i_rDatatype)
421 : : {
422 [ + + ]: 80 : if (i_rProperty.isEmpty())
423 : : {
424 : : OSL_TRACE("AddRDFa: invalid input: xhtml:property empty");
425 [ + - ]: 4 : return ::boost::shared_ptr<ParsedRDFaAttributes>();
426 : : }
427 : : // must parse CURIEs here: need namespace declaration context
428 : 76 : RDFaReader reader(GetImport());
429 [ + - ]: 76 : const ::rtl::OUString about( reader.ReadURIOrSafeCURIE(i_rAbout) );
430 [ + + ]: 76 : if (about.isEmpty()) {
431 [ + - ]: 6 : return ::boost::shared_ptr<ParsedRDFaAttributes>();
432 : : }
433 : : const ::std::vector< ::rtl::OUString > properties(
434 [ + - ]: 70 : reader.ReadCURIEs(i_rProperty) );
435 [ - + ]: 70 : if (!properties.size()) {
436 [ # # ]: 0 : return ::boost::shared_ptr<ParsedRDFaAttributes>();
437 : : }
438 : 70 : const ::rtl::OUString datatype( !i_rDatatype.isEmpty()
439 : : ? reader.ReadCURIE(i_rDatatype)
440 [ + - ][ + + ]: 70 : : ::rtl::OUString() );
441 : : return ::boost::shared_ptr<ParsedRDFaAttributes>(
442 [ + - ][ + - ]: 80 : new ParsedRDFaAttributes(about, properties, i_rContent, datatype));
[ + - ]
443 : : }
444 : :
445 : : void
446 : 70 : RDFaImportHelper::AddRDFa(
447 : : uno::Reference<rdf::XMetadatable> const & i_xObject,
448 : : ::boost::shared_ptr<ParsedRDFaAttributes> & i_pRDFaAttributes)
449 : : {
450 [ - + ]: 70 : if (!i_xObject.is())
451 : : {
452 : : OSL_FAIL("AddRDFa: invalid arg: null textcontent");
453 : 0 : return;
454 : : }
455 [ - + ]: 70 : if (!i_pRDFaAttributes.get())
456 : : {
457 : : OSL_FAIL("AddRDFa: invalid arg: null RDFa attributes");
458 : 0 : return;
459 : : }
460 [ + - ]: 70 : m_RDFaEntries.push_back(RDFaEntry(i_xObject, i_pRDFaAttributes));
461 : : }
462 : :
463 : : void
464 : 72 : RDFaImportHelper::ParseAndAddRDFa(
465 : : uno::Reference<rdf::XMetadatable> const & i_xObject,
466 : : ::rtl::OUString const & i_rAbout,
467 : : ::rtl::OUString const & i_rProperty,
468 : : ::rtl::OUString const & i_rContent,
469 : : ::rtl::OUString const & i_rDatatype)
470 : : {
471 : : ::boost::shared_ptr<ParsedRDFaAttributes> pAttributes(
472 [ + - ]: 72 : ParseRDFa(i_rAbout, i_rProperty, i_rContent, i_rDatatype) );
473 [ + + ]: 72 : if (pAttributes.get())
474 : : {
475 [ + - ]: 62 : AddRDFa(i_xObject, pAttributes);
476 [ + - ]: 72 : }
477 : 72 : }
478 : :
479 : 12 : void RDFaImportHelper::InsertRDFa(
480 : : uno::Reference< rdf::XRepositorySupplier> const & i_xModel)
481 : : {
482 : : OSL_ENSURE(i_xModel.is(), "InsertRDFa: invalid arg: model null");
483 [ + - ]: 12 : if (!i_xModel.is()) return;
484 : : const uno::Reference< rdf::XDocumentRepository > xRepository(
485 [ + - ][ + - ]: 12 : i_xModel->getRDFRepository(), uno::UNO_QUERY);
[ + - ]
486 : : OSL_ENSURE(xRepository.is(), "InsertRDFa: no DocumentRepository?");
487 [ - + ]: 12 : if (!xRepository.is()) return;
488 [ + - ][ + - ]: 12 : RDFaInserter inserter(GetImport().GetComponentContext(), xRepository);
489 : : ::std::for_each(m_RDFaEntries.begin(), m_RDFaEntries.end(),
490 [ + - ][ + - ]: 12 : ::boost::bind(&RDFaInserter::InsertRDFaEntry, &inserter, _1));
[ + - ][ + - ]
491 : : }
492 : :
493 [ + - ][ + - ]: 489 : } // namespace xmloff
494 : :
495 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|