Bump for 3.6-28
[LibreOffice.git] / connectivity / source / commontools / CommonTools.cxx
blob3fbafceaef95c26f5029d69555806f1e50e4b6cc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <stdio.h>
31 #include "connectivity/CommonTools.hxx"
32 #include "connectivity/dbtools.hxx"
33 #include <com/sun/star/util/Date.hpp>
34 #include <com/sun/star/util/Time.hpp>
35 #include <com/sun/star/util/DateTime.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/lang/XComponent.hpp>
38 #include <comphelper/extract.hxx>
39 #include <cppuhelper/interfacecontainer.h>
40 #include "TConnection.hxx"
41 #include <comphelper/types.hxx>
42 #include <com/sun/star/java/XJavaVM.hpp>
43 #include <rtl/process.h>
45 using namespace ::comphelper;
46 inline sal_Unicode rtl_ascii_toUpperCase( sal_Unicode ch )
48 return ch >= 0x0061 && ch <= 0x007a ? ch + 0x20 : ch;
51 namespace connectivity
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::lang;
55 using namespace ::com::sun::star::beans;
56 using namespace dbtools;
57 namespace starjava = com::sun::star::java;
58 //------------------------------------------------------------------------------
59 const sal_Unicode CHAR_PLACE = '_';
60 const sal_Unicode CHAR_WILD = '%';
61 // -------------------------------------------------------------------------
62 sal_Bool match(const sal_Unicode* pWild, const sal_Unicode* pStr, const sal_Unicode cEscape)
64 int pos=0;
65 int flag=0;
67 while ( *pWild || flag )
69 switch (*pWild)
71 case CHAR_PLACE:
72 if ( *pStr == 0 )
73 return sal_False;
74 break;
75 default:
76 if (*pWild && (*pWild == cEscape) && ((*(pWild+1)== CHAR_PLACE) || (*(pWild+1) == CHAR_WILD)) )
77 pWild++;
78 if ( rtl_ascii_toUpperCase(*pWild) != rtl_ascii_toUpperCase(*pStr) )
79 if ( !pos )
80 return sal_False;
81 else
82 pWild += pos;
83 else
84 break; // WARNING in certain circumstances
85 // it will run into the next 'case'!!
86 case CHAR_WILD:
87 while ( *pWild == CHAR_WILD )
88 pWild++;
89 if ( *pWild == 0 )
90 return sal_True;
91 flag = 1;
92 pos = 0;
93 if ( *pStr == 0 )
94 return ( *pWild == 0 );
95 while ( *pStr && *pStr != *pWild )
97 if ( *pWild == CHAR_PLACE ) {
98 pWild++;
99 while ( *pWild == CHAR_WILD )
100 pWild++;
102 pStr++;
103 if ( *pStr == 0 )
104 return ( *pWild == 0 );
106 break;
108 if ( *pWild != 0 )
109 pWild++;
110 if ( *pStr != 0 )
111 pStr++;
112 else
113 flag = 0;
114 if ( flag )
115 pos--;
117 return ( *pStr == 0 ) && ( *pWild == 0 );
119 //------------------------------------------------------------------
120 rtl::OUString toDateString(const ::com::sun::star::util::Date& rDate)
122 sal_Char s[11];
123 snprintf(s,
124 sizeof(s),
125 "%04d-%02d-%02d",
126 (int)rDate.Year,
127 (int)rDate.Month,
128 (int)rDate.Day);
129 s[10] = 0;
130 return rtl::OUString::createFromAscii(s);
133 //------------------------------------------------------------------
134 rtl::OUString toTimeString(const ::com::sun::star::util::Time& rTime)
136 sal_Char s[9];
137 snprintf(s,
138 sizeof(s),
139 "%02d:%02d:%02d",
140 (int)rTime.Hours,
141 (int)rTime.Minutes,
142 (int)rTime.Seconds);
143 s[8] = 0;
144 return rtl::OUString::createFromAscii(s);
147 //------------------------------------------------------------------
148 rtl::OUString toDateTimeString(const ::com::sun::star::util::DateTime& rDateTime)
150 sal_Char s[20];
151 snprintf(s,
152 sizeof(s),
153 "%04d-%02d-%02d %02d:%02d:%02d",
154 (int)rDateTime.Year,
155 (int)rDateTime.Month,
156 (int)rDateTime.Day,
157 (int)rDateTime.Hours,
158 (int)rDateTime.Minutes,
159 (int)rDateTime.Seconds);
160 s[19] = 0;
161 return rtl::OUString::createFromAscii(s);
165 //--------------------------------------------------------------------------------------------------
166 rtl::OUString toString(const Any& rValue)
168 rtl::OUString aRes;
169 TypeClass aDestinationClass = rValue.getValueType().getTypeClass();
171 switch (aDestinationClass)
173 case TypeClass_CHAR:
174 aRes = ::rtl::OUString::valueOf(*(sal_Unicode*)rValue.getValue());
175 break;
176 case TypeClass_FLOAT:
177 aRes = ::rtl::OUString::valueOf(*(float*)rValue.getValue());
178 break;
179 case TypeClass_DOUBLE:
180 aRes = ::rtl::OUString::valueOf(*(double*)rValue.getValue());
181 break;
182 case TypeClass_BOOLEAN:
183 aRes = ::rtl::OUString::valueOf((sal_Int32)*(sal_Bool*)rValue.getValue());
184 break;
185 case TypeClass_BYTE:
186 case TypeClass_SHORT:
187 case TypeClass_LONG:
188 aRes = ::rtl::OUString::valueOf(*(sal_Int32*)rValue.getValue());
189 break;
190 case TypeClass_HYPER:
192 sal_Int64 nValue = 0;
193 OSL_VERIFY( rValue >>= nValue );
194 aRes = ::rtl::OUString::valueOf(nValue);
196 case TypeClass_STRING:
197 rValue >>= aRes;
198 break;
199 case TypeClass_STRUCT:
200 if (rValue.getValueType() == ::getCppuType((const ::com::sun::star::util::Date*)0))
202 ::com::sun::star::util::Date aDate;
203 rValue >>= aDate;
204 aRes = toDateString(aDate);
206 else if (rValue.getValueType() == ::getCppuType((const ::com::sun::star::util::DateTime*)0))
208 ::com::sun::star::util::DateTime aDT;
209 rValue >>= aDT;
210 aRes = toDateTimeString(aDT);
212 else if (rValue.getValueType() == ::getCppuType((const ::com::sun::star::util::Time*)0))
214 ::com::sun::star::util::Time aTime;
215 rValue >>= aTime;
216 aRes = toTimeString(aTime);
219 break;
220 default:
223 return aRes;
226 // -----------------------------------------------------------------------------
227 ::rtl::Reference< jvmaccess::VirtualMachine > getJavaVM(const Reference<XMultiServiceFactory >& _rxFactory)
229 ::rtl::Reference< jvmaccess::VirtualMachine > aRet;
230 OSL_ENSURE(_rxFactory.is(),"No XMultiServiceFactory a.v.!");
231 if(!_rxFactory.is())
232 return aRet;
236 Reference< starjava::XJavaVM > xVM(_rxFactory->createInstance(
237 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine"))), UNO_QUERY);
239 OSL_ENSURE(_rxFactory.is(),"InitJava: I have no factory!");
240 if (!xVM.is() || !_rxFactory.is())
241 throw Exception(); // -2;
243 Sequence<sal_Int8> processID(16);
244 rtl_getGlobalProcessId( (sal_uInt8*) processID.getArray() );
245 processID.realloc(17);
246 processID[16] = 0;
248 Any uaJVM = xVM->getJavaVM( processID );
250 if (!uaJVM.hasValue())
251 throw Exception(); // -5
252 else
254 sal_Int32 nValue = 0;
255 jvmaccess::VirtualMachine* pJVM = NULL;
256 if ( uaJVM >>= nValue )
257 pJVM = reinterpret_cast< jvmaccess::VirtualMachine* > (nValue);
258 else
260 sal_Int64 nTemp = 0;
261 uaJVM >>= nTemp;
262 pJVM = reinterpret_cast< jvmaccess::VirtualMachine* > (nTemp);
264 aRet = pJVM;
267 catch (Exception&)
271 return aRet;
273 //------------------------------------------------------------------------------
274 sal_Bool existsJavaClassByName( const ::rtl::Reference< jvmaccess::VirtualMachine >& _pJVM,const ::rtl::OUString& _sClassName )
276 sal_Bool bRet = sal_False;
277 #ifdef SOLAR_JAVA
278 if ( _pJVM.is() )
280 jvmaccess::VirtualMachine::AttachGuard aGuard(_pJVM);
281 JNIEnv* pEnv = aGuard.getEnvironment();
282 if( pEnv )
284 ::rtl::OString sClassName = ::rtl::OUStringToOString(_sClassName, RTL_TEXTENCODING_ASCII_US);
285 sClassName = sClassName.replace('.','/');
286 jobject out = pEnv->FindClass(sClassName.getStr());
287 bRet = out != NULL;
288 pEnv->DeleteLocalRef( out );
291 #else
292 (void)_pJVM;
293 (void)_sClassName;
294 #endif
295 return bRet;
300 #include <ctype.h> //isdigit
301 namespace dbtools
303 //------------------------------------------------------------------
304 sal_Bool isCharOk(sal_Unicode c,const ::rtl::OUString& _rSpecials)
307 return ( ((c >= 97) && (c <= 122)) || ((c >= 65) && (c <= 90)) || ((c >= 48) && (c <= 57)) ||
308 c == '_' || _rSpecials.indexOf(c) != -1);
311 //------------------------------------------------------------------------------
312 sal_Bool isValidSQLName(const ::rtl::OUString& rName,const ::rtl::OUString& _rSpecials)
314 // Test for correct naming (in SQL sense)
315 // This is important for table names for example
316 const sal_Unicode* pStr = rName.getStr();
317 if (*pStr > 127 || isdigit(*pStr))
318 return sal_False;
320 for (; *pStr; ++pStr )
321 if(!isCharOk(*pStr,_rSpecials))
322 return sal_False;
324 if ( !rName.isEmpty()
325 && ( (rName.toChar() == '_')
326 || ( (rName.toChar() >= '0')
327 && (rName.toChar() <= '9')
331 return sal_False;
332 // the SQL-Standard requires the first character to be an alphabetic character, which
333 // isn't easy to decide in UniCode ...
334 // So we just prohibit the characters which already lead to problems ....
335 // 11.04.00 - 74902 - FS
337 return sal_True;
339 //------------------------------------------------------------------
340 // Creates a new name if necessary
341 ::rtl::OUString convertName2SQLName(const ::rtl::OUString& rName,const ::rtl::OUString& _rSpecials)
343 if(isValidSQLName(rName,_rSpecials))
344 return rName;
345 ::rtl::OUString aNewName(rName);
346 const sal_Unicode* pStr = rName.getStr();
347 sal_Int32 nLength = rName.getLength();
348 sal_Bool bValid(*pStr < 128 && !isdigit(*pStr));
349 for (sal_Int32 i=0; bValid && i < nLength; ++pStr,++i )
350 if(!isCharOk(*pStr,_rSpecials))
352 aNewName = aNewName.replace(*pStr,'_');
353 pStr = aNewName.getStr() + i;
356 if ( !bValid )
357 aNewName = ::rtl::OUString();
359 return aNewName;
361 //------------------------------------------------------------------------------
362 ::rtl::OUString quoteName(const ::rtl::OUString& _rQuote, const ::rtl::OUString& _rName)
364 ::rtl::OUString sName = _rName;
365 if( !_rQuote.isEmpty() && _rQuote.toChar() != ' ')
366 sName = _rQuote + _rName + _rQuote;
367 return sName;
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */