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

Generated by: LCOV version 1.10