1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Parameters.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.util
.Iterator
;
34 import java
.util
.Hashtable
;
35 import java
.util
.HashSet
;
39 import com
.sun
.star
.beans
.Property
;
40 import com
.sun
.star
.beans
.XPropertySet
;
41 import com
.sun
.star
.beans
.XPropertySetInfo
;
42 import com
.sun
.star
.beans
.XPropertyChangeListener
;
43 import com
.sun
.star
.beans
.XVetoableChangeListener
;
44 import com
.sun
.star
.beans
.UnknownPropertyException
;
45 import com
.sun
.star
.lang
.WrappedTargetException
;
46 import com
.sun
.star
.uno
.Type
;
49 * Parameters is a container of String parameters.
53 public class Parameters
implements XPropertySet
{
54 /* final protected Map parameters;
55 final Parameters defaults; */
56 final protected Map parameters
;
57 final Parameters defaults
;
60 public Parameters(Map params
) {
64 public Parameters(Map params
, Parameters defaultParams
) {
66 defaults
= defaultParams
;
67 checkParameters(parameters
);
69 Set paramSet
= new HashSet(parameters
.keySet());
71 if (defaults
!= null) {
72 Set defSet
= defaults
.toMap().keySet();
73 paramSet
.addAll(defSet
);
76 props
= new Property
[paramSet
.size()];
80 for (Iterator i
= paramSet
.iterator(); i
.hasNext(); num
++) {
81 String name
= (String
)i
.next();
83 props
[num
] = new Property(name
, num
, new Type(String
.class), (short)0);
88 public String
get(String paramName
) {
89 Object res
= parameters
.get(paramName
);
91 if (res
!= null && res
instanceof String
)
95 return defaults
.get(paramName
);
100 public Object
getPropertyValue(String name
) {
101 Object erg
= parameters
.get(name
);
102 if (erg
== null && defaults
!= null)
103 return defaults
.getPropertyValue(name
);
107 public void setPropertyValue(String name
, Object value
) {
108 parameters
.put(name
, value
);
109 int size
= props
.length
;
110 Property
[] addProps
= new Property
[size
+1];
111 for (int i
=0; i
<size
; i
++)
113 addProps
[i
] = props
[i
];
115 addProps
[size
] = new Property(name
, size
, new Type(value
.getClass()), (short)0);
119 public void addVetoableChangeListener(String name
, XVetoableChangeListener l
) {
122 public void removeVetoableChangeListener(String name
, XVetoableChangeListener l
) {
125 public void addPropertyChangeListener(String name
, XPropertyChangeListener l
) {
128 public void removePropertyChangeListener(String name
, XPropertyChangeListener l
) {
131 public XPropertySetInfo
getPropertySetInfo() {
132 return new XPropertySetInfo() {
133 public Property
[] getProperties() {
137 public boolean hasPropertyByName(String name
) {
138 for (int i
= 0; i
< props
.length
; i
++) {
139 Property prop
= props
[i
];
141 if (prop
.Name
.equals(name
)) {
149 public Property
getPropertyByName(String name
) throws UnknownPropertyException
{
150 for (int i
= 0; i
< props
.length
; i
++) {
151 Property prop
= props
[i
];
153 if (prop
.Name
.equals(name
)) {
158 throw new UnknownPropertyException(name
);
164 return new Hashtable(parameters
) {
165 public Object
get(Object obj
) {
166 if (obj
instanceof String
) {
167 return Parameters
.this.get((String
) obj
);
175 private static void checkParameters(Map params
) {
176 for (Iterator i
= params
.keySet().iterator(); i
.hasNext();) {
177 Object key
= i
.next();
179 if (!(key
instanceof String
)) {
180 throw new IllegalArgumentException(
181 "Wrong key " + key
+ ", it should be of String type");
184 /* Object value = params.get(key);
186 if (!(value instanceof String)) {
187 throw new IllegalArgumentException(
188 "Wrong value " + value + ", it should be of String type");
193 public static String
getString(XPropertySet props
, String name
) {
195 return (String
)props
.getPropertyValue(name
);
196 } catch (UnknownPropertyException e
) {
198 } catch (WrappedTargetException e
) {
203 public static Object
get(XPropertySet props
, String name
) {
205 return props
.getPropertyValue(name
);
206 } catch (UnknownPropertyException e
) {
208 } catch (WrappedTargetException e
) {
213 public static Map
toMap(XPropertySet props
) {
214 Hashtable result
= new Hashtable(10);
216 XPropertySetInfo setInfo
= props
.getPropertySetInfo();
217 Property
[] properties
= setInfo
.getProperties();
219 for (int i
= 0; i
< properties
.length
; i
++) {
220 String name
= properties
[i
].Name
;
224 value
= props
.getPropertyValue(name
);
225 } catch (WrappedTargetException e
) {
227 } catch (UnknownPropertyException e
) {
231 result
.put(name
, value
);