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