1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.apache.openoffice.comp.sdbc.dbtools.comphelper;
23 
24 import com.sun.star.beans.Property;
25 import com.sun.star.beans.PropertyAttribute;
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.beans.XPropertySetInfo;
28 import com.sun.star.lang.XComponent;
29 import com.sun.star.lang.XServiceInfo;
30 import com.sun.star.uno.UnoRuntime;
31 
32 public class CompHelper {
33     /**
34      * If the given parameter is an XComponent, calls dispose() on it.
35      * @param object the UNO interface to try dispose; may be null.
36      */
disposeComponent(final Object object)37     public static void disposeComponent(final Object object) {
38         final XComponent component = UnoRuntime.queryInterface(XComponent.class, object);
39         if (component != null) {
40             component.dispose();
41         }
42     }
43 
copyProperties(final XPropertySet src, final XPropertySet dst)44     public static void copyProperties(final XPropertySet src, final XPropertySet dst) {
45         if (src == null || dst == null) {
46             return;
47         }
48 
49         XPropertySetInfo srcPropertySetInfo = src.getPropertySetInfo();
50         XPropertySetInfo dstPropertySetInfo = dst.getPropertySetInfo();
51 
52         for (Property srcProperty : srcPropertySetInfo.getProperties()) {
53             if (dstPropertySetInfo.hasPropertyByName(srcProperty.Name)) {
54                 try {
55                     Property dstProperty = dstPropertySetInfo.getPropertyByName(srcProperty.Name);
56                     if ((dstProperty.Attributes & PropertyAttribute.READONLY) == 0) {
57                         Object value = src.getPropertyValue(srcProperty.Name);
58                         if ((dstProperty.Attributes & PropertyAttribute.MAYBEVOID) == 0 || value != null) {
59                             dst.setPropertyValue(srcProperty.Name, value);
60                         }
61                     }
62                 } catch (Exception e) {
63                     String error = "Could not copy property '" + srcProperty.Name +
64                             "' to the destination set";
65                     XServiceInfo serviceInfo = UnoRuntime.queryInterface(XServiceInfo.class, dst);
66                     if (serviceInfo != null) {
67                         error += " (a '" + serviceInfo.getImplementationName() + "' implementation)";
68                     }
69 
70                 }
71             }
72         }
73     }
74 }
75