LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/connectivity/source/commontools - CommonTools.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 29 113 25.7 %
Date: 2013-07-09 Functions: 3 8 37.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             : #include <stdio.h>
      22             : #include "connectivity/CommonTools.hxx"
      23             : #include "connectivity/dbtools.hxx"
      24             : #include <com/sun/star/util/Date.hpp>
      25             : #include <com/sun/star/util/Time.hpp>
      26             : #include <com/sun/star/util/DateTime.hpp>
      27             : #include <com/sun/star/beans/XPropertySet.hpp>
      28             : #include <com/sun/star/lang/XComponent.hpp>
      29             : #include <comphelper/extract.hxx>
      30             : #include <cppuhelper/interfacecontainer.h>
      31             : #include "TConnection.hxx"
      32             : #include <comphelper/types.hxx>
      33             : #include <com/sun/star/java/JavaVirtualMachine.hpp>
      34             : #ifdef SOLAR_JAVA
      35             : #include <jvmaccess/virtualmachine.hxx>
      36             : #endif
      37             : #include <rtl/process.h>
      38             : 
      39             : using namespace ::comphelper;
      40       10088 : inline sal_Unicode rtl_ascii_toUpperCase( sal_Unicode ch )
      41             : {
      42       10088 :     return ch >= 0x0061 && ch <= 0x007a ? ch + 0x20 : ch;
      43             : }
      44             : 
      45             : namespace connectivity
      46             : {
      47             :     using namespace ::com::sun::star::uno;
      48             :     using namespace ::com::sun::star::lang;
      49             :     using namespace ::com::sun::star::beans;
      50             :     using namespace ::com::sun::star::java;
      51             :     using namespace dbtools;
      52             :     //------------------------------------------------------------------------------
      53             :     const sal_Unicode CHAR_PLACE = '_';
      54             :     const sal_Unicode CHAR_WILD  = '%';
      55             :     // -------------------------------------------------------------------------
      56        3153 :     sal_Bool match(const sal_Unicode* pWild, const sal_Unicode* pStr, const sal_Unicode cEscape)
      57             :     {
      58        3153 :         int    pos=0;
      59        3153 :         int    flag=0;
      60             : 
      61        8574 :         while ( *pWild || flag )
      62             :         {
      63        5259 :             switch (*pWild)
      64             :             {
      65             :                 case CHAR_PLACE:
      66          78 :                     if ( *pStr == 0 )
      67           0 :                         return sal_False;
      68          78 :                     break;
      69             :                 default:
      70        5044 :                     if (*pWild && (*pWild == cEscape) && ((*(pWild+1)== CHAR_PLACE) || (*(pWild+1) == CHAR_WILD)) )
      71           0 :                         pWild++;
      72        5044 :                     if ( rtl_ascii_toUpperCase(*pWild) != rtl_ascii_toUpperCase(*pStr) )
      73        2854 :                         if ( !pos )
      74        2854 :                             return sal_False;
      75             :                         else
      76           0 :                             pWild += pos;
      77             :                     else
      78        2190 :                         break;          // WARNING in certain circumstances
      79             :                 // it will run into the next 'case'!!
      80             :                 case CHAR_WILD:
      81         411 :                     while ( *pWild == CHAR_WILD )
      82         137 :                         pWild++;
      83         137 :                     if ( *pWild == 0 )
      84         137 :                         return sal_True;
      85           0 :                     flag = 1;
      86           0 :                     pos  = 0;
      87           0 :                     if ( *pStr == 0 )
      88           0 :                         return ( *pWild == 0 );
      89           0 :                     while ( *pStr && *pStr != *pWild )
      90             :                     {
      91           0 :                         if ( *pWild == CHAR_PLACE ) {
      92           0 :                             pWild++;
      93           0 :                             while ( *pWild == CHAR_WILD )
      94           0 :                                 pWild++;
      95             :                         }
      96           0 :                         pStr++;
      97           0 :                         if ( *pStr == 0 )
      98           0 :                             return ( *pWild == 0 );
      99             :                     }
     100           0 :                     break;
     101             :             }
     102        2268 :             if ( *pWild != 0 )
     103        2268 :                 pWild++;
     104        2268 :             if ( *pStr != 0 )
     105        2268 :                 pStr++;
     106             :             else
     107           0 :                 flag = 0;
     108        2268 :             if ( flag )
     109           0 :                 pos--;
     110             :         }
     111         162 :         return ( *pStr == 0 ) && ( *pWild == 0 );
     112             :     }
     113             :     //------------------------------------------------------------------
     114             : #ifdef SOLAR_JAVA
     115           0 :     ::rtl::Reference< jvmaccess::VirtualMachine > getJavaVM(const Reference<XComponentContext >& _rxContext)
     116             :     {
     117           0 :         ::rtl::Reference< jvmaccess::VirtualMachine > aRet;
     118             :         OSL_ENSURE(_rxContext.is(),"No XMultiServiceFactory a.v.!");
     119           0 :         if(!_rxContext.is())
     120           0 :             return aRet;
     121             : 
     122             :         try
     123             :         {
     124           0 :             Reference< XJavaVM > xVM = JavaVirtualMachine::create(_rxContext);
     125             : 
     126           0 :             Sequence<sal_Int8> processID(16);
     127           0 :             rtl_getGlobalProcessId( (sal_uInt8*) processID.getArray() );
     128           0 :             processID.realloc(17);
     129           0 :             processID[16] = 0;
     130             : 
     131           0 :             Any uaJVM = xVM->getJavaVM( processID );
     132             : 
     133           0 :             if (!uaJVM.hasValue())
     134           0 :                 throw Exception(); // -5
     135             :             else
     136             :             {
     137           0 :                 sal_Int32 nValue = 0;
     138           0 :                 jvmaccess::VirtualMachine* pJVM = NULL;
     139           0 :                 if ( uaJVM >>= nValue )
     140           0 :                     pJVM = reinterpret_cast< jvmaccess::VirtualMachine* > (nValue);
     141             :                 else
     142             :                 {
     143           0 :                     sal_Int64 nTemp = 0;
     144           0 :                     uaJVM >>= nTemp;
     145           0 :                     pJVM = reinterpret_cast< jvmaccess::VirtualMachine* > (nTemp);
     146             :                 }
     147           0 :                 aRet = pJVM;
     148           0 :             }
     149             :         }
     150           0 :         catch (Exception&)
     151             :         {
     152             :         }
     153             : 
     154           0 :         return aRet;
     155             :     }
     156             :     //------------------------------------------------------------------------------
     157           0 :     sal_Bool existsJavaClassByName( const ::rtl::Reference< jvmaccess::VirtualMachine >& _pJVM,const OUString& _sClassName )
     158             :     {
     159           0 :         sal_Bool bRet = sal_False;
     160           0 :         if ( _pJVM.is() )
     161             :         {
     162           0 :             jvmaccess::VirtualMachine::AttachGuard aGuard(_pJVM);
     163           0 :             JNIEnv* pEnv = aGuard.getEnvironment();
     164           0 :             if( pEnv )
     165             :             {
     166           0 :                 OString sClassName = OUStringToOString(_sClassName, RTL_TEXTENCODING_ASCII_US);
     167           0 :                 sClassName = sClassName.replace('.','/');
     168           0 :                 jobject out = pEnv->FindClass(sClassName.getStr());
     169           0 :                 bRet = out != NULL;
     170           0 :                 pEnv->DeleteLocalRef( out );
     171           0 :             }
     172             :         }
     173           0 :         return bRet;
     174             :     }
     175             : #endif
     176             : }
     177             : 
     178             : #include <ctype.h>      //isdigit
     179             : namespace dbtools
     180             : {
     181             : //------------------------------------------------------------------
     182           0 : sal_Bool isCharOk(sal_Unicode c,const OUString& _rSpecials)
     183             : {
     184             : 
     185           0 :     return ( ((c >= 97) && (c <= 122)) || ((c >= 65) && (c <=  90)) || ((c >= 48) && (c <=  57)) ||
     186           0 :           c == '_' || _rSpecials.indexOf(c) != -1);
     187             : }
     188             : 
     189             : //------------------------------------------------------------------------------
     190           0 : sal_Bool isValidSQLName(const OUString& rName,const OUString& _rSpecials)
     191             : {
     192             :     // Test for correct naming (in SQL sense)
     193             :     // This is important for table names for example
     194           0 :     const sal_Unicode* pStr = rName.getStr();
     195           0 :     if (*pStr > 127 || isdigit(*pStr))
     196           0 :         return sal_False;
     197             : 
     198           0 :     for (; *pStr; ++pStr )
     199           0 :         if(!isCharOk(*pStr,_rSpecials))
     200           0 :             return sal_False;
     201             : 
     202           0 :     if  (   !rName.isEmpty()
     203           0 :         &&  (   (rName.toChar() == '_')
     204           0 :             ||  (   (rName.toChar() >= '0')
     205           0 :                 &&  (rName.toChar() <= '9')
     206             :                 )
     207             :             )
     208             :         )
     209           0 :         return sal_False;
     210             :     // the SQL-Standard requires the first character to be an alphabetic character, which
     211             :     // isn't easy to decide in UniCode ...
     212             :     // So we just prohibit the characters which already lead to problems ....
     213             :     // 11.04.00 - 74902 - FS
     214             : 
     215           0 :     return sal_True;
     216             : }
     217             : //------------------------------------------------------------------
     218             : // Creates a new name if necessary
     219           0 : OUString convertName2SQLName(const OUString& rName,const OUString& _rSpecials)
     220             : {
     221           0 :     if(isValidSQLName(rName,_rSpecials))
     222           0 :         return rName;
     223           0 :     OUString aNewName(rName);
     224           0 :     const sal_Unicode* pStr = rName.getStr();
     225           0 :     sal_Int32 nLength = rName.getLength();
     226           0 :     sal_Bool bValid(*pStr < 128 && !isdigit(*pStr));
     227           0 :     for (sal_Int32 i=0; bValid && i < nLength; ++pStr,++i )
     228           0 :         if(!isCharOk(*pStr,_rSpecials))
     229             :         {
     230           0 :             aNewName = aNewName.replace(*pStr,'_');
     231           0 :             pStr = aNewName.getStr() + i;
     232             :         }
     233             : 
     234           0 :     if ( !bValid )
     235           0 :         aNewName = OUString();
     236             : 
     237           0 :     return aNewName;
     238             : }
     239             : //------------------------------------------------------------------------------
     240          35 : OUString quoteName(const OUString& _rQuote, const OUString& _rName)
     241             : {
     242          35 :     OUString sName = _rName;
     243          35 :     if( !_rQuote.isEmpty() && _rQuote.toChar() != ' ')
     244          35 :         sName = _rQuote + _rName + _rQuote;
     245          35 :     return sName;
     246             : }
     247             : 
     248             : 
     249             : }
     250             : 
     251             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10