1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package lib;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.util.Iterator;
31*cdf0e10cSrcweir import java.util.Hashtable;
32*cdf0e10cSrcweir import java.util.HashSet;
33*cdf0e10cSrcweir import java.util.Map;
34*cdf0e10cSrcweir import java.util.Set;
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir import com.sun.star.beans.Property;
37*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
38*cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
39*cdf0e10cSrcweir import com.sun.star.beans.XPropertyChangeListener;
40*cdf0e10cSrcweir import com.sun.star.beans.XVetoableChangeListener;
41*cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException;
42*cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException;
43*cdf0e10cSrcweir import com.sun.star.uno.Type;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir /**
46*cdf0e10cSrcweir  * Parameters is a container of String parameters.
47*cdf0e10cSrcweir  * @deprecated
48*cdf0e10cSrcweir  */
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir public class Parameters implements XPropertySet {
51*cdf0e10cSrcweir /*    final protected Map parameters;
52*cdf0e10cSrcweir     final Parameters defaults; */
53*cdf0e10cSrcweir     final protected Map parameters;
54*cdf0e10cSrcweir     final Parameters defaults;
55*cdf0e10cSrcweir     Property[] props;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     public Parameters(Map params) {
58*cdf0e10cSrcweir         this (params, null);
59*cdf0e10cSrcweir     }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     public Parameters(Map params, Parameters defaultParams) {
62*cdf0e10cSrcweir         parameters = params;
63*cdf0e10cSrcweir         defaults = defaultParams;
64*cdf0e10cSrcweir         checkParameters(parameters);
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir         Set paramSet = new HashSet(parameters.keySet());
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir         if (defaults != null) {
69*cdf0e10cSrcweir             Set defSet = defaults.toMap().keySet();
70*cdf0e10cSrcweir             paramSet.addAll(defSet);
71*cdf0e10cSrcweir         }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         props = new Property[paramSet.size()];
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir         int num = 0;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir         for (Iterator i = paramSet.iterator(); i.hasNext(); num++) {
78*cdf0e10cSrcweir             String name = (String)i.next();
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir             props[num] = new Property(name, num, new Type(String.class), (short)0);
81*cdf0e10cSrcweir         }
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     public String get(String paramName) {
86*cdf0e10cSrcweir         Object res = parameters.get(paramName);
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         if (res != null && res instanceof String)
89*cdf0e10cSrcweir             return (String)res;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         if (defaults != null)
92*cdf0e10cSrcweir             return defaults.get(paramName);
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         return null;
95*cdf0e10cSrcweir     }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     public Object getPropertyValue(String name) {
98*cdf0e10cSrcweir         Object erg = parameters.get(name);
99*cdf0e10cSrcweir         if (erg ==  null && defaults != null)
100*cdf0e10cSrcweir             return defaults.getPropertyValue(name);
101*cdf0e10cSrcweir         return erg;
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     public void setPropertyValue(String name, Object value) {
105*cdf0e10cSrcweir         parameters.put(name, value);
106*cdf0e10cSrcweir         int size = props.length;
107*cdf0e10cSrcweir         Property[] addProps = new Property[size+1];
108*cdf0e10cSrcweir         for (int i=0; i<size; i++)
109*cdf0e10cSrcweir         {
110*cdf0e10cSrcweir             addProps[i] = props[i];
111*cdf0e10cSrcweir         }
112*cdf0e10cSrcweir         addProps[size] = new Property(name, size, new Type(value.getClass()), (short)0);
113*cdf0e10cSrcweir         props = addProps;
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     public void addVetoableChangeListener(String name, XVetoableChangeListener l) {
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     public void removeVetoableChangeListener(String name, XVetoableChangeListener l) {
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     public void addPropertyChangeListener(String name, XPropertyChangeListener l) {
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     public void removePropertyChangeListener(String name, XPropertyChangeListener l) {
126*cdf0e10cSrcweir     }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     public XPropertySetInfo getPropertySetInfo() {
129*cdf0e10cSrcweir         return new XPropertySetInfo() {
130*cdf0e10cSrcweir             public Property[] getProperties() {
131*cdf0e10cSrcweir                 return props;
132*cdf0e10cSrcweir             }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir             public boolean hasPropertyByName(String name) {
135*cdf0e10cSrcweir                 for (int i = 0; i < props.length; i++) {
136*cdf0e10cSrcweir                     Property prop = props[i];
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir                     if (prop.Name.equals(name)) {
139*cdf0e10cSrcweir                         return true;
140*cdf0e10cSrcweir                     }
141*cdf0e10cSrcweir                 }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir                 return false;
144*cdf0e10cSrcweir             }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir             public Property getPropertyByName(String name) throws UnknownPropertyException {
147*cdf0e10cSrcweir                 for (int i = 0; i < props.length; i++) {
148*cdf0e10cSrcweir                     Property prop = props[i];
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir                     if (prop.Name.equals(name)) {
151*cdf0e10cSrcweir                         return prop;
152*cdf0e10cSrcweir                     }
153*cdf0e10cSrcweir                 }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir                 throw new UnknownPropertyException(name);
156*cdf0e10cSrcweir             }
157*cdf0e10cSrcweir         };
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     public Map toMap() {
161*cdf0e10cSrcweir         return new Hashtable(parameters) {
162*cdf0e10cSrcweir             public Object get(Object obj) {
163*cdf0e10cSrcweir                 if (obj instanceof String) {
164*cdf0e10cSrcweir                     return Parameters.this.get((String) obj);
165*cdf0e10cSrcweir                 } else {
166*cdf0e10cSrcweir                     return null;
167*cdf0e10cSrcweir                 }
168*cdf0e10cSrcweir             }
169*cdf0e10cSrcweir         };
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir     private static void checkParameters(Map params) {
173*cdf0e10cSrcweir         for (Iterator i = params.keySet().iterator(); i.hasNext();) {
174*cdf0e10cSrcweir             Object key = i.next();
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir             if (!(key instanceof String)) {
177*cdf0e10cSrcweir                 throw new IllegalArgumentException(
178*cdf0e10cSrcweir                         "Wrong key " + key + ", it should be of String type");
179*cdf0e10cSrcweir             }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir /*            Object value = params.get(key);
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             if (!(value instanceof String)) {
184*cdf0e10cSrcweir                 throw new IllegalArgumentException(
185*cdf0e10cSrcweir                         "Wrong value " + value + ", it should be of String type");
186*cdf0e10cSrcweir             } */
187*cdf0e10cSrcweir         }
188*cdf0e10cSrcweir     }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir     public static String getString(XPropertySet props, String name) {
191*cdf0e10cSrcweir         try {
192*cdf0e10cSrcweir             return (String)props.getPropertyValue(name);
193*cdf0e10cSrcweir         } catch (UnknownPropertyException e) {
194*cdf0e10cSrcweir             return null;
195*cdf0e10cSrcweir         } catch (WrappedTargetException e) {
196*cdf0e10cSrcweir             return null;
197*cdf0e10cSrcweir         }
198*cdf0e10cSrcweir     }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     public static Object get(XPropertySet props, String name) {
201*cdf0e10cSrcweir         try {
202*cdf0e10cSrcweir             return props.getPropertyValue(name);
203*cdf0e10cSrcweir         } catch (UnknownPropertyException e) {
204*cdf0e10cSrcweir             return null;
205*cdf0e10cSrcweir         } catch (WrappedTargetException e) {
206*cdf0e10cSrcweir             return null;
207*cdf0e10cSrcweir         }
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     public static Map toMap(XPropertySet props) {
211*cdf0e10cSrcweir         Hashtable result = new Hashtable(10);
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir         XPropertySetInfo setInfo = props.getPropertySetInfo();
214*cdf0e10cSrcweir         Property[] properties = setInfo.getProperties();
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         for (int i = 0; i < properties.length; i++) {
217*cdf0e10cSrcweir             String name = properties[i].Name;
218*cdf0e10cSrcweir             Object value;
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir             try {
221*cdf0e10cSrcweir                 value = props.getPropertyValue(name);
222*cdf0e10cSrcweir             } catch (WrappedTargetException e) {
223*cdf0e10cSrcweir                 continue;
224*cdf0e10cSrcweir             } catch (UnknownPropertyException e) {
225*cdf0e10cSrcweir                 continue;
226*cdf0e10cSrcweir             }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir             result.put(name, value);
229*cdf0e10cSrcweir         }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir         return result;
232*cdf0e10cSrcweir     }
233*cdf0e10cSrcweir }
234