LibreOffice Module connectivity (master) 1
CommonTools.cxx
Go to the documentation of this file.
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_java.h>
21
24#include <com/sun/star/beans/XPropertySet.hpp>
25#include <com/sun/star/java/JavaVirtualMachine.hpp>
26#if HAVE_FEATURE_JAVA
28#endif
29#include <osl/diagnose.h>
30#include <rtl/character.hxx>
31#include <rtl/process.h>
33
34using namespace ::comphelper;
36{
37 return ch >= 0x0061 && ch <= 0x007a ? ch + 0x20 : ch;
38}
39
40namespace connectivity
41{
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::java;
46 using namespace dbtools;
47
50
51 bool match(const sal_Unicode* pWild, const sal_Unicode* pStr, const sal_Unicode cEscape)
52 {
53 int pos=0;
54 int flag=0;
55
56 while ( *pWild || flag )
57 {
58 switch (*pWild)
59 {
60 case CHAR_PLACE:
61 if ( *pStr == 0 )
62 return false;
63 break;
64 default:
65 if (*pWild && (*pWild == cEscape) && ((*(pWild+1)== CHAR_PLACE) || (*(pWild+1) == CHAR_WILD)) )
66 pWild++;
67 if ( rtl_ascii_toUpperCase(*pWild) != rtl_ascii_toUpperCase(*pStr) )
68 if ( !pos )
69 return false;
70 else
71 pWild += pos;
72 else
73 break;
74 // WARNING/TODO: in certain circumstances it will run into
75 // the next 'case'!
76 [[fallthrough]];
77 case CHAR_WILD:
78 while ( *pWild == CHAR_WILD )
79 pWild++;
80 if ( *pWild == 0 )
81 return true;
82 flag = 1;
83 pos = 0;
84 if ( *pStr == 0 )
85 return ( *pWild == 0 );
86 while ( *pStr && *pStr != *pWild )
87 {
88 if ( *pWild == CHAR_PLACE ) {
89 pWild++;
90 while ( *pWild == CHAR_WILD )
91 pWild++;
92 }
93 pStr++;
94 if ( *pStr == 0 )
95 return ( *pWild == 0 );
96 }
97 break;
98 }
99 if ( *pWild != 0 )
100 pWild++;
101 if ( *pStr != 0 )
102 pStr++;
103 else
104 flag = 0;
105 if ( flag )
106 pos--;
107 }
108 return ( *pStr == 0 ) && ( *pWild == 0 );
109 }
110
111#if HAVE_FEATURE_JAVA
112 ::rtl::Reference< jvmaccess::VirtualMachine > getJavaVM(const Reference<XComponentContext >& _rxContext)
113 {
115 OSL_ENSURE(_rxContext.is(),"No XMultiServiceFactory a.v.!");
116 if(!_rxContext.is())
117 return aRet;
118
119 try
120 {
121 Reference< XJavaVM > xVM = JavaVirtualMachine::create(_rxContext);
122
123 Sequence<sal_Int8> processID(17); // 16 + 1
124 auto pprocessID = processID.getArray();
125 rtl_getGlobalProcessId( reinterpret_cast<sal_uInt8*>(pprocessID) );
126 pprocessID[16] = 0; // RETURN_VIRTUALMACHINE
127
128 Any uaJVM = xVM->getJavaVM( processID );
129 sal_Int64 nTemp;
130 if (!(uaJVM >>= nTemp)) {
131 throw Exception("cannot get result for getJavaVM", nullptr); // -5
132 }
133 aRet = reinterpret_cast<jvmaccess::VirtualMachine *>(
134 static_cast<sal_IntPtr>(nTemp));
135 }
136 catch (Exception&)
137 {
138 TOOLS_WARN_EXCEPTION("connectivity.commontools", "getJavaVM failed:");
139 }
140
141 return aRet;
142 }
143
144 bool existsJavaClassByName( const ::rtl::Reference< jvmaccess::VirtualMachine >& _pJVM,std::u16string_view _sClassName )
145 {
146 bool bRet = false;
147 if ( _pJVM.is() )
148 {
150 JNIEnv* pEnv = aGuard.getEnvironment();
151 if( pEnv )
152 {
153 OString sClassName = OUStringToOString(_sClassName, RTL_TEXTENCODING_ASCII_US);
154 sClassName = sClassName.replace('.','/');
155 jobject out = pEnv->FindClass(sClassName.getStr());
156 bRet = out != nullptr;
157 pEnv->DeleteLocalRef( out );
158 }
159 }
160 return bRet;
161 }
162#endif
163}
164
165namespace dbtools
166{
167
168static bool isCharOk(sal_Unicode c, std::u16string_view _rSpecials)
169{
170
171 return ( ((c >= 97) && (c <= 122)) || ((c >= 65) && (c <= 90)) || ((c >= 48) && (c <= 57)) ||
172 c == '_' || _rSpecials.find(c) != std::u16string_view::npos);
173}
174
175
176bool isValidSQLName(const OUString& rName, std::u16string_view _rSpecials)
177{
178 // Test for correct naming (in SQL sense)
179 // This is important for table names for example
180 const sal_Unicode* pStr = rName.getStr();
181 if (*pStr > 127 || rtl::isAsciiDigit(*pStr))
182 return false;
183
184 for (; *pStr; ++pStr )
185 if(!isCharOk(*pStr,_rSpecials))
186 return false;
187
188 if ( !rName.isEmpty()
189 && ( (rName.toChar() == '_')
190 || ( (rName.toChar() >= '0')
191 && (rName.toChar() <= '9')
192 )
193 )
194 )
195 return false;
196 // the SQL-Standard requires the first character to be an alphabetic character, which
197 // isn't easy to decide in UniCode...
198 // So we just prohibit the characters which already lead to problems...
199 // 11.04.00 - 74902 - FS
200
201 return true;
202}
203
204// Creates a new name if necessary
205OUString convertName2SQLName(const OUString& rName, std::u16string_view _rSpecials)
206{
207 if(isValidSQLName(rName,_rSpecials))
208 return rName;
209
210 const sal_Unicode* pStr = rName.getStr();
211 // if not valid
212 if (*pStr >= 128 || rtl::isAsciiDigit(*pStr))
213 return OUString();
214
215 OUStringBuffer aNewName(rName);
216 sal_Int32 nLength = rName.getLength();
217 for (sal_Int32 i=0; i < nLength; ++i)
218 if(!isCharOk(aNewName[i],_rSpecials))
219 aNewName[i] = '_';
220
221 return aNewName.makeStringAndClear();
222}
223
224OUString quoteName(std::u16string_view _rQuote, const OUString& _rName)
225{
226 OUString sName = _rName;
227 if( !_rQuote.empty() && _rQuote[0] != ' ')
228 sName = _rQuote + _rName + _rQuote;
229 return sName;
230}
231
232
233}
234
235/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static sal_Unicode rtl_ascii_toUpperCase(sal_Unicode ch)
Definition: CommonTools.cxx:35
#define TOOLS_WARN_EXCEPTION(area, stream)
OUString sName
@ Exception
const sal_Unicode CHAR_PLACE
Definition: CommonTools.cxx:48
const sal_Unicode CHAR_WILD
Definition: CommonTools.cxx:49
bool match(const sal_Unicode *pWild, const sal_Unicode *pStr, const sal_Unicode cEscape)
Definition: CommonTools.cxx:51
bool isValidSQLName(const OUString &rName, std::u16string_view _rSpecials)
checks whether the given name is a valid SQL name
OUString quoteName(std::u16string_view _rQuote, const OUString &_rName)
quote the given name with the given quote string.
OUString convertName2SQLName(const OUString &rName, std::u16string_view _rSpecials)
create a name which is a valid SQL 92 identifier name
static bool isCharOk(sal_Unicode c, std::u16string_view _rSpecials)
int i
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Definition: pq_tools.cxx:100
unsigned char sal_uInt8
sal_uInt16 sal_Unicode
size_t pos
sal_Int32 nLength