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/namespacemap.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
;
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
);
109 /** store parsed RDFa attributes */
110 struct ParsedRDFaAttributes
113 ::std::vector
< OUString
> m_Properties
;
117 ParsedRDFaAttributes(
118 OUString
const & i_rAbout
,
119 ::std::vector
< OUString
> const & i_rProperties
,
120 OUString
const & i_rContent
,
121 OUString
const & i_rDatatype
)
123 , m_Properties(i_rProperties
)
124 , m_Content(i_rContent
)
125 , m_Datatype(i_rDatatype
)
129 /** store metadatable object and its RDFa attributes */
132 uno::Reference
<rdf::XMetadatable
> m_xObject
;
133 std::shared_ptr
<ParsedRDFaAttributes
> m_xRDFaAttributes
;
135 RDFaEntry(uno::Reference
<rdf::XMetadatable
> const & i_xObject
,
136 std::shared_ptr
<ParsedRDFaAttributes
> const& i_pRDFaAttributes
)
137 : m_xObject(i_xObject
)
138 , m_xRDFaAttributes(i_pRDFaAttributes
)
142 static bool isWS(const sal_Unicode i_Char
)
144 return ('\t' == i_Char
) || ('\n' == i_Char
) || ('\r' == i_Char
)
148 static OUString
splitAtWS(OUString
& io_rString
)
150 const sal_Int32
len( io_rString
.getLength() );
152 while ((idxstt
< len
) && ( isWS(io_rString
[idxstt
])))
153 ++idxstt
; // skip leading ws
154 sal_Int32
idxend(idxstt
);
155 while ((idxend
< len
) && (!isWS(io_rString
[idxend
])))
156 ++idxend
; // the CURIE
157 const OUString
ret(io_rString
.copy(idxstt
, idxend
- idxstt
));
158 io_rString
= io_rString
.copy(idxend
); // rest
163 RDFaReader::ReadCURIE(OUString
const & i_rCURIE
) const
165 // the RDFa spec says that a prefix is required (it may be empty: ":foo")
166 const sal_Int32
idx( i_rCURIE
.indexOf(':') );
172 // LocalName may contain ':', see "ipchar" in RFC 3987
173 sal_uInt16
nKey( GetImport().GetNamespaceMap().GetKeyByQName(
174 i_rCURIE
, &Prefix
, &LocalName
, &Namespace
, SvXMLNamespaceMap::QNameMode::AttrValue
) );
177 // eeek, it's a bnode!
178 // "_" is not a valid URI scheme => we can identify bnodes
183 SAL_WARN_IF(XML_NAMESPACE_NONE
== nKey
, "xmloff.core", "no namespace?");
184 if ((XML_NAMESPACE_UNKNOWN
!= nKey
) &&
185 (XML_NAMESPACE_XMLNS
!= nKey
))
187 // N.B.: empty LocalName is valid!
188 const OUString
URI(Namespace
+ LocalName
);
189 return GetAbsoluteReference(URI
);
193 SAL_INFO("xmloff.core", "ReadCURIE: invalid CURIE: invalid prefix" );
198 SAL_INFO("xmloff.core", "ReadCURIE: invalid CURIE: no prefix" );
202 ::std::vector
< OUString
>
203 RDFaReader::ReadCURIEs(OUString
const & i_rCURIEs
) const
205 std::vector
< OUString
> vec
;
206 OUString
CURIEs(i_rCURIEs
);
208 OUString
curie( splitAtWS(CURIEs
) );
209 if (!curie
.isEmpty())
211 const OUString
uri(ReadCURIE(curie
));
218 while (!CURIEs
.isEmpty());
221 SAL_INFO("xmloff.core", "ReadCURIEs: invalid CURIEs" );
227 RDFaReader::ReadURIOrSafeCURIE(OUString
const & i_rURIOrSafeCURIE
) const
229 const sal_Int32
len(i_rURIOrSafeCURIE
.getLength());
230 if (len
&& (i_rURIOrSafeCURIE
[0] == '['))
232 if ((len
>= 2) && (i_rURIOrSafeCURIE
[len
- 1] == ']'))
234 return ReadCURIE(i_rURIOrSafeCURIE
.copy(1, len
- 2));
238 SAL_INFO("xmloff.core", "ReadURIOrSafeCURIE: invalid SafeCURIE" );
244 if (i_rURIOrSafeCURIE
.startsWith("_:")) // blank node
246 SAL_INFO("xmloff.core", "ReadURIOrSafeCURIE: invalid URI: scheme is _" );
251 return GetAbsoluteReference(i_rURIOrSafeCURIE
);
256 uno::Reference
< rdf::XBlankNode
>
257 RDFaInserter::LookupBlankNode(OUString
const & i_rNodeId
)
259 uno::Reference
< rdf::XBlankNode
> & rEntry( m_BlankNodeMap
[ i_rNodeId
] );
262 rEntry
= m_xRepository
->createBlankNode();
267 uno::Reference
< rdf::XURI
>
268 RDFaInserter::MakeURI( OUString
const & i_rURI
) const
270 if (i_rURI
.startsWith("_:")) // blank node
272 SAL_INFO("xmloff.core", "MakeURI: cannot create URI for blank node");
279 return rdf::URI::create( m_xContext
, i_rURI
);
281 catch (uno::Exception
&)
283 SAL_WARN("xmloff.core", "MakeURI: cannot create URI");
289 uno::Reference
<rdf::XResource
>
290 RDFaInserter::MakeResource( OUString
const & i_rResource
)
292 if (i_rResource
.startsWith("_:")) // blank node
294 // we cannot use the blank node label as-is: it must be distinct
295 // from labels in other graphs, so create fresh ones per XML stream
296 // N.B.: content.xml and styles.xml are distinct graphs
297 OUString
name( i_rResource
.copy(2) );
298 const uno::Reference
< rdf::XBlankNode
> xBNode( LookupBlankNode(name
) );
299 SAL_WARN_IF(!xBNode
.is(), "xmloff.core", "no blank node?");
304 return MakeURI( i_rResource
);
308 void RDFaInserter::InsertRDFaEntry(
309 struct RDFaEntry
const & i_rEntry
)
311 SAL_WARN_IF(!i_rEntry
.m_xObject
.is(), "xmloff.core", "InsertRDFaEntry: invalid arg: null object");
312 if (!i_rEntry
.m_xObject
.is()) return;
314 const uno::Reference
< rdf::XResource
> xSubject(
315 MakeResource( i_rEntry
.m_xRDFaAttributes
->m_About
) );
321 ::std::vector
< uno::Reference
< rdf::XURI
> > predicates
;
323 predicates
.reserve(i_rEntry
.m_xRDFaAttributes
->m_Properties
.size());
325 for (OUString
const& prop
: i_rEntry
.m_xRDFaAttributes
->m_Properties
)
327 auto const xURI(MakeURI(prop
));
330 predicates
.push_back(xURI
);
334 if (predicates
.empty())
339 uno::Reference
<rdf::XURI
> xDatatype
;
340 if (!i_rEntry
.m_xRDFaAttributes
->m_Datatype
.isEmpty())
342 xDatatype
= MakeURI( i_rEntry
.m_xRDFaAttributes
->m_Datatype
);
347 // N.B.: this will call xMeta->ensureMetadataReference, which is why
348 // this must be done _after_ importing the whole XML file,
349 // to prevent collision between generated ids and ids in the file
350 m_xRepository
->setStatementRDFa(xSubject
, comphelper::containerToSequence(predicates
),
352 i_rEntry
.m_xRDFaAttributes
->m_Content
, xDatatype
);
354 catch (uno::Exception
&)
356 SAL_WARN("xmloff.core", "InsertRDFaEntry: setStatementRDFa failed?");
360 RDFaImportHelper::RDFaImportHelper(const SvXMLImport
& i_rImport
)
361 : m_rImport(i_rImport
)
365 RDFaImportHelper::~RDFaImportHelper()
369 std::shared_ptr
<ParsedRDFaAttributes
>
370 RDFaImportHelper::ParseRDFa(
371 OUString
const & i_rAbout
,
372 OUString
const & i_rProperty
,
373 OUString
const & i_rContent
,
374 OUString
const & i_rDatatype
)
376 if (i_rProperty
.isEmpty())
378 SAL_INFO("xmloff.core", "AddRDFa: invalid input: xhtml:property empty");
379 return std::shared_ptr
<ParsedRDFaAttributes
>();
381 // must parse CURIEs here: need namespace declaration context
382 RDFaReader
reader(GetImport());
383 const OUString
about( reader
.ReadURIOrSafeCURIE(i_rAbout
) );
384 if (about
.isEmpty()) {
385 return std::shared_ptr
<ParsedRDFaAttributes
>();
387 const ::std::vector
< OUString
> properties(
388 reader
.ReadCURIEs(i_rProperty
) );
389 if (properties
.empty()) {
390 return std::shared_ptr
<ParsedRDFaAttributes
>();
392 const OUString
datatype( !i_rDatatype
.isEmpty()
393 ? reader
.ReadCURIE(i_rDatatype
)
395 return std::make_shared
<ParsedRDFaAttributes
>(
396 about
, properties
, i_rContent
, datatype
);
400 RDFaImportHelper::AddRDFa(
401 uno::Reference
<rdf::XMetadatable
> const & i_xObject
,
402 std::shared_ptr
<ParsedRDFaAttributes
> const & i_pRDFaAttributes
)
406 SAL_WARN("xmloff.core", "AddRDFa: invalid arg: null textcontent");
409 if (!i_pRDFaAttributes
)
411 SAL_WARN("xmloff.core", "AddRDFa: invalid arg: null RDFa attributes");
414 m_RDFaEntries
.emplace_back(i_xObject
, i_pRDFaAttributes
);
418 RDFaImportHelper::ParseAndAddRDFa(
419 uno::Reference
<rdf::XMetadatable
> const & i_xObject
,
420 OUString
const & i_rAbout
,
421 OUString
const & i_rProperty
,
422 OUString
const & i_rContent
,
423 OUString
const & i_rDatatype
)
425 std::shared_ptr
<ParsedRDFaAttributes
> pAttributes(
426 ParseRDFa(i_rAbout
, i_rProperty
, i_rContent
, i_rDatatype
) );
429 AddRDFa(i_xObject
, pAttributes
);
433 void RDFaImportHelper::InsertRDFa(
434 uno::Reference
< rdf::XRepositorySupplier
> const & i_xModel
)
436 SAL_WARN_IF(!i_xModel
.is(), "xmloff.core", "InsertRDFa: invalid arg: model null");
437 if (!i_xModel
.is()) return;
438 const uno::Reference
< rdf::XDocumentRepository
> xRepository(
439 i_xModel
->getRDFRepository(), uno::UNO_QUERY
);
440 SAL_WARN_IF(!xRepository
.is(), "xmloff.core", "InsertRDFa: no DocumentRepository?");
441 if (!xRepository
.is()) return;
442 RDFaInserter
inserter(GetImport().GetComponentContext(), xRepository
);
443 for (const auto& RDFaEntry
: m_RDFaEntries
)
444 inserter
.InsertRDFaEntry(RDFaEntry
);
447 } // namespace xmloff
449 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */