1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include "RDFaImportHelper.hxx"
22 #include <xmloff/xmlimp.hxx>
23 #include <xmloff/nmspmap.hxx>
25 #include <comphelper/sequence.hxx>
27 #include <com/sun/star/rdf/URI.hpp>
28 #include <com/sun/star/rdf/XDocumentMetadataAccess.hpp>
29 #include <com/sun/star/rdf/XDocumentRepository.hpp>
31 #include <rtl/ustring.hxx>
38 using namespace ::com::sun::star
;
42 /** a bit of context for parsing RDFa attributes */
45 const SvXMLImport
& m_rImport
;
47 const SvXMLImport
& GetImport() const { return m_rImport
; }
49 //FIXME: this is an ugly hack to workaround buggy SvXMLImport::GetAbsolute
50 OUString
GetAbsoluteReference(OUString
const & i_rURI
) const
52 if (i_rURI
.isEmpty() || i_rURI
[0] == '#')
54 return GetImport().GetBaseURL() + i_rURI
;
58 return GetImport().GetAbsoluteReference(i_rURI
);
63 explicit RDFaReader(SvXMLImport
const & i_rImport
)
64 : m_rImport(i_rImport
)
67 // returns URI or blank node!
68 OUString
ReadCURIE(OUString
const & i_rCURIE
) const;
70 std::vector
< OUString
>
71 ReadCURIEs(OUString
const & i_rCURIEs
) const;
74 ReadURIOrSafeCURIE( OUString
const & i_rURIOrSafeCURIE
) const;
77 /** helper to insert RDFa statements into the RDF repository */
80 const uno::Reference
<uno::XComponentContext
> m_xContext
;
81 uno::Reference
< rdf::XDocumentRepository
> m_xRepository
;
83 typedef ::std::map
< OUString
, uno::Reference
< rdf::XBlankNode
> >
86 BlankNodeMap_t m_BlankNodeMap
;
89 RDFaInserter(uno::Reference
<uno::XComponentContext
> const & i_xContext
,
90 uno::Reference
< rdf::XDocumentRepository
> const & i_xRepository
)
91 : m_xContext(i_xContext
)
92 , m_xRepository(i_xRepository
)
95 uno::Reference
< rdf::XBlankNode
>
96 LookupBlankNode(OUString
const & i_rNodeId
);
98 uno::Reference
< rdf::XURI
>
99 MakeURI( OUString
const & i_rURI
) const;
101 uno::Reference
< rdf::XResource
>
102 MakeResource( OUString
const & i_rResource
);
104 void InsertRDFaEntry(struct RDFaEntry
const & i_rEntry
);
107 /** store parsed RDFa attributes */
108 struct ParsedRDFaAttributes
111 ::std::vector
< OUString
> m_Properties
;
115 ParsedRDFaAttributes(
116 OUString
const & i_rAbout
,
117 ::std::vector
< OUString
> const & i_rProperties
,
118 OUString
const & i_rContent
,
119 OUString
const & i_rDatatype
)
121 , m_Properties(i_rProperties
)
122 , m_Content(i_rContent
)
123 , m_Datatype(i_rDatatype
)
127 /** store metadatable object and its RDFa attributes */
130 uno::Reference
<rdf::XMetadatable
> m_xObject
;
131 std::shared_ptr
<ParsedRDFaAttributes
> m_xRDFaAttributes
;
133 RDFaEntry(uno::Reference
<rdf::XMetadatable
> const & i_xObject
,
134 std::shared_ptr
<ParsedRDFaAttributes
> const& i_pRDFaAttributes
)
135 : m_xObject(i_xObject
)
136 , m_xRDFaAttributes(i_pRDFaAttributes
)
140 static inline bool isWS(const sal_Unicode i_Char
)
142 return ('\t' == i_Char
) || ('\n' == i_Char
) || ('\r' == i_Char
)
146 static OUString
splitAtWS(OUString
& io_rString
)
148 const sal_Int32
len( io_rString
.getLength() );
150 while ((idxstt
< len
) && ( isWS(io_rString
[idxstt
])))
151 ++idxstt
; // skip leading ws
152 sal_Int32
idxend(idxstt
);
153 while ((idxend
< len
) && (!isWS(io_rString
[idxend
])))
154 ++idxend
; // the CURIE
155 const OUString
ret(io_rString
.copy(idxstt
, idxend
- idxstt
));
156 io_rString
= io_rString
.copy(idxend
); // rest
161 RDFaReader::ReadCURIE(OUString
const & i_rCURIE
) const
163 // the RDFa spec says that a prefix is required (it may be empty: ":foo")
164 const sal_Int32
idx( i_rCURIE
.indexOf(':') );
170 sal_uInt16
nKey( GetImport().GetNamespaceMap().GetKeyByAttrName_(
171 i_rCURIE
, &Prefix
, &LocalName
, &Namespace
) );
174 // eeek, it's a bnode!
175 // "_" is not a valid URI scheme => we can identify bnodes
180 SAL_WARN_IF(XML_NAMESPACE_NONE
== nKey
, "xmloff.core", "no namespace?");
181 if ((XML_NAMESPACE_UNKNOWN
!= nKey
) &&
182 (XML_NAMESPACE_XMLNS
!= nKey
))
184 // N.B.: empty LocalName is valid!
185 const OUString
URI(Namespace
+ LocalName
);
186 return GetAbsoluteReference(URI
);
190 SAL_INFO("xmloff.core", "ReadCURIE: invalid CURIE: invalid prefix" );
195 SAL_INFO("xmloff.core", "ReadCURIE: invalid CURIE: no prefix" );
199 ::std::vector
< OUString
>
200 RDFaReader::ReadCURIEs(OUString
const & i_rCURIEs
) const
202 std::vector
< OUString
> vec
;
203 OUString
CURIEs(i_rCURIEs
);
205 OUString
curie( splitAtWS(CURIEs
) );
206 if (!curie
.isEmpty())
208 const OUString
uri(ReadCURIE(curie
));
215 while (!CURIEs
.isEmpty());
218 SAL_INFO("xmloff.core", "ReadCURIEs: invalid CURIEs" );
224 RDFaReader::ReadURIOrSafeCURIE(OUString
const & i_rURIOrSafeCURIE
) const
226 const sal_Int32
len(i_rURIOrSafeCURIE
.getLength());
227 if (len
&& (i_rURIOrSafeCURIE
[0] == '['))
229 if ((len
>= 2) && (i_rURIOrSafeCURIE
[len
- 1] == ']'))
231 return ReadCURIE(i_rURIOrSafeCURIE
.copy(1, len
- 2));
235 SAL_INFO("xmloff.core", "ReadURIOrSafeCURIE: invalid SafeCURIE" );
241 if (i_rURIOrSafeCURIE
.startsWith("_:")) // blank node
243 SAL_INFO("xmloff.core", "ReadURIOrSafeCURIE: invalid URI: scheme is _" );
248 return GetAbsoluteReference(i_rURIOrSafeCURIE
);
253 uno::Reference
< rdf::XBlankNode
>
254 RDFaInserter::LookupBlankNode(OUString
const & i_rNodeId
)
256 uno::Reference
< rdf::XBlankNode
> & rEntry( m_BlankNodeMap
[ i_rNodeId
] );
259 rEntry
= m_xRepository
->createBlankNode();
264 uno::Reference
< rdf::XURI
>
265 RDFaInserter::MakeURI( OUString
const & i_rURI
) const
267 if (i_rURI
.startsWith("_:")) // blank node
269 SAL_INFO("xmloff.core", "MakeURI: cannot create URI for blank node");
276 return rdf::URI::create( m_xContext
, i_rURI
);
278 catch (uno::Exception
&)
280 SAL_WARN("xmloff.core", "MakeURI: cannot create URI");
286 uno::Reference
<rdf::XResource
>
287 RDFaInserter::MakeResource( OUString
const & i_rResource
)
289 if (i_rResource
.startsWith("_:")) // blank node
291 // we cannot use the blank node label as-is: it must be distinct
292 // from labels in other graphs, so create fresh ones per XML stream
293 // N.B.: content.xml and styles.xml are distinct graphs
294 OUString
name( i_rResource
.copy(2) );
295 const uno::Reference
< rdf::XBlankNode
> xBNode( LookupBlankNode(name
) );
296 SAL_WARN_IF(!xBNode
.is(), "xmloff.core", "no blank node?");
297 return uno::Reference
<rdf::XResource
>( xBNode
, uno::UNO_QUERY
);
301 return uno::Reference
<rdf::XResource
>( MakeURI( i_rResource
),
306 void RDFaInserter::InsertRDFaEntry(
307 struct RDFaEntry
const & i_rEntry
)
309 SAL_WARN_IF(!i_rEntry
.m_xObject
.is(), "xmloff.core", "InsertRDFaEntry: invalid arg: null object");
310 if (!i_rEntry
.m_xObject
.is()) return;
312 const uno::Reference
< rdf::XResource
> xSubject(
313 MakeResource( i_rEntry
.m_xRDFaAttributes
->m_About
) );
319 ::std::vector
< uno::Reference
< rdf::XURI
> > predicates
;
321 predicates
.reserve(i_rEntry
.m_xRDFaAttributes
->m_Properties
.size());
323 for (OUString
const& prop
: i_rEntry
.m_xRDFaAttributes
->m_Properties
)
325 auto const xURI(MakeURI(prop
));
328 predicates
.push_back(xURI
);
332 if (predicates
.empty())
337 uno::Reference
<rdf::XURI
> xDatatype
;
338 if (!i_rEntry
.m_xRDFaAttributes
->m_Datatype
.isEmpty())
340 xDatatype
= MakeURI( i_rEntry
.m_xRDFaAttributes
->m_Datatype
);
345 // N.B.: this will call xMeta->ensureMetadataReference, which is why
346 // this must be done _after_ importing the whole XML file,
347 // to prevent collision between generated ids and ids in the file
348 m_xRepository
->setStatementRDFa(xSubject
, comphelper::containerToSequence(predicates
),
350 i_rEntry
.m_xRDFaAttributes
->m_Content
, xDatatype
);
352 catch (uno::Exception
&)
354 SAL_WARN("xmloff.core", "InsertRDFaEntry: setStatementRDFa failed?");
358 RDFaImportHelper::RDFaImportHelper(const SvXMLImport
& i_rImport
)
359 : m_rImport(i_rImport
)
363 RDFaImportHelper::~RDFaImportHelper()
367 std::shared_ptr
<ParsedRDFaAttributes
>
368 RDFaImportHelper::ParseRDFa(
369 OUString
const & i_rAbout
,
370 OUString
const & i_rProperty
,
371 OUString
const & i_rContent
,
372 OUString
const & i_rDatatype
)
374 if (i_rProperty
.isEmpty())
376 SAL_INFO("xmloff.core", "AddRDFa: invalid input: xhtml:property empty");
377 return std::shared_ptr
<ParsedRDFaAttributes
>();
379 // must parse CURIEs here: need namespace declaration context
380 RDFaReader
reader(GetImport());
381 const OUString
about( reader
.ReadURIOrSafeCURIE(i_rAbout
) );
382 if (about
.isEmpty()) {
383 return std::shared_ptr
<ParsedRDFaAttributes
>();
385 const ::std::vector
< OUString
> properties(
386 reader
.ReadCURIEs(i_rProperty
) );
387 if (properties
.empty()) {
388 return std::shared_ptr
<ParsedRDFaAttributes
>();
390 const OUString
datatype( !i_rDatatype
.isEmpty()
391 ? reader
.ReadCURIE(i_rDatatype
)
393 return std::make_shared
<ParsedRDFaAttributes
>(
394 about
, properties
, i_rContent
, datatype
);
398 RDFaImportHelper::AddRDFa(
399 uno::Reference
<rdf::XMetadatable
> const & i_xObject
,
400 std::shared_ptr
<ParsedRDFaAttributes
> & i_pRDFaAttributes
)
404 SAL_WARN("xmloff.core", "AddRDFa: invalid arg: null textcontent");
407 if (!i_pRDFaAttributes
.get())
409 SAL_WARN("xmloff.core", "AddRDFa: invalid arg: null RDFa attributes");
412 m_RDFaEntries
.push_back(RDFaEntry(i_xObject
, i_pRDFaAttributes
));
416 RDFaImportHelper::ParseAndAddRDFa(
417 uno::Reference
<rdf::XMetadatable
> const & i_xObject
,
418 OUString
const & i_rAbout
,
419 OUString
const & i_rProperty
,
420 OUString
const & i_rContent
,
421 OUString
const & i_rDatatype
)
423 std::shared_ptr
<ParsedRDFaAttributes
> pAttributes(
424 ParseRDFa(i_rAbout
, i_rProperty
, i_rContent
, i_rDatatype
) );
425 if (pAttributes
.get())
427 AddRDFa(i_xObject
, pAttributes
);
431 void RDFaImportHelper::InsertRDFa(
432 uno::Reference
< rdf::XRepositorySupplier
> const & i_xModel
)
434 SAL_WARN_IF(!i_xModel
.is(), "xmloff.core", "InsertRDFa: invalid arg: model null");
435 if (!i_xModel
.is()) return;
436 const uno::Reference
< rdf::XDocumentRepository
> xRepository(
437 i_xModel
->getRDFRepository(), uno::UNO_QUERY
);
438 SAL_WARN_IF(!xRepository
.is(), "xmloff.core", "InsertRDFa: no DocumentRepository?");
439 if (!xRepository
.is()) return;
440 RDFaInserter
inserter(GetImport().GetComponentContext(), xRepository
);
441 for (const auto& RDFaEntry
: m_RDFaEntries
)
442 inserter
.InsertRDFaEntry(RDFaEntry
);
445 } // namespace xmloff
447 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */