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_stoc.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "stocservices.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "UriReference.hxx"
34*cdf0e10cSrcweir #include "supportsService.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include "com/sun/star/lang/IllegalArgumentException.hpp"
37*cdf0e10cSrcweir #include "com/sun/star/lang/XServiceInfo.hpp"
38*cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
39*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
40*cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
41*cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp"
42*cdf0e10cSrcweir #include "com/sun/star/uri/XUriReference.hpp"
43*cdf0e10cSrcweir #include "com/sun/star/uri/XUriSchemeParser.hpp"
44*cdf0e10cSrcweir #include "com/sun/star/uri/XVndSunStarScriptUrlReference.hpp"
45*cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
46*cdf0e10cSrcweir #include "cppuhelper/implbase2.hxx"
47*cdf0e10cSrcweir #include "cppuhelper/weak.hxx"
48*cdf0e10cSrcweir #include "osl/mutex.hxx"
49*cdf0e10cSrcweir #include "rtl/uri.hxx"
50*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
51*cdf0e10cSrcweir #include "rtl/ustring.hxx"
52*cdf0e10cSrcweir #include "sal/types.h"
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #include <new>
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir namespace css = com::sun::star;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir namespace {
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir int getHexWeight(sal_Unicode c) {
61*cdf0e10cSrcweir     return c >= '0' && c <= '9' ? static_cast< int >(c - '0')
62*cdf0e10cSrcweir         : c >= 'A' && c <= 'F' ? static_cast< int >(c - 'A' + 10)
63*cdf0e10cSrcweir         : c >= 'a' && c <= 'f' ? static_cast< int >(c - 'a' + 10)
64*cdf0e10cSrcweir         : -1;
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir int parseEscaped(rtl::OUString const & part, sal_Int32 * index) {
68*cdf0e10cSrcweir     if (part.getLength() - *index < 3 || part[*index] != '%') {
69*cdf0e10cSrcweir         return -1;
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir     int n1 = getHexWeight(part[*index + 1]);
72*cdf0e10cSrcweir     int n2 = getHexWeight(part[*index + 2]);
73*cdf0e10cSrcweir     if (n1 < 0 || n2 < 0) {
74*cdf0e10cSrcweir         return -1;
75*cdf0e10cSrcweir     }
76*cdf0e10cSrcweir     *index += 3;
77*cdf0e10cSrcweir     return (n1 << 4) | n2;
78*cdf0e10cSrcweir }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir rtl::OUString parsePart(
81*cdf0e10cSrcweir     rtl::OUString const & part, bool namePart, sal_Int32 * index)
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     rtl::OUStringBuffer buf;
84*cdf0e10cSrcweir     while (*index < part.getLength()) {
85*cdf0e10cSrcweir         sal_Unicode c = part[*index];
86*cdf0e10cSrcweir         if (namePart ? c == '?' : c == '&' || c == '=') {
87*cdf0e10cSrcweir             break;
88*cdf0e10cSrcweir         } else if (c == '%') {
89*cdf0e10cSrcweir             sal_Int32 i = *index;
90*cdf0e10cSrcweir             int n = parseEscaped(part, &i);
91*cdf0e10cSrcweir             if (n >= 0 && n <= 0x7F) {
92*cdf0e10cSrcweir                 buf.append(static_cast< sal_Unicode >(n));
93*cdf0e10cSrcweir             } else if (n >= 0xC0 && n <= 0xFC) {
94*cdf0e10cSrcweir                 sal_Int32 encoded;
95*cdf0e10cSrcweir                 int shift;
96*cdf0e10cSrcweir                 sal_Int32 min;
97*cdf0e10cSrcweir                 if (n <= 0xDF) {
98*cdf0e10cSrcweir                     encoded = (n & 0x1F) << 6;
99*cdf0e10cSrcweir                     shift = 0;
100*cdf0e10cSrcweir                     min = 0x80;
101*cdf0e10cSrcweir                 } else if (n <= 0xEF) {
102*cdf0e10cSrcweir                     encoded = (n & 0x0F) << 12;
103*cdf0e10cSrcweir                     shift = 6;
104*cdf0e10cSrcweir                     min = 0x800;
105*cdf0e10cSrcweir                 } else if (n <= 0xF7) {
106*cdf0e10cSrcweir                     encoded = (n & 0x07) << 18;
107*cdf0e10cSrcweir                     shift = 12;
108*cdf0e10cSrcweir                     min = 0x10000;
109*cdf0e10cSrcweir                 } else if (n <= 0xFB) {
110*cdf0e10cSrcweir                     encoded = (n & 0x03) << 24;
111*cdf0e10cSrcweir                     shift = 18;
112*cdf0e10cSrcweir                     min = 0x200000;
113*cdf0e10cSrcweir                 } else {
114*cdf0e10cSrcweir                     encoded = 0;
115*cdf0e10cSrcweir                     shift = 24;
116*cdf0e10cSrcweir                     min = 0x4000000;
117*cdf0e10cSrcweir                 }
118*cdf0e10cSrcweir                 bool utf8 = true;
119*cdf0e10cSrcweir                 for (; shift >= 0; shift -= 6) {
120*cdf0e10cSrcweir                     n = parseEscaped(part, &i);
121*cdf0e10cSrcweir                     if (n < 0x80 || n > 0xBF) {
122*cdf0e10cSrcweir                         utf8 = false;
123*cdf0e10cSrcweir                         break;
124*cdf0e10cSrcweir                     }
125*cdf0e10cSrcweir                     encoded |= (n & 0x3F) << shift;
126*cdf0e10cSrcweir                 }
127*cdf0e10cSrcweir                 if (!utf8 || encoded < min
128*cdf0e10cSrcweir                     || (encoded >= 0xD800 && encoded <= 0xDFFF)
129*cdf0e10cSrcweir                     || encoded > 0x10FFFF)
130*cdf0e10cSrcweir                 {
131*cdf0e10cSrcweir                     break;
132*cdf0e10cSrcweir                 }
133*cdf0e10cSrcweir                 if (encoded <= 0xFFFF) {
134*cdf0e10cSrcweir                     buf.append(static_cast< sal_Unicode >(encoded));
135*cdf0e10cSrcweir                 } else {
136*cdf0e10cSrcweir                     buf.append(static_cast< sal_Unicode >(
137*cdf0e10cSrcweir                         (encoded >> 10) | 0xD800));
138*cdf0e10cSrcweir                     buf.append(static_cast< sal_Unicode >(
139*cdf0e10cSrcweir                         (encoded & 0x3FF) | 0xDC00));
140*cdf0e10cSrcweir                 }
141*cdf0e10cSrcweir             } else {
142*cdf0e10cSrcweir                 break;
143*cdf0e10cSrcweir             }
144*cdf0e10cSrcweir             *index = i;
145*cdf0e10cSrcweir         } else {
146*cdf0e10cSrcweir             buf.append(c);
147*cdf0e10cSrcweir             ++*index;
148*cdf0e10cSrcweir         }
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir     return buf.makeStringAndClear();
151*cdf0e10cSrcweir }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir namespace
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir     static rtl::OUString encodeNameOrParamFragment( rtl::OUString const & fragment )
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir         static sal_Bool const aCharClass[] =
158*cdf0e10cSrcweir         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* NameOrParamFragment */
159*cdf0e10cSrcweir           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160*cdf0e10cSrcweir           0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* !"#$%&'()*+,-./*/
161*cdf0e10cSrcweir           1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, /*0123456789:;<=>?*/
162*cdf0e10cSrcweir           1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*@ABCDEFGHIJKLMNO*/
163*cdf0e10cSrcweir           1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, /*PQRSTUVWXYZ[\]^_*/
164*cdf0e10cSrcweir           0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*`abcdefghijklmno*/
165*cdf0e10cSrcweir           1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0  /*pqrstuvwxyz{|}~ */
166*cdf0e10cSrcweir         };
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         return rtl::Uri::encode(
169*cdf0e10cSrcweir             fragment,
170*cdf0e10cSrcweir             aCharClass,
171*cdf0e10cSrcweir             rtl_UriEncodeIgnoreEscapes,
172*cdf0e10cSrcweir             RTL_TEXTENCODING_UTF8
173*cdf0e10cSrcweir         );
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir bool parseSchemeSpecificPart(rtl::OUString const & part) {
178*cdf0e10cSrcweir     sal_Int32 len = part.getLength();
179*cdf0e10cSrcweir     sal_Int32 i = 0;
180*cdf0e10cSrcweir     if (parsePart(part, true, &i).getLength() == 0 || part[0] == '/') {
181*cdf0e10cSrcweir         return false;
182*cdf0e10cSrcweir     }
183*cdf0e10cSrcweir     if (i == len) {
184*cdf0e10cSrcweir         return true;
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir     for (;;) {
187*cdf0e10cSrcweir         ++i; // skip '?' or '&'
188*cdf0e10cSrcweir         if (parsePart(part, false, &i).getLength() == 0 || i == len
189*cdf0e10cSrcweir             || part[i] != '=')
190*cdf0e10cSrcweir         {
191*cdf0e10cSrcweir             return false;
192*cdf0e10cSrcweir         }
193*cdf0e10cSrcweir         ++i;
194*cdf0e10cSrcweir         parsePart(part, false, &i);
195*cdf0e10cSrcweir         if (i == len) {
196*cdf0e10cSrcweir             return true;
197*cdf0e10cSrcweir         }
198*cdf0e10cSrcweir         if (part[i] != '&') {
199*cdf0e10cSrcweir             return false;
200*cdf0e10cSrcweir         }
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir class UrlReference:
205*cdf0e10cSrcweir     public cppu::WeakImplHelper1< css::uri::XVndSunStarScriptUrlReference >
206*cdf0e10cSrcweir {
207*cdf0e10cSrcweir public:
208*cdf0e10cSrcweir     UrlReference(rtl::OUString const & scheme, rtl::OUString const & path):
209*cdf0e10cSrcweir         m_base(
210*cdf0e10cSrcweir             scheme, false, false, rtl::OUString(), path, false, rtl::OUString())
211*cdf0e10cSrcweir     {}
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getUriReference()
214*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
215*cdf0e10cSrcweir     { return m_base.getUriReference(); }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL isAbsolute()
218*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
219*cdf0e10cSrcweir     { return m_base.isAbsolute(); }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getScheme()
222*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
223*cdf0e10cSrcweir     { return m_base.getScheme(); }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getSchemeSpecificPart()
226*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
227*cdf0e10cSrcweir     { return m_base.getSchemeSpecificPart(); }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL isHierarchical()
230*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
231*cdf0e10cSrcweir     { return m_base.isHierarchical(); }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasAuthority()
234*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
235*cdf0e10cSrcweir     { return m_base.hasAuthority(); }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getAuthority()
238*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
239*cdf0e10cSrcweir     { return m_base.getAuthority(); }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getPath()
242*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
243*cdf0e10cSrcweir     { return m_base.getPath(); }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasRelativePath()
246*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
247*cdf0e10cSrcweir     { return m_base.hasRelativePath(); }
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL getPathSegmentCount()
250*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
251*cdf0e10cSrcweir     { return m_base.getPathSegmentCount(); }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getPathSegment(sal_Int32 index)
254*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
255*cdf0e10cSrcweir     { return m_base.getPathSegment(index); }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasQuery()
258*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
259*cdf0e10cSrcweir     { return m_base.hasQuery(); }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getQuery()
262*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
263*cdf0e10cSrcweir     { return m_base.getQuery(); }
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasFragment()
266*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
267*cdf0e10cSrcweir     { return m_base.hasFragment(); }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getFragment()
270*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
271*cdf0e10cSrcweir     { return m_base.getFragment(); }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir     virtual void SAL_CALL setFragment(rtl::OUString const & fragment)
274*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
275*cdf0e10cSrcweir     { m_base.setFragment(fragment); }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir     virtual void SAL_CALL clearFragment()
278*cdf0e10cSrcweir         throw (com::sun::star::uno::RuntimeException)
279*cdf0e10cSrcweir     { m_base.clearFragment(); }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getName() throw (css::uno::RuntimeException);
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir     virtual void SAL_CALL setName(rtl::OUString const & name)
284*cdf0e10cSrcweir         throw (css::uno::RuntimeException, css::lang::IllegalArgumentException);
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasParameter(rtl::OUString const & key)
287*cdf0e10cSrcweir         throw (css::uno::RuntimeException);
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getParameter(rtl::OUString const & key)
290*cdf0e10cSrcweir         throw (css::uno::RuntimeException);
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir     virtual void SAL_CALL setParameter(rtl::OUString const & key, rtl::OUString const & value)
293*cdf0e10cSrcweir         throw (css::uno::RuntimeException, css::lang::IllegalArgumentException);
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir private:
296*cdf0e10cSrcweir     UrlReference(UrlReference &); // not implemented
297*cdf0e10cSrcweir     void operator =(UrlReference); // not implemented
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir     virtual ~UrlReference() {}
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir     sal_Int32 findParameter(rtl::OUString const & key);
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir     stoc::uriproc::UriReference m_base;
304*cdf0e10cSrcweir };
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir rtl::OUString UrlReference::getName() throw (css::uno::RuntimeException) {
307*cdf0e10cSrcweir     osl::MutexGuard g(m_base.m_mutex);
308*cdf0e10cSrcweir     sal_Int32 i = 0;
309*cdf0e10cSrcweir     return parsePart(m_base.m_path, true, &i);
310*cdf0e10cSrcweir }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir void SAL_CALL UrlReference::setName(rtl::OUString const & name) throw (css::uno::RuntimeException, css::lang::IllegalArgumentException)
313*cdf0e10cSrcweir {
314*cdf0e10cSrcweir     if (name.getLength() == 0)
315*cdf0e10cSrcweir         throw css::lang::IllegalArgumentException(
316*cdf0e10cSrcweir             ::rtl::OUString(), *this, 1);
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir     osl::MutexGuard g(m_base.m_mutex);
319*cdf0e10cSrcweir     sal_Int32 i = 0;
320*cdf0e10cSrcweir     parsePart(m_base.m_path, true, &i);
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir     rtl::OUStringBuffer newPath;
323*cdf0e10cSrcweir     newPath.append(encodeNameOrParamFragment(name));
324*cdf0e10cSrcweir     newPath.append(m_base.m_path.copy(i));
325*cdf0e10cSrcweir     m_base.m_path = newPath.makeStringAndClear();
326*cdf0e10cSrcweir }
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir sal_Bool UrlReference::hasParameter(rtl::OUString const & key)
329*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
330*cdf0e10cSrcweir {
331*cdf0e10cSrcweir     osl::MutexGuard g(m_base.m_mutex);
332*cdf0e10cSrcweir     return findParameter(key) >= 0;
333*cdf0e10cSrcweir }
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir rtl::OUString UrlReference::getParameter(rtl::OUString const & key)
336*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
337*cdf0e10cSrcweir {
338*cdf0e10cSrcweir     osl::MutexGuard g(m_base.m_mutex);
339*cdf0e10cSrcweir     sal_Int32 i = findParameter(key);
340*cdf0e10cSrcweir     return i >= 0 ? parsePart(m_base.m_path, false, &i) : rtl::OUString();
341*cdf0e10cSrcweir }
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir void UrlReference::setParameter(rtl::OUString const & key, rtl::OUString const & value)
344*cdf0e10cSrcweir     throw (css::uno::RuntimeException, css::lang::IllegalArgumentException)
345*cdf0e10cSrcweir {
346*cdf0e10cSrcweir     if (key.getLength() == 0)
347*cdf0e10cSrcweir         throw css::lang::IllegalArgumentException(
348*cdf0e10cSrcweir             ::rtl::OUString(), *this, 1);
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir     osl::MutexGuard g(m_base.m_mutex);
351*cdf0e10cSrcweir     sal_Int32 i = findParameter(key);
352*cdf0e10cSrcweir     bool bExistent = ( i>=0 );
353*cdf0e10cSrcweir     if (!bExistent) {
354*cdf0e10cSrcweir         i = m_base.m_path.getLength();
355*cdf0e10cSrcweir     }
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir     rtl::OUStringBuffer newPath;
358*cdf0e10cSrcweir     newPath.append(m_base.m_path.copy(0, i));
359*cdf0e10cSrcweir     if (!bExistent) {
360*cdf0e10cSrcweir         newPath.append(sal_Unicode(m_base.m_path.indexOf('?') < 0 ? '?' : '&'));
361*cdf0e10cSrcweir         newPath.append(encodeNameOrParamFragment(key));
362*cdf0e10cSrcweir         newPath.append(sal_Unicode('='));
363*cdf0e10cSrcweir     }
364*cdf0e10cSrcweir     newPath.append(encodeNameOrParamFragment(value));
365*cdf0e10cSrcweir     if (bExistent) {
366*cdf0e10cSrcweir         /*oldValue = */
367*cdf0e10cSrcweir         parsePart(m_base.m_path, false, &i); // skip key
368*cdf0e10cSrcweir         newPath.append(m_base.m_path.copy(i));
369*cdf0e10cSrcweir     }
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir     m_base.m_path = newPath.makeStringAndClear();
372*cdf0e10cSrcweir }
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir sal_Int32 UrlReference::findParameter(rtl::OUString const & key) {
375*cdf0e10cSrcweir     sal_Int32 i = 0;
376*cdf0e10cSrcweir     parsePart(m_base.m_path, true, &i); // skip name
377*cdf0e10cSrcweir     for (;;) {
378*cdf0e10cSrcweir         if (i == m_base.m_path.getLength()) {
379*cdf0e10cSrcweir             return -1;
380*cdf0e10cSrcweir         }
381*cdf0e10cSrcweir         ++i; // skip '?' or '&'
382*cdf0e10cSrcweir         rtl::OUString k = parsePart(m_base.m_path, false, &i);
383*cdf0e10cSrcweir         ++i; // skip '='
384*cdf0e10cSrcweir         if (k == key) {
385*cdf0e10cSrcweir             return i;
386*cdf0e10cSrcweir         }
387*cdf0e10cSrcweir         parsePart(m_base.m_path, false, &i); // skip value
388*cdf0e10cSrcweir     }
389*cdf0e10cSrcweir }
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir class Parser: public cppu::WeakImplHelper2<
392*cdf0e10cSrcweir     css::lang::XServiceInfo, css::uri::XUriSchemeParser >
393*cdf0e10cSrcweir {
394*cdf0e10cSrcweir public:
395*cdf0e10cSrcweir     Parser() {}
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir     virtual rtl::OUString SAL_CALL getImplementationName()
398*cdf0e10cSrcweir         throw (css::uno::RuntimeException);
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & serviceName)
401*cdf0e10cSrcweir         throw (css::uno::RuntimeException);
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir     virtual css::uno::Sequence< rtl::OUString > SAL_CALL
404*cdf0e10cSrcweir     getSupportedServiceNames() throw (css::uno::RuntimeException);
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir     virtual css::uno::Reference< css::uri::XUriReference > SAL_CALL
407*cdf0e10cSrcweir     parse(
408*cdf0e10cSrcweir         rtl::OUString const & scheme, rtl::OUString const & schemeSpecificPart)
409*cdf0e10cSrcweir         throw (css::uno::RuntimeException);
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir private:
412*cdf0e10cSrcweir     Parser(Parser &); // not implemented
413*cdf0e10cSrcweir     void operator =(Parser); // not implemented
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir     virtual ~Parser() {}
416*cdf0e10cSrcweir };
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir rtl::OUString Parser::getImplementationName()
419*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
420*cdf0e10cSrcweir {
421*cdf0e10cSrcweir     return stoc_services::UriSchemeParser_vndDOTsunDOTstarDOTscript::
422*cdf0e10cSrcweir         getImplementationName();
423*cdf0e10cSrcweir }
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir sal_Bool Parser::supportsService(rtl::OUString const & serviceName)
426*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
427*cdf0e10cSrcweir {
428*cdf0e10cSrcweir     return stoc::uriproc::supportsService(
429*cdf0e10cSrcweir         getSupportedServiceNames(), serviceName);
430*cdf0e10cSrcweir }
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir css::uno::Sequence< rtl::OUString > Parser::getSupportedServiceNames()
433*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
434*cdf0e10cSrcweir {
435*cdf0e10cSrcweir     return stoc_services::UriSchemeParser_vndDOTsunDOTstarDOTscript::
436*cdf0e10cSrcweir         getSupportedServiceNames();
437*cdf0e10cSrcweir }
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir css::uno::Reference< css::uri::XUriReference >
440*cdf0e10cSrcweir Parser::parse(
441*cdf0e10cSrcweir     rtl::OUString const & scheme, rtl::OUString const & schemeSpecificPart)
442*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
443*cdf0e10cSrcweir {
444*cdf0e10cSrcweir     if (!parseSchemeSpecificPart(schemeSpecificPart)) {
445*cdf0e10cSrcweir         return 0;
446*cdf0e10cSrcweir     }
447*cdf0e10cSrcweir     try {
448*cdf0e10cSrcweir         return new UrlReference(scheme, schemeSpecificPart);
449*cdf0e10cSrcweir     } catch (std::bad_alloc &) {
450*cdf0e10cSrcweir         throw css::uno::RuntimeException(
451*cdf0e10cSrcweir             rtl::OUString::createFromAscii("std::bad_alloc"), 0);
452*cdf0e10cSrcweir     }
453*cdf0e10cSrcweir }
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir }
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir namespace stoc_services {
458*cdf0e10cSrcweir namespace UriSchemeParser_vndDOTsunDOTstarDOTscript {
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > create(
461*cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > const &)
462*cdf0e10cSrcweir     SAL_THROW((css::uno::Exception))
463*cdf0e10cSrcweir {
464*cdf0e10cSrcweir     //TODO: single instance
465*cdf0e10cSrcweir     try {
466*cdf0e10cSrcweir         return static_cast< cppu::OWeakObject * >(new Parser);
467*cdf0e10cSrcweir     } catch (std::bad_alloc &) {
468*cdf0e10cSrcweir         throw css::uno::RuntimeException(
469*cdf0e10cSrcweir             rtl::OUString::createFromAscii("std::bad_alloc"), 0);
470*cdf0e10cSrcweir     }
471*cdf0e10cSrcweir }
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir rtl::OUString getImplementationName() {
474*cdf0e10cSrcweir     return rtl::OUString::createFromAscii(
475*cdf0e10cSrcweir         "com.sun.star.comp.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript");
476*cdf0e10cSrcweir }
477*cdf0e10cSrcweir 
478*cdf0e10cSrcweir css::uno::Sequence< rtl::OUString > getSupportedServiceNames() {
479*cdf0e10cSrcweir     css::uno::Sequence< rtl::OUString > s(1);
480*cdf0e10cSrcweir     s[0] = rtl::OUString::createFromAscii(
481*cdf0e10cSrcweir         "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript");
482*cdf0e10cSrcweir     return s;
483*cdf0e10cSrcweir }
484*cdf0e10cSrcweir 
485*cdf0e10cSrcweir } }
486