Debian Codegraph @

source/org/jfree/report/structure/Element.java

raw
1 /**
2 * ========================================
3 * JFreeReport : a free Java report library
4 * ========================================
5 *
6 * Project Info: http://reporting.pentaho.org/
7 *
8 * (C) Copyright 2000-2007, by Object Refinery Limited, 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 * $Id: Element.java 10756 2009-12-02 15:58:24Z tmorgner $
27 * ------------
28 * (C) Copyright 2000-2005, by Object Refinery Limited.
29 * (C) Copyright 2005-2007, by Pentaho Corporation.
30 */
31
32 package org.jfree.report.structure;
33
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.Locale;
38 import java.util.Map;
39 import java.util.Iterator;
40 import java.util.Collections;
41
42 import org.jfree.layouting.input.style.CSSStyleRule;
43 import org.jfree.layouting.input.style.keys.box.BoxStyleKeys;
44 import org.jfree.layouting.input.style.values.CSSConstant;
45 import org.jfree.layouting.namespace.Namespaces;
46 import org.jfree.layouting.util.AttributeMap;
47 import org.jfree.layouting.util.LocaleUtility;
48 import org.jfree.report.JFreeReportInfo;
49 import org.jfree.report.expressions.Expression;
50
51 /**
52 * An element is a node that can have attributes. The 'id' and the 'name'
53 * attribute is defined for all elements.
54 * <p/>
55 * Both the name and the id attribute may be null.
56 * <p/>
57 * Properties in the 'http://jfreereport.sourceforge.net/namespaces/engine/flow'
58 * namespace and in the 'http://jfreereport.sourceforge.net/namespaces/engine/compatibility'
59 * namespace are considered internal. You should only touch them, if you really
60 * know what you are doing.
61 *
62 * @author Thomas Morgner
63 */
64 public abstract class Element extends Node
65 {
66 private static final Expression[] EMPTY_EXPRESSIONS = new Expression[0];
67 private static final Map EMPTY_MAP = Collections.unmodifiableMap(new HashMap());
68 public static final String NAME_ATTRIBUTE = "name";
69 public static final String ID_ATTRIBUTE = "id";
70 /**
71 * The type corresponds (somewhat) to the tagname of HTML.
72 */
73 public static final String TYPE_ATTRIBUTE = "type";
74 /**
75 * See XML-Namespaces for the idea of that one ...
76 */
77 public static final String NAMESPACE_ATTRIBUTE = "namespace";
78 public static final String VIRTUAL_ATTRIBUTE = "virtual";
79
80 private transient AttributeMap readOnlyAttributes;
81 private AttributeMap attributes;
82 private CSSStyleRule style;
83 private ArrayList expressions;
84 private transient AttributeMap readOnlyAttributeExpressions;
85 private AttributeMap attributeExpressions;
86 private HashMap styleExpressions;
87 private Map cachedStyleExpressions;
88 private boolean enabled;
89 private boolean virtual;
90 private Expression displayCondition;
91
92 /**
93 * Constructs an element.
94 * <p/>
95 * The element inherits the element's defined default ElementStyleSheet to
96 * provide reasonable default values for common stylekeys. When the element is
97 * added to the band, the bands stylesheet is set as parent to the element's
98 * stylesheet.
99 * <p/>
100 * A datasource is assigned with this element is set to a default source,
101 * which always returns null.
102 */
103 protected Element()
104 {
105 this.style = new CSSStyleRule(null, null);
106 this.attributes = new AttributeMap();
107 this.enabled = true;
108 setNamespace(JFreeReportInfo.REPORT_NAMESPACE);
109 }
110
111 public String getNamespace()
112 {
113 return (String) getAttribute
114 (JFreeReportInfo.REPORT_NAMESPACE, Element.NAMESPACE_ATTRIBUTE);
115 }
116
117 public void setNamespace(final String id)
118 {
119 setAttribute
120 (JFreeReportInfo.REPORT_NAMESPACE, Element.NAMESPACE_ATTRIBUTE, id);
121 }
122
123 public String getId()
124 {
125 return (String) getAttribute
126 (Namespaces.XML_NAMESPACE, Element.ID_ATTRIBUTE);
127 }
128
129 public void setId(final String id)
130 {
131 setAttribute(Namespaces.XML_NAMESPACE, Element.ID_ATTRIBUTE, id);
132 }
133
134 public String getType()
135 {
136 return (String) getAttribute
137 (JFreeReportInfo.REPORT_NAMESPACE, Element.TYPE_ATTRIBUTE);
138 }
139
140 public void setType(final String type)
141 {
142 setAttribute
143 (JFreeReportInfo.REPORT_NAMESPACE, Element.TYPE_ATTRIBUTE, type);
144 }
145
146 /**
147 * Defines the name for this Element. The name must not be empty, or a
148 * NullPointerException is thrown.
149 * <p/>
150 * Names can be used to lookup an element within a band. There is no
151 * requirement for element names to be unique.
152 *
153 * @param name the name of this element
154 */
155 public void setName(final String name)
156 {
157 setAttribute(Namespaces.XML_NAMESPACE, Element.NAME_ATTRIBUTE, name);
158 }
159
160
161 /**
162 * Returns the name of the Element. The name of the Element is never null.
163 *
164 * @return the name.
165 */
166 public String getName()
167 {
168 return (String) getAttribute
169 (Namespaces.XML_NAMESPACE, Element.NAME_ATTRIBUTE);
170 }
171
172 public void setAttribute(final String name, final Object value)
173 {
174 setAttribute(getNamespace(), name, value);
175 }
176
177 public void setAttribute(final String namespace,
178 final String name,
179 final Object value)
180 {
181 if (name == null)
182 {
183 throw new NullPointerException();
184 }
185 this.attributes.setAttribute(namespace, name, value);
186 readOnlyAttributes = null;
187 }
188
189 public Object getAttribute(final String name)
190 {
191 return getAttribute(getNamespace(), name);
192 }
193
194 public Object getAttribute(final String namespace, final String name)
195 {
196 return this.attributes.getAttribute(namespace, name);
197 }
198
199 public AttributeMap getAttributeMap()
200 {
201 if (this.readOnlyAttributes == null)
202 {
203 this.readOnlyAttributes = this.attributes.createUnmodifiableMap();
204 }
205 else if (readOnlyAttributes.getChangeTracker() != this.attributes.getChangeTracker())
206 {
207 this.readOnlyAttributes = this.attributes.createUnmodifiableMap();
208 }
209 return readOnlyAttributes;
210 }
211
212 /**
213 * Returns this elements private stylesheet. This sheet can be used to
214 * override the default values set in one of the parent-stylesheets.
215 *
216 * @return the Element's stylesheet
217 */
218 public CSSStyleRule getStyle()
219 {
220 return style;
221 }
222
223 public void setVisibility(final CSSConstant v)
224 {
225 getStyle().setPropertyValue(BoxStyleKeys.VISIBILITY, v);
226 }
227
228
229 public CSSConstant getVisibility()
230 {
231 return (CSSConstant) getStyle().getPropertyCSSValue(BoxStyleKeys.VISIBILITY);
232 }
233
234 public void setAttributeExpression(final String attr,
235 final Expression function)
236 {
237 setAttribute(getNamespace(), attr, function);
238 }
239
240 /**
241 * Adds a function to the report's collection of expressions.
242 *
243 * @param namespace
244 * @param attr
245 * @param function the function.
246 */
247 public void setAttributeExpression(final String namespace,
248 final String attr,
249 final Expression function)
250 {
251
252 if (attributeExpressions == null)
253 {
254 if (function == null)
255 {
256 return;
257 }
258 this.attributeExpressions = new AttributeMap();
259 }
260 attributeExpressions.setAttribute(namespace, attr, function);
261 readOnlyAttributeExpressions = null;
262 }
263
264 /**
265 * Returns the expressions for the report.
266 *
267 * @param attr
268 * @return the expressions.
269 */
270 public Expression getAttributeExpression(final String attr)
271 {
272 return getAttributeExpression(getNamespace(), attr);
273 }
274
275 public Expression getAttributeExpression(final String namespace,
276 final String attr)
277 {
278 if (attributeExpressions == null)
279 {
280 return null;
281 }
282 return (Expression) attributeExpressions.getAttribute(namespace, attr);
283 }
284
285 public Map getAttributeExpressions(final String namespace)
286 {
287 if (attributeExpressions == null)
288 {
289 return null;
290 }
291 return attributeExpressions.getAttributes(namespace);
292 }
293
294 public AttributeMap getAttributeExpressionMap()
295 {
296 if (this.attributeExpressions == null)
297 {
298 this.readOnlyAttributeExpressions = new AttributeMap();
299 return readOnlyAttributeExpressions;
300 }
301
302 if (this.readOnlyAttributeExpressions == null)
303 {
304 this.readOnlyAttributeExpressions = this.attributeExpressions.createUnmodifiableMap();
305 }
306 else if (readOnlyAttributeExpressions.getChangeTracker() != this.attributeExpressions.getChangeTracker())
307 {
308 this.readOnlyAttributeExpressions = this.attributeExpressions.createUnmodifiableMap();
309 }
310 return readOnlyAttributeExpressions;
311 }
312
313
314 /**
315 * Adds a function to the report's collection of expressions.
316 *
317 * @param function the function.
318 * @param property
319 */
320 public void setStyleExpression(final String property,
321 final Expression function)
322 {
323 if (function == null)
324 {
325 if (styleExpressions != null)
326 {
327 styleExpressions.remove(property);
328 cachedStyleExpressions = null;
329 }
330 }
331 else
332 {
333 if (styleExpressions == null)
334 {
335 styleExpressions = new HashMap();
336 }
337 styleExpressions.put(property, function);
338 cachedStyleExpressions = null;
339 }
340 }
341
342 /**
343 * Returns the expressions for the report.
344 *
345 * @param property
346 * @return the expressions.
347 */
348 public Expression getStyleExpression(final String property)
349 {
350 if (styleExpressions == null)
351 {
352 return null;
353 }
354 return (Expression) styleExpressions.get(property);
355 }
356
357 /** @noinspection ReturnOfCollectionOrArrayField*/
358 public Map getStyleExpressions()
359 {
360 if (styleExpressions == null)
361 {
362 return Element.EMPTY_MAP;
363 }
364 if (cachedStyleExpressions != null)
365 {
366 return cachedStyleExpressions;
367 }
368 cachedStyleExpressions = Collections.unmodifiableMap(styleExpressions);
369 return cachedStyleExpressions;
370 }
371
372 /**
373 * Adds a function to the report's collection of expressions.
374 *
375 * @param function the function.
376 */
377 public void addExpression(final Expression function)
378 {
379 if (expressions == null)
380 {
381 expressions = new ArrayList();
382 }
383 expressions.add(function);
384 }
385
386 /**
387 * Returns the expressions for the report.
388 *
389 * @return the expressions.
390 */
391 public Expression[] getExpressions()
392 {
393 if (expressions == null)
394 {
395 return Element.EMPTY_EXPRESSIONS;
396 }
397 return (Expression[]) expressions.toArray
398 (new Expression[expressions.size()]);
399 }
400
401 /**
402 * Sets the expressions for the report.
403 *
404 * @param expressions the expressions (<code>null</code> not permitted).
405 */
406 public void setExpressions(final Expression[] expressions)
407 {
408 if (expressions == null)
409 {
410 throw new NullPointerException(
411 "JFreeReport.setExpressions(...) : null not permitted.");
412 }
413 if (this.expressions == null)
414 {
415 this.expressions = new ArrayList(expressions.length);
416 }
417 else
418 {
419 this.expressions.clear();
420 }
421 this.expressions.addAll(Arrays.asList(expressions));
422 }
423
424 /**
425 * Returns true, if the element is enabled.
426 *
427 * @return true or false
428 */
429 public boolean isEnabled()
430 {
431 return enabled;
432 }
433
434 /**
435 * Defines whether the element is enabled. Disabled elements will be fully
436 * ignored by the report processor. This is a design time property to exclude
437 * elements from the processing without actually having to deal with the other
438 * complex properties.
439 *
440 * @param enabled
441 */
442 public void setEnabled(final boolean enabled)
443 {
444 this.enabled = enabled;
445 }
446
447 public Expression getDisplayCondition()
448 {
449 return displayCondition;
450 }
451
452 public void setDisplayCondition(final Expression displayCondition)
453 {
454 this.displayCondition = displayCondition;
455 }
456
457 public Locale getLocale()
458 {
459 final Locale locale = getLocaleFromAttributes();
460 if (locale != null)
461 {
462 return locale;
463 }
464 return super.getLocale();
465 }
466
467 protected Locale getLocaleFromAttributes()
468 {
469 final Object mayBeXmlLang = getAttribute(Namespaces.XML_NAMESPACE, "lang");
470 if (mayBeXmlLang instanceof String)
471 {
472 return LocaleUtility.createLocale((String) mayBeXmlLang);
473 }
474 else if (mayBeXmlLang instanceof Locale)
475 {
476 return (Locale) mayBeXmlLang;
477 }
478
479 final Object mayBeXhtmlLang = getAttribute(Namespaces.XHTML_NAMESPACE,
480 "lang");
481 if (mayBeXhtmlLang instanceof String)
482 {
483 return LocaleUtility.createLocale((String) mayBeXhtmlLang);
484 }
485 else if (mayBeXhtmlLang instanceof Locale)
486 {
487 return (Locale) mayBeXhtmlLang;
488 }
489 //
490 // final Object mayBeHtmlLang = getAttribute(Namespaces.XHTML_NAMESPACE, "lang");
491 // if (mayBeHtmlLang instanceof String)
492 // {
493 // return LocaleUtility.createLocale((String) mayBeHtmlLang);
494 // }
495 // else if (mayBeHtmlLang instanceof Locale)
496 // {
497 // return (Locale) mayBeHtmlLang;
498 // }
499
500 return null;
501 }
502
503 public boolean isVirtual()
504 {
505 return virtual;
506 }
507
508 public void setVirtual(final boolean virtual)
509 {
510 this.virtual = virtual;
511 }
512
513
514 public Object clone()
515 throws CloneNotSupportedException
516 {
517 final Element element = (Element) super.clone();
518 element.style = (CSSStyleRule) style.clone();
519 if (attributes != null)
520 {
521 element.attributes = (AttributeMap) attributes.clone();
522 }
523
524 if (attributeExpressions != null)
525 {
526 element.attributeExpressions = (AttributeMap) attributeExpressions.clone();
527 final String[] namespaces = element.attributeExpressions.getNameSpaces();
528 for (int i = 0; i < namespaces.length; i++)
529 {
530 final String namespace = namespaces[i];
531 final Map attrsNs = element.attributeExpressions.getAttributes(
532 namespace);
533 final Iterator it =
534 attrsNs.entrySet().iterator();
535 while (it.hasNext())
536 {
537 final Map.Entry entry = (Map.Entry) it.next();
538 final Expression exp = (Expression) entry.getValue();
539 entry.setValue(exp.clone());
540 }
541 }
542 }
543
544 if (expressions != null)
545 {
546 element.expressions = (ArrayList) expressions.clone();
547 element.expressions.clear();
548 for (int i = 0; i < expressions.size(); i++)
549 {
550 final Expression expression = (Expression) expressions.get(i);
551 element.expressions.add(expression.clone());
552 }
553 }
554 if (styleExpressions != null)
555 {
556 element.styleExpressions = (HashMap) styleExpressions.clone();
557 final Iterator styleExpressionsIt =
558 element.styleExpressions.entrySet().iterator();
559 while (styleExpressionsIt.hasNext())
560 {
561 final Map.Entry entry = (Map.Entry) styleExpressionsIt.next();
562 final Expression exp = (Expression) entry.getValue();
563 entry.setValue(exp.clone());
564 }
565 }
566
567 if (displayCondition != null)
568 {
569 element.displayCondition = (Expression) displayCondition.clone();
570 }
571 return element;
572 }
573 }