1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_unotools.hxx"
26 
27 #include "unotools/configpathes.hxx"
28 #include <rtl/ustring.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <osl/diagnose.h>
31 
32 //----------------------------------------------------------------------------
33 namespace utl
34 {
35 //----------------------------------------------------------------------------
36 
37     using ::rtl::OUString;
38     using ::rtl::OUStringBuffer;
39 
40 //----------------------------------------------------------------------------
41 
42 static
lcl_resolveCharEntities(OUString & aLocalString)43 void lcl_resolveCharEntities(OUString & aLocalString)
44 {
45     sal_Int32 nEscapePos=aLocalString.indexOf('&');
46     if (nEscapePos < 0) return;
47 
48     OUStringBuffer aResult;
49     sal_Int32 nStart = 0;
50 
51     do
52     {
53         sal_Unicode ch = 0;
54         if (aLocalString.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("&amp;"),nEscapePos))
55             ch = '&';
56 
57         else if (aLocalString.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("&apos;"),nEscapePos))
58             ch = '\'';
59 
60         else if (aLocalString.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("&quot;"),nEscapePos))
61             ch = '"';
62 
63         OSL_ENSURE(ch,"Configuration path contains '&' that is not part of a valid character escape");
64         if (ch)
65         {
66             aResult.append(aLocalString.copy(nStart,nEscapePos-nStart)).append(ch);
67 
68             sal_Int32 nEscapeEnd=aLocalString.indexOf(';',nEscapePos);
69             nStart = nEscapeEnd+1;
70             nEscapePos=aLocalString.indexOf('&',nStart);
71         }
72         else
73         {
74             nEscapePos=aLocalString.indexOf('&',nEscapePos+1);
75         }
76     }
77     while ( nEscapePos > 0);
78 
79     aResult.append(aLocalString.copy(nStart));
80 
81     aLocalString = aResult.makeStringAndClear();
82 }
83 
84 //----------------------------------------------------------------------------
splitLastFromConfigurationPath(OUString const & _sInPath,OUString & _rsOutPath,OUString & _rsLocalName)85 sal_Bool splitLastFromConfigurationPath(OUString const& _sInPath,
86                                         OUString& _rsOutPath,
87                                         OUString& _rsLocalName)
88 {
89     sal_Int32 nStart,nEnd;
90 
91     sal_Int32 nPos = _sInPath.getLength()-1;
92 
93     // strip trailing slash
94     if (nPos > 0 && _sInPath[ nPos ] == sal_Unicode('/'))
95     {
96         OSL_ENSURE(false, "Invalid config path: trailing '/' is not allowed");
97         --nPos;
98     }
99 
100     // check for predicate ['xxx'] or ["yyy"]
101     if (nPos  > 0 && _sInPath[ nPos ] == sal_Unicode(']'))
102     {
103         sal_Unicode chQuote = _sInPath[--nPos];
104 
105         if (chQuote == '\'' || chQuote == '\"')
106         {
107             nEnd = nPos;
108             nPos = _sInPath.lastIndexOf(chQuote,nEnd);
109             nStart = nPos + 1;
110             --nPos; // nPos = rInPath.lastIndexOf('[',nPos);
111         }
112         else // allow [xxx]
113         {
114             nEnd = nPos + 1;
115             nPos = _sInPath.lastIndexOf('[',nEnd);
116             nStart = nPos + 1;
117         }
118 
119         OSL_ENSURE(nPos >= 0 && _sInPath[nPos] == '[', "Invalid config path: unmatched quotes or brackets");
120         if (nPos >= 0 && _sInPath[nPos] == '[')
121         {
122             nPos =  _sInPath.lastIndexOf('/',nPos);
123         }
124         else // defined behavior for invalid pathes
125         {
126             nStart = 0, nEnd = _sInPath.getLength();
127             nPos = -1;
128         }
129 
130     }
131     else
132     {
133         nEnd = nPos+1;
134         nPos = _sInPath.lastIndexOf('/',nEnd);
135         nStart = nPos + 1;
136     }
137     OSL_ASSERT( -1 <= nPos &&
138                 nPos < nStart &&
139                 nStart < nEnd &&
140                 nEnd <= _sInPath.getLength() );
141 
142     OSL_ASSERT(nPos == -1 || _sInPath[nPos] == '/');
143     OSL_ENSURE(nPos != 0 , "Invalid config child path: immediate child of root");
144 
145     _rsLocalName = _sInPath.copy(nStart, nEnd-nStart);
146     _rsOutPath = (nPos > 0) ? _sInPath.copy(0,nPos) : OUString();
147     lcl_resolveCharEntities(_rsLocalName);
148 
149     return nPos >= 0;
150 }
151 
152 //----------------------------------------------------------------------------
extractFirstFromConfigurationPath(OUString const & _sInPath,OUString * _sOutPath)153 OUString extractFirstFromConfigurationPath(OUString const& _sInPath, OUString* _sOutPath)
154 {
155     sal_Int32 nSep      = _sInPath.indexOf('/');
156     sal_Int32 nBracket  = _sInPath.indexOf('[');
157 
158     sal_Int32 nStart    = nBracket + 1;
159     sal_Int32 nEnd      = nSep;
160 
161     if (0 <= nBracket) // found a bracket-quoted relative path
162     {
163         if (nSep < 0 || nBracket < nSep) // and the separator comes after it
164         {
165             sal_Unicode chQuote = _sInPath[nStart];
166             if (chQuote == '\'' || chQuote == '\"')
167             {
168                 ++nStart;
169                 nEnd      = _sInPath.indexOf(chQuote, nStart+1);
170                 nBracket  = nEnd+1;
171             }
172             else
173             {
174                 nEnd = _sInPath.indexOf(']',nStart);
175                 nBracket = nEnd;
176             }
177             OSL_ENSURE(nEnd > nStart && _sInPath[nBracket] == ']', "Invalid config path: improper mismatch of quote or bracket");
178             OSL_ENSURE((nBracket+1 == _sInPath.getLength() && nSep == -1) || (_sInPath[nBracket+1] == '/' && nSep == nBracket+1), "Invalid config path: brackets not followed by slash");
179         }
180         else // ... but our initial element name is in simple form
181             nStart = 0;
182     }
183 
184     OUString sResult = (nEnd >= 0) ? _sInPath.copy(nStart, nEnd-nStart) : _sInPath;
185     lcl_resolveCharEntities(sResult);
186 
187     if (_sOutPath != 0)
188     {
189         *_sOutPath = (nSep >= 0) ? _sInPath.copy(nSep + 1) : OUString();
190     }
191 
192     return sResult;
193 }
194 
195 //----------------------------------------------------------------------------
196 
197 // find the position after the prefix in the nested path
198 static inline
lcl_findPrefixEnd(OUString const & _sNestedPath,OUString const & _sPrefixPath)199 sal_Int32 lcl_findPrefixEnd(OUString const& _sNestedPath, OUString const& _sPrefixPath)
200 {
201     // TODO: currently handles only exact prefix matches
202     sal_Int32 nPrefixLength = _sPrefixPath.getLength();
203 
204     OSL_ENSURE(nPrefixLength == 0 || _sPrefixPath[nPrefixLength-1] != '/',
205                 "Cannot handle slash-terminated prefix pathes");
206 
207     sal_Bool bIsPrefix;
208     if (_sNestedPath.getLength() > nPrefixLength)
209     {
210         bIsPrefix = _sNestedPath[nPrefixLength] == '/' &&
211                     _sNestedPath.compareTo(_sPrefixPath,nPrefixLength) == 0;
212         ++nPrefixLength;
213     }
214     else if (_sNestedPath.getLength() == nPrefixLength)
215     {
216         bIsPrefix = _sNestedPath.equals(_sPrefixPath);
217     }
218     else
219     {
220         bIsPrefix = false;
221     }
222 
223     return bIsPrefix ? nPrefixLength : 0;
224 }
225 
226 //----------------------------------------------------------------------------
isPrefixOfConfigurationPath(OUString const & _sNestedPath,OUString const & _sPrefixPath)227 sal_Bool isPrefixOfConfigurationPath(OUString const& _sNestedPath,
228                                      OUString const& _sPrefixPath)
229 {
230     return _sPrefixPath.getLength() == 0 || lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) != 0;
231 }
232 
233 //----------------------------------------------------------------------------
dropPrefixFromConfigurationPath(OUString const & _sNestedPath,OUString const & _sPrefixPath)234 OUString dropPrefixFromConfigurationPath(OUString const& _sNestedPath,
235                                          OUString const& _sPrefixPath)
236 {
237     if ( sal_Int32 nPrefixEnd = lcl_findPrefixEnd(_sNestedPath,_sPrefixPath) )
238     {
239 	    return _sNestedPath.copy(nPrefixEnd);
240     }
241     else
242     {
243         OSL_ENSURE(_sPrefixPath.getLength() == 0,  "Path does not start with expected prefix");
244 
245         return _sNestedPath;
246     }
247 }
248 
249 //----------------------------------------------------------------------------
250 static
lcl_wrapName(const OUString & _sContent,const OUString & _sType)251 OUString lcl_wrapName(const OUString& _sContent, const OUString& _sType)
252 {
253     const sal_Unicode * const pBeginContent = _sContent.getStr();
254     const sal_Unicode * const pEndContent   = pBeginContent + _sContent.getLength();
255 
256     OSL_PRECOND(_sType.getLength(), "Unexpected config type name: empty");
257     OSL_PRECOND(pBeginContent <= pEndContent, "Invalid config name: empty");
258 
259     if (pBeginContent == pEndContent)
260         return _sType;
261 
262     rtl::OUStringBuffer aNormalized(_sType.getLength() + _sContent.getLength() + 4); // reserve approximate size initially
263 
264     // prefix: type, opening bracket and quote
265     aNormalized.append( _sType ).appendAscii( RTL_CONSTASCII_STRINGPARAM("['") );
266 
267     // content: copy over each char and handle escaping
268     for(const sal_Unicode* pCur = pBeginContent; pCur != pEndContent; ++pCur)
269     {
270         // append (escape if needed)
271         switch(*pCur)
272         {
273         case sal_Unicode('&') : aNormalized.appendAscii( RTL_CONSTASCII_STRINGPARAM("&amp;") ); break;
274         case sal_Unicode('\''): aNormalized.appendAscii( RTL_CONSTASCII_STRINGPARAM("&apos;") ); break;
275         case sal_Unicode('\"'): aNormalized.appendAscii( RTL_CONSTASCII_STRINGPARAM("&quot;") ); break;
276 
277         default: aNormalized.append( *pCur );
278         }
279     }
280 
281     // suffix: closing quote and bracket
282     aNormalized.appendAscii( RTL_CONSTASCII_STRINGPARAM("']") );
283 
284     return aNormalized.makeStringAndClear();
285 }
286 
287 //----------------------------------------------------------------------------
288 
wrapConfigurationElementName(OUString const & _sElementName)289 OUString wrapConfigurationElementName(OUString const& _sElementName)
290 {
291     return lcl_wrapName(_sElementName, OUString(RTL_CONSTASCII_USTRINGPARAM("*")) );
292 }
293 
294 //----------------------------------------------------------------------------
295 
wrapConfigurationElementName(OUString const & _sElementName,OUString const & _sTypeName)296 OUString wrapConfigurationElementName(OUString const& _sElementName,
297                                       OUString const& _sTypeName)
298 {
299     // todo: check that _sTypeName is valid
300     return lcl_wrapName(_sElementName, _sTypeName);
301 }
302 
303 //----------------------------------------------------------------------------
304 } // namespace utl
305