1 /**
2 * =========================================================
3 * Pentaho-Reporting-Classic : a free Java reporting library
4 * =========================================================
5 *
6 * Project Info: http://reporting.pentaho.org/
7 *
8 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
9 *
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23 * in the United States and other countries.]
24 *
25 * ------------
26 * ReportProperties.java
27 * ------------
28 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
29 */
30
31 package org.jfree.report.util;
32
33 import java.io.Serializable;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.TreeSet;
37 import java.util.Arrays;
38
39 import org.pentaho.reporting.libraries.base.util.LinkedMap;
40
41 /**
42 * The report properties is a hashtable with string keys. ReportProperties are bound to a
43 * report as a general purpose storage. ReportProperties bound to a JFreeReport object are
44 * visible to all generated report-state chains. A ReportState will inherit all
45 * ReportProperties bound to the JFreeReport-object when the ReportState.StartState object
46 * is created. Properties bound to the report definition after the report state is
47 * created are not visible to the ReportState and its children.
48 * <p/>
49 * ReportProperties bound to a ReportState are not visible to the report definition (the
50 * JFreeReport object), but are visible to all ReportStates of that ReportState-chain. So
51 * when you add a property at the end of a report run to a ReportState, the value of this
52 * property will be visible to all ReportStates when the report is restarted at a certain
53 * point.
54 * <p/>
55 * ReportProperties can be seen as a stateless shared report internal storage area. All
56 * functions have access to the properties by using the ReportState.getProperty() and
57 * ReportState.setProperty() functions.
58 * <p/>
59 * For a list of defined default properties, have a look at the
60 * {@link org.jfree.report.JFreeReport} class.
61 *
62 * @author Thomas Morgner
63 * @deprecated This is no longer valid. The properties are a weird concept and are inherently unclean.
64 * They have been replaced by the report environment and the bundle metadata.
65 */
66 public class ReportProperties implements Serializable, Cloneable
67 {
68 /**
69 * Storage for the properties.
70 */
71 private LinkedMap properties;
72
73 /**
74 * The fall-back property-collection.
75 */
76 private ReportProperties masterProperties;
77
78 /**
79 * Copy constructor.
80 *
81 * @param props an existing ReportProperties instance.
82 */
83 public ReportProperties (final ReportProperties props)
84 {
85 try
86 {
87 this.properties = (LinkedMap) props.properties.clone();
88 }
89 catch (CloneNotSupportedException e)
90 {
91 throw new IllegalStateException("Should not happen");
92 }
93 }
94
95 /**
96 * Default constructor.
97 */
98 public ReportProperties ()
99 {
100 this.properties = new LinkedMap();
101 }
102
103 /**
104 * Adds a property to this properties collection. If a property with the given name
105 * exist, the property will be replaced with the new value. If the value is null, the
106 * property will be removed.
107 *
108 * @param key the property key.
109 * @param value the property value.
110 */
111 public void put (final String key, final Object value)
112 {
113 if (key == null)
114 {
115 throw new NullPointerException
116 ("ReportProperties.put (..): Parameter 'key' must not be null");
117 }
118 if (value == null)
119 {
120 this.properties.remove(key);
121 }
122 else
123 {
124 this.properties.put(key, value);
125 }
126 }
127
128 /**
129 * Retrieves the value stored for a key in this properties collection.
130 *
131 * @param key the property key.
132 * @return The stored value, or <code>null</code> if the key does not exist in this
133 * collection.
134 */
135 public Object get (final String key)
136 {
137 if (key == null)
138 {
139 throw new NullPointerException
140 ("ReportProperties.get (..): Parameter 'key' must not be null");
141 }
142 return get(key, null);
143 }
144
145 /**
146 * Retrieves the value stored for a key in this properties collection, and returning the
147 * default value if the key was not stored in this properties collection.
148 *
149 * @param key the property key.
150 * @param defaultValue the default value to be returned when the key is not stored in
151 * this properties collection.
152 * @return The stored value, or the default value if the key does not exist in this
153 * collection.
154 */
155 public Object get (final String key, final Object defaultValue)
156 {
157 if (key == null)
158 {
159 throw new NullPointerException
160 ("ReportProperties.get (..): Parameter 'key' must not be null");
161 }
162 final Object o = this.properties.get(key);
163 if (o == null)
164 {
165 if (masterProperties != null)
166 {
167 return masterProperties.get(key, defaultValue);
168 }
169 return defaultValue;
170 }
171 return o;
172 }
173
174 /**
175 * Returns all property keys as enumeration.
176 *
177 * @return an enumeration of the property keys.
178 */
179 public Iterator keys ()
180 {
181 return Arrays.asList(this.properties.keys()).iterator();
182 }
183
184 /**
185 * Removes all properties stored in this collection.
186 */
187 public void clear ()
188 {
189 this.properties.clear();
190 }
191
192 /**
193 * Checks whether the given key is stored in this collection of ReportProperties.
194 *
195 * @param key the property key.
196 * @return true, if the given key is known.
197 */
198 public boolean containsKey (final String key)
199 {
200 if (key == null)
201 {
202 throw new NullPointerException
203 ("ReportProperties.containsKey (..): Parameter key must not be null");
204 }
205 return this.properties.containsKey(key);
206 }
207
208 /**
209 * Clones the properties.
210 *
211 * @return a copy of this ReportProperties object.
212 *
213 * @throws CloneNotSupportedException this should never happen.
214 */
215 public Object clone ()
216 throws CloneNotSupportedException
217 {
218 final ReportProperties p = (ReportProperties) super.clone();
219 p.properties = (LinkedMap) this.properties.clone();
220 p.masterProperties = null;
221 return p;
222 }
223
224 /**
225 * Returns true, if there is at least one marked property.
226 *
227 * @return true, if there are some properties marked, false otherwise.
228 */
229 public boolean containsMarkedProperties ()
230 {
231 return true;
232 }
233
234 /**
235 * Returns the fall-back property-collection. If defined, this collection will be used if a queried property is not
236 * defined in this collection.
237 *
238 * @return the fall-back collection.
239 */
240 public ReportProperties getMasterProperties()
241 {
242 return masterProperties;
243 }
244
245 /**
246 * Defines the fall-back property-collection. If defined, this collection will be used if a queried property is not
247 * defined in this collection.
248 *
249 * @param masterProperties the fall-back collection.
250 */
251 public void setMasterProperties(final ReportProperties masterProperties)
252 {
253 this.masterProperties = masterProperties;
254 }
255
256 /**
257 * Returns all defined keys as string-array.
258 *
259 * @return the keys as array.
260 */
261 public String[] keyArray()
262 {
263 return (String[]) properties.keys(new String[properties.size()]);
264 }
265
266 /**
267 * Returns the number of entries in this collection.
268 *
269 * @return the number of properties defined here.
270 */
271 public int size()
272 {
273 return properties.size();
274 }
275 }