diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /qadevOOo/runner/util/db | |
parent | Initial commit. (diff) | |
download | libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | qadevOOo/runner/util/db/DataSource.java | 146 | ||||
-rw-r--r-- | qadevOOo/runner/util/db/DataSourceDescriptor.java | 60 | ||||
-rw-r--r-- | qadevOOo/runner/util/db/DatabaseDocument.java | 71 | ||||
-rw-r--r-- | qadevOOo/runner/util/dbg.java | 274 |
4 files changed, 551 insertions, 0 deletions
diff --git a/qadevOOo/runner/util/db/DataSource.java b/qadevOOo/runner/util/db/DataSource.java new file mode 100644 index 000000000..b9f41c332 --- /dev/null +++ b/qadevOOo/runner/util/db/DataSource.java @@ -0,0 +1,146 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package util.db; + +import com.sun.star.beans.XPropertySet; +import com.sun.star.container.NoSuchElementException; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.sdbc.XDataSource; +import com.sun.star.uno.Exception; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XNamingService; +import lib.StatusException; + +/** wraps a com.sun.star.sdb.DataSource + */ +public class DataSource +{ + protected DataSource( XMultiServiceFactory _orb, DataSourceDescriptor _descriptor ) + { + m_orb = _orb; + try + { + m_dataSource = UnoRuntime.queryInterface( XDataSource.class, + m_orb.createInstance( "com.sun.star.sdb.DataSource" ) ); + m_properties = UnoRuntime.queryInterface( XPropertySet.class, + m_dataSource ); + + Object[] descriptorProperties = new Object[] { + null, _descriptor.URL, _descriptor.Info, _descriptor.User, _descriptor.Password, + _descriptor.IsPasswordRequired }; + String[] propertyNames = new String[] { + "Name", "URL", "Info", "User", "Password", "IsPasswordRequired" }; + for ( int i=0; i < descriptorProperties.length; ++i ) + if ( descriptorProperties[i] != null ) + m_properties.setPropertyValue( propertyNames[i], descriptorProperties[i] ); + } + catch ( Exception e ) + { + throw new StatusException( "could not create/fill a css.sdb.DataSource object", e ); + } + } + + public XDataSource getDataSource() + { + return m_dataSource; + } + + /** + * retrieves the css.sdb.OfficeDatabaseDocument associated with the data source + */ + public synchronized DatabaseDocument getDatabaseDocument() + { + if ( m_document == null ) + m_document = new DatabaseDocument( this ); + return m_document; + } + + public void revokeRegistration() + { + String dataSourceName = ""; + try + { + dataSourceName = (String)m_properties.getPropertyValue( "Name" ); + XNamingService dbContext = UnoRuntime.queryInterface( XNamingService.class, + m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) ); + dbContext.revokeObject( dataSourceName ); + } + catch ( Exception e ) + { + throw new StatusException( "DataSource.revokeRegistration: could not revoke the object (" + dataSourceName + ")", e ); + } + } + + public void registerAs( final String _registrationName, final boolean _revokeIfRegistered ) + { + String doing = null; + try + { + doing = "creating database context"; + XNamingService dbContext = UnoRuntime.queryInterface( XNamingService.class, + m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) ); + + if ( _revokeIfRegistered ) + { + doing = "revoking previously registered data source"; + try + { + dbContext.revokeObject( _registrationName ); + } + catch( NoSuchElementException e ) + { /* allowed here */ } + } + + // if the document associated with the database document has not yet been saved, then we need to do so + DatabaseDocument doc = getDatabaseDocument(); + String docURL = doc.getURL(); + if ( docURL.length() == 0 ) + { + final java.io.File tempFile = java.io.File.createTempFile( _registrationName + "_", ".odb" ); + if ( tempFile.exists() ) { + // we did not really want to create that file, we just wanted its local name, but + // createTempFile actually creates it => throw it away + // (This is necessary since some JVM/platform combinations seem to actually lock the file) + boolean bDeleteOk = tempFile.delete(); + if (!bDeleteOk) { + System.out.println("delete failed"); + } + } + String localPart = tempFile.toURI().toURL().toString(); + localPart = localPart.substring( localPart.lastIndexOf( '/' ) + 1 ); + docURL = util.utils.getOfficeTemp( m_orb ) + localPart; + doing = "storing database document to temporary location (" + docURL + ")"; + doc.storeAsURL( docURL ); + } + + // register the data source + doing = "registering the data source at the database context"; + dbContext.registerObject( _registrationName, m_dataSource ); + } + catch( final java.lang.Exception e ) + { + throw new StatusException( "DataSource.registerAs: error during " + doing, e ); + } + } + + private final XMultiServiceFactory m_orb; + private final XDataSource m_dataSource; + private final XPropertySet m_properties; + private DatabaseDocument m_document = null; +} diff --git a/qadevOOo/runner/util/db/DataSourceDescriptor.java b/qadevOOo/runner/util/db/DataSourceDescriptor.java new file mode 100644 index 000000000..6b699445f --- /dev/null +++ b/qadevOOo/runner/util/db/DataSourceDescriptor.java @@ -0,0 +1,60 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package util.db; + +import com.sun.star.beans.PropertyValue; +import com.sun.star.lang.XMultiServiceFactory; + +/** a descriptor for creating a com.sun.star.sdb.DataSource + */ +public class DataSourceDescriptor +{ + /** + * Representation of <code>'URL'</code> property. + */ + public String URL = null ; + /** + * Representation of <code>'Info'</code> property. + */ + public PropertyValue[] Info = null ; + /** + * Representation of <code>'User'</code> property. + */ + public String User = null ; + /** + * Representation of <code>'Password'</code> property. + */ + public String Password = null ; + /** + * Representation of <code>'IsPasswordRequired'</code> property. + */ + public Boolean IsPasswordRequired = null ; + + public DataSourceDescriptor( XMultiServiceFactory _orb ) + { + m_orb = _orb; + } + + public DataSource createDataSource() + { + return new DataSource( m_orb, this ); + } + + private final XMultiServiceFactory m_orb; +} diff --git a/qadevOOo/runner/util/db/DatabaseDocument.java b/qadevOOo/runner/util/db/DatabaseDocument.java new file mode 100644 index 000000000..f9c4913e7 --- /dev/null +++ b/qadevOOo/runner/util/db/DatabaseDocument.java @@ -0,0 +1,71 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package util.db; + +import com.sun.star.beans.PropertyValue; +import com.sun.star.frame.XModel; +import com.sun.star.frame.XStorable; +import com.sun.star.io.IOException; +import com.sun.star.sdb.XDocumentDataSource; +import com.sun.star.sdb.XOfficeDatabaseDocument; +import com.sun.star.uno.UnoRuntime; + +/** + * encapsulates a css.sdb.DatabaseDocument + */ +public class DatabaseDocument +{ + protected DatabaseDocument( final DataSource _dataSource ) + { + XDocumentDataSource docDataSource = UnoRuntime.queryInterface( + XDocumentDataSource.class, _dataSource.getDataSource() ); + m_databaseDocument = UnoRuntime.queryInterface(XOfficeDatabaseDocument.class, + docDataSource.getDatabaseDocument() ); + + m_model = UnoRuntime.queryInterface( XModel.class, m_databaseDocument ); + m_storeDoc = UnoRuntime.queryInterface( XStorable.class, m_databaseDocument ); + } + + public XOfficeDatabaseDocument getDatabaseDocument() + { + return m_databaseDocument; + } + + /** + * passes through to XModel.getURL. + */ + public String getURL() + { + return m_model.getURL(); + } + + /** + * simplified version (taking no arguments except the target URL) of XStorage.storeAsURL + * @param _url + * specifies the location to where to store the document + */ + public void storeAsURL( final String _url ) throws IOException + { + m_storeDoc.storeAsURL( _url, new PropertyValue[] { } ); + } + + private final XOfficeDatabaseDocument m_databaseDocument; + private final XModel m_model; + private final XStorable m_storeDoc; +} diff --git a/qadevOOo/runner/util/dbg.java b/qadevOOo/runner/util/dbg.java new file mode 100644 index 000000000..5416e92f6 --- /dev/null +++ b/qadevOOo/runner/util/dbg.java @@ -0,0 +1,274 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package util; + +import com.sun.star.uno.XInterface; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.Type; +import com.sun.star.beans.XPropertySet; +import com.sun.star.beans.XPropertySetInfo; +import com.sun.star.beans.Property; +import com.sun.star.beans.PropertyAttribute; +import com.sun.star.beans.PropertyValue; +import com.sun.star.lang.XTypeProvider; +import com.sun.star.lang.XServiceInfo; +import java.io.PrintWriter; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Method; + +/** + * This class accumulates all kinds of methods for accessing debug information + * from UNO implementations. + */ +public class dbg { + + /** + * Prints information about the supported interfaces of an implementation + * to standard out. + * @param xTarget The implementation which should be analysed. + * @see com.sun.star.uno.XInterface + */ + public static void printInterfaces(XInterface xTarget) { + printInterfaces(xTarget, false); + } + + /** + * Prints information about the supported interfaces of an implementation + * to standard out. Extended information can be printed. + * @param xTarget The implementation which should be analysed. + * @param extendedInfo Should extended information be printed? + * @see com.sun.star.uno.XInterface + */ + private static void printInterfaces(XInterface xTarget, + boolean extendedInfo){ + Type[] types = getInterfaceTypes(xTarget); + if( null != types ) { + int nLen = types.length; + for( int i = 0; i < nLen ; i++ ) { + System.out.println(types[i].getTypeName()); + if (extendedInfo) { + printInterfaceInfo(types[i]); + System.out.println(); + } + } + } + } + + /** + * Returns all interface types of an implementation as a type array. + * @param xTarget The implementation which should be analyzed. + * @return An array with all interface types; null if there are none. + * @see com.sun.star.uno.XInterface + */ + private static Type[] getInterfaceTypes(XInterface xTarget) { + Type[] types = null; + XTypeProvider xTypeProvider = UnoRuntime.queryInterface( XTypeProvider.class, xTarget); + if( xTypeProvider != null ) + types = xTypeProvider.getTypes(); + return types; + } + + + + /** + * Prints information about an interface type. + * + * @param aType The type of the given interface. + * @see com.sun.star.uno.Type + */ + private static void printInterfaceInfo(Type aType) { + try { + Class<?> zClass = aType.getZClass(); + Method[] methods = zClass.getDeclaredMethods(); + for (int i=0; i<methods.length; i++) { + System.out.println("\t" + methods[i].getReturnType().getName() + + " " + methods[i].getName() + "()"); + } + } + catch (Exception ex) { + System.out.println("Exception occurred while printing InterfaceInfo"); + ex.printStackTrace(); + } + } + + /** + * Prints a string array to standard out. + * + * @param entries : The array to be printed. + */ + public static void printArray( String [] entries ) { + for ( int i=0; i< entries.length;i++ ) { + System.out.println(entries[i]); + } + } + + /** + * Print all information about the property <code>name</code> from + * the property set <code>PS</code> to standard out. + * @param PS The property set which should contain a property called + * <code>name</code>. + * @param name The name of the property. + * @see com.sun.star.beans.XPropertySet + */ + public static void printPropertyInfo(XPropertySet PS, String name) throws UnsupportedEncodingException { + printPropertyInfo(PS, name, new PrintWriter(new OutputStreamWriter(System.out, "UTF-8"))); + } + + /** + * Print all information about the property <code>name</code> from + * the property set <code>PS</code> to a print writer. + * @param PS The property set which should contain a property called + * <code>name</code>. + * @param name The name of the property. + * @param out The print writer which is used as output. + * @see com.sun.star.beans.XPropertySet + */ + public static void printPropertyInfo(XPropertySet PS, String name, + PrintWriter out) { + try { + XPropertySetInfo PSI = PS.getPropertySetInfo(); + PSI.getProperties(); + Property prop = PSI.getPropertyByName(name); + out.println("Property name is " + prop.Name); + out.println("Property handle is " + prop.Handle); + out.println("Property type is " + prop.Type.getTypeName()); + out.println("Property current value is " + + PS.getPropertyValue(name)); + out.println("Attributes :"); + short attr = prop.Attributes; + + if ((attr & PropertyAttribute.BOUND) != 0) + out.println("\t-BOUND"); + + if ((attr & PropertyAttribute.CONSTRAINED) != 0) + out.println("\t-CONSTRAINED"); + + if ((attr & PropertyAttribute.MAYBEAMBIGUOUS) != 0) + out.println("\t-MAYBEAMBIGUOUS"); + + if ((attr & PropertyAttribute.MAYBEDEFAULT) != 0) + out.println("\t-MAYBEDEFAULT"); + + if ((attr & PropertyAttribute.MAYBEVOID) != 0) + out.println("\t-MAYBEVOID"); + + if ((attr & PropertyAttribute.READONLY) != 0) + out.println("\t-READONLY"); + + if ((attr & PropertyAttribute.REMOVABLE) != 0) + out.println("\t-REMOVABLE"); + + if ((attr & PropertyAttribute.TRANSIENT) != 0) + out.println("\t-TRANSIENT"); + } catch(com.sun.star.uno.Exception e) { + out.println("Exception!!!!"); + e.printStackTrace(out); + } + } + + + + /** + * Print the names and the values of a sequence of <code>PropertyValue</code> + * to a print writer. + * @param ps The property which should displayed + * @param out The print writer which is used as output. + * @see com.sun.star.beans.PropertyValue + */ + private static void printPropertyValueSequencePairs(PropertyValue[] ps, PrintWriter out){ + for( int i = 0; i < ps.length; i++){ + printPropertyValuePairs(ps[i], out); + } + } + + + + /** + * Print the name and the value of a <code>PropertyValue</code> to a print writer. + * @param ps The property which should displayed + * @param out The print writer which is used as output. + * @see com.sun.star.beans.PropertyValue + */ + private static void printPropertyValuePairs(PropertyValue ps, PrintWriter out){ + + if (ps.Value instanceof String[] ){ + String[] values = (String[]) ps.Value; + StringBuilder oneValue = new StringBuilder("value is an empty String[]"); + if (values.length > 0){ + oneValue.append("['"); + for( int i=0; i < values.length; i++){ + oneValue.append(values[i]); + if (i+1 < values.length) oneValue.append("';'"); + } + oneValue.append("']"); + } + out.println("--------"); + out.println(" Name: '" + ps.Name + "' contains String[]:"); + out.println(oneValue.toString()); + out.println("--------"); + + } else if (ps.Value instanceof PropertyValue){ + out.println("--------"); + out.println(" Name: '" + ps.Name + "' contains PropertyValue:"); + printPropertyValuePairs((PropertyValue)ps.Value, out); + out.println("--------"); + + } else if (ps.Value instanceof PropertyValue[]){ + out.println("--------"); + out.println(" Name: '" + ps.Name + "' contains PropertyValue[]:"); + printPropertyValueSequencePairs((PropertyValue[])ps.Value, out); + out.println("--------"); + + } else { + out.println("Name: '" + ps.Name + "' Value: '" + ps.Value.toString() + "'"); + } + } + + /** + * Print the names of all properties inside this property set + * @param ps The property set which is printed. + * @see com.sun.star.beans.XPropertySet + */ + public static void printPropertiesNames(XPropertySet ps) { + XPropertySetInfo psi = ps.getPropertySetInfo(); + Property[] props = psi.getProperties(); + for (int i = 0; i < props.length; i++) + System.out.println(i + ". " + props[i].Name); + } + + /** + * Print the supported services of a UNO object. + * @param aObject A UNO object. + */ + public static void getSuppServices (Object aObject) { + XServiceInfo xSI = UnoRuntime.queryInterface(XServiceInfo.class,aObject); + printArray(xSI.getSupportedServiceNames()); + StringBuilder str = new StringBuilder("Therein not Supported Service"); + boolean notSupportedServices = false; + for (int i=0;i<xSI.getSupportedServiceNames().length;i++) { + if (! xSI.supportsService(xSI.getSupportedServiceNames()[i])) { + notSupportedServices = true; + str.append("\n").append(xSI.getSupportedServiceNames()[i]); + } + } + if (notSupportedServices) + System.out.println(str.toString()); + } +} |