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