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 28 package lib; 29 30 import java.util.Iterator; 31 import java.util.Hashtable; 32 import java.util.HashSet; 33 import java.util.Map; 34 import java.util.Set; 35 36 import com.sun.star.beans.Property; 37 import com.sun.star.beans.XPropertySet; 38 import com.sun.star.beans.XPropertySetInfo; 39 import com.sun.star.beans.XPropertyChangeListener; 40 import com.sun.star.beans.XVetoableChangeListener; 41 import com.sun.star.beans.UnknownPropertyException; 42 import com.sun.star.lang.WrappedTargetException; 43 import com.sun.star.uno.Type; 44 45 /** 46 * Parameters is a container of String parameters. 47 * @deprecated 48 */ 49 50 public class Parameters implements XPropertySet { 51 /* final protected Map parameters; 52 final Parameters defaults; */ 53 final protected Map parameters; 54 final Parameters defaults; 55 Property[] props; 56 57 public Parameters(Map params) { 58 this (params, null); 59 } 60 61 public Parameters(Map params, Parameters defaultParams) { 62 parameters = params; 63 defaults = defaultParams; 64 checkParameters(parameters); 65 66 Set paramSet = new HashSet(parameters.keySet()); 67 68 if (defaults != null) { 69 Set defSet = defaults.toMap().keySet(); 70 paramSet.addAll(defSet); 71 } 72 73 props = new Property[paramSet.size()]; 74 75 int num = 0; 76 77 for (Iterator i = paramSet.iterator(); i.hasNext(); num++) { 78 String name = (String)i.next(); 79 80 props[num] = new Property(name, num, new Type(String.class), (short)0); 81 } 82 } 83 84 85 public String get(String paramName) { 86 Object res = parameters.get(paramName); 87 88 if (res != null && res instanceof String) 89 return (String)res; 90 91 if (defaults != null) 92 return defaults.get(paramName); 93 94 return null; 95 } 96 97 public Object getPropertyValue(String name) { 98 Object erg = parameters.get(name); 99 if (erg == null && defaults != null) 100 return defaults.getPropertyValue(name); 101 return erg; 102 } 103 104 public void setPropertyValue(String name, Object value) { 105 parameters.put(name, value); 106 int size = props.length; 107 Property[] addProps = new Property[size+1]; 108 for (int i=0; i<size; i++) 109 { 110 addProps[i] = props[i]; 111 } 112 addProps[size] = new Property(name, size, new Type(value.getClass()), (short)0); 113 props = addProps; 114 } 115 116 public void addVetoableChangeListener(String name, XVetoableChangeListener l) { 117 } 118 119 public void removeVetoableChangeListener(String name, XVetoableChangeListener l) { 120 } 121 122 public void addPropertyChangeListener(String name, XPropertyChangeListener l) { 123 } 124 125 public void removePropertyChangeListener(String name, XPropertyChangeListener l) { 126 } 127 128 public XPropertySetInfo getPropertySetInfo() { 129 return new XPropertySetInfo() { 130 public Property[] getProperties() { 131 return props; 132 } 133 134 public boolean hasPropertyByName(String name) { 135 for (int i = 0; i < props.length; i++) { 136 Property prop = props[i]; 137 138 if (prop.Name.equals(name)) { 139 return true; 140 } 141 } 142 143 return false; 144 } 145 146 public Property getPropertyByName(String name) throws UnknownPropertyException { 147 for (int i = 0; i < props.length; i++) { 148 Property prop = props[i]; 149 150 if (prop.Name.equals(name)) { 151 return prop; 152 } 153 } 154 155 throw new UnknownPropertyException(name); 156 } 157 }; 158 } 159 160 public Map toMap() { 161 return new Hashtable(parameters) { 162 public Object get(Object obj) { 163 if (obj instanceof String) { 164 return Parameters.this.get((String) obj); 165 } else { 166 return null; 167 } 168 } 169 }; 170 } 171 172 private static void checkParameters(Map params) { 173 for (Iterator i = params.keySet().iterator(); i.hasNext();) { 174 Object key = i.next(); 175 176 if (!(key instanceof String)) { 177 throw new IllegalArgumentException( 178 "Wrong key " + key + ", it should be of String type"); 179 } 180 181 /* Object value = params.get(key); 182 183 if (!(value instanceof String)) { 184 throw new IllegalArgumentException( 185 "Wrong value " + value + ", it should be of String type"); 186 } */ 187 } 188 } 189 190 public static String getString(XPropertySet props, String name) { 191 try { 192 return (String)props.getPropertyValue(name); 193 } catch (UnknownPropertyException e) { 194 return null; 195 } catch (WrappedTargetException e) { 196 return null; 197 } 198 } 199 200 public static Object get(XPropertySet props, String name) { 201 try { 202 return props.getPropertyValue(name); 203 } catch (UnknownPropertyException e) { 204 return null; 205 } catch (WrappedTargetException e) { 206 return null; 207 } 208 } 209 210 public static Map toMap(XPropertySet props) { 211 Hashtable result = new Hashtable(10); 212 213 XPropertySetInfo setInfo = props.getPropertySetInfo(); 214 Property[] properties = setInfo.getProperties(); 215 216 for (int i = 0; i < properties.length; i++) { 217 String name = properties[i].Name; 218 Object value; 219 220 try { 221 value = props.getPropertyValue(name); 222 } catch (WrappedTargetException e) { 223 continue; 224 } catch (UnknownPropertyException e) { 225 continue; 226 } 227 228 result.put(name, value); 229 } 230 231 return result; 232 } 233 } 234