Update ooo320-m1
[ooovba.git] / qadevOOo / runner / lib / Parameters.java
blobb3267318205f49040084917b74b9f1cb4c41c501
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Parameters.java,v $
10 * $Revision: 1.3 $
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 ************************************************************************/
31 package lib;
33 import java.util.Iterator;
34 import java.util.Hashtable;
35 import java.util.HashSet;
36 import java.util.Map;
37 import java.util.Set;
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;
48 /**
49 * Parameters is a container of String parameters.
50 * @deprecated
53 public class Parameters implements XPropertySet {
54 /* final protected Map parameters;
55 final Parameters defaults; */
56 final protected Map parameters;
57 final Parameters defaults;
58 Property[] props;
60 public Parameters(Map params) {
61 this (params, null);
64 public Parameters(Map params, Parameters defaultParams) {
65 parameters = params;
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()];
78 int num = 0;
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)
92 return (String)res;
94 if (defaults != null)
95 return defaults.get(paramName);
97 return null;
100 public Object getPropertyValue(String name) {
101 Object erg = parameters.get(name);
102 if (erg == null && defaults != null)
103 return defaults.getPropertyValue(name);
104 return erg;
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);
116 props = addProps;
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() {
134 return props;
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)) {
142 return true;
146 return false;
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)) {
154 return prop;
158 throw new UnknownPropertyException(name);
163 public Map toMap() {
164 return new Hashtable(parameters) {
165 public Object get(Object obj) {
166 if (obj instanceof String) {
167 return Parameters.this.get((String) obj);
168 } else {
169 return null;
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");
189 } */
193 public static String getString(XPropertySet props, String name) {
194 try {
195 return (String)props.getPropertyValue(name);
196 } catch (UnknownPropertyException e) {
197 return null;
198 } catch (WrappedTargetException e) {
199 return null;
203 public static Object get(XPropertySet props, String name) {
204 try {
205 return props.getPropertyValue(name);
206 } catch (UnknownPropertyException e) {
207 return null;
208 } catch (WrappedTargetException e) {
209 return null;
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;
221 Object value;
223 try {
224 value = props.getPropertyValue(name);
225 } catch (WrappedTargetException e) {
226 continue;
227 } catch (UnknownPropertyException e) {
228 continue;
231 result.put(name, value);
234 return result;