xref: /AOO41X/main/qadevOOo/testdocs/qadevlibs/source/com/sun/star/cmp/MyPersistObject.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package com.sun.star.cmp;
28 
29 import com.sun.star.io.XPersistObject;
30 import com.sun.star.io.XObjectInputStream;
31 import com.sun.star.io.XObjectOutputStream;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.beans.XPropertySetInfo;
34 import com.sun.star.beans.Property;
35 import com.sun.star.beans.XPropertyChangeListener;
36 import com.sun.star.beans.XVetoableChangeListener;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.lang.XSingleServiceFactory;
39 import com.sun.star.lang.XServiceInfo;
40 import com.sun.star.uno.XInterface;
41 import com.sun.star.lang.XTypeProvider;
42 import com.sun.star.registry.XRegistryKey;
43 import com.sun.star.comp.loader.FactoryHelper;
44 import com.sun.star.uno.Type;
45 
46 /**
47  * Class MyPersistObject implements an XPersistObject, XServiceInfo,
48  * XTypeProvider and XPropertySet.
49  *
50  * Warning: In XPropertySet only the following methods that are
51  *          used for testing are really implemented:
52  *
53  *          - public XPropertySetInfo getPropertySetInfo()
54  *          - public void setPropertyValue(String property, Object value)
55  *          - public Object getPropertyValue(String property)
56  */
57 public class MyPersistObject implements XPersistObject, XTypeProvider,
58                                                 XServiceInfo, XPropertySet {
59 
60     private class MyPropertySetInfo implements XPropertySetInfo {
61         Property[] _props;
62         public MyPropertySetInfo(Property[] props) {
63             _props = props;
64         }
65         public Property[] getProperties() {
66             return _props;
67         }
68         public Property getPropertyByName(String name) {
69             int i = getPropertyIndexByName(name);
70             return i>0?_props[i]:null;
71         }
72         public int getPropertyIndexByName(String name) {
73             for ( int i=0; i<_props.length; i++ )
74                 if (name.equals(_props[i].Name))
75                     return i;
76             return -1;
77         }
78         public boolean hasPropertyByName(String name) {
79             int i = getPropertyIndexByName(name);
80             return i>0?true:false;
81         }
82     }
83 
84     static private final boolean verbose = false;
85 
86     static public final String __serviceName =
87                                         "com.sun.star.cmp.PersistObject";
88     static public final String __implName =
89                                         "com.sun.star.cmp.MyPersistObject";
90 
91     // lots of props to write
92     Property[] props;
93     private byte by;
94     private int i;
95     private char c;
96     private double d;
97     private float f;
98     private short s;
99     private String st;
100     // property set info
101     XPropertySetInfo xInfo;
102 
103     /**
104      * Constructor: sets all properties
105      **/
106     public MyPersistObject() {
107         int prop_count = 7;
108         props = new Property[prop_count];
109         for (int i=0; i<prop_count; i++ ) {
110             props[i] = new Property();
111         }
112         by = 1;
113         props[0].Name = "byte";
114         i = 3;
115         props[1].Name = "int";
116         c = 'c';
117         props[2].Name = "char";
118         d = 3.142;
119         props[3].Name = "double";
120         f = 2.718f;
121         props[4].Name = "float";
122         s = 1;
123         props[5].Name = "short";
124         st = "Though this be madness, yet there is method in 't.";
125         props[6].Name = "String";
126         xInfo = new MyPropertySetInfo(props);
127     }
128     /**
129      * This function provides the service name
130      * @return the service name
131      * @see com.sun.star.io.XPersistObject
132      */
133     public String getServiceName() {
134         if ( verbose ) {
135             System.out.println("get service name");
136         }
137         return __serviceName;
138     }
139 
140     /**
141      * Fuction reads properties from this input stream
142      * @param inStream the input stream
143      * @see com.sun.star.io.XPersistObject
144      */
145     public void read(XObjectInputStream inStream)
146                             throws com.sun.star.io.IOException {
147         s = inStream.readShort();
148         i = inStream.readLong();
149         by = inStream.readByte();
150         c = inStream.readChar();
151         d = inStream.readDouble();
152         f = inStream.readFloat();
153         st = inStream.readUTF();
154         if ( verbose )
155             System.out.println("read called" + s + " " + i + " " + st);
156     }
157 
158     /**
159      * Fuction writes properties on this output stream
160      * @param outStream the output stream
161      * @see com.sun.star.io.XPersistObject
162      */
163     public void write(XObjectOutputStream outStream)
164                             throws com.sun.star.io.IOException {
165         if ( verbose )
166             System.out.println("write called");
167         outStream.writeShort(s);
168         outStream.writeLong(i);
169         outStream.writeByte(by);
170         outStream.writeChar(c);
171         outStream.writeDouble(d);
172         outStream.writeFloat(f);
173         outStream.writeUTF(st);
174 
175     }
176 
177 
178     /**
179      * Fuction to get information about the property set.
180      * @return The information
181      * @see com.sun.star.io.XPropertySet
182      */
183     public XPropertySetInfo getPropertySetInfo() {
184         return xInfo;
185     }
186 
187     /**
188      * Set a property value
189      * @param property The name of the property.
190      * @param value The new value of the property.
191      * @see com.sun.star.io.XPropertySet
192      */
193     public void setPropertyValue(String property, Object value) {
194         if ( property.equals(props[0].Name))
195             by = ((Byte)value).byteValue();
196         if ( property.equals(props[1].Name))
197             i = ((Integer)value).intValue();
198         if ( property.equals(props[2].Name))
199             c = ((Character)value).charValue();
200         if ( property.equals(props[3].Name))
201             d = ((Double)value).doubleValue();
202         if ( property.equals(props[4].Name))
203             f = ((Float)value).floatValue();
204         if ( property.equals(props[5].Name))
205             s = ((Short)value).shortValue();
206         if ( property.equals(props[6].Name))
207             st = (String)value;
208     }
209 
210     /**
211      * Get a property value
212      * @param property The property name.
213      * @return The value of the property.
214      * @see com.sun.star.io.XPropertySet
215      */
216     public Object getPropertyValue(String property) {
217         if ( property.equals(props[0].Name))
218             return new Byte(by);
219         if ( property.equals(props[1].Name))
220             return new Integer(i);
221         if ( property.equals(props[2].Name))
222             return new Character(c);
223         if ( property.equals(props[3].Name))
224             return new Double(d);
225         if ( property.equals(props[4].Name))
226             return new Float(f);
227         if ( property.equals(props[5].Name))
228             return new Short(s);
229         if ( property.equals(props[6].Name))
230             return st;
231         return new Object();
232     }
233 
234     /**
235      * Empty implementation: not needed for tests.
236      */
237     public void addPropertyChangeListener(String aPropertyName,
238              XPropertyChangeListener xListener ) {}
239 
240     /**
241      * Empty implementation: not needed for tests.
242      */
243     public void removePropertyChangeListener(String aPropertyName,
244              XPropertyChangeListener aListener ) {}
245 
246     /**
247      * Empty implementation: not needed for tests.
248      */
249     public void addVetoableChangeListener(String PropertyName,
250              XVetoableChangeListener aListener ) {}
251 
252     /**
253      * Empty implementation: not needed for tests.
254      */
255     public void removeVetoableChangeListener(String PropertyName,
256              XVetoableChangeListener aListener ) {}
257 
258     /**
259      * Get all implemented types of this class.
260      * @return An array of implemented interface types.
261      * @see com.sun.star.lang.XTypeProvider
262      */
263     public Type[] getTypes() {
264         Type[] type = new Type[5];
265         type[0] = new Type(XInterface.class);
266         type[1] = new Type(XTypeProvider.class);
267         type[2] = new Type(XPersistObject.class);
268         type[3] = new Type(XServiceInfo.class);
269         type[4] = new Type(XPropertySet.class);
270         return type;
271     }
272 
273     /**
274      * Get the implementation id.
275      * @return An empty implementation id.
276      * @see com.sun.star.lang.XTypeProvider
277      */
278     public byte[] getImplementationId() {
279         return new byte[0];
280     }
281     /**
282      * Function for reading the implementation name.
283      *
284      * @return the implementation name
285      * @see com.sun.star.lang.XServiceInfo
286      */
287     public String getImplementationName() {
288         return __implName;
289     }
290 
291     /**
292      * Does the implementation support this service?
293      *
294      * @param serviceName The name of the service in question
295      * @return true, if service is supported, false otherwise
296      * @see com.sun.star.lang.XServiceInfo
297      */
298     public boolean supportsService(String serviceName) {
299         if(serviceName.equals(__serviceName))
300             return true;
301         return false;
302     }
303 
304     /**
305      * Function for reading all supported services
306      *
307      * @return An aaray with all supported service names
308      * @see com.sun.star.lang.XServiceInfo
309      */
310     public String[] getSupportedServiceNames() {
311         String[] supServiceNames = {__serviceName};
312         return supServiceNames;
313     }
314 
315    /**
316    *
317    * Gives a factory for creating the service.
318    * This method is called by the <code>JavaLoader</code>
319    * <p>
320    * @return  returns a <code>XSingleServiceFactory</code> for creating the component
321    * @param   implName     the name of the implementation for which a service is desired
322    * @param   multiFactory the service manager to be used if needed
323    * @param   regKey       the registryKey
324    * @see                  com.sun.star.comp.loader.JavaLoader
325    */
326     public static XSingleServiceFactory __getServiceFactory(String implName,
327                     XMultiServiceFactory multiFactory, XRegistryKey regKey)
328     {
329         XSingleServiceFactory xSingleServiceFactory = null;
330 
331         if (implName.equals(MyPersistObject.class.getName()))
332             xSingleServiceFactory = FactoryHelper.getServiceFactory(
333                 MyPersistObject.class, __serviceName, multiFactory, regKey);
334 
335         return xSingleServiceFactory;
336     }
337 
338   /**
339    * Writes the service information into the given registry key.
340    * This method is called by the <code>JavaLoader</code>
341    * <p>
342    * @return  returns true if the operation succeeded
343    * @param   regKey       the registryKey
344    * @see                  com.sun.star.comp.loader.JavaLoader
345    */
346     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
347         return FactoryHelper.writeRegistryServiceInfo(MyPersistObject.class.getName(),
348         __serviceName, regKey);
349     }
350 
351 
352 
353 
354 }   // finish class MyPersistObject
355 
356 
357