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/XDocumentRepository.hpp>
29 #include <com/sun/star/rdf/XRepositorySupplier.hpp>
31 #include <rtl/ustring.hxx>
32 #include <sal/log.hxx>
36 using namespace ::com::sun::star
;
40 /** a bit of context for parsing RDFa attributes */
43 const SvXMLImport
& m_rImport
;
45 const SvXMLImport
& GetImport() const { return m_rImport
; }
47 //FIXME: this is an ugly hack to workaround buggy SvXMLImport::GetAbsolute
48 OUString
GetAbsoluteReference(OUString
const & i_rURI
) const
50 if (i_rURI
.isEmpty() || i_rURI
[0] == '#')
52 return GetImport().GetBaseURL() + i_rURI
;
56 return GetImport().GetAbsoluteReference(i_rURI
);
61 explicit RDFaReader(SvXMLImport
const & i_rImport
)
62 : m_rImport(i_rImport
)
65 // returns URI or blank node!
66 OUString
ReadCURIE(OUString
const & i_rCURIE
) const;
68 std::vector
< OUString
>
69 ReadCURIEs(OUString
const & i_rCURIEs
) const;
72 ReadURIOrSafeCURIE( OUString
const & i_rURIOrSafeCURIE
) const;
75 /** helper to insert RDFa statements into the RDF repository */
78 const uno::Reference
<uno::XComponentContext
> m_xContext
;
79 uno::Reference
< rdf::XDocumentRepository
> m_xRepository
;
81 typedef ::std::map
< OUString
, uno::Reference
< rdf::XBlankNode
> >
84 BlankNodeMap_t m_BlankNodeMap
;
87 RDFaInserter(uno::Reference
<uno::XComponentContext
> const & i_xContext
,
88 uno::Reference
< rdf::XDocumentRepository
> const & i_xRepository
)
89 : m_xContext(i_xContext
)
90 , m_xRepository(i_xRepository
)
93 uno::Reference
< rdf::XBlankNode
>
94 LookupBlankNode(OUString
const & i_rNodeId
);
96 uno::Reference
< rdf::XURI
>
97 MakeURI( OUString
const & i_rURI
) const;
99 uno::Reference
< rdf::XResource
>
100 MakeResource( OUString
const & i_rResource
);
102 void InsertRDFaEntry(struct RDFaEntry
const & i_rEntry
);
105 /** store parsed RDFa attributes */
106 struct ParsedRDFaAttributes
108 OUString
const m_About
;
109 ::std::vector
< OUString
> const m_Properties
;
110 OUString
const m_Content
;
111 OUString
const m_Datatype
;
113 ParsedRDFaAttributes(
114 OUString
const & i_rAbout
,
115 ::std::vector
< OUString
> const & i_rProperties
,
116 OUString
const & i_rContent
,
117 OUString
const & i_rDatatype
)
119 , m_Properties(i_rProperties
)
120 , m_Content(i_rContent
)
121 , m_Datatype(i_rDatatype
)
125 /** store metadatable object and its RDFa attributes */
128 uno::Reference
<rdf::XMetadatable
> m_xObject
;
129 std::shared_ptr
<ParsedRDFaAttributes
> m_xRDFaAttributes
;
131 RDFaEntry(uno::Reference
<rdf::XMetadatable
> const & i_xObject
,
132 std::shared_ptr
<ParsedRDFaAttributes
> const& i_pRDFaAttributes
)
133 : m_xObject(i_xObject
)
134 , m_xRDFaAttributes(i_pRDFaAttributes
)
138 static bool isWS(const sal_Unicode i_Char
)
140 return ('\t' == i_Char
) || ('\n' == i_Char
) || ('\r' == i_Char
)
144 static OUString
splitAtWS(OUString
& io_rString
)
146 const sal_Int32
len( io_rString
.getLength() );
148 while ((idxstt
< len
) && ( isWS(io_rString
[idxstt
])))
149 ++idxstt
; // skip leading ws
150 sal_Int32
idxend(idxstt
);
151 while ((idxend
< len
) && (!isWS(io_rString
[idxend
])))
152 ++idxend
; // the CURIE
153 const OUString
ret(io_rString
.copy(idxstt
, idxend
- idxstt
));
154 io_rString
= io_rString
.copy(idxend
); // rest
159 RDFaReader::ReadCURIE(OUString
const & i_rCURIE
) const
161 // the RDFa spec says that a prefix is required (it may be empty: ":foo")
162 const sal_Int32
idx( i_rCURIE
.indexOf(':') );
168 sal_uInt16
nKey( GetImport().GetNamespaceMap().GetKeyByAttrName_(
169 i_rCURIE
, &Prefix
, &LocalName
, &Namespace
) );
172 // eeek, it's a bnode!
173 // "_" is not a valid URI scheme => we can identify bnodes
178 SAL_WARN_IF(XML_NAMESPACE_NONE
== nKey
, "xmloff.core", "no namespace?");
179 if ((XML_NAMESPACE_UNKNOWN
!= nKey
) &&
180 (XML_NAMESPACE_XMLNS
!= nKey
))
182 // N.B.: empty LocalName is valid!
183 const OUString
URI(Namespace
+ LocalName
);
184 return GetAbsoluteReference(URI
);
188 SAL_INFO("xmloff.core", "ReadCURIE: invalid CURIE: invalid prefix" );
193 SAL_INFO("xmloff.core", "ReadCURIE: invalid CURIE: no prefix" );
197 ::std::vector
< OUString
>
198 RDFaReader::ReadCURIEs(OUString
const & i_rCURIEs
) const
200 std::vector
< OUString
> vec
;
201 OUString
CURIEs(i_rCURIEs
);
203 OUString
curie( splitAtWS(CURIEs
) );
204 if (!curie
.isEmpty())
206 const OUString
uri(ReadCURIE(curie
));
213 while (!CURIEs
.isEmpty());
216 SAL_INFO("xmloff.core", "ReadCURIEs: invalid CURIEs" );
222 RDFaReader::ReadURIOrSafeCURIE(OUString
const & i_rURIOrSafeCURIE
) const
224 const sal_Int32
len(i_rURIOrSafeCURIE
.getLength());
225 if (len
&& (i_rURIOrSafeCURIE
[0] == '['))
227 if ((len
>= 2) && (i_rURIOrSafeCURIE
[len
- 1] == ']'))
229 return ReadCURIE(i_rURIOrSafeCURIE
.copy(1, len
- 2));
233 SAL_INFO("xmloff.core", "ReadURIOrSafeCURIE: invalid SafeCURIE" );
239 if (i_rURIOrSafeCURIE
.startsWith("_:")) // blank node
241 SAL_INFO("xmloff.core", "ReadURIOrSafeCURIE: invalid URI: scheme is _" );
246 return GetAbsoluteReference(i_rURIOrSafeCURIE
);
251 uno::Reference
< rdf::XBlankNode
>
252 RDFaInserter::LookupBlankNode(OUString
const & i_rNodeId
)
254 uno::Reference
< rdf::XBlankNode
> & rEntry( m_BlankNodeMap
[ i_rNodeId
] );
257 rEntry
= m_xRepository
->createBlankNode();
262 uno::Reference
< rdf::XURI
>
263 RDFaInserter::MakeURI( OUString
const & i_rURI
) const
265 if (i_rURI
.startsWith("_:")) // blank node
267 SAL_INFO("xmloff.core", "MakeURI: cannot create URI for blank node");
274 return rdf::URI::create( m_xContext
, i_rURI
);
276 catch (uno::Exception
&)
278 SAL_WARN("xmloff.core", "MakeURI: cannot create URI");
284 uno::Reference
<rdf::XResource
>
285 RDFaInserter::MakeResource( OUString
const & i_rResource
)
287 if (i_rResource
.startsWith("_:")) // blank node
289 // we cannot use the blank node label as-is: it must be distinct
290 // from labels in other graphs, so create fresh ones per XML stream
291 // N.B.: content.xml and styles.xml are distinct graphs
292 OUString
name( i_rResource
.copy(2) );
293 const uno::Reference
< rdf::XBlankNode
> xBNode( LookupBlankNode(name
) );
294 SAL_WARN_IF(!xBNode
.is(), "xmloff.core", "no blank node?");
299 return MakeURI( i_rResource
);
303 void RDFaInserter::InsertRDFaEntry(
304 struct RDFaEntry
const & i_rEntry
)
306 SAL_WARN_IF(!i_rEntry
.m_xObject
.is(), "xmloff.core", "InsertRDFaEntry: invalid arg: null object");
307 if (!i_rEntry
.m_xObject
.is()) return;
309 const uno::Reference
< rdf::XResource
> xSubject(
310 MakeResource( i_rEntry
.m_xRDFaAttributes
->m_About
) );
316 ::std::vector
< uno::Reference
< rdf::XURI
> > predicates
;
318 predicates
.reserve(i_rEntry
.m_xRDFaAttributes
->m_Properties
.size());
320 for (OUString
const& prop
: i_rEntry
.m_xRDFaAttributes
->m_Properties
)
322 auto const xURI(MakeURI(prop
));
325 predicates
.push_back(xURI
);
329 if (predicates
.empty())
334 uno::Reference
<rdf::XURI
> xDatatype
;
335 if (!i_rEntry
.m_xRDFaAttributes
->m_Datatype
.isEmpty())
337 xDatatype
= MakeURI( i_rEntry
.m_xRDFaAttributes
->m_Datatype
);
342 // N.B.: this will call xMeta->ensureMetadataReference, which is why
343 // this must be done _after_ importing the whole XML file,
344 // to prevent collision between generated ids and ids in the file
345 m_xRepository
->setStatementRDFa(xSubject
, comphelper::containerToSequence(predicates
),
347 i_rEntry
.m_xRDFaAttributes
->m_Content
, xDatatype
);
349 catch (uno::Exception
&)
351 SAL_WARN("xmloff.core", "InsertRDFaEntry: setStatementRDFa failed?");
355 RDFaImportHelper::RDFaImportHelper(const SvXMLImport
& i_rImport
)
356 : m_rImport(i_rImport
)
360 RDFaImportHelper::~RDFaImportHelper()
364 std::shared_ptr
<ParsedRDFaAttributes
>
365 RDFaImportHelper::ParseRDFa(
366 OUString
const & i_rAbout
,
367 OUString
const & i_rProperty
,
368 OUString
const & i_rContent
,
369 OUString
const & i_rDatatype
)
371 if (i_rProperty
.isEmpty())
373 SAL_INFO("xmloff.core", "AddRDFa: invalid input: xhtml:property empty");
374 return std::shared_ptr
<ParsedRDFaAttributes
>();
376 // must parse CURIEs here: need namespace declaration context
377 RDFaReader
reader(GetImport());
378 const OUString
about( reader
.ReadURIOrSafeCURIE(i_rAbout
) );
379 if (about
.isEmpty()) {
380 return std::shared_ptr
<ParsedRDFaAttributes
>();
382 const ::std::vector
< OUString
> properties(
383 reader
.ReadCURIEs(i_rProperty
) );
384 if (properties
.empty()) {
385 return std::shared_ptr
<ParsedRDFaAttributes
>();
387 const OUString
datatype( !i_rDatatype
.isEmpty()
388 ? reader
.ReadCURIE(i_rDatatype
)
390 return std::make_shared
<ParsedRDFaAttributes
>(
391 about
, properties
, i_rContent
, datatype
);
395 RDFaImportHelper::AddRDFa(
396 uno::Reference
<rdf::XMetadatable
> const & i_xObject
,
397 std::shared_ptr
<ParsedRDFaAttributes
> const & i_pRDFaAttributes
)
401 SAL_WARN("xmloff.core", "AddRDFa: invalid arg: null textcontent");
404 if (!i_pRDFaAttributes
.get())
406 SAL_WARN("xmloff.core", "AddRDFa: invalid arg: null RDFa attributes");
409 m_RDFaEntries
.emplace_back(i_xObject
, i_pRDFaAttributes
);
413 RDFaImportHelper::ParseAndAddRDFa(
414 uno::Reference
<rdf::XMetadatable
> const & i_xObject
,
415 OUString
const & i_rAbout
,
416 OUString
const & i_rProperty
,
417 OUString
const & i_rContent
,
418 OUString
const & i_rDatatype
)
420 std::shared_ptr
<ParsedRDFaAttributes
> pAttributes(
421 ParseRDFa(i_rAbout
, i_rProperty
, i_rContent
, i_rDatatype
) );
422 if (pAttributes
.get())
424 AddRDFa(i_xObject
, pAttributes
);
428 void RDFaImportHelper::InsertRDFa(
429 uno::Reference
< rdf::XRepositorySupplier
> const & i_xModel
)
431 SAL_WARN_IF(!i_xModel
.is(), "xmloff.core", "InsertRDFa: invalid arg: model null");
432 if (!i_xModel
.is()) return;
433 const uno::Reference
< rdf::XDocumentRepository
> xRepository(
434 i_xModel
->getRDFRepository(), uno::UNO_QUERY
);
435 SAL_WARN_IF(!xRepository
.is(), "xmloff.core", "InsertRDFa: no DocumentRepository?");
436 if (!xRepository
.is()) return;
437 RDFaInserter
inserter(GetImport().GetComponentContext(), xRepository
);
438 for (const auto& RDFaEntry
: m_RDFaEntries
)
439 inserter
.InsertRDFaEntry(RDFaEntry
);
442 } // namespace xmloff
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */