Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / source / config / configpaths.cxx
blob81d8cb828c6e34d5b5c119cdec5cbbb0a77239e4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <string_view>
24 #include <unotools/configpaths.hxx>
25 #include <rtl/ustring.hxx>
26 #include <rtl/ustrbuf.hxx>
27 #include <osl/diagnose.h>
29 namespace utl
32 static
33 void lcl_resolveCharEntities(OUString & aLocalString)
35 sal_Int32 nEscapePos=aLocalString.indexOf('&');
36 if (nEscapePos < 0) return;
38 OUStringBuffer aResult;
39 sal_Int32 nStart = 0;
43 sal_Unicode ch = 0;
44 if (aLocalString.match("&amp;",nEscapePos))
45 ch = '&';
47 else if (aLocalString.match("&apos;",nEscapePos))
48 ch = '\'';
50 else if (aLocalString.match("&quot;",nEscapePos))
51 ch = '"';
53 OSL_ENSURE(ch,"Configuration path contains '&' that is not part of a valid character escape");
54 if (ch)
56 aResult.append(std::u16string_view(aLocalString).substr(nStart,nEscapePos-nStart)).append(ch);
58 sal_Int32 nEscapeEnd=aLocalString.indexOf(';',nEscapePos);
59 nStart = nEscapeEnd+1;
60 nEscapePos=aLocalString.indexOf('&',nStart);
62 else
64 nEscapePos=aLocalString.indexOf('&',nEscapePos+1);
67 while ( nEscapePos > 0);
69 aResult.append(std::u16string_view(aLocalString).substr(nStart));
71 aLocalString = aResult.makeStringAndClear();
74 bool splitLastFromConfigurationPath(OUString const& _sInPath,
75 OUString& _rsOutPath,
76 OUString& _rsLocalName)
78 sal_Int32 nStart,nEnd;
80 sal_Int32 nPos = _sInPath.getLength()-1;
82 // strip trailing slash
83 if (nPos > 0 && _sInPath[ nPos ] == '/')
85 OSL_FAIL("Invalid config path: trailing '/' is not allowed");
86 --nPos;
89 // check for predicate ['xxx'] or ["yyy"]
90 if (nPos > 0 && _sInPath[ nPos ] == ']')
92 sal_Unicode chQuote = _sInPath[--nPos];
94 if (chQuote == '\'' || chQuote == '\"')
96 nEnd = nPos;
97 nPos = _sInPath.lastIndexOf(chQuote,nEnd);
98 nStart = nPos + 1;
99 --nPos; // nPos = rInPath.lastIndexOf('[',nPos);
101 else // allow [xxx]
103 nEnd = nPos + 1;
104 nPos = _sInPath.lastIndexOf('[',nEnd);
105 nStart = nPos + 1;
108 OSL_ENSURE(nPos >= 0 && _sInPath[nPos] == '[', "Invalid config path: unmatched quotes or brackets");
109 if (nPos >= 0 && _sInPath[nPos] == '[')
111 nPos = _sInPath.lastIndexOf('/',nPos);
113 else // defined behavior for invalid paths
115 nStart = 0;
116 nEnd = _sInPath.getLength();
117 nPos = -1;
121 else
123 nEnd = nPos+1;
124 nPos = _sInPath.lastIndexOf('/',nEnd);
125 nStart = nPos + 1;
127 OSL_ASSERT( -1 <= nPos &&
128 nPos < nStart &&
129 nStart < nEnd &&
130 nEnd <= _sInPath.getLength() );
132 OSL_ASSERT(nPos == -1 || _sInPath[nPos] == '/');
133 OSL_ENSURE(nPos != 0 , "Invalid config child path: immediate child of root");
135 _rsLocalName = _sInPath.copy(nStart, nEnd-nStart);
136 _rsOutPath = (nPos > 0) ? _sInPath.copy(0,nPos) : OUString();
137 lcl_resolveCharEntities(_rsLocalName);
139 return nPos >= 0;
142 OUString extractFirstFromConfigurationPath(OUString const& _sInPath, OUString* _sOutPath)
144 sal_Int32 nSep = _sInPath.indexOf('/');
145 sal_Int32 nBracket = _sInPath.indexOf('[');
147 sal_Int32 nStart = nBracket + 1;
148 sal_Int32 nEnd = nSep;
150 if (0 <= nBracket) // found a bracket-quoted relative path
152 if (nSep < 0 || nBracket < nSep) // and the separator comes after it
154 sal_Unicode chQuote = _sInPath[nStart];
155 if (chQuote == '\'' || chQuote == '\"')
157 ++nStart;
158 nEnd = _sInPath.indexOf(chQuote, nStart+1);
159 nBracket = nEnd+1;
161 else
163 nEnd = _sInPath.indexOf(']',nStart);
164 nBracket = nEnd;
166 OSL_ENSURE(nEnd > nStart && _sInPath[nBracket] == ']', "Invalid config path: improper mismatch of quote or bracket");
167 OSL_ENSURE((nBracket+1 == _sInPath.getLength() && nSep == -1) || (_sInPath[nBracket+1] == '/' && nSep == nBracket+1), "Invalid config path: brackets not followed by slash");
169 else // ... but our initial element name is in simple form
170 nStart = 0;
173 OUString sResult = (nEnd >= 0) ? _sInPath.copy(nStart, nEnd-nStart) : _sInPath;
174 lcl_resolveCharEntities(sResult);
176 if (_sOutPath != nullptr)
178 *_sOutPath = (nSep >= 0) ? _sInPath.copy(nSep + 1) : OUString();
181 return sResult;
184 // find the position after the prefix in the nested path
185 static sal_Int32 lcl_findPrefixEnd(OUString const& _sNestedPath, OUString const& _sPrefixPath)
187 // TODO: currently handles only exact prefix matches
188 sal_Int32 nPrefixLength = _sPrefixPath.getLength();
190 OSL_ENSURE(nPrefixLength == 0 || _sPrefixPath[nPrefixLength-1] != '/',
191 "Cannot handle slash-terminated prefix paths");
193 bool bIsPrefix;
194 if (_sNestedPath.getLength() > nPrefixLength)
196 bIsPrefix = _sNestedPath[nPrefixLength] == '/' &&
197 _sNestedPath.startsWith(_sPrefixPath);
198 ++nPrefixLength;
200 else if (_sNestedPath.getLength() == nPrefixLength)
202 bIsPrefix = _sNestedPath == _sPrefixPath;
204 else
206 bIsPrefix = false;
209 return bIsPrefix ? nPrefixLength : 0;
212 bool isPrefixOfConfigurationPath(OUString const& _sNestedPath,
213 OUString const& _sPrefixPath)
215 return _sPrefixPath.isEmpty() || lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) != 0;
218 OUString dropPrefixFromConfigurationPath(OUString const& _sNestedPath,
219 OUString const& _sPrefixPath)
221 if ( sal_Int32 nPrefixEnd = lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) )
223 return _sNestedPath.copy(nPrefixEnd);
225 else
227 OSL_ENSURE(_sPrefixPath.isEmpty(), "Path does not start with expected prefix");
229 return _sNestedPath;
233 static
234 OUString lcl_wrapName(const OUString& _sContent, const OUString& _sType)
236 const sal_Unicode * const pBeginContent = _sContent.getStr();
237 const sal_Unicode * const pEndContent = pBeginContent + _sContent.getLength();
239 OSL_PRECOND(!_sType.isEmpty(), "Unexpected config type name: empty");
240 OSL_PRECOND(pBeginContent <= pEndContent, "Invalid config name: empty");
242 if (pBeginContent == pEndContent)
243 return _sType;
245 OUStringBuffer aNormalized(_sType.getLength() + _sContent.getLength() + 4); // reserve approximate size initially
247 // prefix: type, opening bracket and quote
248 aNormalized.append( _sType ).append( "['" );
250 // content: copy over each char and handle escaping
251 for(const sal_Unicode* pCur = pBeginContent; pCur != pEndContent; ++pCur)
253 // append (escape if needed)
254 switch(*pCur)
256 case u'&' : aNormalized.append( "&amp;" ); break;
257 case u'\'': aNormalized.append( "&apos;" ); break;
258 case u'\"': aNormalized.append( "&quot;" ); break;
260 default: aNormalized.append( *pCur );
264 // suffix: closing quote and bracket
265 aNormalized.append( "']" );
267 return aNormalized.makeStringAndClear();
270 OUString wrapConfigurationElementName(OUString const& _sElementName)
272 return lcl_wrapName(_sElementName, "*" );
275 OUString wrapConfigurationElementName(OUString const& _sElementName,
276 OUString const& _sTypeName)
278 // todo: check that _sTypeName is valid
279 return lcl_wrapName(_sElementName, _sTypeName);
282 } // namespace utl
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */