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

Generated by: LCOV version 1.11