LCOV - code coverage report
Current view: top level - cppuhelper/source - unourl.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 105 119 88.2 %
Date: 2014-04-11 Functions: 18 26 69.2 %
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 "cppuhelper/unourl.hxx"
      22             : 
      23             : #include "osl/diagnose.h"
      24             : #include "rtl/malformeduriexception.hxx"
      25             : #include "rtl/string.h"
      26             : #include "rtl/textenc.h"
      27             : #include "rtl/uri.h"
      28             : #include "rtl/uri.hxx"
      29             : #include "rtl/ustring.h"
      30             : #include "rtl/ustring.hxx"
      31             : #include "sal/types.h"
      32             : 
      33             : #include <map>
      34             : 
      35             : using cppu::UnoUrl;
      36             : using cppu::UnoUrlDescriptor;
      37             : 
      38             : namespace {
      39             : 
      40        1989 : inline bool isAlphanum(sal_Unicode c)
      41             : {
      42        1961 :     return (c >= 0x30 && c <= 0x39) // '0'--'9'
      43        1955 :         || (c >= 0x41 && c <= 0x5A) // 'A'--'Z'
      44        3844 :         || (c >= 0x61 && c <= 0x7A); // 'a'--'z'
      45             : }
      46             : 
      47             : }
      48             : 
      49         258 : class UnoUrlDescriptor::Impl
      50             : {
      51             : public:
      52             :     typedef std::map< rtl::OUString, rtl::OUString > Parameters;
      53             : 
      54             :     rtl::OUString m_aDescriptor;
      55             :     rtl::OUString m_aName;
      56             :     Parameters m_aParameters;
      57             : 
      58             :     /** @exception rtl::MalformedUriException
      59             :      */
      60             :     explicit inline Impl(rtl::OUString const & m_aDescriptor);
      61             : 
      62           0 :     inline Impl * clone() const { return new Impl(*this); }
      63             : };
      64             : 
      65         280 : inline UnoUrlDescriptor::Impl::Impl(rtl::OUString const & rDescriptor)
      66             : {
      67         269 :     m_aDescriptor = rDescriptor;
      68             :     enum State { STATE_NAME0, STATE_NAME, STATE_KEY0, STATE_KEY, STATE_VALUE };
      69         269 :     State eState = STATE_NAME0;
      70         269 :     sal_Int32 nStart = 0;
      71         269 :     rtl::OUString aKey;
      72        5232 :     for (sal_Int32 i = 0;; ++i)
      73             :     {
      74        5232 :         bool bEnd = i == rDescriptor.getLength();
      75        5232 :         sal_Unicode c = bEnd ? 0 : rDescriptor[i];
      76        5232 :         switch (eState)
      77             :         {
      78             :         case STATE_NAME0:
      79         269 :             if (bEnd || !isAlphanum(c))
      80             :                 throw rtl::MalformedUriException(
      81           1 :                     rtl::OUString("UNO URL contains bad descriptor name"));
      82         268 :             nStart = i;
      83         268 :             eState = STATE_NAME;
      84         268 :             break;
      85             : 
      86             :         case STATE_NAME:
      87         881 :             if (bEnd || c == 0x2C) // ','
      88             :             {
      89             :                 m_aName
      90         266 :                     = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
      91         266 :                 eState = STATE_KEY0;
      92             :             }
      93         615 :             else if (!isAlphanum(c))
      94             :                 throw rtl::MalformedUriException(
      95           2 :                     rtl::OUString("UNO URL contains bad descriptor name"));
      96         879 :             break;
      97             : 
      98             :         case STATE_KEY0:
      99         218 :             if (bEnd || !isAlphanum(c))
     100             :                 throw rtl::MalformedUriException(
     101           5 :                     rtl::OUString("UNO URL contains bad parameter key"));
     102         213 :             nStart = i;
     103         213 :             eState = STATE_KEY;
     104         213 :             break;
     105             : 
     106             :         case STATE_KEY:
     107         715 :             if (c == 0x3D) // '='
     108             :             {
     109         211 :                 aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
     110         211 :                 nStart = i + 1;
     111         211 :                 eState = STATE_VALUE;
     112             :             }
     113         504 :             else if (bEnd || !isAlphanum(c))
     114             :                 throw rtl::MalformedUriException(
     115           2 :                     rtl::OUString("UNO URL contains bad parameter key"));
     116         713 :             break;
     117             : 
     118             :         case STATE_VALUE:
     119        3149 :             if (bEnd || c == 0x2C) // ','
     120             :             {
     121         422 :                 if (!m_aParameters.insert(
     122             :                         Parameters::value_type(
     123             :                             aKey,
     124             :                             rtl::Uri::decode(rDescriptor.copy(nStart,
     125             :                                                               i - nStart),
     126             :                                              rtl_UriDecodeWithCharset,
     127         422 :                                              RTL_TEXTENCODING_UTF8))).second)
     128             :                     throw rtl::MalformedUriException(
     129           1 :                         rtl::OUString("UNO URL contains duplicated parameter"));
     130         210 :                 eState = STATE_KEY0;
     131             :             }
     132        3148 :             break;
     133             :         }
     134        5221 :         if (bEnd)
     135         258 :             break;
     136        4974 :     }
     137         258 : }
     138             : 
     139         183 : UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString const & rDescriptor):
     140         193 :     m_xImpl(new Impl(rDescriptor))
     141         173 : {}
     142             : 
     143             : SAL_WNODEPRECATED_DECLARATIONS_PUSH
     144          80 : UnoUrlDescriptor::UnoUrlDescriptor(std::auto_ptr< Impl > & rImpl):
     145          80 :     m_xImpl(rImpl)
     146          80 : {}
     147             : SAL_WNODEPRECATED_DECLARATIONS_POP
     148             : 
     149           0 : UnoUrlDescriptor::UnoUrlDescriptor(UnoUrlDescriptor const & rOther):
     150           0 :     m_xImpl(rOther.m_xImpl->clone())
     151           0 : {}
     152             : 
     153         253 : UnoUrlDescriptor::~UnoUrlDescriptor()
     154         253 : {}
     155             : 
     156           0 : UnoUrlDescriptor & UnoUrlDescriptor::operator =(UnoUrlDescriptor const & rOther)
     157             : {
     158           0 :     m_xImpl.reset(rOther.m_xImpl->clone());
     159           0 :     return *this;
     160             : }
     161             : 
     162          52 : rtl::OUString const & UnoUrlDescriptor::getDescriptor() const
     163             : {
     164          52 :     return m_xImpl->m_aDescriptor;
     165             : }
     166             : 
     167          79 : rtl::OUString const & UnoUrlDescriptor::getName() const
     168             : {
     169          79 :     return m_xImpl->m_aName;
     170             : }
     171             : 
     172          17 : bool UnoUrlDescriptor::hasParameter(rtl::OUString const & rKey) const
     173             : {
     174          34 :     return m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase())
     175          51 :         != m_xImpl->m_aParameters.end();
     176             : }
     177             : 
     178         102 : rtl::OUString UnoUrlDescriptor::getParameter(rtl::OUString const & rKey) const
     179             : {
     180             :     Impl::Parameters::const_iterator
     181         102 :         aIt(m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase()));
     182         102 :     return aIt == m_xImpl->m_aParameters.end() ? rtl::OUString() : aIt->second;
     183             : }
     184             : 
     185          40 : class UnoUrl::Impl
     186             : {
     187             : public:
     188             :     UnoUrlDescriptor m_aConnection;
     189             :     UnoUrlDescriptor m_aProtocol;
     190             :     rtl::OUString m_aObjectName;
     191             : 
     192           0 :     inline Impl * clone() const { return new Impl(*this); }
     193             : 
     194             :     /** @exception rtl::MalformedUriException
     195             :      */
     196             :     static inline Impl * create(rtl::OUString const & rUrl);
     197             : 
     198             : private:
     199             : SAL_WNODEPRECATED_DECLARATIONS_PUSH
     200          40 :     Impl(std::auto_ptr< UnoUrlDescriptor::Impl > & rConnection,
     201             :                               std::auto_ptr< UnoUrlDescriptor::Impl > & rProtocol,
     202             :                               rtl::OUString const & rObjectName):
     203             :         m_aConnection(rConnection),
     204             :         m_aProtocol(rProtocol),
     205          40 :         m_aObjectName(rObjectName)
     206          40 :     {}
     207             : SAL_WNODEPRECATED_DECLARATIONS_POP
     208             : };
     209             : 
     210          47 : inline UnoUrl::Impl * UnoUrl::Impl::create(rtl::OUString const & rUrl)
     211             : {
     212          47 :     if (!rUrl.startsWithIgnoreAsciiCase("uno:"))
     213             :         throw rtl::MalformedUriException(
     214           3 :             rtl::OUString("UNO URL does not start with \"uno:\""));
     215          44 :     sal_Int32 i = RTL_CONSTASCII_LENGTH("uno:");
     216          44 :     sal_Int32 j = rUrl.indexOf(';', i);
     217          44 :     if (j < 0)
     218             :         throw rtl::MalformedUriException(
     219           1 :             rtl::OUString("UNO URL has too few semicolons"));
     220             :     SAL_WNODEPRECATED_DECLARATIONS_PUSH
     221             :     std::auto_ptr< UnoUrlDescriptor::Impl >
     222          43 :         xConnection(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
     223             :     SAL_WNODEPRECATED_DECLARATIONS_POP
     224          43 :     i = j + 1;
     225          43 :     j = rUrl.indexOf(0x3B, i); // ';'
     226          43 :     if (j < 0)
     227             :         throw rtl::MalformedUriException(
     228           0 :             rtl::OUString("UNO URL has too few semicolons"));
     229             :     SAL_WNODEPRECATED_DECLARATIONS_PUSH
     230             :     std::auto_ptr< UnoUrlDescriptor::Impl >
     231          86 :         xProtocol(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
     232             :     SAL_WNODEPRECATED_DECLARATIONS_POP
     233          42 :     i = j + 1;
     234          42 :     if (i == rUrl.getLength())
     235             :         throw rtl::MalformedUriException(
     236           1 :             rtl::OUString("UNO URL contains empty ObjectName"));
     237         428 :     for (j = i; j < rUrl.getLength(); ++j)
     238             :     {
     239         388 :         sal_Unicode c = rUrl[j];
     240         807 :         if (!isAlphanum(c) && c != 0x21 && c != 0x24 // '!', '$'
     241          29 :             && c != 0x26 && c != 0x27 && c != 0x28 // '&', ''', '('
     242          27 :             && c != 0x29 && c != 0x2A && c != 0x2B // ')', '*', '+'
     243          25 :             && c != 0x2C && c != 0x2D && c != 0x2E // ',', '-', '.'
     244          11 :             && c != 0x2F && c != 0x3A && c != 0x3D // '/', ':', '='
     245           5 :             && c != 0x3F && c != 0x40 && c != 0x5F // '?', '@', '_'
     246         389 :             && c != 0x7E) // '~'
     247             :             throw rtl::MalformedUriException(
     248           1 :                 rtl::OUString("UNO URL contains invalid ObjectName"));
     249             :     }
     250          83 :     return new Impl(xConnection, xProtocol, rUrl.copy(i));
     251             : }
     252             : 
     253          47 : UnoUrl::UnoUrl(rtl::OUString const & rUrl): m_xImpl(Impl::create(rUrl))
     254          40 : {}
     255             : 
     256           0 : UnoUrl::UnoUrl(UnoUrl const & rOther): m_xImpl(rOther.m_xImpl->clone())
     257           0 : {}
     258             : 
     259          40 : UnoUrl::~UnoUrl()
     260          40 : {}
     261             : 
     262           0 : UnoUrl & UnoUrl::operator =(UnoUrl const & rOther)
     263             : {
     264           0 :     m_xImpl.reset(rOther.m_xImpl->clone());
     265           0 :     return *this;
     266             : }
     267             : 
     268          17 : UnoUrlDescriptor const & UnoUrl::getConnection() const
     269             : {
     270          17 :     return m_xImpl->m_aConnection;
     271             : }
     272             : 
     273          17 : UnoUrlDescriptor const & UnoUrl::getProtocol() const
     274             : {
     275          17 :     return m_xImpl->m_aProtocol;
     276             : }
     277             : 
     278          21 : rtl::OUString const & UnoUrl::getObjectName() const
     279             : {
     280          21 :     return m_xImpl->m_aObjectName;
     281             : }
     282             : 
     283             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10