LCOV - code coverage report
Current view: top level - jvmfwk/source - fwkutil.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 75 112 67.0 %
Date: 2014-04-14 Functions: 7 8 87.5 %
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             : #if defined WNT
      22             : #if defined _MSC_VER
      23             : #pragma warning(push, 1)
      24             : #endif
      25             : #include <windows.h>
      26             : #if defined _MSC_VER
      27             : #pragma warning(pop)
      28             : #endif
      29             : #endif
      30             : 
      31             : #include <string>
      32             : #include <string.h>
      33             : #include "osl/mutex.hxx"
      34             : #include "osl/module.hxx"
      35             : #include "osl/thread.hxx"
      36             : #include "rtl/ustring.hxx"
      37             : #include "rtl/ustrbuf.hxx"
      38             : #include "rtl/bootstrap.hxx"
      39             : #include "osl/file.hxx"
      40             : #include "osl/process.h"
      41             : #include "rtl/instance.hxx"
      42             : #include "rtl/uri.hxx"
      43             : #include "osl/getglobalmutex.hxx"
      44             : #include "com/sun/star/lang/IllegalArgumentException.hpp"
      45             : #include "cppuhelper/bootstrap.hxx"
      46             : 
      47             : #include "framework.hxx"
      48             : #include "fwkutil.hxx"
      49             : 
      50             : using namespace osl;
      51             : 
      52             : 
      53             : namespace jfw
      54             : {
      55             : 
      56           1 : bool isAccessibilitySupportDesired()
      57             : {
      58           1 :     OUString sValue;
      59           1 :     if (::rtl::Bootstrap::get( "JFW_PLUGIN_DO_NOT_CHECK_ACCESSIBILITY", sValue) &&
      60           0 :         sValue == "1" )
      61           0 :         return false;
      62             : 
      63           1 :     bool retVal = false;
      64             : #ifdef WNT
      65             :     HKEY    hKey = 0;
      66             :     if (RegOpenKeyEx(HKEY_CURRENT_USER,
      67             :                      "Software\\LibreOffice\\Accessibility\\AtToolSupport",
      68             :                      0, KEY_READ, &hKey) == ERROR_SUCCESS)
      69             :     {
      70             :         DWORD   dwType = 0;
      71             :         DWORD   dwLen = 16;
      72             :         unsigned char arData[16];
      73             :         if( RegQueryValueEx(hKey, "SupportAssistiveTechnology", NULL, &dwType, arData,
      74             :                             & dwLen)== ERROR_SUCCESS)
      75             :         {
      76             :             if (dwType == REG_SZ)
      77             :             {
      78             :                 if (strcmp((char*) arData, "true") == 0
      79             :                     || strcmp((char*) arData, "1") == 0)
      80             :                     retVal = true;
      81             :                 else if (strcmp((char*) arData, "false") == 0
      82             :                          || strcmp((char*) arData, "0") == 0)
      83             :                     retVal = false;
      84             : #if OSL_DEBUG_LEVEL > 1
      85             :                 else
      86             :                     OSL_ASSERT(0);
      87             : #endif
      88             :             }
      89             :             else if (dwType == REG_DWORD)
      90             :             {
      91             :                 if (arData[0] == 1)
      92             :                     retVal = true;
      93             :                 else if (arData[0] == 0)
      94             :                     retVal = false;
      95             : #if OSL_DEBUG_LEVEL > 1
      96             :                 else
      97             :                     OSL_ASSERT(0);
      98             : #endif
      99             :             }
     100             :         }
     101             :     }
     102             :     RegCloseKey(hKey);
     103             : 
     104             : #elif defined UNX
     105             :     // Java is no longer required for a11y - we use atk directly.
     106           1 :     retVal = ::rtl::Bootstrap::get( "JFW_PLUGIN_FORCE_ACCESSIBILITY", sValue) && sValue == "1";
     107             : #endif
     108             : 
     109           1 :     return retVal;
     110             : }
     111             : 
     112           1 : rtl::ByteSequence encodeBase16(const rtl::ByteSequence& rawData)
     113             : {
     114             :     static const char EncodingTable[] =
     115             :         {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
     116           1 :     sal_Int32 lenRaw = rawData.getLength();
     117           1 :     char* pBuf = new char[lenRaw * 2];
     118           1 :     const sal_Int8* arRaw = rawData.getConstArray();
     119             : 
     120           1 :     char* pCurBuf = pBuf;
     121         827 :     for (int i = 0; i < lenRaw; i++)
     122             :     {
     123         826 :         unsigned char curChar = arRaw[i];
     124         826 :         curChar >>= 4;
     125             : 
     126         826 :         *pCurBuf = EncodingTable[curChar];
     127         826 :         pCurBuf++;
     128             : 
     129         826 :         curChar = arRaw[i];
     130         826 :         curChar &= 0x0F;
     131             : 
     132         826 :         *pCurBuf = EncodingTable[curChar];
     133         826 :         pCurBuf++;
     134             :     }
     135             : 
     136           1 :     rtl::ByteSequence ret((sal_Int8*) pBuf, lenRaw * 2);
     137           1 :     delete [] pBuf;
     138           1 :     return ret;
     139             : }
     140             : 
     141       85008 : rtl::ByteSequence decodeBase16(const rtl::ByteSequence& data)
     142             : {
     143             :     static const char decodingTable[] =
     144             :         {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
     145       85008 :     sal_Int32 lenData = data.getLength();
     146       85008 :     sal_Int32 lenBuf = lenData / 2; //always divisable by two
     147       85008 :     unsigned char* pBuf = new unsigned char[lenBuf];
     148       85008 :     const sal_Int8* pData = data.getConstArray();
     149    70301616 :     for (sal_Int32 i = 0; i < lenBuf; i++)
     150             :     {
     151    70216608 :         sal_Int8 curChar = *pData++;
     152             :         //find the index of the first 4bits
     153             :         //  TODO  What happens if text is not valid Hex characters?
     154    70216608 :         unsigned char nibble = 0;
     155   218895600 :         for (unsigned char j = 0; j < 16; j++)
     156             :         {
     157   218895600 :             if (curChar == decodingTable[j])
     158             :             {
     159    70216608 :                 nibble = j;
     160    70216608 :                 break;
     161             :             }
     162             :         }
     163    70216608 :         nibble <<= 4;
     164    70216608 :         curChar = *pData++;
     165             :         //find the index for the next 4bits
     166   354483360 :         for (unsigned char j = 0; j < 16; j++)
     167             :         {
     168   354483360 :             if (curChar == decodingTable[j])
     169             :             {
     170    70216608 :                 nibble |= j;
     171    70216608 :                 break;
     172             :             }
     173             :         }
     174    70216608 :         pBuf[i] = nibble;
     175             :     }
     176       85008 :     rtl::ByteSequence ret((sal_Int8*) pBuf, lenBuf );
     177       85008 :     delete [] pBuf;
     178       85008 :     return ret;
     179             : }
     180             : 
     181       42506 : OUString getDirFromFile(const OUString& usFilePath)
     182             : {
     183       42506 :     sal_Int32 index= usFilePath.lastIndexOf('/');
     184       42506 :     return OUString(usFilePath.getStr(), index);
     185             : }
     186             : 
     187           0 : OUString getExecutableDirectory()
     188             : {
     189           0 :     rtl_uString* sExe = NULL;
     190           0 :     if (osl_getExecutableFile( & sExe) != osl_Process_E_None)
     191             :         throw FrameworkException(
     192             :             JFW_E_ERROR,
     193           0 :             "[Java framework] Error in function getExecutableDirectory (fwkutil.cxx)");
     194             : 
     195           0 :     OUString ouExe(sExe, SAL_NO_ACQUIRE);
     196           0 :     return getDirFromFile(ouExe);
     197             : }
     198             : 
     199       42510 : OUString findPlugin(
     200             :     const OUString & baseUrl, const OUString & plugin)
     201             : {
     202       42510 :     OUString expandedPlugin;
     203             :     try
     204             :     {
     205       42510 :         expandedPlugin = cppu::bootstrap_expandUri(plugin);
     206             :     }
     207           0 :     catch (const com::sun::star::lang::IllegalArgumentException & e)
     208             :     {
     209             :         throw FrameworkException(
     210             :             JFW_E_ERROR,
     211             :             OString("[Java framework] IllegalArgumentException in findPlugin: ")
     212           0 :              + OUStringToOString(e.Message, osl_getThreadTextEncoding()));
     213             :     }
     214       85020 :     OUString sUrl;
     215             :     try
     216             :     {
     217       42510 :         sUrl = rtl::Uri::convertRelToAbs(baseUrl, expandedPlugin);
     218             :     }
     219           0 :     catch (const rtl::MalformedUriException & e)
     220             :     {
     221             :         throw FrameworkException(
     222             :             JFW_E_ERROR,
     223             :             OString("[Java framework] rtl::MalformedUriException in findPlugin: ")
     224           0 :              + OUStringToOString(
     225           0 :                  e.getMessage(), osl_getThreadTextEncoding()));
     226             :     }
     227       42510 :     if (checkFileURL(sUrl) == jfw::FILE_OK)
     228             :     {
     229       42510 :         return sUrl;
     230             :     }
     231           0 :     OUString retVal;
     232           0 :     OUString sProgDir = getExecutableDirectory();
     233           0 :     sUrl = sProgDir + "/" + plugin;
     234           0 :     jfw::FileStatus s = checkFileURL(sUrl);
     235           0 :     if (s == jfw::FILE_INVALID || s == jfw::FILE_DOES_NOT_EXIST)
     236             :     {
     237             :         //If only the name of the library is given, then
     238             :         //use PATH, LD_LIBRARY_PATH etc. to locate the plugin
     239           0 :         if (plugin.indexOf('/') == -1)
     240             :         {
     241           0 :             OUString url;
     242             : #ifdef UNX
     243             : #if defined(MACOSX)
     244             :             OUString path = "DYLD_LIBRARY_PATH";
     245             : #elif defined(AIX)
     246             :             OUString path = "LIBPATH";
     247             : #else
     248           0 :             OUString path = "LD_LIBRARY_PATH";
     249             : #endif
     250           0 :             OUString env_path;
     251           0 :             oslProcessError err = osl_getEnvironment(path.pData, &env_path.pData);
     252           0 :             if (err != osl_Process_E_None && err != osl_Process_E_NotFound)
     253             :                 throw FrameworkException(
     254             :                     JFW_E_ERROR,
     255           0 :                     "[Java framework] Error in function findPlugin (fwkutil.cxx).");
     256           0 :             if (err == osl_Process_E_NotFound)
     257           0 :                 return retVal;
     258           0 :             if (osl_searchFileURL(plugin.pData, env_path.pData, &url.pData)
     259             :                                 == osl_File_E_None)
     260             : #else
     261             :             if (osl_searchFileURL(plugin.pData, NULL, &url.pData)
     262             :                 == osl_File_E_None)
     263             : #endif
     264           0 :                 retVal = url;
     265             :             else
     266             :                 throw FrameworkException(
     267             :                     JFW_E_ERROR,
     268           0 :                     "[Java framework] Error in function findPlugin (fwkutil.cxx).");
     269           0 :         }
     270             :     }
     271             :     else
     272             :     {
     273           0 :         retVal = sUrl;
     274             :     }
     275       42510 :     return retVal;
     276             : }
     277             : 
     278       42505 : OUString getLibraryLocation()
     279             : {
     280             :     OString sExcMsg("[Java framework] Error in function getLibraryLocation "
     281       42505 :                          "(fwkutil.cxx).");
     282       85010 :     OUString libraryFileUrl;
     283             : 
     284       42505 :     if (!osl::Module::getUrlFromAddress(
     285             :             reinterpret_cast< oslGenericFunction >(getLibraryLocation),
     286       42505 :             libraryFileUrl))
     287           0 :         throw FrameworkException(JFW_E_ERROR, sExcMsg);
     288             : 
     289       85010 :     return getDirFromFile(libraryFileUrl);
     290             : }
     291             : 
     292      212530 : jfw::FileStatus checkFileURL(const OUString & sURL)
     293             : {
     294      212530 :     jfw::FileStatus ret = jfw::FILE_OK;
     295      212530 :     DirectoryItem item;
     296      212530 :     File::RC rc_item = DirectoryItem::get(sURL, item);
     297      212530 :     if (File::E_None == rc_item)
     298             :     {
     299      127520 :         osl::FileStatus status(osl_FileStatus_Mask_Validate);
     300             : 
     301      127520 :         File::RC rc_stat = item.getFileStatus(status);
     302      127520 :         if (File::E_None == rc_stat)
     303             :         {
     304      127520 :             ret = FILE_OK;
     305             :         }
     306           0 :         else if (File::E_NOENT == rc_stat)
     307             :         {
     308           0 :             ret = FILE_DOES_NOT_EXIST;
     309             :         }
     310             :         else
     311             :         {
     312           0 :             ret = FILE_INVALID;
     313      127520 :         }
     314             :     }
     315       85010 :     else if (File::E_NOENT == rc_item)
     316             :     {
     317       85010 :         ret = FILE_DOES_NOT_EXIST;
     318             :     }
     319             :     else
     320             :     {
     321           0 :         ret = FILE_INVALID;
     322             :     }
     323      212530 :     return ret;
     324             : }
     325             : 
     326             : }
     327             : 
     328             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10