LCOV - code coverage report
Current view: top level - jvmaccess/source - classpath.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 25 58 43.1 %
Date: 2012-08-25 Functions: 1 2 50.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 27 92 29.3 %

           Branch data     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                 :            : #include "sal/config.h"
      21                 :            : 
      22                 :            : #include "jvmaccess/classpath.hxx"
      23                 :            : 
      24                 :            : #include <vector>
      25                 :            : 
      26                 :            : #include "com/sun/star/lang/IllegalArgumentException.hpp"
      27                 :            : #include "com/sun/star/uno/Any.hxx"
      28                 :            : #include "com/sun/star/uno/Reference.hxx"
      29                 :            : #include "com/sun/star/uno/RuntimeException.hpp"
      30                 :            : #include "com/sun/star/uno/XComponentContext.hpp"
      31                 :            : #include "com/sun/star/uno/XInterface.hpp"
      32                 :            : #include "com/sun/star/uri/UriReferenceFactory.hpp"
      33                 :            : #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
      34                 :            : #include "com/sun/star/util/XMacroExpander.hpp"
      35                 :            : #include "osl/diagnose.h"
      36                 :            : #include "rtl/ustring.hxx"
      37                 :            : #include "sal/types.h"
      38                 :            : 
      39                 :            : #if defined SOLAR_JAVA
      40                 :            : #include "jni.h"
      41                 :            : #endif
      42                 :            : 
      43                 :            : namespace {
      44                 :            : 
      45                 :            : namespace css = ::com::sun::star;
      46                 :            : 
      47                 :            : }
      48                 :            : 
      49                 :          4 : void * ::jvmaccess::ClassPath::doTranslateToUrls(
      50                 :            :     css::uno::Reference< css::uno::XComponentContext > const & context,
      51                 :            :     void * environment, ::rtl::OUString const & classPath)
      52                 :            : {
      53                 :            :     OSL_ASSERT(context.is() && environment != 0);
      54                 :            : #if defined SOLAR_JAVA
      55                 :          4 :     ::JNIEnv * const env = static_cast< ::JNIEnv * >(environment);
      56         [ +  - ]:          4 :     jclass classUrl(env->FindClass("java/net/URL"));
      57         [ -  + ]:          4 :     if (classUrl == 0) {
      58                 :          0 :         return 0;
      59                 :            :     }
      60                 :            :     jmethodID ctorUrl(
      61         [ +  - ]:          4 :         env->GetMethodID(classUrl, "<init>", "(Ljava/lang/String;)V"));
      62         [ -  + ]:          4 :     if (ctorUrl == 0) {
      63                 :          0 :         return 0;
      64                 :            :     }
      65         [ +  - ]:          4 :     ::std::vector< jobject > urls;
      66         [ +  + ]:         24 :     for (::sal_Int32 i = 0; i != -1;) {
      67                 :         20 :         ::rtl::OUString url(classPath.getToken(0, ' ', i));
      68         [ +  + ]:         20 :         if (!url.isEmpty()) {
      69                 :            :             css::uno::Reference< css::uri::XVndSunStarExpandUrlReference >
      70                 :            :                 expUrl(
      71 [ +  - ][ +  - ]:         16 :                     css::uri::UriReferenceFactory::create(context)->parse(url),
      72 [ +  - ][ +  - ]:          8 :                     css::uno::UNO_QUERY);
      73         [ -  + ]:          8 :             if (expUrl.is()) {
      74                 :            :                 css::uno::Reference< css::util::XMacroExpander > expander(
      75         [ #  # ]:          0 :                     context->getValueByName(
      76                 :            :                         ::rtl::OUString(
      77                 :            :                             RTL_CONSTASCII_USTRINGPARAM(
      78                 :            :                                 "/singletons/"
      79                 :          0 :                                 "com.sun.star.util.theMacroExpander"))),
      80 [ #  # ][ #  # ]:          0 :                     css::uno::UNO_QUERY_THROW);
                 [ #  # ]
      81                 :            :                 try {
      82 [ #  # ][ #  # ]:          0 :                     url = expUrl->expand(expander);
      83         [ #  # ]:          0 :                 } catch (const css::lang::IllegalArgumentException & e) {
      84                 :            :                     throw css::uno::RuntimeException(
      85                 :            :                         (::rtl::OUString(
      86                 :            :                             RTL_CONSTASCII_USTRINGPARAM(
      87                 :            :                                 "com.sun.star.lang.IllegalArgumentException: "))
      88                 :            :                          + e.Message),
      89   [ #  #  #  # ]:          0 :                         css::uno::Reference< css::uno::XInterface >());
      90                 :          0 :                 }
      91                 :            :             }
      92                 :            :             jvalue arg;
      93                 :            :             arg.l = env->NewString(
      94                 :            :                 static_cast< jchar const * >(url.getStr()),
      95         [ +  - ]:          8 :                 static_cast< jsize >(url.getLength()));
      96         [ -  + ]:          8 :             if (arg.l == 0) {
      97                 :          0 :                 return 0;
      98                 :            :             }
      99         [ +  - ]:          8 :             jobject o(env->NewObjectA(classUrl, ctorUrl, &arg));
     100         [ -  + ]:          8 :             if (o == 0) {
     101                 :          0 :                 return 0;
     102                 :            :             }
     103 [ +  - ][ +  - ]:         20 :             urls.push_back(o);
     104                 :            :         }
     105         [ +  - ]:         20 :     }
     106                 :            :     jobjectArray result = env->NewObjectArray(
     107         [ +  - ]:          4 :         static_cast< jsize >(urls.size()), classUrl, 0);
     108                 :            :         // static_cast is ok, as each element of urls occupied at least one
     109                 :            :         // character of the ::rtl::OUString classPath
     110         [ -  + ]:          4 :     if (result == 0) {
     111                 :          0 :         return 0;
     112                 :            :     }
     113                 :          4 :     jsize idx = 0;
     114 [ +  - ][ +  + ]:         12 :     for (std::vector< jobject >::iterator i(urls.begin()); i != urls.end(); ++i)
     115                 :            :     {
     116         [ +  - ]:          8 :         env->SetObjectArrayElement(result, idx++, *i);
     117                 :            :     }
     118                 :          4 :     return result;
     119                 :            : #else
     120                 :            :     (void) context;
     121                 :            :     (void) environment;
     122                 :            :     (void) classPath;
     123                 :            :     return 0;
     124                 :            : #endif
     125                 :            : }
     126                 :            : 
     127                 :          0 : void * ::jvmaccess::ClassPath::doLoadClass(
     128                 :            :     css::uno::Reference< css::uno::XComponentContext > const & context,
     129                 :            :     void * environment, ::rtl::OUString const & classPath,
     130                 :            :     ::rtl::OUString const & name)
     131                 :            : {
     132                 :            :     OSL_ASSERT(context.is() && environment != 0);
     133                 :            : #if defined SOLAR_JAVA
     134                 :          0 :     ::JNIEnv * const env = static_cast< ::JNIEnv * >(environment);
     135         [ #  # ]:          0 :     jclass classLoader(env->FindClass("java/net/URLClassLoader"));
     136         [ #  # ]:          0 :     if (classLoader == 0) {
     137                 :          0 :         return 0;
     138                 :            :     }
     139                 :            :     jmethodID ctorLoader(
     140         [ #  # ]:          0 :         env->GetMethodID(classLoader, "<init>", "([Ljava/net/URL;)V"));
     141         [ #  # ]:          0 :     if (ctorLoader == 0) {
     142                 :          0 :         return 0;
     143                 :            :     }
     144                 :            :     jvalue arg;
     145         [ #  # ]:          0 :     arg.l = translateToUrls(context, env, classPath);
     146         [ #  # ]:          0 :     if (arg.l == 0) {
     147                 :          0 :         return 0;
     148                 :            :     }
     149         [ #  # ]:          0 :     jobject cl = env->NewObjectA(classLoader, ctorLoader, &arg);
     150         [ #  # ]:          0 :     if (cl == 0) {
     151                 :          0 :         return 0;
     152                 :            :     }
     153                 :            :     jmethodID methLoadClass(
     154                 :            :         env->GetMethodID(
     155         [ #  # ]:          0 :             classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
     156         [ #  # ]:          0 :     if (methLoadClass == 0) {
     157                 :          0 :         return 0;
     158                 :            :     }
     159                 :            :     arg.l = env->NewString(
     160                 :            :         static_cast< jchar const * >(name.getStr()),
     161         [ #  # ]:          0 :         static_cast< jsize >(name.getLength()));
     162         [ #  # ]:          0 :     if (arg.l == 0) {
     163                 :          0 :         return 0;
     164                 :            :     }
     165         [ #  # ]:          0 :     return env->CallObjectMethodA(cl, methLoadClass, &arg);
     166                 :            : #else
     167                 :            :     (void) context;
     168                 :            :     (void) environment;
     169                 :            :     (void) classPath;
     170                 :            :     (void) name;
     171                 :            :     return 0;
     172                 :            : #endif
     173                 :            : }
     174                 :            : 
     175                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10