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

Generated by: LCOV version 1.10