update dev300-m58
[ooovba.git] / connectivity / source / commontools / CommonTools.cxx
blob63d8594cdde0ea11434de466d45476cea20914ec
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CommonTools.cxx,v $
10 * $Revision: 1.26.56.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #include <stdio.h>
35 #include "connectivity/CommonTools.hxx"
36 #include "connectivity/dbtools.hxx"
37 #include <com/sun/star/util/Date.hpp>
38 #include <com/sun/star/util/Time.hpp>
39 #include <com/sun/star/util/DateTime.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/lang/XComponent.hpp>
42 #include <comphelper/extract.hxx>
43 #include <cppuhelper/interfacecontainer.h>
44 #include "TConnection.hxx"
45 #include <comphelper/types.hxx>
46 #include <com/sun/star/java/XJavaVM.hpp>
47 #include <rtl/process.h>
49 using namespace ::comphelper;
50 inline sal_Unicode rtl_ascii_toUpperCase( sal_Unicode ch )
52 return ch >= 0x0061 && ch <= 0x007a ? ch + 0x20 : ch;
55 namespace connectivity
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::beans;
60 using namespace dbtools;
61 namespace starjava = com::sun::star::java;
62 //------------------------------------------------------------------------------
63 const sal_Unicode CHAR_PLACE = '_';
64 const sal_Unicode CHAR_WILD = '%';
65 // -------------------------------------------------------------------------
66 sal_Bool match(const sal_Unicode* pWild, const sal_Unicode* pStr, const sal_Unicode cEscape)
68 int pos=0;
69 int flag=0;
71 while ( *pWild || flag )
73 switch (*pWild)
75 case CHAR_PLACE:
76 if ( *pStr == 0 )
77 return sal_False;
78 break;
79 default:
80 if (*pWild && (*pWild == cEscape) && ((*(pWild+1)== CHAR_PLACE) || (*(pWild+1) == CHAR_WILD)) )
81 pWild++;
82 if ( rtl_ascii_toUpperCase(*pWild) != rtl_ascii_toUpperCase(*pStr) )
83 if ( !pos )
84 return sal_False;
85 else
86 pWild += pos;
87 else
88 break; // ACHTUNG laeuft unter bestimmten
89 // Umstaenden in den nachsten case rein!!
90 case CHAR_WILD:
91 while ( *pWild == CHAR_WILD )
92 pWild++;
93 if ( *pWild == 0 )
94 return sal_True;
95 flag = 1;
96 pos = 0;
97 if ( *pStr == 0 )
98 return ( *pWild == 0 );
99 while ( *pStr && *pStr != *pWild )
101 if ( *pWild == CHAR_PLACE ) {
102 pWild++;
103 while ( *pWild == CHAR_WILD )
104 pWild++;
106 pStr++;
107 if ( *pStr == 0 )
108 return ( *pWild == 0 );
110 break;
112 if ( *pWild != 0 )
113 pWild++;
114 if ( *pStr != 0 )
115 pStr++;
116 else
117 flag = 0;
118 if ( flag )
119 pos--;
121 return ( *pStr == 0 ) && ( *pWild == 0 );
123 //------------------------------------------------------------------
124 rtl::OUString toDateString(const ::com::sun::star::util::Date& rDate)
126 sal_Char s[11];
127 snprintf(s,
128 sizeof(s),
129 "%04d-%02d-%02d",
130 (int)rDate.Year,
131 (int)rDate.Month,
132 (int)rDate.Day);
133 s[10] = 0;
134 return rtl::OUString::createFromAscii(s);
137 //------------------------------------------------------------------
138 rtl::OUString toTimeString(const ::com::sun::star::util::Time& rTime)
140 sal_Char s[9];
141 snprintf(s,
142 sizeof(s),
143 "%02d:%02d:%02d",
144 (int)rTime.Hours,
145 (int)rTime.Minutes,
146 (int)rTime.Seconds);
147 s[8] = 0;
148 return rtl::OUString::createFromAscii(s);
151 //------------------------------------------------------------------
152 rtl::OUString toDateTimeString(const ::com::sun::star::util::DateTime& rDateTime)
154 sal_Char s[21];
155 snprintf(s,
156 sizeof(s),
157 "%04d-%02d-%02d %02d:%02d:%02d",
158 (int)rDateTime.Year,
159 (int)rDateTime.Month,
160 (int)rDateTime.Day,
161 (int)rDateTime.Hours,
162 (int)rDateTime.Minutes,
163 (int)rDateTime.Seconds);
164 s[20] = 0;
165 return rtl::OUString::createFromAscii(s);
169 //--------------------------------------------------------------------------------------------------
170 rtl::OUString toString(const Any& rValue)
172 rtl::OUString aRes;
173 TypeClass aDestinationClass = rValue.getValueType().getTypeClass();
175 switch (aDestinationClass)
177 case TypeClass_CHAR:
178 aRes = ::rtl::OUString::valueOf(*(sal_Unicode*)rValue.getValue());
179 break;
180 case TypeClass_FLOAT:
181 aRes = ::rtl::OUString::valueOf(*(float*)rValue.getValue());
182 break;
183 case TypeClass_DOUBLE:
184 aRes = ::rtl::OUString::valueOf(*(double*)rValue.getValue());
185 break;
186 case TypeClass_BOOLEAN:
187 aRes = ::rtl::OUString::valueOf((sal_Int32)*(sal_Bool*)rValue.getValue());
188 break;
189 case TypeClass_BYTE:
190 case TypeClass_SHORT:
191 case TypeClass_LONG:
192 aRes = ::rtl::OUString::valueOf(*(sal_Int32*)rValue.getValue());
193 break;
194 case TypeClass_HYPER:
196 sal_Int64 nValue = 0;
197 OSL_VERIFY( rValue >>= nValue );
198 aRes = ::rtl::OUString::valueOf(nValue);
200 case TypeClass_STRING:
201 rValue >>= aRes;
202 break;
203 case TypeClass_STRUCT:
204 if (rValue.getValueType() == ::getCppuType((const ::com::sun::star::util::Date*)0))
206 ::com::sun::star::util::Date aDate;
207 rValue >>= aDate;
208 aRes = toDateString(aDate);
210 else if (rValue.getValueType() == ::getCppuType((const ::com::sun::star::util::DateTime*)0))
212 ::com::sun::star::util::DateTime aDT;
213 rValue >>= aDT;
214 aRes = toDateTimeString(aDT);
216 else if (rValue.getValueType() == ::getCppuType((const ::com::sun::star::util::Time*)0))
218 ::com::sun::star::util::Time aTime;
219 rValue >>= aTime;
220 aRes = toTimeString(aTime);
223 break;
224 default:
227 return aRes;
230 // -----------------------------------------------------------------------------
231 ::rtl::Reference< jvmaccess::VirtualMachine > getJavaVM(const Reference<XMultiServiceFactory >& _rxFactory)
233 ::rtl::Reference< jvmaccess::VirtualMachine > aRet;
234 OSL_ENSURE(_rxFactory.is(),"No XMultiServiceFactory a.v.!");
235 if(!_rxFactory.is())
236 return aRet;
240 Reference< starjava::XJavaVM > xVM(_rxFactory->createInstance(
241 rtl::OUString::createFromAscii("com.sun.star.java.JavaVirtualMachine")), UNO_QUERY);
243 OSL_ENSURE(_rxFactory.is(),"InitJava: I have no factory!");
244 if (!xVM.is() || !_rxFactory.is())
245 throw Exception(); // -2;
247 Sequence<sal_Int8> processID(16);
248 rtl_getGlobalProcessId( (sal_uInt8*) processID.getArray() );
249 processID.realloc(17);
250 processID[16] = 0;
252 Any uaJVM = xVM->getJavaVM( processID );
254 if (!uaJVM.hasValue())
255 throw Exception(); // -5
256 else
258 sal_Int32 nValue = 0;
259 jvmaccess::VirtualMachine* pJVM = NULL;
260 if ( uaJVM >>= nValue )
261 pJVM = reinterpret_cast< jvmaccess::VirtualMachine* > (nValue);
262 else
264 sal_Int64 nTemp = 0;
265 uaJVM >>= nTemp;
266 pJVM = reinterpret_cast< jvmaccess::VirtualMachine* > (nTemp);
268 aRet = pJVM;
271 catch (Exception&)
275 return aRet;
277 //------------------------------------------------------------------------------
278 sal_Bool existsJavaClassByName( const ::rtl::Reference< jvmaccess::VirtualMachine >& _pJVM,const ::rtl::OUString& _sClassName )
280 sal_Bool bRet = sal_False;
281 #ifdef SOLAR_JAVA
282 if ( _pJVM.is() )
284 jvmaccess::VirtualMachine::AttachGuard aGuard(_pJVM);
285 JNIEnv* pEnv = aGuard.getEnvironment();
286 if( pEnv )
288 ::rtl::OString sClassName = ::rtl::OUStringToOString(_sClassName, RTL_TEXTENCODING_ASCII_US);
289 sClassName = sClassName.replace('.','/');
290 jobject out = pEnv->FindClass(sClassName);
291 bRet = out != NULL;
292 pEnv->DeleteLocalRef( out );
295 #endif
296 return bRet;
301 #include <ctype.h> //isdigit
302 namespace dbtools
304 //------------------------------------------------------------------
305 sal_Bool isCharOk(sal_Unicode c,const ::rtl::OUString& _rSpecials)
308 return ( ((c >= 97) && (c <= 122)) || ((c >= 65) && (c <= 90)) || ((c >= 48) && (c <= 57)) ||
309 c == '_' || _rSpecials.indexOf(c) != -1);
312 //------------------------------------------------------------------------------
313 sal_Bool isValidSQLName(const ::rtl::OUString& rName,const ::rtl::OUString& _rSpecials)
315 // Ueberpruefung auf korrekte Namensgebung im SQL Sinne
316 // Dieses ist wichtig fuer Tabellennamen beispielsweise
317 const sal_Unicode* pStr = rName.getStr();
318 if (*pStr > 127 || isdigit(*pStr))
319 return sal_False;
321 for (; *pStr; ++pStr )
322 if(!isCharOk(*pStr,_rSpecials))
323 return sal_False;
325 if ( rName.getLength()
326 && ( (rName.toChar() == '_')
327 || ( (rName.toChar() >= '0')
328 && (rName.toChar() <= '9')
332 return sal_False;
333 // the SQL-Standard requires the first character to be an alphabetic character, which
334 // isn't easy to decide in UniCode ...
335 // So we just prohibit the characters which already lead to problems ....
336 // 11.04.00 - 74902 - FS
338 return sal_True;
340 //------------------------------------------------------------------
341 // Erzeugt einen neuen Namen falls noetig
342 ::rtl::OUString convertName2SQLName(const ::rtl::OUString& rName,const ::rtl::OUString& _rSpecials)
344 if(isValidSQLName(rName,_rSpecials))
345 return rName;
346 ::rtl::OUString aNewName(rName);
347 const sal_Unicode* pStr = rName.getStr();
348 sal_Int32 nLength = rName.getLength();
349 sal_Bool bValid(*pStr < 128 && !isdigit(*pStr));
350 for (sal_Int32 i=0; bValid && i < nLength; ++pStr,++i )
351 if(!isCharOk(*pStr,_rSpecials))
353 aNewName = aNewName.replace(*pStr,'_');
354 pStr = aNewName.getStr() + i;
357 if ( !bValid )
358 aNewName = ::rtl::OUString();
360 return aNewName;
362 //------------------------------------------------------------------------------
363 ::rtl::OUString quoteName(const ::rtl::OUString& _rQuote, const ::rtl::OUString& _rName)
365 ::rtl::OUString sName = _rName;
366 if(_rQuote.getLength() && _rQuote.toChar() != ' ')
367 sName = _rQuote + _rName + _rQuote;
368 return sName;