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