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-17 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        4082 : inline bool isAlphanum(sal_Unicode c)
      41             : {
      42             :     return (c >= 0x30 && c <= 0x39) // '0'--'9'
      43             :         || (c >= 0x41 && c <= 0x5A) // 'A'--'Z'
      44        4082 :         || (c >= 0x61 && c <= 0x7A); // 'a'--'z'
      45             : }
      46             : 
      47             : }
      48             : 
      49         477 : 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         521 : inline UnoUrlDescriptor::Impl::Impl(rtl::OUString const & rDescriptor)
      66             : {
      67         499 :     m_aDescriptor = rDescriptor;
      68             :     enum State { STATE_NAME0, STATE_NAME, STATE_KEY0, STATE_KEY, STATE_VALUE };
      69         499 :     State eState = STATE_NAME0;
      70         499 :     sal_Int32 nStart = 0;
      71         499 :     rtl::OUString aKey;
      72        5878 :     for (sal_Int32 i = 0;; ++i)
      73             :     {
      74        5878 :         bool bEnd = i == rDescriptor.getLength();
      75        5878 :         sal_Unicode c = bEnd ? 0 : rDescriptor.getStr()[i];
      76        5878 :         switch (eState)
      77             :         {
      78             :         case STATE_NAME0:
      79         499 :             if (bEnd || !isAlphanum(c))
      80             :                 throw rtl::MalformedUriException(
      81             :                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
      82           2 :                                       "UNO URL contains bad descriptor name")));
      83         497 :             nStart = i;
      84         497 :             eState = STATE_NAME;
      85         497 :             break;
      86             : 
      87             :         case STATE_NAME:
      88        1589 :             if (bEnd || c == 0x2C) // ','
      89             :             {
      90             :                 m_aName
      91         493 :                     = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
      92         493 :                 eState = STATE_KEY0;
      93             :             }
      94        1096 :             else if (!isAlphanum(c))
      95             :                 throw rtl::MalformedUriException(
      96             :                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
      97           4 :                                       "UNO URL contains bad descriptor name")));
      98        1585 :             break;
      99             : 
     100             :         case STATE_KEY0:
     101         380 :             if (bEnd || !isAlphanum(c))
     102             :                 throw rtl::MalformedUriException(
     103             :                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
     104          10 :                                       "UNO URL contains bad parameter key")));
     105         370 :             nStart = i;
     106         370 :             eState = STATE_KEY;
     107         370 :             break;
     108             : 
     109             :         case STATE_KEY:
     110        1206 :             if (c == 0x3D) // '='
     111             :             {
     112         366 :                 aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase();
     113         366 :                 nStart = i + 1;
     114         366 :                 eState = STATE_VALUE;
     115             :             }
     116         840 :             else if (bEnd || !isAlphanum(c))
     117             :                 throw rtl::MalformedUriException(
     118             :                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
     119           4 :                                       "UNO URL contains bad parameter key")));
     120        1202 :             break;
     121             : 
     122             :         case STATE_VALUE:
     123        2204 :             if (bEnd || c == 0x2C) // ','
     124             :             {
     125         732 :                 if (!m_aParameters.insert(
     126             :                         Parameters::value_type(
     127             :                             aKey,
     128             :                             rtl::Uri::decode(rDescriptor.copy(nStart,
     129             :                                                               i - nStart),
     130             :                                              rtl_UriDecodeWithCharset,
     131         732 :                                              RTL_TEXTENCODING_UTF8))).second)
     132             :                     throw rtl::MalformedUriException(
     133             :                         rtl::OUString(
     134             :                             RTL_CONSTASCII_USTRINGPARAM(
     135           2 :                                 "UNO URL contains duplicated parameter")));
     136         364 :                 eState = STATE_KEY0;
     137             :             }
     138        2202 :             break;
     139             :         }
     140        5856 :         if (bEnd)
     141         477 :             break;
     142         499 :     }
     143         477 : }
     144             : 
     145         293 : UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString const & rDescriptor):
     146         313 :     m_xImpl(new Impl(rDescriptor))
     147         273 : {}
     148             : 
     149             : SAL_WNODEPRECATED_DECLARATIONS_PUSH
     150         194 : UnoUrlDescriptor::UnoUrlDescriptor(std::auto_ptr< Impl > & rImpl):
     151         194 :     m_xImpl(rImpl)
     152         194 : {}
     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         467 : UnoUrlDescriptor::~UnoUrlDescriptor()
     160         467 : {}
     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         138 : rtl::OUString const & UnoUrlDescriptor::getDescriptor() const
     169             : {
     170         138 :     return m_xImpl->m_aDescriptor;
     171             : }
     172             : 
     173          85 : rtl::OUString const & UnoUrlDescriptor::getName() const
     174             : {
     175          85 :     return m_xImpl->m_aName;
     176             : }
     177             : 
     178          34 : bool UnoUrlDescriptor::hasParameter(rtl::OUString const & rKey) const
     179             : {
     180          68 :     return m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase())
     181         102 :         != m_xImpl->m_aParameters.end();
     182             : }
     183             : 
     184         131 : rtl::OUString UnoUrlDescriptor::getParameter(rtl::OUString const & rKey) const
     185             : {
     186             :     Impl::Parameters::const_iterator
     187         131 :         aIt(m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase()));
     188         131 :     return aIt == m_xImpl->m_aParameters.end() ? rtl::OUString() : aIt->second;
     189             : }
     190             : 
     191          97 : 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          97 :     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          97 :         m_aObjectName(rObjectName)
     212          97 :     {}
     213             : SAL_WNODEPRECATED_DECLARATIONS_POP
     214             : };
     215             : 
     216         111 : inline UnoUrl::Impl * UnoUrl::Impl::create(rtl::OUString const & rUrl)
     217             : {
     218         111 :     if (!rUrl.matchIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("uno:"), 0))
     219             :         throw rtl::MalformedUriException(
     220             :             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
     221           6 :                               "UNO URL does not start with \"uno:\"")));
     222         105 :     sal_Int32 i = RTL_CONSTASCII_LENGTH("uno:");
     223         105 :     sal_Int32 j = rUrl.indexOf(';', i);
     224         105 :     if (j < 0)
     225             :         throw rtl::MalformedUriException(
     226             :             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
     227           2 :                               "UNO URL has too few semicolons")));
     228             :     SAL_WNODEPRECATED_DECLARATIONS_PUSH
     229             :     std::auto_ptr< UnoUrlDescriptor::Impl >
     230         103 :         xConnection(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
     231             :     SAL_WNODEPRECATED_DECLARATIONS_POP
     232         103 :     i = j + 1;
     233         103 :     j = rUrl.indexOf(0x3B, i); // ';'
     234         103 :     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         105 :         xProtocol(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i)));
     241             :     SAL_WNODEPRECATED_DECLARATIONS_POP
     242         101 :     i = j + 1;
     243         101 :     if (i == rUrl.getLength())
     244             :         throw rtl::MalformedUriException(
     245             :             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
     246           2 :                               "UNO URL contains empty ObjectName")));
     247        1374 :     for (j = i; j < rUrl.getLength(); ++j)
     248             :     {
     249        1277 :         sal_Unicode c = rUrl.getStr()[j];
     250        1277 :         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           2 :                                   "UNO URL contains invalid ObjectName")));
     260             :     }
     261          97 :     return new Impl(xConnection, xProtocol, rUrl.copy(i));
     262             : }
     263             : 
     264         111 : UnoUrl::UnoUrl(rtl::OUString const & rUrl): m_xImpl(Impl::create(rUrl))
     265          97 : {}
     266             : 
     267           0 : UnoUrl::UnoUrl(UnoUrl const & rOther): m_xImpl(rOther.m_xImpl->clone())
     268           0 : {}
     269             : 
     270          97 : UnoUrl::~UnoUrl()
     271          97 : {}
     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          51 : UnoUrlDescriptor const & UnoUrl::getConnection() const
     280             : {
     281          51 :     return m_xImpl->m_aConnection;
     282             : }
     283             : 
     284          51 : UnoUrlDescriptor const & UnoUrl::getProtocol() const
     285             : {
     286          51 :     return m_xImpl->m_aProtocol;
     287             : }
     288             : 
     289          59 : rtl::OUString const & UnoUrl::getObjectName() const
     290             : {
     291          59 :     return m_xImpl->m_aObjectName;
     292             : }
     293             : 
     294             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10