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