tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / connectivity / source / commontools / CommonTools.cxx
blobcae16e17eb6eefe74fed848f5313e76933f67b18
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_java.h>
22 #include <connectivity/CommonTools.hxx>
23 #include <connectivity/dbtools.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/java/JavaVirtualMachine.hpp>
26 #if HAVE_FEATURE_JAVA
27 #include <jvmaccess/virtualmachine.hxx>
28 #endif
29 #include <osl/diagnose.h>
30 #include <rtl/character.hxx>
31 #include <rtl/process.h>
32 #include <comphelper/diagnose_ex.hxx>
34 static sal_Unicode rtl_ascii_toUpperCase( sal_Unicode ch )
36 return ch >= 0x0061 && ch <= 0x007a ? ch + 0x20 : ch;
39 namespace connectivity
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::java;
43 using namespace dbtools;
45 const sal_Unicode CHAR_PLACE = '_';
46 const sal_Unicode CHAR_WILD = '%';
48 bool match(const sal_Unicode* pWild, const sal_Unicode* pStr, const sal_Unicode cEscape)
50 int pos=0;
51 int flag=0;
53 while ( *pWild || flag )
55 switch (*pWild)
57 case CHAR_PLACE:
58 if ( *pStr == 0 )
59 return false;
60 break;
61 default:
62 if (*pWild && (*pWild == cEscape) && ((*(pWild+1)== CHAR_PLACE) || (*(pWild+1) == CHAR_WILD)) )
63 pWild++;
64 if ( rtl_ascii_toUpperCase(*pWild) != rtl_ascii_toUpperCase(*pStr) )
65 if ( !pos )
66 return false;
67 else
68 pWild += pos;
69 else
70 break;
71 // WARNING/TODO: in certain circumstances it will run into
72 // the next 'case'!
73 [[fallthrough]];
74 case CHAR_WILD:
75 while ( *pWild == CHAR_WILD )
76 pWild++;
77 if ( *pWild == 0 )
78 return true;
79 flag = 1;
80 pos = 0;
81 if ( *pStr == 0 )
82 return ( *pWild == 0 );
83 while ( *pStr && *pStr != *pWild )
85 if ( *pWild == CHAR_PLACE ) {
86 pWild++;
87 while ( *pWild == CHAR_WILD )
88 pWild++;
90 pStr++;
91 if ( *pStr == 0 )
92 return ( *pWild == 0 );
94 break;
96 if ( *pWild != 0 )
97 pWild++;
98 if ( *pStr != 0 )
99 pStr++;
100 else
101 flag = 0;
102 if ( flag )
103 pos--;
105 return ( *pStr == 0 ) && ( *pWild == 0 );
108 #if HAVE_FEATURE_JAVA
109 ::rtl::Reference< jvmaccess::VirtualMachine > getJavaVM(const Reference<XComponentContext >& _rxContext)
111 ::rtl::Reference< jvmaccess::VirtualMachine > aRet;
112 OSL_ENSURE(_rxContext.is(),"No XMultiServiceFactory a.v.!");
113 if(!_rxContext.is())
114 return aRet;
118 Reference< XJavaVM > xVM = JavaVirtualMachine::create(_rxContext);
120 Sequence<sal_Int8> processID(17); // 16 + 1
121 auto pprocessID = processID.getArray();
122 rtl_getGlobalProcessId( reinterpret_cast<sal_uInt8*>(pprocessID) );
123 pprocessID[16] = 0; // RETURN_VIRTUALMACHINE
125 Any uaJVM = xVM->getJavaVM( processID );
126 sal_Int64 nTemp;
127 if (!(uaJVM >>= nTemp)) {
128 throw Exception(u"cannot get result for getJavaVM"_ustr, nullptr); // -5
130 aRet = reinterpret_cast<jvmaccess::VirtualMachine *>(
131 static_cast<sal_IntPtr>(nTemp));
133 catch (Exception&)
135 TOOLS_WARN_EXCEPTION("connectivity.commontools", "getJavaVM failed:");
138 return aRet;
141 bool existsJavaClassByName( const ::rtl::Reference< jvmaccess::VirtualMachine >& _pJVM,std::u16string_view _sClassName )
143 bool bRet = false;
144 if ( _pJVM.is() )
146 jvmaccess::VirtualMachine::AttachGuard aGuard(_pJVM);
147 JNIEnv* pEnv = aGuard.getEnvironment();
148 if( pEnv )
150 OString sClassName = OUStringToOString(_sClassName, RTL_TEXTENCODING_ASCII_US);
151 sClassName = sClassName.replace('.','/');
152 jobject out = pEnv->FindClass(sClassName.getStr());
153 bRet = out != nullptr;
154 pEnv->DeleteLocalRef( out );
157 return bRet;
159 #endif
162 namespace dbtools
165 static bool isCharOk(sal_Unicode c, std::u16string_view _rSpecials)
168 return ( ((c >= 97) && (c <= 122)) || ((c >= 65) && (c <= 90)) || ((c >= 48) && (c <= 57)) ||
169 c == '_' || _rSpecials.find(c) != std::u16string_view::npos);
173 bool isValidSQLName(const OUString& rName, std::u16string_view _rSpecials)
175 // Test for correct naming (in SQL sense)
176 // This is important for table names for example
177 const sal_Unicode* pStr = rName.getStr();
178 if (*pStr > 127 || rtl::isAsciiDigit(*pStr))
179 return false;
181 for (; *pStr; ++pStr )
182 if(!isCharOk(*pStr,_rSpecials))
183 return false;
185 if ( !rName.isEmpty()
186 && ( (rName.toChar() == '_')
187 || ( (rName.toChar() >= '0')
188 && (rName.toChar() <= '9')
192 return false;
193 // the SQL-Standard requires the first character to be an alphabetic character, which
194 // isn't easy to decide in UniCode...
195 // So we just prohibit the characters which already lead to problems...
196 // 11.04.00 - 74902 - FS
198 return true;
201 // Creates a new name if necessary
202 OUString convertName2SQLName(const OUString& rName, std::u16string_view _rSpecials)
204 if(isValidSQLName(rName,_rSpecials))
205 return rName;
207 const sal_Unicode* pStr = rName.getStr();
208 // if not valid
209 if (*pStr >= 128 || rtl::isAsciiDigit(*pStr))
210 return OUString();
212 OUStringBuffer aNewName(rName);
213 sal_Int32 nLength = rName.getLength();
214 for (sal_Int32 i=0; i < nLength; ++i)
215 if(!isCharOk(aNewName[i],_rSpecials))
216 aNewName[i] = '_';
218 return aNewName.makeStringAndClear();
221 OUString quoteName(std::u16string_view _rQuote, const OUString& _rName)
223 OUString sName = _rName;
224 if( !_rQuote.empty() && _rQuote[0] != ' ')
225 sName = _rQuote + _rName + _rQuote;
226 return sName;
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */