28#include <com/sun/star/rdf/URI.hpp>
29#include <com/sun/star/rdf/XDocumentRepository.hpp>
30#include <com/sun/star/rdf/XRepositorySupplier.hpp>
32#include <rtl/ustring.hxx>
48 const SvXMLImport & GetImport()
const {
return m_rImport; }
51 OUString GetAbsoluteReference(OUString
const & i_rURI)
const
53 if (i_rURI.isEmpty() || i_rURI[0] ==
'#')
55 return GetImport().GetBaseURL() + i_rURI;
59 return GetImport().GetAbsoluteReference(i_rURI);
64 explicit RDFaReader(SvXMLImport
const & i_rImport)
69 OUString ReadCURIE(OUString
const & i_rCURIE)
const;
71 std::vector< OUString >
72 ReadCURIEs(OUString
const & i_rCURIEs)
const;
75 ReadURIOrSafeCURIE( OUString
const & i_rURIOrSafeCURIE)
const;
84 typedef ::std::map< OUString, uno::Reference< rdf::XBlankNode > >
90 RDFaInserter(uno::Reference<uno::XComponentContext> i_xContext,
91 uno::Reference< rdf::XDocumentRepository > i_xRepository)
96 uno::Reference< rdf::XBlankNode >
97 LookupBlankNode(OUString
const & i_rNodeId );
99 uno::Reference< rdf::XURI >
100 MakeURI( OUString
const & i_rURI)
const;
102 uno::Reference< rdf::XResource>
103 MakeResource( OUString
const & i_rResource);
105 void InsertRDFaEntry(
struct RDFaEntry
const & i_rEntry);
120 ::std::vector< OUString >&& i_rProperties,
122 OUString i_sDatatype)
137 std::shared_ptr<ParsedRDFaAttributes> i_pRDFaAttributes)
145 return (
'\t' == i_Char) || (
'\n' == i_Char) || (
'\r' == i_Char)
151 const sal_Int32 len( io_rString.getLength() );
153 while ((idxstt < len) && (
isWS(io_rString[idxstt])))
155 sal_Int32 idxend(idxstt);
156 while ((idxend < len) && (!
isWS(io_rString[idxend])))
158 const OUString ret(io_rString.copy(idxstt, idxend - idxstt));
159 io_rString = io_rString.copy(idxend);
164RDFaReader::ReadCURIE(OUString
const & i_rCURIE)
const
167 const sal_Int32
idx( i_rCURIE.indexOf(
':') );
174 sal_uInt16 nKey( GetImport().GetNamespaceMap().GetKeyByQName(
189 const OUString URI(Namespace + LocalName);
190 return GetAbsoluteReference(URI);
194 SAL_INFO(
"xmloff.core",
"ReadCURIE: invalid CURIE: invalid prefix" );
199 SAL_INFO(
"xmloff.core",
"ReadCURIE: invalid CURIE: no prefix" );
203::std::vector< OUString >
204RDFaReader::ReadCURIEs(OUString
const & i_rCURIEs)
const
206 std::vector< OUString > vec;
207 OUString CURIEs(i_rCURIEs);
210 if (!curie.isEmpty())
212 const OUString uri(ReadCURIE(curie));
219 while (!CURIEs.isEmpty());
222 SAL_INFO(
"xmloff.core",
"ReadCURIEs: invalid CURIEs" );
228RDFaReader::ReadURIOrSafeCURIE(OUString
const & i_rURIOrSafeCURIE)
const
230 const sal_Int32 len(i_rURIOrSafeCURIE.getLength());
231 if (len && (i_rURIOrSafeCURIE[0] ==
'['))
233 if ((len >= 2) && (i_rURIOrSafeCURIE[len - 1] ==
']'))
235 return ReadCURIE(i_rURIOrSafeCURIE.copy(1, len - 2));
239 SAL_INFO(
"xmloff.core",
"ReadURIOrSafeCURIE: invalid SafeCURIE" );
245 if (i_rURIOrSafeCURIE.startsWith(
"_:"))
247 SAL_INFO(
"xmloff.core",
"ReadURIOrSafeCURIE: invalid URI: scheme is _" );
252 return GetAbsoluteReference(i_rURIOrSafeCURIE);
257uno::Reference< rdf::XBlankNode >
258RDFaInserter::LookupBlankNode(OUString
const & i_rNodeId )
260 uno::Reference< rdf::XBlankNode > & rEntry(
m_BlankNodeMap[ i_rNodeId ] );
268uno::Reference< rdf::XURI >
269RDFaInserter::MakeURI( OUString
const & i_rURI)
const
271 if (i_rURI.startsWith(
"_:"))
273 SAL_INFO(
"xmloff.core",
"MakeURI: cannot create URI for blank node");
280 return rdf::URI::create(
m_xContext, i_rURI );
282 catch (uno::Exception &)
284 SAL_WARN(
"xmloff.core",
"MakeURI: cannot create URI");
290uno::Reference<rdf::XResource>
291RDFaInserter::MakeResource( OUString
const & i_rResource)
293 if (i_rResource.startsWith(
"_:"))
298 OUString
name( i_rResource.copy(2) );
299 const uno::Reference< rdf::XBlankNode > xBNode( LookupBlankNode(name) );
300 SAL_WARN_IF(!xBNode.is(),
"xmloff.core",
"no blank node?");
305 return MakeURI( i_rResource );
309void RDFaInserter::InsertRDFaEntry(
310 struct RDFaEntry
const & i_rEntry)
312 SAL_WARN_IF(!i_rEntry.m_xObject.is(),
"xmloff.core",
"InsertRDFaEntry: invalid arg: null object");
313 if (!i_rEntry.m_xObject.is())
return;
315 const uno::Reference< rdf::XResource > xSubject(
316 MakeResource( i_rEntry.m_xRDFaAttributes->m_About ) );
322 ::std::vector< uno::Reference< rdf::XURI > > predicates;
324 predicates.reserve(i_rEntry.m_xRDFaAttributes->m_Properties.size());
326 for (OUString
const& prop : i_rEntry.m_xRDFaAttributes->m_Properties)
328 auto const xURI(MakeURI(prop));
331 predicates.push_back(xURI);
335 if (predicates.empty())
340 uno::Reference<rdf::XURI> xDatatype;
341 if (!i_rEntry.m_xRDFaAttributes->m_Datatype.isEmpty())
343 xDatatype = MakeURI( i_rEntry.m_xRDFaAttributes->m_Datatype );
353 i_rEntry.m_xRDFaAttributes->m_Content, xDatatype);
355 catch (uno::Exception &)
357 SAL_WARN(
"xmloff.core",
"InsertRDFaEntry: setStatementRDFa failed?");
370std::shared_ptr<ParsedRDFaAttributes>
372 OUString
const & i_rAbout,
373 OUString
const & i_rProperty,
374 OUString
const & i_rContent,
375 OUString
const & i_rDatatype)
377 if (i_rProperty.isEmpty())
379 SAL_INFO(
"xmloff.core",
"AddRDFa: invalid input: xhtml:property empty");
380 return std::shared_ptr<ParsedRDFaAttributes>();
384 const OUString about( reader.ReadURIOrSafeCURIE(i_rAbout) );
385 if (about.isEmpty()) {
386 return std::shared_ptr<ParsedRDFaAttributes>();
389 reader.ReadCURIEs(i_rProperty) );
391 return std::shared_ptr<ParsedRDFaAttributes>();
393 const OUString datatype( !i_rDatatype.isEmpty()
394 ? reader.ReadCURIE(i_rDatatype)
396 return std::make_shared<ParsedRDFaAttributes>(
397 about, std::move(
properties), i_rContent, datatype);
402 uno::Reference<rdf::XMetadatable>
const & i_xObject,
403 std::shared_ptr<ParsedRDFaAttributes>
const & i_pRDFaAttributes)
407 SAL_WARN(
"xmloff.core",
"AddRDFa: invalid arg: null textcontent");
410 if (!i_pRDFaAttributes)
412 SAL_WARN(
"xmloff.core",
"AddRDFa: invalid arg: null RDFa attributes");
420 uno::Reference<rdf::XMetadatable>
const & i_xObject,
421 OUString
const & i_rAbout,
422 OUString
const & i_rProperty,
423 OUString
const & i_rContent,
424 OUString
const & i_rDatatype)
426 std::shared_ptr<ParsedRDFaAttributes> pAttributes(
427 ParseRDFa(i_rAbout, i_rProperty, i_rContent, i_rDatatype) );
430 AddRDFa(i_xObject, pAttributes);
435 uno::Reference< rdf::XRepositorySupplier>
const & i_xModel)
437 SAL_WARN_IF(!i_xModel.is(),
"xmloff.core",
"InsertRDFa: invalid arg: model null");
438 if (!i_xModel.is())
return;
439 const uno::Reference< rdf::XDocumentRepository > xRepository(
440 i_xModel->getRDFRepository(), uno::UNO_QUERY);
441 SAL_WARN_IF(!xRepository.is(),
"xmloff.core",
"InsertRDFa: no DocumentRepository?");
442 if (!xRepository.is())
return;
443 RDFaInserter inserter(
GetImport().GetComponentContext(), xRepository);
BlankNodeMap_t m_BlankNodeMap
uno::Reference< rdf::XDocumentRepository > m_xRepository
const uno::Reference< uno::XComponentContext > m_xContext
const SvXMLImport & m_rImport
void InsertRDFa(css::uno::Reference< css::rdf::XRepositorySupplier > const &i_xModel)
Insert all added statements into the RDF repository.
::std::vector< RDFaEntry > m_RDFaEntries
RDFaImportHelper(const SvXMLImport &i_rImport)
void AddRDFa(css::uno::Reference< css::rdf::XMetadatable > const &i_xObject, std::shared_ptr< ParsedRDFaAttributes > const &i_pRDFaAttributes)
Add a RDFa statement; must have been parsed with ParseRDFa.
const SvXMLImport & GetImport() const
std::shared_ptr< ParsedRDFaAttributes > ParseRDFa(OUString const &i_rAbout, OUString const &i_rProperty, OUString const &i_rContent, OUString const &i_rDatatype)
Parse RDFa attributes.
void ParseAndAddRDFa(css::uno::Reference< css::rdf::XMetadatable > const &i_xObject, OUString const &i_rAbout, OUString const &i_rProperty, OUString const &i_rContent, OUString const &i_rDatatype)
Parse and add a RDFa statement; parameters are XML attribute values.
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
const sal_uInt16 XML_NAMESPACE_NONE
const sal_uInt16 XML_NAMESPACE_UNKNOWN
const sal_uInt16 XML_NAMESPACE_XMLNS
static bool isWS(const sal_Unicode i_Char)
static OUString splitAtWS(OUString &io_rString)
store parsed RDFa attributes
::std::vector< OUString > m_Properties
ParsedRDFaAttributes(OUString i_sAbout, ::std::vector< OUString > &&i_rProperties, OUString i_sContent, OUString i_sDatatype)
store metadatable object and its RDFa attributes
uno::Reference< rdf::XMetadatable > m_xObject
RDFaEntry(uno::Reference< rdf::XMetadatable > i_xObject, std::shared_ptr< ParsedRDFaAttributes > i_pRDFaAttributes)
std::shared_ptr< ParsedRDFaAttributes > m_xRDFaAttributes