merged tag ooo/DEV300_m102
[LibreOffice.git] / qadevOOo / runner / lib / Parameters.java
blob1c4a43c4fd3a725a2e197c34463096db0368ced8
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 ************************************************************************/
28 package lib;
30 import java.util.Iterator;
31 import java.util.Hashtable;
32 import java.util.HashSet;
33 import java.util.Map;
34 import java.util.Set;
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;
45 /**
46 * Parameters is a container of String parameters.
47 * @deprecated
50 public class Parameters implements XPropertySet {
51 /* final protected Map parameters;
52 final Parameters defaults; */
53 final protected Map parameters;
54 final Parameters defaults;
55 Property[] props;
57 public Parameters(Map params) {
58 this (params, null);
61 public Parameters(Map params, Parameters defaultParams) {
62 parameters = params;
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()];
75 int num = 0;
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)
89 return (String)res;
91 if (defaults != null)
92 return defaults.get(paramName);
94 return null;
97 public Object getPropertyValue(String name) {
98 Object erg = parameters.get(name);
99 if (erg == null && defaults != null)
100 return defaults.getPropertyValue(name);
101 return erg;
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);
113 props = addProps;
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() {
131 return props;
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)) {
139 return true;
143 return false;
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)) {
151 return prop;
155 throw new UnknownPropertyException(name);
160 public Map toMap() {
161 return new Hashtable(parameters) {
162 public Object get(Object obj) {
163 if (obj instanceof String) {
164 return Parameters.this.get((String) obj);
165 } else {
166 return null;
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");
186 } */
190 public static String getString(XPropertySet props, String name) {
191 try {
192 return (String)props.getPropertyValue(name);
193 } catch (UnknownPropertyException e) {
194 return null;
195 } catch (WrappedTargetException e) {
196 return null;
200 public static Object get(XPropertySet props, String name) {
201 try {
202 return props.getPropertyValue(name);
203 } catch (UnknownPropertyException e) {
204 return null;
205 } catch (WrappedTargetException e) {
206 return null;
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;
218 Object value;
220 try {
221 value = props.getPropertyValue(name);
222 } catch (WrappedTargetException e) {
223 continue;
224 } catch (UnknownPropertyException e) {
225 continue;
228 result.put(name, value);
231 return result;