1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
23 #include <string_view>
25 #include <unotools/configpaths.hxx>
26 #include <rtl/ustring.hxx>
27 #include <rtl/ustrbuf.hxx>
28 #include <osl/diagnose.h>
29 #include <o3tl/string_view.hxx>
35 void lcl_resolveCharEntities(OUString
& aLocalString
)
37 sal_Int32 nEscapePos
=aLocalString
.indexOf('&');
38 if (nEscapePos
< 0) return;
40 OUStringBuffer aResult
;
46 if (aLocalString
.match("&",nEscapePos
))
49 else if (aLocalString
.match("'",nEscapePos
))
52 else if (aLocalString
.match(""",nEscapePos
))
55 OSL_ENSURE(ch
,"Configuration path contains '&' that is not part of a valid character escape");
58 aResult
.append(aLocalString
.subView(nStart
,nEscapePos
-nStart
) + OUStringChar(ch
));
60 sal_Int32 nEscapeEnd
=aLocalString
.indexOf(';',nEscapePos
);
61 nStart
= nEscapeEnd
+1;
62 nEscapePos
=aLocalString
.indexOf('&',nStart
);
66 nEscapePos
=aLocalString
.indexOf('&',nEscapePos
+1);
69 while ( nEscapePos
> 0);
71 aResult
.append(aLocalString
.subView(nStart
));
73 aLocalString
= aResult
.makeStringAndClear();
76 bool splitLastFromConfigurationPath(std::u16string_view _sInPath
,
78 OUString
& _rsLocalName
)
82 size_t nPos
= _sInPath
.size();
84 // for backwards compatibility, strip trailing slash
85 if (nPos
> 1 && _sInPath
[ nPos
- 1 ] == '/')
90 // check for set element ['xxx'] or ["yyy"]
92 if (nPos
> 0 && _sInPath
[ nPos
- 1 ] == ']')
95 if (nPos
< 3) { // expect at least chQuote + chQuote + ']' at _sInPath[nPos-3..nPos-1]
99 sal_Unicode chQuote
= _sInPath
[nPos
];
101 if (chQuote
== '\'' || chQuote
== '\"')
104 nPos
= _sInPath
.rfind(chQuote
,nEnd
- 1);
105 if (nPos
== std::u16string_view::npos
) {
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]
119 nPos
= _sInPath
.rfind('/',nPos
- 2);
120 if (nPos
== std::u16string_view::npos
) {
137 nPos
= _sInPath
.rfind('/',nEnd
- 1);
138 if (nPos
== std::u16string_view::npos
) {
143 assert( nPos
!= std::u16string_view::npos
&&
146 nEnd
<= _sInPath
.size() );
148 assert(_sInPath
[nPos
] == '/');
149 OSL_ENSURE(nPos
!= 0 , "Invalid config child path: immediate child of root");
151 _rsLocalName
= _sInPath
.substr(nStart
, nEnd
-nStart
);
152 _rsOutPath
= (nPos
> 0) ? OUString(_sInPath
.substr(0,nPos
)) : OUString();
154 lcl_resolveCharEntities(_rsLocalName
);
161 _rsLocalName
= _sInPath
;
165 OUString
extractFirstFromConfigurationPath(OUString
const& _sInPath
, OUString
* _sOutPath
)
167 sal_Int32 nSep
= _sInPath
.indexOf('/');
168 sal_Int32 nBracket
= _sInPath
.indexOf('[');
170 sal_Int32 nStart
= nBracket
+ 1;
171 sal_Int32 nEnd
= nSep
;
173 if (0 <= nBracket
) // found a bracket-quoted relative path
175 if (nSep
< 0 || nBracket
< nSep
) // and the separator comes after it
177 sal_Unicode chQuote
= _sInPath
[nStart
];
178 if (chQuote
== '\'' || chQuote
== '\"')
181 nEnd
= _sInPath
.indexOf(chQuote
, nStart
+1);
186 nEnd
= _sInPath
.indexOf(']',nStart
);
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");
192 else // ... but our initial element name is in simple form
196 OUString sResult
= (nEnd
>= 0) ? _sInPath
.copy(nStart
, nEnd
-nStart
) : _sInPath
;
197 lcl_resolveCharEntities(sResult
);
199 if (_sOutPath
!= nullptr)
201 *_sOutPath
= (nSep
>= 0) ? _sInPath
.copy(nSep
+ 1) : OUString();
207 // find the position after the prefix in the nested path
208 static sal_Int32
lcl_findPrefixEnd(std::u16string_view _sNestedPath
, std::u16string_view _sPrefixPath
)
210 // TODO: currently handles only exact prefix matches
211 size_t nPrefixLength
= _sPrefixPath
.size();
213 OSL_ENSURE(nPrefixLength
== 0 || _sPrefixPath
[nPrefixLength
-1] != '/',
214 "Cannot handle slash-terminated prefix paths");
217 if (_sNestedPath
.size() > nPrefixLength
)
219 bIsPrefix
= _sNestedPath
[nPrefixLength
] == '/' &&
220 o3tl::starts_with(_sNestedPath
, _sPrefixPath
);
223 else if (_sNestedPath
.size() == nPrefixLength
)
225 bIsPrefix
= _sNestedPath
== _sPrefixPath
;
232 return bIsPrefix
? nPrefixLength
: 0;
235 bool isPrefixOfConfigurationPath(std::u16string_view _sNestedPath
,
236 std::u16string_view _sPrefixPath
)
238 return _sPrefixPath
.empty() || lcl_findPrefixEnd(_sNestedPath
,_sPrefixPath
) != 0;
241 OUString
dropPrefixFromConfigurationPath(OUString
const& _sNestedPath
,
242 std::u16string_view _sPrefixPath
)
244 if ( sal_Int32 nPrefixEnd
= lcl_findPrefixEnd(_sNestedPath
,_sPrefixPath
) )
246 return _sNestedPath
.copy(nPrefixEnd
);
250 OSL_ENSURE(_sPrefixPath
.empty(), "Path does not start with expected prefix");
257 OUString
lcl_wrapName(std::u16string_view _sContent
, const OUString
& _sType
)
259 const sal_Unicode
* const pBeginContent
= _sContent
.data();
260 const sal_Unicode
* const pEndContent
= pBeginContent
+ _sContent
.size();
262 OSL_PRECOND(!_sType
.isEmpty(), "Unexpected config type name: empty");
263 OSL_PRECOND(pBeginContent
<= pEndContent
, "Invalid config name: empty");
265 if (pBeginContent
== pEndContent
)
268 OUStringBuffer
aNormalized(_sType
.getLength() + _sContent
.size() + 4); // reserve approximate size initially
270 // prefix: type, opening bracket and quote
271 aNormalized
.append( _sType
+ "['" );
273 // content: copy over each char and handle escaping
274 for(const sal_Unicode
* pCur
= pBeginContent
; pCur
!= pEndContent
; ++pCur
)
276 // append (escape if needed)
279 case u
'&' : aNormalized
.append( "&" ); break;
280 case u
'\'': aNormalized
.append( "'" ); break;
281 case u
'\"': aNormalized
.append( """ ); break;
283 default: aNormalized
.append( *pCur
);
287 // suffix: closing quote and bracket
288 aNormalized
.append( "']" );
290 return aNormalized
.makeStringAndClear();
293 OUString
wrapConfigurationElementName(std::u16string_view _sElementName
)
295 return lcl_wrapName(_sElementName
, "*" );
298 OUString
wrapConfigurationElementName(std::u16string_view _sElementName
,
299 OUString
const& _sTypeName
)
301 // todo: check that _sTypeName is valid
302 return lcl_wrapName(_sElementName
, _sTypeName
);
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */