LCOV - code coverage report
Current view: top level - unotools/source/config - configpaths.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 59 102 57.8 %
Date: 2014-11-03 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       68242 : void lcl_resolveCharEntities(OUString & aLocalString)
      30             : {
      31       68242 :     sal_Int32 nEscapePos=aLocalString.indexOf('&');
      32      136484 :     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       51414 : bool splitLastFromConfigurationPath(OUString const& _sInPath,
      71             :                                         OUString& _rsOutPath,
      72             :                                         OUString& _rsLocalName)
      73             : {
      74             :     sal_Int32 nStart,nEnd;
      75             : 
      76       51414 :     sal_Int32 nPos = _sInPath.getLength()-1;
      77             : 
      78             :     // strip trailing slash
      79       51414 :     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       51414 :     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       51414 :         nEnd = nPos+1;
     119       51414 :         nPos = _sInPath.lastIndexOf('/',nEnd);
     120       51414 :         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       51414 :     _rsLocalName = _sInPath.copy(nStart, nEnd-nStart);
     131       51414 :     _rsOutPath = (nPos > 0) ? _sInPath.copy(0,nPos) : OUString();
     132       51414 :     lcl_resolveCharEntities(_rsLocalName);
     133             : 
     134       51414 :     return nPos >= 0;
     135             : }
     136             : 
     137       16828 : OUString extractFirstFromConfigurationPath(OUString const& _sInPath, OUString* _sOutPath)
     138             : {
     139       16828 :     sal_Int32 nSep      = _sInPath.indexOf('/');
     140       16828 :     sal_Int32 nBracket  = _sInPath.indexOf('[');
     141             : 
     142       16828 :     sal_Int32 nStart    = nBracket + 1;
     143       16828 :     sal_Int32 nEnd      = nSep;
     144             : 
     145       16828 :     if (0 <= nBracket) // found a bracket-quoted relative path
     146             :     {
     147        4508 :         if (nSep < 0 || nBracket < nSep) // and the separator comes after it
     148             :         {
     149        4508 :             sal_Unicode chQuote = _sInPath[nStart];
     150        4508 :             if (chQuote == '\'' || chQuote == '\"')
     151             :             {
     152        4508 :                 ++nStart;
     153        4508 :                 nEnd      = _sInPath.indexOf(chQuote, nStart+1);
     154        4508 :                 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        4508 :             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       16828 :     OUString sResult = (nEnd >= 0) ? _sInPath.copy(nStart, nEnd-nStart) : _sInPath;
     169       16828 :     lcl_resolveCharEntities(sResult);
     170             : 
     171       16828 :     if (_sOutPath != 0)
     172             :     {
     173           0 :         *_sOutPath = (nSep >= 0) ? _sInPath.copy(nSep + 1) : OUString();
     174             :     }
     175             : 
     176       16828 :     return sResult;
     177             : }
     178             : 
     179             : // find the position after the prefix in the nested path
     180             : static inline
     181      428890 : sal_Int32 lcl_findPrefixEnd(OUString const& _sNestedPath, OUString const& _sPrefixPath)
     182             : {
     183             :     // TODO: currently handles only exact prefix matches
     184      428890 :     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      428890 :     if (_sNestedPath.getLength() > nPrefixLength)
     191             :     {
     192      340096 :         bIsPrefix = _sNestedPath[nPrefixLength] == '/' &&
     193      340096 :                     _sNestedPath.startsWith(_sPrefixPath);
     194      314954 :         ++nPrefixLength;
     195             :     }
     196      113936 :     else if (_sNestedPath.getLength() == nPrefixLength)
     197             :     {
     198       41902 :         bIsPrefix = _sNestedPath.equals(_sPrefixPath);
     199             :     }
     200             :     else
     201             :     {
     202       72034 :         bIsPrefix = false;
     203             :     }
     204             : 
     205      428890 :     return bIsPrefix ? nPrefixLength : 0;
     206             : }
     207             : 
     208      417936 : bool isPrefixOfConfigurationPath(OUString const& _sNestedPath,
     209             :                                      OUString const& _sPrefixPath)
     210             : {
     211      417936 :     return _sPrefixPath.isEmpty() || lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) != 0;
     212             : }
     213             : 
     214       13738 : OUString dropPrefixFromConfigurationPath(OUString const& _sNestedPath,
     215             :                                          OUString const& _sPrefixPath)
     216             : {
     217       13738 :     if ( sal_Int32 nPrefixEnd = lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) )
     218             :     {
     219       13738 :         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        4656 : OUString lcl_wrapName(const OUString& _sContent, const OUString& _sType)
     231             : {
     232        4656 :     const sal_Unicode * const pBeginContent = _sContent.getStr();
     233        4656 :     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        4656 :     if (pBeginContent == pEndContent)
     239           0 :         return _sType;
     240             : 
     241        4656 :     OUStringBuffer aNormalized(_sType.getLength() + _sContent.getLength() + 4); // reserve approximate size initially
     242             : 
     243             :     // prefix: type, opening bracket and quote
     244        4656 :     aNormalized.append( _sType ).append( "['" );
     245             : 
     246             :     // content: copy over each char and handle escaping
     247      183475 :     for(const sal_Unicode* pCur = pBeginContent; pCur != pEndContent; ++pCur)
     248             :     {
     249             :         // append (escape if needed)
     250      178819 :         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      178819 :         default: aNormalized.append( *pCur );
     257             :         }
     258             :     }
     259             : 
     260             :     // suffix: closing quote and bracket
     261        4656 :     aNormalized.append( "']" );
     262             : 
     263        4656 :     return aNormalized.makeStringAndClear();
     264             : }
     265             : 
     266        4656 : OUString wrapConfigurationElementName(OUString const& _sElementName)
     267             : {
     268        4656 :     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.10