1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <RDFaImportHelper.hxx>
#include <utility>
#include <xmloff/xmlimp.hxx>
#include <xmloff/namespacemap.hxx>
#include <comphelper/sequence.hxx>
#include <com/sun/star/rdf/URI.hpp>
#include <com/sun/star/rdf/XDocumentRepository.hpp>
#include <com/sun/star/rdf/XRepositorySupplier.hpp>
#include <rtl/ustring.hxx>
#include <sal/log.hxx>
#include <map>
using namespace ::com::sun::star;
namespace xmloff {
namespace {
/** a bit of context for parsing RDFa attributes */
class RDFaReader
{
const SvXMLImport & m_rImport;
const SvXMLImport & GetImport() const { return m_rImport; }
//FIXME: this is an ugly hack to workaround buggy SvXMLImport::GetAbsolute
OUString GetAbsoluteReference(OUString const & i_rURI) const
{
if (i_rURI.isEmpty() || i_rURI[0] == '#')
{
return GetImport().GetBaseURL() + i_rURI;
}
else
{
return GetImport().GetAbsoluteReference(i_rURI);
}
}
public:
explicit RDFaReader(SvXMLImport const & i_rImport)
: m_rImport(i_rImport)
{ }
// returns URI or blank node!
OUString ReadCURIE(OUString const & i_rCURIE) const;
std::vector< OUString >
ReadCURIEs(OUString const & i_rCURIEs) const;
OUString
ReadURIOrSafeCURIE( OUString const & i_rURIOrSafeCURIE) const;
};
/** helper to insert RDFa statements into the RDF repository */
class RDFaInserter
{
const uno::Reference<uno::XComponentContext> m_xContext;
uno::Reference< rdf::XDocumentRepository > m_xRepository;
typedef ::std::map< OUString, uno::Reference< rdf::XBlankNode > >
BlankNodeMap_t;
BlankNodeMap_t m_BlankNodeMap;
public:
RDFaInserter(uno::Reference<uno::XComponentContext> i_xContext,
uno::Reference< rdf::XDocumentRepository > i_xRepository)
: m_xContext(std::move(i_xContext))
, m_xRepository(std::move(i_xRepository))
{}
uno::Reference< rdf::XBlankNode >
LookupBlankNode(OUString const & i_rNodeId );
uno::Reference< rdf::XURI >
MakeURI( OUString const & i_rURI) const;
uno::Reference< rdf::XResource>
MakeResource( OUString const & i_rResource);
void InsertRDFaEntry(struct RDFaEntry const & i_rEntry);
};
}
/** store parsed RDFa attributes */
struct ParsedRDFaAttributes
{
OUString m_About;
::std::vector< OUString > m_Properties;
OUString m_Content;
OUString m_Datatype;
ParsedRDFaAttributes(
OUString i_sAbout,
::std::vector< OUString >&& i_rProperties,
OUString i_sContent,
OUString i_sDatatype)
: m_About(std::move(i_sAbout))
, m_Properties(std::move(i_rProperties))
, m_Content(std::move(i_sContent))
, m_Datatype(std::move(i_sDatatype))
{ }
};
/** store metadatable object and its RDFa attributes */
struct RDFaEntry
{
uno::Reference<rdf::XMetadatable> m_xObject;
std::shared_ptr<ParsedRDFaAttributes> m_xRDFaAttributes;
RDFaEntry(uno::Reference<rdf::XMetadatable> i_xObject,
std::shared_ptr<ParsedRDFaAttributes> i_pRDFaAttributes)
: m_xObject(std::move(i_xObject))
, m_xRDFaAttributes(std::move(i_pRDFaAttributes))
{ }
};
static bool isWS(const sal_Unicode i_Char)
{
return ('\t' == i_Char) || ('\n' == i_Char) || ('\r' == i_Char)
|| (' ' == i_Char);
}
static OUString splitAtWS(OUString & io_rString)
{
const sal_Int32 len( io_rString.getLength() );
sal_Int32 idxstt(0);
while ((idxstt < len) && ( isWS(io_rString[idxstt])))
++idxstt; // skip leading ws
sal_Int32 idxend(idxstt);
while ((idxend < len) && (!isWS(io_rString[idxend])))
++idxend; // the CURIE
const OUString ret(io_rString.copy(idxstt, idxend - idxstt));
io_rString = io_rString.copy(idxend); // rest
return ret;
}
OUString
RDFaReader::ReadCURIE(OUString const & i_rCURIE) const
{
// the RDFa spec says that a prefix is required (it may be empty: ":foo")
const sal_Int32 idx( i_rCURIE.indexOf(':') );
if (idx >= 0)
{
OUString Prefix;
OUString LocalName;
OUString Namespace;
// LocalName may contain ':', see "ipchar" in RFC 3987
sal_uInt16 nKey( GetImport().GetNamespaceMap().GetKeyByQName(
i_rCURIE, &Prefix, &LocalName, &Namespace, SvXMLNamespaceMap::QNameMode::AttrValue) );
if ( Prefix == "_" )
{
// eeek, it's a bnode!
// "_" is not a valid URI scheme => we can identify bnodes
return i_rCURIE;
}
else
{
SAL_WARN_IF(XML_NAMESPACE_NONE == nKey, "xmloff.core", "no namespace?");
if ((XML_NAMESPACE_UNKNOWN != nKey) &&
(XML_NAMESPACE_XMLNS != nKey))
{
// N.B.: empty LocalName is valid!
const OUString URI(Namespace + LocalName);
return GetAbsoluteReference(URI);
}
else
{
SAL_INFO("xmloff.core", "ReadCURIE: invalid CURIE: invalid prefix" );
return OUString();
}
}
}
SAL_INFO("xmloff.core", "ReadCURIE: invalid CURIE: no prefix" );
return OUString();
}
::std::vector< OUString >
RDFaReader::ReadCURIEs(OUString const & i_rCURIEs) const
{
std::vector< OUString > vec;
OUString CURIEs(i_rCURIEs);
do {
OUString curie( splitAtWS(CURIEs) );
if (!curie.isEmpty())
{
const OUString uri(ReadCURIE(curie));
if (!uri.isEmpty())
{
vec.push_back(uri);
}
}
}
while (!CURIEs.isEmpty());
if (vec.empty())
{
SAL_INFO("xmloff.core", "ReadCURIEs: invalid CURIEs" );
}
return vec;
}
OUString
RDFaReader::ReadURIOrSafeCURIE(OUString const & i_rURIOrSafeCURIE) const
{
const sal_Int32 len(i_rURIOrSafeCURIE.getLength());
if (len && (i_rURIOrSafeCURIE[0] == '['))
{
if ((len >= 2) && (i_rURIOrSafeCURIE[len - 1] == ']'))
{
return ReadCURIE(i_rURIOrSafeCURIE.copy(1, len - 2));
}
else
{
SAL_INFO("xmloff.core", "ReadURIOrSafeCURIE: invalid SafeCURIE" );
return OUString();
}
}
else
{
if (i_rURIOrSafeCURIE.startsWith("_:")) // blank node
{
SAL_INFO("xmloff.core", "ReadURIOrSafeCURIE: invalid URI: scheme is _" );
return OUString();
}
else
{
return GetAbsoluteReference(i_rURIOrSafeCURIE);
}
}
}
uno::Reference< rdf::XBlankNode >
RDFaInserter::LookupBlankNode(OUString const & i_rNodeId )
{
uno::Reference< rdf::XBlankNode > & rEntry( m_BlankNodeMap[ i_rNodeId ] );
if (!rEntry.is())
{
rEntry = m_xRepository->createBlankNode();
}
return rEntry;
}
uno::Reference< rdf::XURI >
RDFaInserter::MakeURI( OUString const & i_rURI) const
{
if (i_rURI.startsWith("_:")) // blank node
{
SAL_INFO("xmloff.core", "MakeURI: cannot create URI for blank node");
return nullptr;
}
else
{
try
{
return rdf::URI::create( m_xContext, i_rURI );
}
catch (uno::Exception &)
{
SAL_WARN("xmloff.core", "MakeURI: cannot create URI");
return nullptr;
}
}
}
uno::Reference<rdf::XResource>
RDFaInserter::MakeResource( OUString const & i_rResource)
{
if (i_rResource.startsWith("_:")) // blank node
{
// we cannot use the blank node label as-is: it must be distinct
// from labels in other graphs, so create fresh ones per XML stream
// N.B.: content.xml and styles.xml are distinct graphs
OUString name( i_rResource.copy(2) );
const uno::Reference< rdf::XBlankNode > xBNode( LookupBlankNode(name) );
SAL_WARN_IF(!xBNode.is(), "xmloff.core", "no blank node?");
return xBNode;
}
else
{
return MakeURI( i_rResource );
}
}
void RDFaInserter::InsertRDFaEntry(
struct RDFaEntry const & i_rEntry)
{
SAL_WARN_IF(!i_rEntry.m_xObject.is(), "xmloff.core", "InsertRDFaEntry: invalid arg: null object");
if (!i_rEntry.m_xObject.is()) return;
const uno::Reference< rdf::XResource > xSubject(
MakeResource( i_rEntry.m_xRDFaAttributes->m_About ) );
if (!xSubject.is())
{
return; // invalid
}
::std::vector< uno::Reference< rdf::XURI > > predicates;
predicates.reserve(i_rEntry.m_xRDFaAttributes->m_Properties.size());
for (OUString const& prop : i_rEntry.m_xRDFaAttributes->m_Properties)
{
auto const xURI(MakeURI(prop));
if (xURI.is())
{
predicates.push_back(xURI);
}
}
if (predicates.empty())
{
return; // invalid
}
uno::Reference<rdf::XURI> xDatatype;
if (!i_rEntry.m_xRDFaAttributes->m_Datatype.isEmpty())
{
xDatatype = MakeURI( i_rEntry.m_xRDFaAttributes->m_Datatype );
}
try
{
// N.B.: this will call xMeta->ensureMetadataReference, which is why
// this must be done _after_ importing the whole XML file,
// to prevent collision between generated ids and ids in the file
m_xRepository->setStatementRDFa(xSubject, comphelper::containerToSequence(predicates),
i_rEntry.m_xObject,
i_rEntry.m_xRDFaAttributes->m_Content, xDatatype);
}
catch (uno::Exception &)
{
SAL_WARN("xmloff.core", "InsertRDFaEntry: setStatementRDFa failed?");
}
}
RDFaImportHelper::RDFaImportHelper(const SvXMLImport & i_rImport)
: m_rImport(i_rImport)
{
}
RDFaImportHelper::~RDFaImportHelper()
{
}
std::shared_ptr<ParsedRDFaAttributes>
RDFaImportHelper::ParseRDFa(
OUString const & i_rAbout,
OUString const & i_rProperty,
OUString const & i_rContent,
OUString const & i_rDatatype)
{
if (i_rProperty.isEmpty())
{
SAL_INFO("xmloff.core", "AddRDFa: invalid input: xhtml:property empty");
return std::shared_ptr<ParsedRDFaAttributes>();
}
// must parse CURIEs here: need namespace declaration context
RDFaReader reader(GetImport());
const OUString about( reader.ReadURIOrSafeCURIE(i_rAbout) );
if (about.isEmpty()) {
return std::shared_ptr<ParsedRDFaAttributes>();
}
::std::vector< OUString > properties(
reader.ReadCURIEs(i_rProperty) );
if (properties.empty()) {
return std::shared_ptr<ParsedRDFaAttributes>();
}
const OUString datatype( !i_rDatatype.isEmpty()
? reader.ReadCURIE(i_rDatatype)
: OUString() );
return std::make_shared<ParsedRDFaAttributes>(
about, std::move(properties), i_rContent, datatype);
}
void
RDFaImportHelper::AddRDFa(
uno::Reference<rdf::XMetadatable> const & i_xObject,
std::shared_ptr<ParsedRDFaAttributes> const & i_pRDFaAttributes)
{
if (!i_xObject.is())
{
SAL_WARN("xmloff.core", "AddRDFa: invalid arg: null textcontent");
return;
}
if (!i_pRDFaAttributes)
{
SAL_WARN("xmloff.core", "AddRDFa: invalid arg: null RDFa attributes");
return;
}
m_RDFaEntries.emplace_back(i_xObject, i_pRDFaAttributes);
}
void
RDFaImportHelper::ParseAndAddRDFa(
uno::Reference<rdf::XMetadatable> const & i_xObject,
OUString const & i_rAbout,
OUString const & i_rProperty,
OUString const & i_rContent,
OUString const & i_rDatatype)
{
std::shared_ptr<ParsedRDFaAttributes> pAttributes(
ParseRDFa(i_rAbout, i_rProperty, i_rContent, i_rDatatype) );
if (pAttributes)
{
AddRDFa(i_xObject, pAttributes);
}
}
void RDFaImportHelper::InsertRDFa(
uno::Reference< rdf::XRepositorySupplier> const & i_xModel)
{
SAL_WARN_IF(!i_xModel.is(), "xmloff.core", "InsertRDFa: invalid arg: model null");
if (!i_xModel.is()) return;
const uno::Reference< rdf::XDocumentRepository > xRepository(
i_xModel->getRDFRepository(), uno::UNO_QUERY);
SAL_WARN_IF(!xRepository.is(), "xmloff.core", "InsertRDFa: no DocumentRepository?");
if (!xRepository.is()) return;
RDFaInserter inserter(GetImport().GetComponentContext(), xRepository);
for (const auto& RDFaEntry : m_RDFaEntries)
inserter.InsertRDFaEntry(RDFaEntry);
}
} // namespace xmloff
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|