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

Generated by: LCOV version 1.10