LCOV - code coverage report
Current view: top level - libreoffice/stoc/source/uriproc - UriSchemeParser_vndDOTsunDOTstarDOTscript.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 65 197 33.0 %
Date: 2012-12-27 Functions: 16 41 39.0 %
Legend: Lines: hit not hit

          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             :     return c >= '0' && c <= '9' ? static_cast< int >(c - '0')
      50             :         : c >= 'A' && c <= 'F' ? static_cast< int >(c - 'A' + 10)
      51             :         : c >= 'a' && c <= 'f' ? static_cast< int >(c - 'a' + 10)
      52           0 :         : -1;
      53             : }
      54             : 
      55           0 : int parseEscaped(rtl::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          31 : rtl::OUString parsePart(
      69             :     rtl::OUString const & part, bool namePart, sal_Int32 * index)
      70             : {
      71          31 :     rtl::OUStringBuffer buf;
      72         436 :     while (*index < part.getLength()) {
      73         401 :         sal_Unicode c = part[*index];
      74         401 :         if (namePart ? c == '?' : c == '&' || c == '=') {
      75          27 :             break;
      76         374 :         } 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             :                     || (encoded >= 0xD800 && encoded <= 0xDFFF)
     117             :                     || 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           0 :                 }
     129             :             } else {
     130             :                 break;
     131             :             }
     132           0 :             *index = i;
     133             :         } else {
     134         374 :             buf.append(c);
     135         374 :             ++*index;
     136             :         }
     137             :     }
     138          31 :     return buf.makeStringAndClear();
     139             : }
     140             : 
     141             : namespace
     142             : {
     143           0 :     static rtl::OUString encodeNameOrParamFragment( rtl::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           2 : bool parseSchemeSpecificPart(rtl::OUString const & part) {
     166           2 :     sal_Int32 len = part.getLength();
     167           2 :     sal_Int32 i = 0;
     168           2 :     if (parsePart(part, true, &i).isEmpty() || part[0] == '/') {
     169           0 :         return false;
     170             :     }
     171           2 :     if (i == len) {
     172           0 :         return true;
     173             :     }
     174           2 :     for (;;) {
     175           4 :         ++i; // skip '?' or '&'
     176           8 :         if (parsePart(part, false, &i).isEmpty() || i == len
     177           4 :             || part[i] != '=')
     178             :         {
     179           0 :             return false;
     180             :         }
     181           4 :         ++i;
     182           4 :         parsePart(part, false, &i);
     183           4 :         if (i == len) {
     184           2 :             return true;
     185             :         }
     186           2 :         if (part[i] != '&') {
     187           0 :             return false;
     188             :         }
     189             :     }
     190             : }
     191             : 
     192             : class UrlReference:
     193             :     public cppu::WeakImplHelper1< css::uri::XVndSunStarScriptUrlReference >
     194             : {
     195             : public:
     196           2 :     UrlReference(rtl::OUString const & scheme, rtl::OUString const & path):
     197             :         m_base(
     198           2 :             scheme, false, false, rtl::OUString(), path, false, rtl::OUString())
     199           2 :     {}
     200             : 
     201           0 :     virtual rtl::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           0 :     virtual rtl::OUString SAL_CALL getScheme()
     210             :         throw (com::sun::star::uno::RuntimeException)
     211           0 :     { return m_base.getScheme(); }
     212             : 
     213           0 :     virtual rtl::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 rtl::OUString SAL_CALL getAuthority()
     226             :         throw (com::sun::star::uno::RuntimeException)
     227           0 :     { return m_base.getAuthority(); }
     228             : 
     229           0 :     virtual rtl::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 rtl::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 rtl::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 rtl::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(rtl::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 rtl::OUString SAL_CALL getName() throw (css::uno::RuntimeException);
     270             : 
     271             :     virtual void SAL_CALL setName(rtl::OUString const & name)
     272             :         throw (css::uno::RuntimeException, css::lang::IllegalArgumentException);
     273             : 
     274             :     virtual sal_Bool SAL_CALL hasParameter(rtl::OUString const & key)
     275             :         throw (css::uno::RuntimeException);
     276             : 
     277             :     virtual rtl::OUString SAL_CALL getParameter(rtl::OUString const & key)
     278             :         throw (css::uno::RuntimeException);
     279             : 
     280             :     virtual void SAL_CALL setParameter(rtl::OUString const & key, rtl::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           4 :     virtual ~UrlReference() {}
     288             : 
     289             :     sal_Int32 findParameter(rtl::OUString const & key);
     290             : 
     291             :     stoc::uriproc::UriReference m_base;
     292             : };
     293             : 
     294           2 : rtl::OUString UrlReference::getName() throw (css::uno::RuntimeException) {
     295           2 :     osl::MutexGuard g(m_base.m_mutex);
     296           2 :     sal_Int32 i = 0;
     297           2 :     return parsePart(m_base.m_path, true, &i);
     298             : }
     299             : 
     300           0 : void SAL_CALL UrlReference::setName(rtl::OUString const & name) throw (css::uno::RuntimeException, css::lang::IllegalArgumentException)
     301             : {
     302           0 :     if (name.isEmpty())
     303             :         throw css::lang::IllegalArgumentException(
     304           0 :             ::rtl::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 :     rtl::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           2 : sal_Bool UrlReference::hasParameter(rtl::OUString const & key)
     317             :     throw (css::uno::RuntimeException)
     318             : {
     319           2 :     osl::MutexGuard g(m_base.m_mutex);
     320           2 :     return findParameter(key) >= 0;
     321             : }
     322             : 
     323           3 : rtl::OUString UrlReference::getParameter(rtl::OUString const & key)
     324             :     throw (css::uno::RuntimeException)
     325             : {
     326           3 :     osl::MutexGuard g(m_base.m_mutex);
     327           3 :     sal_Int32 i = findParameter(key);
     328           3 :     return i >= 0 ? parsePart(m_base.m_path, false, &i) : rtl::OUString();
     329             : }
     330             : 
     331           0 : void UrlReference::setParameter(rtl::OUString const & key, rtl::OUString const & value)
     332             :     throw (css::uno::RuntimeException, css::lang::IllegalArgumentException)
     333             : {
     334           0 :     if (key.isEmpty())
     335             :         throw css::lang::IllegalArgumentException(
     336           0 :             ::rtl::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 :     rtl::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           5 : sal_Int32 UrlReference::findParameter(rtl::OUString const & key) {
     363           5 :     sal_Int32 i = 0;
     364           5 :     parsePart(m_base.m_path, true, &i); // skip name
     365           3 :     for (;;) {
     366           8 :         if (i == m_base.m_path.getLength()) {
     367           0 :             return -1;
     368             :         }
     369           8 :         ++i; // skip '?' or '&'
     370           8 :         rtl::OUString k = parsePart(m_base.m_path, false, &i);
     371           8 :         ++i; // skip '='
     372           8 :         if (k == key) {
     373           5 :             return i;
     374             :         }
     375           3 :         parsePart(m_base.m_path, false, &i); // skip value
     376           8 :     }
     377             : }
     378             : 
     379             : class Parser: public cppu::WeakImplHelper2<
     380             :     css::lang::XServiceInfo, css::uri::XUriSchemeParser >
     381             : {
     382             : public:
     383           2 :     Parser() {}
     384             : 
     385             :     virtual rtl::OUString SAL_CALL getImplementationName()
     386             :         throw (css::uno::RuntimeException);
     387             : 
     388             :     virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & serviceName)
     389             :         throw (css::uno::RuntimeException);
     390             : 
     391             :     virtual css::uno::Sequence< rtl::OUString > SAL_CALL
     392             :     getSupportedServiceNames() throw (css::uno::RuntimeException);
     393             : 
     394             :     virtual css::uno::Reference< css::uri::XUriReference > SAL_CALL
     395             :     parse(
     396             :         rtl::OUString const & scheme, rtl::OUString const & schemeSpecificPart)
     397             :         throw (css::uno::RuntimeException);
     398             : 
     399             : private:
     400             :     Parser(Parser &); // not implemented
     401             :     void operator =(Parser); // not implemented
     402             : 
     403           4 :     virtual ~Parser() {}
     404             : };
     405             : 
     406           0 : rtl::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(rtl::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< rtl::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           2 : Parser::parse(
     429             :     rtl::OUString const & scheme, rtl::OUString const & schemeSpecificPart)
     430             :     throw (css::uno::RuntimeException)
     431             : {
     432           2 :     if (!parseSchemeSpecificPart(schemeSpecificPart)) {
     433           0 :         return 0;
     434             :     }
     435             :     try {
     436           2 :         return new UrlReference(scheme, schemeSpecificPart);
     437           0 :     } catch (std::bad_alloc &) {
     438             :         throw css::uno::RuntimeException(
     439           0 :             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")), 0);
     440             :     }
     441             : }
     442             : 
     443             : }
     444             : 
     445             : namespace stoc_services {
     446             : namespace UriSchemeParser_vndDOTsunDOTstarDOTscript {
     447             : 
     448           2 : 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           2 :         return static_cast< cppu::OWeakObject * >(new Parser);
     456           0 :     } catch (std::bad_alloc &) {
     457             :         throw css::uno::RuntimeException(
     458           0 :             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")), 0);
     459             :     }
     460             : }
     461             : 
     462          28 : rtl::OUString getImplementationName() {
     463             :     return rtl::OUString(
     464          28 :         RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript"));
     465             : }
     466             : 
     467           1 : css::uno::Sequence< rtl::OUString > getSupportedServiceNames() {
     468           1 :     css::uno::Sequence< rtl::OUString > s(1);
     469           1 :     s[0] = rtl::OUString(
     470           2 :         RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript"));
     471           1 :     return s;
     472             : }
     473             : 
     474             : } }
     475             : 
     476             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10