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