Updated core
[LibreOffice.git] / unotools / source / config / configpaths.cxx
blob7ff97d403f60c95cb9fb5e84bac5794c1159e3ea
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 .
21 #include "unotools/configpaths.hxx"
22 #include <rtl/ustring.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include <osl/diagnose.h>
26 //----------------------------------------------------------------------------
27 namespace utl
29 //----------------------------------------------------------------------------
32 //----------------------------------------------------------------------------
34 static
35 void lcl_resolveCharEntities(OUString & aLocalString)
37 sal_Int32 nEscapePos=aLocalString.indexOf('&');
38 if (nEscapePos < 0) return;
40 OUStringBuffer aResult;
41 sal_Int32 nStart = 0;
45 sal_Unicode ch = 0;
46 if (aLocalString.match("&amp;",nEscapePos))
47 ch = '&';
49 else if (aLocalString.match("&apos;",nEscapePos))
50 ch = '\'';
52 else if (aLocalString.match("&quot;",nEscapePos))
53 ch = '"';
55 OSL_ENSURE(ch,"Configuration path contains '&' that is not part of a valid character escape");
56 if (ch)
58 aResult.append(aLocalString.copy(nStart,nEscapePos-nStart)).append(ch);
60 sal_Int32 nEscapeEnd=aLocalString.indexOf(';',nEscapePos);
61 nStart = nEscapeEnd+1;
62 nEscapePos=aLocalString.indexOf('&',nStart);
64 else
66 nEscapePos=aLocalString.indexOf('&',nEscapePos+1);
69 while ( nEscapePos > 0);
71 aResult.append(aLocalString.copy(nStart));
73 aLocalString = aResult.makeStringAndClear();
76 //----------------------------------------------------------------------------
77 sal_Bool splitLastFromConfigurationPath(OUString const& _sInPath,
78 OUString& _rsOutPath,
79 OUString& _rsLocalName)
81 sal_Int32 nStart,nEnd;
83 sal_Int32 nPos = _sInPath.getLength()-1;
85 // strip trailing slash
86 if (nPos > 0 && _sInPath[ nPos ] == sal_Unicode('/'))
88 OSL_FAIL("Invalid config path: trailing '/' is not allowed");
89 --nPos;
92 // check for predicate ['xxx'] or ["yyy"]
93 if (nPos > 0 && _sInPath[ nPos ] == sal_Unicode(']'))
95 sal_Unicode chQuote = _sInPath[--nPos];
97 if (chQuote == '\'' || chQuote == '\"')
99 nEnd = nPos;
100 nPos = _sInPath.lastIndexOf(chQuote,nEnd);
101 nStart = nPos + 1;
102 --nPos; // nPos = rInPath.lastIndexOf('[',nPos);
104 else // allow [xxx]
106 nEnd = nPos + 1;
107 nPos = _sInPath.lastIndexOf('[',nEnd);
108 nStart = nPos + 1;
111 OSL_ENSURE(nPos >= 0 && _sInPath[nPos] == '[', "Invalid config path: unmatched quotes or brackets");
112 if (nPos >= 0 && _sInPath[nPos] == '[')
114 nPos = _sInPath.lastIndexOf('/',nPos);
116 else // defined behavior for invalid paths
118 nStart = 0, nEnd = _sInPath.getLength();
119 nPos = -1;
123 else
125 nEnd = nPos+1;
126 nPos = _sInPath.lastIndexOf('/',nEnd);
127 nStart = nPos + 1;
129 OSL_ASSERT( -1 <= nPos &&
130 nPos < nStart &&
131 nStart < nEnd &&
132 nEnd <= _sInPath.getLength() );
134 OSL_ASSERT(nPos == -1 || _sInPath[nPos] == '/');
135 OSL_ENSURE(nPos != 0 , "Invalid config child path: immediate child of root");
137 _rsLocalName = _sInPath.copy(nStart, nEnd-nStart);
138 _rsOutPath = (nPos > 0) ? _sInPath.copy(0,nPos) : OUString();
139 lcl_resolveCharEntities(_rsLocalName);
141 return nPos >= 0;
144 //----------------------------------------------------------------------------
145 OUString extractFirstFromConfigurationPath(OUString const& _sInPath, OUString* _sOutPath)
147 sal_Int32 nSep = _sInPath.indexOf('/');
148 sal_Int32 nBracket = _sInPath.indexOf('[');
150 sal_Int32 nStart = nBracket + 1;
151 sal_Int32 nEnd = nSep;
153 if (0 <= nBracket) // found a bracket-quoted relative path
155 if (nSep < 0 || nBracket < nSep) // and the separator comes after it
157 sal_Unicode chQuote = _sInPath[nStart];
158 if (chQuote == '\'' || chQuote == '\"')
160 ++nStart;
161 nEnd = _sInPath.indexOf(chQuote, nStart+1);
162 nBracket = nEnd+1;
164 else
166 nEnd = _sInPath.indexOf(']',nStart);
167 nBracket = nEnd;
169 OSL_ENSURE(nEnd > nStart && _sInPath[nBracket] == ']', "Invalid config path: improper mismatch of quote or bracket");
170 OSL_ENSURE((nBracket+1 == _sInPath.getLength() && nSep == -1) || (_sInPath[nBracket+1] == '/' && nSep == nBracket+1), "Invalid config path: brackets not followed by slash");
172 else // ... but our initial element name is in simple form
173 nStart = 0;
176 OUString sResult = (nEnd >= 0) ? _sInPath.copy(nStart, nEnd-nStart) : _sInPath;
177 lcl_resolveCharEntities(sResult);
179 if (_sOutPath != 0)
181 *_sOutPath = (nSep >= 0) ? _sInPath.copy(nSep + 1) : OUString();
184 return sResult;
187 //----------------------------------------------------------------------------
189 // find the position after the prefix in the nested path
190 static inline
191 sal_Int32 lcl_findPrefixEnd(OUString const& _sNestedPath, OUString const& _sPrefixPath)
193 // TODO: currently handles only exact prefix matches
194 sal_Int32 nPrefixLength = _sPrefixPath.getLength();
196 OSL_ENSURE(nPrefixLength == 0 || _sPrefixPath[nPrefixLength-1] != '/',
197 "Cannot handle slash-terminated prefix paths");
199 sal_Bool bIsPrefix;
200 if (_sNestedPath.getLength() > nPrefixLength)
202 bIsPrefix = _sNestedPath[nPrefixLength] == '/' &&
203 _sNestedPath.compareTo(_sPrefixPath,nPrefixLength) == 0;
204 ++nPrefixLength;
206 else if (_sNestedPath.getLength() == nPrefixLength)
208 bIsPrefix = _sNestedPath.equals(_sPrefixPath);
210 else
212 bIsPrefix = false;
215 return bIsPrefix ? nPrefixLength : 0;
218 //----------------------------------------------------------------------------
219 sal_Bool isPrefixOfConfigurationPath(OUString const& _sNestedPath,
220 OUString const& _sPrefixPath)
222 return _sPrefixPath.isEmpty() || lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) != 0;
225 //----------------------------------------------------------------------------
226 OUString dropPrefixFromConfigurationPath(OUString const& _sNestedPath,
227 OUString const& _sPrefixPath)
229 if ( sal_Int32 nPrefixEnd = lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) )
231 return _sNestedPath.copy(nPrefixEnd);
233 else
235 OSL_ENSURE(_sPrefixPath.isEmpty(), "Path does not start with expected prefix");
237 return _sNestedPath;
241 //----------------------------------------------------------------------------
242 static
243 OUString lcl_wrapName(const OUString& _sContent, const OUString& _sType)
245 const sal_Unicode * const pBeginContent = _sContent.getStr();
246 const sal_Unicode * const pEndContent = pBeginContent + _sContent.getLength();
248 OSL_PRECOND(!_sType.isEmpty(), "Unexpected config type name: empty");
249 OSL_PRECOND(pBeginContent <= pEndContent, "Invalid config name: empty");
251 if (pBeginContent == pEndContent)
252 return _sType;
254 OUStringBuffer aNormalized(_sType.getLength() + _sContent.getLength() + 4); // reserve approximate size initially
256 // prefix: type, opening bracket and quote
257 aNormalized.append( _sType ).append( "['" );
259 // content: copy over each char and handle escaping
260 for(const sal_Unicode* pCur = pBeginContent; pCur != pEndContent; ++pCur)
262 // append (escape if needed)
263 switch(*pCur)
265 case sal_Unicode('&') : aNormalized.append( "&amp;" ); break;
266 case sal_Unicode('\''): aNormalized.append( "&apos;" ); break;
267 case sal_Unicode('\"'): aNormalized.append( "&quot;" ); break;
269 default: aNormalized.append( *pCur );
273 // suffix: closing quote and bracket
274 aNormalized.append( "']" );
276 return aNormalized.makeStringAndClear();
279 //----------------------------------------------------------------------------
281 OUString wrapConfigurationElementName(OUString const& _sElementName)
283 return lcl_wrapName(_sElementName, "*" );
286 //----------------------------------------------------------------------------
288 OUString wrapConfigurationElementName(OUString const& _sElementName,
289 OUString const& _sTypeName)
291 // todo: check that _sTypeName is valid
292 return lcl_wrapName(_sElementName, _sTypeName);
295 //----------------------------------------------------------------------------
296 } // namespace utl
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */