2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.util
.Iterator
;
22 import java
.util
.HashMap
;
23 import java
.util
.HashSet
;
27 import com
.sun
.star
.beans
.Property
;
28 import com
.sun
.star
.beans
.XPropertySet
;
29 import com
.sun
.star
.beans
.XPropertySetInfo
;
30 import com
.sun
.star
.beans
.XPropertyChangeListener
;
31 import com
.sun
.star
.beans
.XVetoableChangeListener
;
32 import com
.sun
.star
.beans
.UnknownPropertyException
;
33 import com
.sun
.star
.lang
.WrappedTargetException
;
34 import com
.sun
.star
.uno
.Type
;
37 * Parameters is a container of String parameters.
41 public class Parameters
implements XPropertySet
{
42 /* final protected Map parameters;
43 final Parameters defaults; */
44 final protected Map
<String
, Object
> parameters
;
45 final Parameters defaults
;
48 public Parameters(Map
<String
, Object
> params
) {
52 public Parameters(Map
<String
, Object
> params
, Parameters defaultParams
) {
54 defaults
= defaultParams
;
55 checkParameters(parameters
);
57 Set
<String
> paramSet
= new HashSet
<String
>(parameters
.keySet());
59 if (defaults
!= null) {
60 Set
<String
> defSet
= defaults
.toMap().keySet();
61 paramSet
.addAll(defSet
);
64 props
= new Property
[paramSet
.size()];
68 for (Iterator
<String
> i
= paramSet
.iterator(); i
.hasNext(); num
++) {
69 String name
= i
.next();
71 props
[num
] = new Property(name
, num
, new Type(String
.class), (short)0);
76 public String
get(String paramName
) {
77 Object res
= parameters
.get(paramName
);
79 if (res
!= null && res
instanceof String
)
83 return defaults
.get(paramName
);
88 public Object
getPropertyValue(String name
) {
89 Object erg
= parameters
.get(name
);
90 if (erg
== null && defaults
!= null)
91 return defaults
.getPropertyValue(name
);
95 public void setPropertyValue(String name
, Object value
) {
96 parameters
.put(name
, value
);
97 int size
= props
.length
;
98 Property
[] addProps
= new Property
[size
+1];
99 for (int i
=0; i
<size
; i
++)
101 addProps
[i
] = props
[i
];
103 addProps
[size
] = new Property(name
, size
, new Type(value
.getClass()), (short)0);
107 public void addVetoableChangeListener(String name
, XVetoableChangeListener l
) {
110 public void removeVetoableChangeListener(String name
, XVetoableChangeListener l
) {
113 public void addPropertyChangeListener(String name
, XPropertyChangeListener l
) {
116 public void removePropertyChangeListener(String name
, XPropertyChangeListener l
) {
119 public XPropertySetInfo
getPropertySetInfo() {
120 return new XPropertySetInfo() {
121 public Property
[] getProperties() {
125 public boolean hasPropertyByName(String name
) {
126 for (int i
= 0; i
< props
.length
; i
++) {
127 Property prop
= props
[i
];
129 if (prop
.Name
.equals(name
)) {
137 public Property
getPropertyByName(String name
) throws UnknownPropertyException
{
138 for (int i
= 0; i
< props
.length
; i
++) {
139 Property prop
= props
[i
];
141 if (prop
.Name
.equals(name
)) {
146 throw new UnknownPropertyException(name
);
151 public Map
<String
,Object
> toMap() {
152 return new HashMap
<String
,Object
>(parameters
) {
153 public String
get(String obj
) {
154 return Parameters
.this.get(obj
);
159 private static void checkParameters(Map
<String
, Object
> params
) {
160 for (Iterator
<String
> i
= params
.keySet().iterator(); i
.hasNext();) {
161 Object key
= i
.next();
163 if (!(key
instanceof String
)) {
164 throw new IllegalArgumentException(
165 "Wrong key " + key
+ ", it should be of String type");
168 /* Object value = params.get(key);
170 if (!(value instanceof String)) {
171 throw new IllegalArgumentException(
172 "Wrong value " + value + ", it should be of String type");
177 public static String
getString(XPropertySet props
, String name
) {
179 return (String
)props
.getPropertyValue(name
);
180 } catch (UnknownPropertyException e
) {
182 } catch (WrappedTargetException e
) {
187 public static Object
get(XPropertySet props
, String name
) {
189 return props
.getPropertyValue(name
);
190 } catch (UnknownPropertyException e
) {
192 } catch (WrappedTargetException e
) {
197 public static Map
<String
, Object
> toMap(XPropertySet props
) {
198 HashMap
<String
, Object
> result
= new HashMap
<String
, Object
>(10);
200 XPropertySetInfo setInfo
= props
.getPropertySetInfo();
201 Property
[] properties
= setInfo
.getProperties();
203 for (int i
= 0; i
< properties
.length
; i
++) {
204 String name
= properties
[i
].Name
;
208 value
= props
.getPropertyValue(name
);
209 } catch (WrappedTargetException e
) {
211 } catch (UnknownPropertyException e
) {
215 result
.put(name
, value
);