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

Generated by: LCOV version 1.10