LCOV - code coverage report
Current view: top level - stoc/source/uriproc - ExternalUriReferenceTranslator.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 14 69 20.3 %
Date: 2014-11-03 Functions: 7 11 63.6 %
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 "stocservices.hxx"
      22             : 
      23             : #include <boost/noncopyable.hpp>
      24             : #include <com/sun/star/lang/XServiceInfo.hpp>
      25             : #include <com/sun/star/uno/Exception.hpp>
      26             : #include <com/sun/star/uno/Reference.hxx>
      27             : #include <com/sun/star/uno/RuntimeException.hpp>
      28             : #include <com/sun/star/uno/Sequence.hxx>
      29             : #include <com/sun/star/uno/XComponentContext.hpp>
      30             : #include <com/sun/star/uno/XInterface.hpp>
      31             : #include <com/sun/star/uri/XExternalUriReferenceTranslator.hpp>
      32             : #include <cppuhelper/implbase2.hxx>
      33             : #include <cppuhelper/supportsservice.hxx>
      34             : #include <cppuhelper/weak.hxx>
      35             : #include <osl/thread.h>
      36             : #include <rtl/string.h>
      37             : #include <rtl/textenc.h>
      38             : #include <rtl/uri.h>
      39             : #include <rtl/uri.hxx>
      40             : #include <rtl/ustrbuf.hxx>
      41             : #include <rtl/ustring.hxx>
      42             : #include <sal/types.h>
      43             : 
      44             : namespace {
      45             : 
      46             : class Translator:
      47             :     public cppu::WeakImplHelper2<
      48             :         css::lang::XServiceInfo, css::uri::XExternalUriReferenceTranslator>,
      49             :     private boost::noncopyable
      50             : {
      51             : public:
      52          98 :     explicit Translator(
      53             :         css::uno::Reference< css::uno::XComponentContext > const & context):
      54          98 :         m_context(context) {}
      55             : 
      56             :     virtual OUString SAL_CALL getImplementationName()
      57             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      58             : 
      59             :     virtual sal_Bool SAL_CALL supportsService(OUString const & serviceName)
      60             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      61             : 
      62             :     virtual css::uno::Sequence< OUString > SAL_CALL
      63             :     getSupportedServiceNames() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      64             : 
      65             :     virtual OUString SAL_CALL
      66             :     translateToInternal(OUString const & externalUriReference)
      67             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      68             : 
      69             :     virtual OUString SAL_CALL
      70             :     translateToExternal(OUString const & internalUriReference)
      71             :         throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      72             : 
      73             : private:
      74         196 :     virtual ~Translator() {}
      75             : 
      76             :     css::uno::Reference< css::uno::XComponentContext > m_context;
      77             : };
      78             : 
      79           0 : OUString Translator::getImplementationName()
      80             :     throw (css::uno::RuntimeException, std::exception)
      81             : {
      82             :     return
      83           0 :         stoc_services::ExternalUriReferenceTranslator::getImplementationName();
      84             : }
      85             : 
      86           0 : sal_Bool Translator::supportsService(OUString const & serviceName)
      87             :     throw (css::uno::RuntimeException, std::exception)
      88             : {
      89           0 :     return cppu::supportsService(this, serviceName);
      90             : }
      91             : 
      92           0 : css::uno::Sequence< OUString > Translator::getSupportedServiceNames()
      93             :     throw (css::uno::RuntimeException, std::exception)
      94             : {
      95             :     return stoc_services::ExternalUriReferenceTranslator::
      96           0 :         getSupportedServiceNames();
      97             : }
      98             : 
      99          96 : OUString Translator::translateToInternal(
     100             :     OUString const & externalUriReference)
     101             :     throw (css::uno::RuntimeException, std::exception)
     102             : {
     103          96 :     if (!externalUriReference.matchIgnoreAsciiCase("file:/"))
     104             :     {
     105          96 :         return externalUriReference;
     106             :     }
     107           0 :     sal_Int32 i = RTL_CONSTASCII_LENGTH("file:");
     108           0 :     OUStringBuffer buf;
     109           0 :     buf.append(externalUriReference.getStr(), i);
     110             :     // Some environments (e.g., Java) produce illegal file URLs without an
     111             :     // authority part; treat them as having an empty authority part:
     112           0 :     if (!externalUriReference.match("//", i))
     113             :     {
     114           0 :         buf.append("//");
     115             :     }
     116           0 :     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     117           0 :     for (bool path = true;;) {
     118           0 :         sal_Int32 j = i;
     119           0 :         while (j != externalUriReference.getLength()
     120           0 :                && externalUriReference[j] != '#'
     121           0 :                && (!path || externalUriReference[j] != '/'))
     122             :         {
     123           0 :             ++j;
     124             :         }
     125           0 :         if (j != i) {
     126             :             OUString seg(
     127             :                 rtl::Uri::encode(
     128             :                     rtl::Uri::decode(
     129             :                         externalUriReference.copy(i, j - i),
     130             :                         rtl_UriDecodeStrict, encoding),
     131             :                     rtl_UriCharClassPchar, rtl_UriEncodeStrict,
     132           0 :                     RTL_TEXTENCODING_UTF8));
     133           0 :             if (seg.isEmpty()) {
     134           0 :                 return OUString();
     135             :             }
     136           0 :             buf.append(seg);
     137             :         }
     138           0 :         if (j == externalUriReference.getLength()) {
     139           0 :             break;
     140             :         }
     141           0 :         buf.append(externalUriReference[j]);
     142           0 :         path = externalUriReference[j] == '/';
     143           0 :         i = j + 1;
     144           0 :     }
     145           0 :     return buf.makeStringAndClear();
     146             : }
     147             : 
     148           0 : OUString Translator::translateToExternal(
     149             :     OUString const & internalUriReference)
     150             :     throw (css::uno::RuntimeException, std::exception)
     151             : {
     152           0 :     if (!internalUriReference.matchIgnoreAsciiCase("file://"))
     153             :     {
     154           0 :         return internalUriReference;
     155             :     }
     156           0 :     sal_Int32 i = RTL_CONSTASCII_LENGTH("file://");
     157           0 :     OUStringBuffer buf;
     158           0 :     buf.append(internalUriReference.getStr(), i);
     159           0 :     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
     160           0 :     for (bool path = true;;) {
     161           0 :         sal_Int32 j = i;
     162           0 :         while (j != internalUriReference.getLength()
     163           0 :                && internalUriReference[j] != '#'
     164           0 :                && (!path || internalUriReference[j] != '/'))
     165             :         {
     166           0 :             ++j;
     167             :         }
     168           0 :         if (j != i) {
     169             :             // Use rtl_UriDecodeToIuri -> rtl_UriEncodeStrictKeepEscapes instead
     170             :             // of rtl_UriDecodeStrict -> rtl_UriEncodeStrict, so that spurious
     171             :             // non--UTF-8 octets like "%FE" are copied verbatim:
     172             :             OUString seg(
     173             :                 rtl::Uri::encode(
     174             :                     rtl::Uri::decode(
     175             :                         internalUriReference.copy(i, j - i),
     176             :                         rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8),
     177             :                     rtl_UriCharClassPchar, rtl_UriEncodeStrictKeepEscapes,
     178           0 :                     encoding));
     179           0 :             if (seg.isEmpty()) {
     180           0 :                 return OUString();
     181             :             }
     182           0 :             buf.append(seg);
     183             :         }
     184           0 :         if (j == internalUriReference.getLength()) {
     185           0 :             break;
     186             :         }
     187           0 :         buf.append(internalUriReference[j]);
     188           0 :         path = internalUriReference[j] == '/';
     189           0 :         i = j + 1;
     190           0 :     }
     191           0 :     return buf.makeStringAndClear();
     192             : }
     193             : 
     194             : }
     195             : 
     196             : namespace stoc_services  { namespace ExternalUriReferenceTranslator {
     197             : 
     198          98 : css::uno::Reference< css::uno::XInterface > create(
     199             :     css::uno::Reference< css::uno::XComponentContext > const & context)
     200             : {
     201          98 :     return static_cast< cppu::OWeakObject * >(new Translator(context));
     202             : }
     203             : 
     204         411 : OUString getImplementationName() {
     205         411 :     return OUString("com.sun.star.comp.uri.ExternalUriReferenceTranslator");
     206             : }
     207             : 
     208          98 : css::uno::Sequence< OUString > getSupportedServiceNames() {
     209          98 :     css::uno::Sequence< OUString > s(1);
     210          98 :     s[0] = "com.sun.star.uri.ExternalUriReferenceTranslator";
     211          98 :     return s;
     212             : }
     213             : 
     214             : } }
     215             : 
     216             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10