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

Generated by: LCOV version 1.10