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>
22 #include <string_view>
24 #include <unotools/configpaths.hxx>
25 #include <rtl/ustring.hxx>
26 #include <rtl/ustrbuf.hxx>
27 #include <osl/diagnose.h>
33 void lcl_resolveCharEntities(OUString
& aLocalString
)
35 sal_Int32 nEscapePos
=aLocalString
.indexOf('&');
36 if (nEscapePos
< 0) return;
38 OUStringBuffer aResult
;
44 if (aLocalString
.match("&",nEscapePos
))
47 else if (aLocalString
.match("'",nEscapePos
))
50 else if (aLocalString
.match(""",nEscapePos
))
53 OSL_ENSURE(ch
,"Configuration path contains '&' that is not part of a valid character escape");
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
);
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
,
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");
89 // check for predicate ['xxx'] or ["yyy"]
90 if (nPos
> 0 && _sInPath
[ nPos
] == ']')
92 sal_Unicode chQuote
= _sInPath
[--nPos
];
94 if (chQuote
== '\'' || chQuote
== '\"')
97 nPos
= _sInPath
.lastIndexOf(chQuote
,nEnd
);
99 --nPos
; // nPos = rInPath.lastIndexOf('[',nPos);
104 nPos
= _sInPath
.lastIndexOf('[',nEnd
);
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
116 nEnd
= _sInPath
.getLength();
124 nPos
= _sInPath
.lastIndexOf('/',nEnd
);
127 OSL_ASSERT( -1 <= nPos
&&
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
);
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
== '\"')
158 nEnd
= _sInPath
.indexOf(chQuote
, nStart
+1);
163 nEnd
= _sInPath
.indexOf(']',nStart
);
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
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();
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");
194 if (_sNestedPath
.getLength() > nPrefixLength
)
196 bIsPrefix
= _sNestedPath
[nPrefixLength
] == '/' &&
197 _sNestedPath
.startsWith(_sPrefixPath
);
200 else if (_sNestedPath
.getLength() == nPrefixLength
)
202 bIsPrefix
= _sNestedPath
== _sPrefixPath
;
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
);
227 OSL_ENSURE(_sPrefixPath
.isEmpty(), "Path does not start with expected prefix");
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
)
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)
256 case u
'&' : aNormalized
.append( "&" ); break;
257 case u
'\'': aNormalized
.append( "'" ); break;
258 case u
'\"': aNormalized
.append( """ ); 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
);
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */