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