1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
30 import java
.util
.Iterator
;
31 import java
.util
.Hashtable
;
32 import java
.util
.HashSet
;
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
;
46 * Parameters is a container of String parameters.
50 public class Parameters
implements XPropertySet
{
51 /* final protected Map parameters;
52 final Parameters defaults; */
53 final protected Map parameters
;
54 final Parameters defaults
;
57 public Parameters(Map params
) {
61 public Parameters(Map params
, Parameters defaultParams
) {
63 defaults
= defaultParams
;
64 checkParameters(parameters
);
66 Set paramSet
= new HashSet(parameters
.keySet());
68 if (defaults
!= null) {
69 Set defSet
= defaults
.toMap().keySet();
70 paramSet
.addAll(defSet
);
73 props
= new Property
[paramSet
.size()];
77 for (Iterator i
= paramSet
.iterator(); i
.hasNext(); num
++) {
78 String name
= (String
)i
.next();
80 props
[num
] = new Property(name
, num
, new Type(String
.class), (short)0);
85 public String
get(String paramName
) {
86 Object res
= parameters
.get(paramName
);
88 if (res
!= null && res
instanceof String
)
92 return defaults
.get(paramName
);
97 public Object
getPropertyValue(String name
) {
98 Object erg
= parameters
.get(name
);
99 if (erg
== null && defaults
!= null)
100 return defaults
.getPropertyValue(name
);
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
++)
110 addProps
[i
] = props
[i
];
112 addProps
[size
] = new Property(name
, size
, new Type(value
.getClass()), (short)0);
116 public void addVetoableChangeListener(String name
, XVetoableChangeListener l
) {
119 public void removeVetoableChangeListener(String name
, XVetoableChangeListener l
) {
122 public void addPropertyChangeListener(String name
, XPropertyChangeListener l
) {
125 public void removePropertyChangeListener(String name
, XPropertyChangeListener l
) {
128 public XPropertySetInfo
getPropertySetInfo() {
129 return new XPropertySetInfo() {
130 public Property
[] getProperties() {
134 public boolean hasPropertyByName(String name
) {
135 for (int i
= 0; i
< props
.length
; i
++) {
136 Property prop
= props
[i
];
138 if (prop
.Name
.equals(name
)) {
146 public Property
getPropertyByName(String name
) throws UnknownPropertyException
{
147 for (int i
= 0; i
< props
.length
; i
++) {
148 Property prop
= props
[i
];
150 if (prop
.Name
.equals(name
)) {
155 throw new UnknownPropertyException(name
);
161 return new Hashtable(parameters
) {
162 public Object
get(Object obj
) {
163 if (obj
instanceof String
) {
164 return Parameters
.this.get((String
) obj
);
172 private static void checkParameters(Map params
) {
173 for (Iterator i
= params
.keySet().iterator(); i
.hasNext();) {
174 Object key
= i
.next();
176 if (!(key
instanceof String
)) {
177 throw new IllegalArgumentException(
178 "Wrong key " + key
+ ", it should be of String type");
181 /* Object value = params.get(key);
183 if (!(value instanceof String)) {
184 throw new IllegalArgumentException(
185 "Wrong value " + value + ", it should be of String type");
190 public static String
getString(XPropertySet props
, String name
) {
192 return (String
)props
.getPropertyValue(name
);
193 } catch (UnknownPropertyException e
) {
195 } catch (WrappedTargetException e
) {
200 public static Object
get(XPropertySet props
, String name
) {
202 return props
.getPropertyValue(name
);
203 } catch (UnknownPropertyException e
) {
205 } catch (WrappedTargetException e
) {
210 public static Map
toMap(XPropertySet props
) {
211 Hashtable result
= new Hashtable(10);
213 XPropertySetInfo setInfo
= props
.getPropertySetInfo();
214 Property
[] properties
= setInfo
.getProperties();
216 for (int i
= 0; i
< properties
.length
; i
++) {
217 String name
= properties
[i
].Name
;
221 value
= props
.getPropertyValue(name
);
222 } catch (WrappedTargetException e
) {
224 } catch (UnknownPropertyException e
) {
228 result
.put(name
, value
);