LCOV - code coverage report
Current view: top level - unotools/source/config - configpaths.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 62 102 60.8 %
Date: 2012-08-25 Functions: 8 9 88.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 44 104 42.3 %

           Branch data     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 "unotools/configpaths.hxx"
      22                 :            : #include <rtl/ustring.hxx>
      23                 :            : #include <rtl/ustrbuf.hxx>
      24                 :            : #include <osl/diagnose.h>
      25                 :            : 
      26                 :            : //----------------------------------------------------------------------------
      27                 :            : namespace utl
      28                 :            : {
      29                 :            : //----------------------------------------------------------------------------
      30                 :            : 
      31                 :            :     using ::rtl::OUString;
      32                 :            :     using ::rtl::OUStringBuffer;
      33                 :            : 
      34                 :            : //----------------------------------------------------------------------------
      35                 :            : 
      36                 :            : static
      37                 :      69110 : void lcl_resolveCharEntities(OUString & aLocalString)
      38                 :            : {
      39                 :      69110 :     sal_Int32 nEscapePos=aLocalString.indexOf('&');
      40         [ -  + ]:     138220 :     if (nEscapePos < 0) return;
      41                 :            : 
      42                 :          0 :     OUStringBuffer aResult;
      43                 :          0 :     sal_Int32 nStart = 0;
      44                 :            : 
      45         [ #  # ]:          0 :     do
      46                 :            :     {
      47                 :          0 :         sal_Unicode ch = 0;
      48         [ #  # ]:          0 :         if (aLocalString.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("&amp;"),nEscapePos))
      49                 :          0 :             ch = '&';
      50                 :            : 
      51         [ #  # ]:          0 :         else if (aLocalString.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("&apos;"),nEscapePos))
      52                 :          0 :             ch = '\'';
      53                 :            : 
      54         [ #  # ]:          0 :         else if (aLocalString.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("&quot;"),nEscapePos))
      55                 :          0 :             ch = '"';
      56                 :            : 
      57                 :            :         OSL_ENSURE(ch,"Configuration path contains '&' that is not part of a valid character escape");
      58         [ #  # ]:          0 :         if (ch)
      59                 :            :         {
      60 [ #  # ][ #  # ]:          0 :             aResult.append(aLocalString.copy(nStart,nEscapePos-nStart)).append(ch);
      61                 :            : 
      62                 :          0 :             sal_Int32 nEscapeEnd=aLocalString.indexOf(';',nEscapePos);
      63                 :          0 :             nStart = nEscapeEnd+1;
      64                 :          0 :             nEscapePos=aLocalString.indexOf('&',nStart);
      65                 :            :         }
      66                 :            :         else
      67                 :            :         {
      68                 :          0 :             nEscapePos=aLocalString.indexOf('&',nEscapePos+1);
      69                 :            :         }
      70                 :            :     }
      71                 :            :     while ( nEscapePos > 0);
      72                 :            : 
      73         [ #  # ]:          0 :     aResult.append(aLocalString.copy(nStart));
      74                 :            : 
      75         [ #  # ]:      69110 :     aLocalString = aResult.makeStringAndClear();
      76                 :            : }
      77                 :            : 
      78                 :            : //----------------------------------------------------------------------------
      79                 :      36564 : sal_Bool splitLastFromConfigurationPath(OUString const& _sInPath,
      80                 :            :                                         OUString& _rsOutPath,
      81                 :            :                                         OUString& _rsLocalName)
      82                 :            : {
      83                 :            :     sal_Int32 nStart,nEnd;
      84                 :            : 
      85                 :      36564 :     sal_Int32 nPos = _sInPath.getLength()-1;
      86                 :            : 
      87                 :            :     // strip trailing slash
      88 [ -  + ][ -  + ]:      36564 :     if (nPos > 0 && _sInPath[ nPos ] == sal_Unicode('/'))
                 [ +  - ]
      89                 :            :     {
      90                 :            :         OSL_FAIL("Invalid config path: trailing '/' is not allowed");
      91                 :          0 :         --nPos;
      92                 :            :     }
      93                 :            : 
      94                 :            :     // check for predicate ['xxx'] or ["yyy"]
      95 [ +  - ][ -  + ]:      36564 :     if (nPos  > 0 && _sInPath[ nPos ] == sal_Unicode(']'))
                 [ -  + ]
      96                 :            :     {
      97                 :          0 :         sal_Unicode chQuote = _sInPath[--nPos];
      98                 :            : 
      99 [ #  # ][ #  # ]:          0 :         if (chQuote == '\'' || chQuote == '\"')
     100                 :            :         {
     101                 :          0 :             nEnd = nPos;
     102                 :          0 :             nPos = _sInPath.lastIndexOf(chQuote,nEnd);
     103                 :          0 :             nStart = nPos + 1;
     104                 :          0 :             --nPos; // nPos = rInPath.lastIndexOf('[',nPos);
     105                 :            :         }
     106                 :            :         else // allow [xxx]
     107                 :            :         {
     108                 :          0 :             nEnd = nPos + 1;
     109                 :          0 :             nPos = _sInPath.lastIndexOf('[',nEnd);
     110                 :          0 :             nStart = nPos + 1;
     111                 :            :         }
     112                 :            : 
     113                 :            :         OSL_ENSURE(nPos >= 0 && _sInPath[nPos] == '[', "Invalid config path: unmatched quotes or brackets");
     114 [ #  # ][ #  # ]:          0 :         if (nPos >= 0 && _sInPath[nPos] == '[')
                 [ #  # ]
     115                 :            :         {
     116                 :          0 :             nPos =  _sInPath.lastIndexOf('/',nPos);
     117                 :            :         }
     118                 :            :         else // defined behavior for invalid paths
     119                 :            :         {
     120                 :          0 :             nStart = 0, nEnd = _sInPath.getLength();
     121                 :          0 :             nPos = -1;
     122                 :            :         }
     123                 :            : 
     124                 :            :     }
     125                 :            :     else
     126                 :            :     {
     127                 :      36564 :         nEnd = nPos+1;
     128                 :      36564 :         nPos = _sInPath.lastIndexOf('/',nEnd);
     129                 :      36564 :         nStart = nPos + 1;
     130                 :            :     }
     131                 :            :     OSL_ASSERT( -1 <= nPos &&
     132                 :            :                 nPos < nStart &&
     133                 :            :                 nStart < nEnd &&
     134                 :            :                 nEnd <= _sInPath.getLength() );
     135                 :            : 
     136                 :            :     OSL_ASSERT(nPos == -1 || _sInPath[nPos] == '/');
     137                 :            :     OSL_ENSURE(nPos != 0 , "Invalid config child path: immediate child of root");
     138                 :            : 
     139                 :      36564 :     _rsLocalName = _sInPath.copy(nStart, nEnd-nStart);
     140         [ +  + ]:      36564 :     _rsOutPath = (nPos > 0) ? _sInPath.copy(0,nPos) : OUString();
     141                 :      36564 :     lcl_resolveCharEntities(_rsLocalName);
     142                 :            : 
     143                 :      36564 :     return nPos >= 0;
     144                 :            : }
     145                 :            : 
     146                 :            : //----------------------------------------------------------------------------
     147                 :      32546 : OUString extractFirstFromConfigurationPath(OUString const& _sInPath, OUString* _sOutPath)
     148                 :            : {
     149                 :      32546 :     sal_Int32 nSep      = _sInPath.indexOf('/');
     150                 :      32546 :     sal_Int32 nBracket  = _sInPath.indexOf('[');
     151                 :            : 
     152                 :      32546 :     sal_Int32 nStart    = nBracket + 1;
     153                 :      32546 :     sal_Int32 nEnd      = nSep;
     154                 :            : 
     155         [ +  + ]:      32546 :     if (0 <= nBracket) // found a bracket-quoted relative path
     156                 :            :     {
     157 [ +  + ][ +  - ]:       3746 :         if (nSep < 0 || nBracket < nSep) // and the separator comes after it
     158                 :            :         {
     159                 :       3746 :             sal_Unicode chQuote = _sInPath[nStart];
     160 [ #  # ][ -  + ]:       3746 :             if (chQuote == '\'' || chQuote == '\"')
     161                 :            :             {
     162                 :       3746 :                 ++nStart;
     163                 :       3746 :                 nEnd      = _sInPath.indexOf(chQuote, nStart+1);
     164                 :       3746 :                 nBracket  = nEnd+1;
     165                 :            :             }
     166                 :            :             else
     167                 :            :             {
     168                 :          0 :                 nEnd = _sInPath.indexOf(']',nStart);
     169                 :          0 :                 nBracket = nEnd;
     170                 :            :             }
     171                 :            :             OSL_ENSURE(nEnd > nStart && _sInPath[nBracket] == ']', "Invalid config path: improper mismatch of quote or bracket");
     172                 :       3746 :             OSL_ENSURE((nBracket+1 == _sInPath.getLength() && nSep == -1) || (_sInPath[nBracket+1] == '/' && nSep == nBracket+1), "Invalid config path: brackets not followed by slash");
     173                 :            :         }
     174                 :            :         else // ... but our initial element name is in simple form
     175                 :       3746 :             nStart = 0;
     176                 :            :     }
     177                 :            : 
     178         [ +  + ]:      32546 :     OUString sResult = (nEnd >= 0) ? _sInPath.copy(nStart, nEnd-nStart) : _sInPath;
     179         [ +  - ]:      32546 :     lcl_resolveCharEntities(sResult);
     180                 :            : 
     181         [ -  + ]:      32546 :     if (_sOutPath != 0)
     182                 :            :     {
     183         [ #  # ]:          0 :         *_sOutPath = (nSep >= 0) ? _sInPath.copy(nSep + 1) : OUString();
     184                 :            :     }
     185                 :            : 
     186                 :      32546 :     return sResult;
     187                 :            : }
     188                 :            : 
     189                 :            : //----------------------------------------------------------------------------
     190                 :            : 
     191                 :            : // find the position after the prefix in the nested path
     192                 :            : static inline
     193                 :     602392 : sal_Int32 lcl_findPrefixEnd(OUString const& _sNestedPath, OUString const& _sPrefixPath)
     194                 :            : {
     195                 :            :     // TODO: currently handles only exact prefix matches
     196                 :     602392 :     sal_Int32 nPrefixLength = _sPrefixPath.getLength();
     197                 :            : 
     198                 :            :     OSL_ENSURE(nPrefixLength == 0 || _sPrefixPath[nPrefixLength-1] != '/',
     199                 :            :                 "Cannot handle slash-terminated prefix paths");
     200                 :            : 
     201                 :            :     sal_Bool bIsPrefix;
     202         [ +  + ]:     602392 :     if (_sNestedPath.getLength() > nPrefixLength)
     203                 :            :     {
     204                 :     446351 :         bIsPrefix = _sNestedPath[nPrefixLength] == '/' &&
     205 [ +  + ][ +  + ]:     446351 :                     _sNestedPath.compareTo(_sPrefixPath,nPrefixLength) == 0;
     206                 :     446351 :         ++nPrefixLength;
     207                 :            :     }
     208         [ +  + ]:     156041 :     else if (_sNestedPath.getLength() == nPrefixLength)
     209                 :            :     {
     210                 :      40875 :         bIsPrefix = _sNestedPath.equals(_sPrefixPath);
     211                 :            :     }
     212                 :            :     else
     213                 :            :     {
     214                 :     115166 :         bIsPrefix = false;
     215                 :            :     }
     216                 :            : 
     217         [ +  + ]:     602392 :     return bIsPrefix ? nPrefixLength : 0;
     218                 :            : }
     219                 :            : 
     220                 :            : //----------------------------------------------------------------------------
     221                 :     574958 : sal_Bool isPrefixOfConfigurationPath(OUString const& _sNestedPath,
     222                 :            :                                      OUString const& _sPrefixPath)
     223                 :            : {
     224 [ +  + ][ +  + ]:     574958 :     return _sPrefixPath.isEmpty() || lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) != 0;
     225                 :            : }
     226                 :            : 
     227                 :            : //----------------------------------------------------------------------------
     228                 :      30218 : OUString dropPrefixFromConfigurationPath(OUString const& _sNestedPath,
     229                 :            :                                          OUString const& _sPrefixPath)
     230                 :            : {
     231         [ +  - ]:      30218 :     if ( sal_Int32 nPrefixEnd = lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) )
     232                 :            :     {
     233                 :      30218 :         return _sNestedPath.copy(nPrefixEnd);
     234                 :            :     }
     235                 :            :     else
     236                 :            :     {
     237                 :            :         OSL_ENSURE(_sPrefixPath.isEmpty(),  "Path does not start with expected prefix");
     238                 :            : 
     239                 :      30218 :         return _sNestedPath;
     240                 :            :     }
     241                 :            : }
     242                 :            : 
     243                 :            : //----------------------------------------------------------------------------
     244                 :            : static
     245                 :       3710 : OUString lcl_wrapName(const OUString& _sContent, const OUString& _sType)
     246                 :            : {
     247                 :       3710 :     const sal_Unicode * const pBeginContent = _sContent.getStr();
     248                 :       3710 :     const sal_Unicode * const pEndContent   = pBeginContent + _sContent.getLength();
     249                 :            : 
     250                 :            :     OSL_PRECOND(!_sType.isEmpty(), "Unexpected config type name: empty");
     251                 :            :     OSL_PRECOND(pBeginContent <= pEndContent, "Invalid config name: empty");
     252                 :            : 
     253         [ -  + ]:       3710 :     if (pBeginContent == pEndContent)
     254                 :          0 :         return _sType;
     255                 :            : 
     256                 :       3710 :     rtl::OUStringBuffer aNormalized(_sType.getLength() + _sContent.getLength() + 4); // reserve approximate size initially
     257                 :            : 
     258                 :            :     // prefix: type, opening bracket and quote
     259 [ +  - ][ +  - ]:       3710 :     aNormalized.append( _sType ).appendAscii( RTL_CONSTASCII_STRINGPARAM("['") );
     260                 :            : 
     261                 :            :     // content: copy over each char and handle escaping
     262         [ +  + ]:     129465 :     for(const sal_Unicode* pCur = pBeginContent; pCur != pEndContent; ++pCur)
     263                 :            :     {
     264                 :            :         // append (escape if needed)
     265   [ -  -  -  + ]:     125755 :         switch(*pCur)
     266                 :            :         {
     267         [ #  # ]:          0 :         case sal_Unicode('&') : aNormalized.appendAscii( RTL_CONSTASCII_STRINGPARAM("&amp;") ); break;
     268         [ #  # ]:          0 :         case sal_Unicode('\''): aNormalized.appendAscii( RTL_CONSTASCII_STRINGPARAM("&apos;") ); break;
     269         [ #  # ]:          0 :         case sal_Unicode('\"'): aNormalized.appendAscii( RTL_CONSTASCII_STRINGPARAM("&quot;") ); break;
     270                 :            : 
     271         [ +  - ]:     125755 :         default: aNormalized.append( *pCur );
     272                 :            :         }
     273                 :            :     }
     274                 :            : 
     275                 :            :     // suffix: closing quote and bracket
     276         [ +  - ]:       3710 :     aNormalized.appendAscii( RTL_CONSTASCII_STRINGPARAM("']") );
     277                 :            : 
     278         [ +  - ]:       3710 :     return aNormalized.makeStringAndClear();
     279                 :            : }
     280                 :            : 
     281                 :            : //----------------------------------------------------------------------------
     282                 :            : 
     283                 :       3710 : OUString wrapConfigurationElementName(OUString const& _sElementName)
     284                 :            : {
     285         [ +  - ]:       3710 :     return lcl_wrapName(_sElementName, OUString(RTL_CONSTASCII_USTRINGPARAM("*")) );
     286                 :            : }
     287                 :            : 
     288                 :            : //----------------------------------------------------------------------------
     289                 :            : 
     290                 :          0 : OUString wrapConfigurationElementName(OUString const& _sElementName,
     291                 :            :                                       OUString const& _sTypeName)
     292                 :            : {
     293                 :            :     // todo: check that _sTypeName is valid
     294                 :          0 :     return lcl_wrapName(_sElementName, _sTypeName);
     295                 :            : }
     296                 :            : 
     297                 :            : //----------------------------------------------------------------------------
     298                 :            : } // namespace utl
     299                 :            : 
     300                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10