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