LCOV - code coverage report
Current view: top level - xmloff/source/core - RDFaExportHelper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 54 56 96.4 %
Date: 2014-11-03 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "RDFaExportHelper.hxx"
      21             : 
      22             : #include <xmloff/xmlnmspe.hxx>
      23             : 
      24             : #include <xmloff/xmlexp.hxx>
      25             : #include <xmloff/xmltoken.hxx>
      26             : 
      27             : #include <comphelper/stl_types.hxx>
      28             : #include <comphelper/processfactory.hxx>
      29             : 
      30             : #include <com/sun/star/uri/XUriReference.hpp>
      31             : #include <com/sun/star/uri/UriReferenceFactory.hpp>
      32             : #include <com/sun/star/rdf/Statement.hpp>
      33             : #include <com/sun/star/rdf/URIs.hpp>
      34             : #include <com/sun/star/rdf/URI.hpp>
      35             : #include <com/sun/star/rdf/XLiteral.hpp>
      36             : #include <com/sun/star/rdf/XRepositorySupplier.hpp>
      37             : #include <com/sun/star/rdf/XDocumentRepository.hpp>
      38             : 
      39             : #include <rtl/ustrbuf.hxx>
      40             : 
      41             : #include <boost/bind.hpp>
      42             : #include <boost/iterator_adaptors.hpp>
      43             : #ifndef BOOST_ITERATOR_ADAPTOR_DWA053000_HPP_ // from iterator_adaptors.hpp
      44             : // N.B.: the check for the header guard _of a specific version of boost_
      45             : //       is here so this may work on different versions of boost,
      46             : //       which sadly put the goods in different header files
      47             : #include <boost/iterator/transform_iterator.hpp>
      48             : #endif
      49             : 
      50             : #include <functional>
      51             : #include <algorithm>
      52             : 
      53             : using namespace ::com::sun::star;
      54             : 
      55             : namespace xmloff {
      56             : 
      57             : static OUString
      58          50 : makeCURIE(SvXMLExport * i_pExport,
      59             :     uno::Reference<rdf::XURI> const & i_xURI)
      60             : {
      61             :     OSL_ENSURE(i_xURI.is(), "makeCURIE: null URI");
      62          50 :     if (!i_xURI.is()) throw uno::RuntimeException();
      63             : 
      64          50 :     const OUString Namespace( i_xURI->getNamespace() );
      65             :     OSL_ENSURE(!Namespace.isEmpty(), "makeCURIE: no namespace");
      66          50 :     if (Namespace.isEmpty()) throw uno::RuntimeException();
      67             : 
      68             :     // N.B.: empty LocalName is valid!
      69          50 :     return i_pExport->EnsureNamespace(Namespace) + ":" + i_xURI->getLocalName();
      70             : }
      71             : 
      72             : // #i112473# SvXMLExport::GetRelativeReference() not right for RDF on SaveAs
      73             : // because the URIs in the repository are not rewritten on SaveAs, the
      74             : // URI of the loaded document has to be used, not the URI of the target doc.
      75             : static OUString
      76          28 : getRelativeReference(SvXMLExport const& rExport, OUString const& rURI)
      77             : {
      78             :     uno::Reference< rdf::XURI > const xModelURI(
      79          28 :         rExport.GetModel(), uno::UNO_QUERY_THROW );
      80          56 :     OUString const baseURI( xModelURI->getStringValue() );
      81             : 
      82          56 :     uno::Reference<uno::XComponentContext> xContext( comphelper::getProcessComponentContext() );
      83             :     uno::Reference<uri::XUriReferenceFactory> const xUriFactory =
      84          56 :         uri::UriReferenceFactory::create( xContext );
      85             : 
      86             :     uno::Reference< uri::XUriReference > const xBaseURI(
      87          56 :         xUriFactory->parse(baseURI), uno::UNO_SET_THROW );
      88             :     uno::Reference< uri::XUriReference > const xAbsoluteURI(
      89          56 :         xUriFactory->parse(rURI), uno::UNO_SET_THROW );
      90             :     uno::Reference< uri::XUriReference > const xRelativeURI(
      91          28 :         xUriFactory->makeRelative(xBaseURI, xAbsoluteURI, true, true, false),
      92          56 :         uno::UNO_SET_THROW );
      93          28 :     OUString const relativeURI(xRelativeURI->getUriReference());
      94             : 
      95          56 :     return relativeURI;
      96             : }
      97             : 
      98           8 : RDFaExportHelper::RDFaExportHelper(SvXMLExport & i_rExport)
      99           8 :     : m_rExport(i_rExport), m_xRepository(0), m_Counter(0)
     100             : {
     101           8 :     const uno::Reference<rdf::XRepositorySupplier> xRS( m_rExport.GetModel(),
     102           8 :             uno::UNO_QUERY);
     103             :     OSL_ENSURE(xRS.is(), "AddRDFa: model is no rdf::XRepositorySupplier");
     104           8 :     if (!xRS.is()) throw uno::RuntimeException();
     105           8 :     m_xRepository.set(xRS->getRDFRepository(), uno::UNO_QUERY_THROW);
     106           8 : }
     107             : 
     108             : OUString
     109           6 : RDFaExportHelper::LookupBlankNode(
     110             :     uno::Reference<rdf::XBlankNode> const & i_xBlankNode)
     111             : {
     112             :     OSL_ENSURE(i_xBlankNode.is(), "null BlankNode?");
     113           6 :     if (!i_xBlankNode.is()) throw uno::RuntimeException();
     114             :     OUString & rEntry(
     115           6 :         m_BlankNodeMap[ i_xBlankNode->getStringValue() ] );
     116           6 :     if (rEntry.isEmpty())
     117             :     {
     118           4 :         rEntry = "_:b" + OUString::number(++m_Counter);
     119             :     }
     120           6 :     return rEntry;
     121             : }
     122             : 
     123             : void
     124          60 : RDFaExportHelper::AddRDFa(
     125             :     uno::Reference<rdf::XMetadatable> const & i_xMetadatable)
     126             : {
     127             :     try
     128             :     {
     129             :         beans::Pair< uno::Sequence<rdf::Statement>, sal_Bool > const
     130          60 :             RDFaResult( m_xRepository->getStatementRDFa(i_xMetadatable) );
     131             : 
     132          60 :         uno::Sequence<rdf::Statement> const & rStatements( RDFaResult.First );
     133             : 
     134          60 :         if (0 == rStatements.getLength())
     135             :         {
     136          86 :             return; // no RDFa
     137             :         }
     138             : 
     139             :         // all stmts have the same subject, so we only handle first one
     140          34 :         const uno::Reference<rdf::XURI> xSubjectURI(rStatements[0].Subject,
     141          68 :             uno::UNO_QUERY);
     142             :         const uno::Reference<rdf::XBlankNode> xSubjectBNode(
     143          68 :             rStatements[0].Subject, uno::UNO_QUERY);
     144          34 :         if (!xSubjectURI.is() && !xSubjectBNode.is())
     145             :         {
     146           0 :             throw uno::RuntimeException();
     147             :         }
     148          34 :         const OUString about( xSubjectURI.is()
     149          56 :             ?   getRelativeReference(m_rExport, xSubjectURI->getStringValue())
     150          40 :             :   "[" + LookupBlankNode(xSubjectBNode) + "]"
     151         164 :             );
     152             : 
     153             :         const uno::Reference<rdf::XLiteral> xContent(
     154          68 :             rStatements[0].Object, uno::UNO_QUERY_THROW );
     155          68 :         const uno::Reference<rdf::XURI> xDatatype(xContent->getDatatype());
     156          34 :         if (xDatatype.is())
     157             :         {
     158             :             const OUString datatype(
     159          10 :                 makeCURIE(&m_rExport, xDatatype) );
     160             :             m_rExport.AddAttribute(XML_NAMESPACE_XHTML,
     161          10 :                 token::XML_DATATYPE, datatype);
     162             :         }
     163          34 :         if (RDFaResult.Second) // there is xhtml:content
     164             :         {
     165             :             m_rExport.AddAttribute(XML_NAMESPACE_XHTML, token::XML_CONTENT,
     166          16 :                 xContent->getValue());
     167             :         }
     168             : 
     169          68 :         OUStringBuffer property;
     170             :         ::comphelper::intersperse(
     171             :             ::boost::make_transform_iterator(rStatements.begin(),
     172             :                 ::boost::bind(&makeCURIE, &m_rExport,
     173             :                     ::boost::bind(&rdf::Statement::Predicate, _1))),
     174             :             // argh, this must be the same type :(
     175             :             ::boost::make_transform_iterator(
     176             :                 rStatements.end(),
     177             :                 ::boost::bind(&makeCURIE, &m_rExport,
     178             :                     ::boost::bind(&rdf::Statement::Predicate, _1))),
     179             :             ::comphelper::OUStringBufferAppender(property),
     180          34 :             OUString(" "));
     181             : 
     182             :         m_rExport.AddAttribute(XML_NAMESPACE_XHTML, token::XML_PROPERTY,
     183          34 :             property.makeStringAndClear());
     184             : 
     185          68 :         m_rExport.AddAttribute(XML_NAMESPACE_XHTML, token::XML_ABOUT, about);
     186             :     }
     187           0 :     catch (uno::Exception &)
     188             :     {
     189             :         OSL_FAIL("AddRDFa: exception");
     190             :     }
     191             : }
     192             : 
     193         699 : } // namespace xmloff
     194             : 
     195             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10