Debian Codegraph @

source/org/jfree/report/flow/layoutprocessor/LayoutControllerUtil.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: LayoutControllerUtil.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.flow.layoutprocessor;
33
34 import java.util.Iterator;
35 import java.util.Map;
36
37 import org.jfree.layouting.input.style.CSSDeclarationRule;
38 import org.jfree.layouting.input.style.CSSStyleRule;
39 import org.jfree.layouting.input.style.StyleKey;
40 import org.jfree.layouting.input.style.StyleKeyRegistry;
41 import org.jfree.layouting.input.style.StyleRule;
42 import org.jfree.layouting.input.style.values.CSSValue;
43 import org.jfree.layouting.namespace.NamespaceDefinition;
44 import org.jfree.layouting.namespace.Namespaces;
45 import org.jfree.layouting.util.AttributeMap;
46 import org.jfree.report.DataSourceException;
47 import org.jfree.report.EmptyReportData;
48 import org.jfree.report.JFreeReportInfo;
49 import org.jfree.report.ReportDataFactoryException;
50 import org.jfree.report.ReportProcessingException;
51 import org.jfree.report.data.GlobalMasterRow;
52 import org.jfree.report.data.PrecomputeNode;
53 import org.jfree.report.data.PrecomputeNodeKey;
54 import org.jfree.report.data.PrecomputedValueRegistry;
55 import org.jfree.report.data.ReportDataRow;
56 import org.jfree.report.data.StaticExpressionRuntimeData;
57 import org.jfree.report.expressions.Expression;
58 import org.jfree.report.expressions.ExpressionRuntime;
59 import org.jfree.report.flow.EmptyReportTarget;
60 import org.jfree.report.flow.FlowControlOperation;
61 import org.jfree.report.flow.FlowController;
62 import org.jfree.report.flow.LayoutExpressionRuntime;
63 import org.jfree.report.flow.ReportContext;
64 import org.jfree.report.flow.ReportJob;
65 import org.jfree.report.flow.ReportStructureRoot;
66 import org.jfree.report.flow.ReportTarget;
67 import org.jfree.report.structure.Element;
68 import org.jfree.report.structure.Group;
69 import org.jfree.report.structure.Node;
70 import org.jfree.report.structure.Section;
71 import org.pentaho.reporting.libraries.resourceloader.Resource;
72 import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
73 import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
74
75 /**
76 * Creation-Date: 24.11.2006, 15:01:22
77 *
78 * @author Thomas Morgner
79 */
80 public class LayoutControllerUtil
81 {
82 public static final EmptyReportData EMPTY_REPORT_DATA = new EmptyReportData();
83
84 private LayoutControllerUtil()
85 {
86 }
87
88 public static int findNodeInParent(final Section parentSection,
89 final Node n)
90 {
91 final Node[] nodes = parentSection.getNodeArray();
92 for (int i = 0; i < nodes.length; i++)
93 {
94 final Node node = nodes[i];
95 if (node == n)
96 {
97 return i;
98 }
99 }
100 return -1;
101 }
102
103 public static StaticExpressionRuntimeData getStaticExpressionRuntime
104 (final FlowController fc,
105 final Object declaringParent)
106 {
107 final GlobalMasterRow dataRow = fc.getMasterRow();
108 final ReportJob reportJob = fc.getReportJob();
109 final StaticExpressionRuntimeData sdd = new StaticExpressionRuntimeData();
110 sdd.setData(dataRow.getReportDataRow().getReportData());
111 sdd.setDeclaringParent(declaringParent);
112 sdd.setConfiguration(reportJob.getConfiguration());
113 sdd.setReportContext(fc.getReportContext());
114 return sdd;
115 }
116
117
118 public static LayoutExpressionRuntime getExpressionRuntime
119 (final FlowController fc, final Object node)
120 {
121 final LayoutExpressionRuntime ler = new LayoutExpressionRuntime();
122 ler.setConfiguration(fc.getReportJob().getConfiguration());
123 ler.setReportContext(fc.getReportContext());
124
125 final GlobalMasterRow masterRow = fc.getMasterRow();
126 ler.setDataRow(masterRow.getGlobalView());
127
128 final ReportDataRow reportDataRow = masterRow.getReportDataRow();
129 if (reportDataRow == null)
130 {
131 ler.setData(EMPTY_REPORT_DATA);
132 ler.setCurrentRow(-1);
133 }
134 else
135 {
136 ler.setData(reportDataRow.getReportData());
137 ler.setCurrentRow(reportDataRow.getCursor());
138 }
139
140 ler.setDeclaringParent(node);
141 return ler;
142 }
143
144
145 public static FlowController processFlowOperations(FlowController fc,
146 final FlowControlOperation[] ops)
147 throws DataSourceException
148 {
149 for (int i = 0; i < ops.length; i++)
150 {
151 final FlowControlOperation op = ops[i];
152 fc = fc.performOperation(op);
153 }
154 return fc;
155 }
156
157
158 /**
159 * Checks, whether the current group should continue. If there is no group, we assume that we should continue. (This
160 * emulates the control-break-algorithm's default behaviour if testing an empty set of arguments.)
161 *
162 * @param fc the current flow controller holding the data
163 * @param node the current node.
164 * @return true, if the group is finished and we should stop reiterating it, false if the group is not finished and we
165 * can start iterating it again.
166 * @throws org.jfree.report.DataSourceException
167 *
168 */
169 public static boolean isGroupFinished(final FlowController fc,
170 final Node node)
171 throws DataSourceException
172 {
173 final Node nodeParent = node.getParent();
174 if (nodeParent == null)
175 {
176 return false;
177 }
178 Group group = nodeParent.getGroup();
179 if (group == null)
180 {
181 return false;
182 }
183
184 // maybe we can move this state into the layoutstate itself so that
185 // we do not have to rebuild that crap all the time.
186 LayoutExpressionRuntime ler = null;
187
188 // OK, now we are almost complete.
189 while (group != null)
190 {
191 if (ler == null)
192 {
193 ler = getExpressionRuntime(fc, node);
194 }
195
196 ler.setDeclaringParent(group);
197
198 final Expression groupingExpression = group.getGroupingExpression();
199 if (groupingExpression != null)
200 {
201 groupingExpression.setRuntime(ler);
202 final Object groupFinished;
203 try
204 {
205 groupFinished = groupingExpression.computeValue();
206 }
207 finally
208 {
209 groupingExpression.setRuntime(null);
210 }
211
212 if (Boolean.TRUE.equals(groupFinished))
213 {
214 // If the group expression returns true, we should pack our belongings
215 // and stop with that process. The group is finished.
216
217 // In Cobol, this would mean that one of the group-fields has changed.
218 return true;
219 }
220 }
221
222 final Node parent = group.getParent();
223 if (parent == null)
224 {
225 group = null;
226 }
227 else
228 {
229 group = parent.getGroup();
230 }
231 }
232 return false;
233 }
234
235
236 private static void mergeDeclarationRule(final CSSDeclarationRule target,
237 final CSSDeclarationRule source)
238 {
239 final StyleKey[] styleKeys = source.getPropertyKeysAsArray();
240 for (int i = 0; i < styleKeys.length; i++)
241 {
242 final StyleKey key = styleKeys[i];
243 final CSSValue value = source.getPropertyCSSValue(key);
244 if (value == null)
245 {
246 continue;
247 }
248
249 final boolean sourceImportant = source.isImportant(key);
250 final boolean targetImportant = target.isImportant(key);
251 if (targetImportant)
252 {
253 continue;
254 }
255 target.setPropertyValue(key, value);
256 target.setImportant(key, sourceImportant);
257 }
258 }
259
260 private static CSSDeclarationRule processStyleAttribute
261 (final Object styleAttributeValue,
262 final Element node,
263 final ExpressionRuntime runtime,
264 CSSDeclarationRule targetRule)
265 throws DataSourceException
266 {
267 if (targetRule == null)
268 {
269 try
270 {
271 targetRule = (CSSDeclarationRule) node.getStyle().clone();
272 }
273 catch (CloneNotSupportedException e)
274 {
275 targetRule = new CSSStyleRule(null, null);
276 }
277 }
278
279
280 if (styleAttributeValue instanceof String)
281 {
282 // ugly, we have to parse that thing. Cant think of nothing
283 // worse than that.
284 final String styleText = (String) styleAttributeValue;
285 try
286 {
287 final ReportContext reportContext = runtime.getReportContext();
288 final ReportStructureRoot root = reportContext.getReportStructureRoot();
289 final ResourceKey baseResource = root.getBaseResource();
290 final ResourceManager resourceManager = root.getResourceManager();
291
292 final byte[] bytes = styleText.getBytes("UTF-8");
293 final ResourceKey key = resourceManager.createKey(bytes);
294 final Resource resource = resourceManager.create
295 (key, baseResource, StyleRule.class);
296
297 final CSSDeclarationRule parsedRule =
298 (CSSDeclarationRule) resource.getResource();
299 mergeDeclarationRule(targetRule, parsedRule);
300 }
301 catch (Exception e)
302 {
303 // ignore ..
304 e.printStackTrace();
305 }
306 }
307 else if (styleAttributeValue instanceof CSSStyleRule)
308 {
309 final CSSStyleRule styleRule =
310 (CSSStyleRule) styleAttributeValue;
311 mergeDeclarationRule(targetRule, styleRule);
312 }
313
314 // ok, not lets fill in the stuff from the style expressions ..
315 final Map styleExpressions = node.getStyleExpressions();
316 final Iterator styleExIt = styleExpressions.entrySet().iterator();
317
318 while (styleExIt.hasNext())
319 {
320 final Map.Entry entry = (Map.Entry) styleExIt.next();
321 final String name = (String) entry.getKey();
322 final Expression expression = (Expression) entry.getValue();
323 try
324 {
325 expression.setRuntime(runtime);
326 final Object value = expression.computeValue();
327 final StyleKey keyByName =
328 StyleKeyRegistry.getRegistry().findKeyByName(name);
329
330 if (value instanceof CSSValue)
331 {
332 final CSSValue cssvalue = (CSSValue) value;
333 if (keyByName != null)
334 {
335 targetRule.setPropertyValue(keyByName, cssvalue);
336 }
337 else
338 {
339 targetRule.setPropertyValueAsString(name, cssvalue.getCSSText());
340 }
341 }
342 else if (value != null)
343 {
344 targetRule.setPropertyValueAsString(name, String.valueOf(value));
345 }
346 }
347 finally
348 {
349 expression.setRuntime(null);
350 }
351 }
352 return targetRule;
353 }
354
355 private static AttributeMap collectAttributes(final Element node,
356 final ExpressionRuntime runtime)
357 throws DataSourceException
358 {
359
360 AttributeMap attributes = node.getAttributeMap();
361 final AttributeMap attributeExpressions = node.getAttributeExpressionMap();
362 if (attributeExpressions.isEmpty())
363 {
364 return attributes;
365 }
366
367 final String[] namespaces = attributeExpressions.getNameSpaces();
368 for (int i = 0; i < namespaces.length; i++)
369 {
370 final String namespace = namespaces[i];
371 final Map attrEx = attributeExpressions.getAttributes(namespace);
372
373 final Iterator attributeExIt = attrEx.entrySet().iterator();
374 while (attributeExIt.hasNext())
375 {
376 final Map.Entry entry = (Map.Entry) attributeExIt.next();
377 final String name = (String) entry.getKey();
378 final Expression expression = (Expression) entry.getValue();
379 try
380 {
381 expression.setRuntime(runtime);
382 final Object value = expression.computeValue();
383 if (attributes.isReadOnly())
384 {
385 attributes = new AttributeMap(attributes);
386 }
387 attributes.setAttribute(namespace, name, value);
388 }
389 finally
390 {
391 expression.setRuntime(null);
392 }
393 }
394 }
395 return attributes.createUnmodifiableMap();
396 }
397
398 public static AttributeMap processAttributes(final Element node,
399 final ReportTarget target,
400 final ExpressionRuntime runtime)
401 throws DataSourceException
402 {
403 final AttributeMap attributes = collectAttributes(node, runtime);
404 CSSDeclarationRule rule = null;
405
406
407 final String[] attrNamespaces = attributes.getNameSpaces();
408 for (int i = 0; i < attrNamespaces.length; i++)
409 {
410 final String namespace = attrNamespaces[i];
411 final Map attributeMap = attributes.getAttributes(namespace);
412 if (attributeMap == null || attributeMap.isEmpty())
413 {
414 continue;
415 }
416
417 final NamespaceDefinition nsDef = target.getNamespaceByUri(namespace);
418 final Iterator attributeIt = attributeMap.entrySet().iterator();
419 while (attributeIt.hasNext())
420 {
421 final Map.Entry entry = (Map.Entry) attributeIt.next();
422 final String key = (String) entry.getKey();
423 if (isStyleAttribute(nsDef, node.getType(), key))
424 {
425 final Object styleAttributeValue = entry.getValue();
426 rule = processStyleAttribute(styleAttributeValue, node, runtime, rule);
427 }
428 }
429 }
430
431 // Just in case there was no style-attribute but there are style-expressions
432 if (rule == null)
433 {
434 rule = processStyleAttribute(null, node, runtime, rule);
435 }
436
437 if (rule != null && rule.isEmpty() == false)
438 {
439 final AttributeMap retval = new AttributeMap(attributes);
440 retval.setAttribute(Namespaces.LIBLAYOUT_NAMESPACE, "style", rule);
441 retval.makeReadOnly();
442 return retval;
443 }
444
445 return attributes;
446 }
447
448 private static boolean isStyleAttribute(final NamespaceDefinition def,
449 final String elementName,
450 final String attrName)
451 {
452 if (def == null)
453 {
454 return false;
455 }
456
457 final String[] styleAttr = def.getStyleAttribute(elementName);
458 for (int i = 0; i < styleAttr.length; i++)
459 {
460 final String styleAttrib = styleAttr[i];
461 if (attrName.equals(styleAttrib))
462 {
463 return true;
464 }
465 }
466 return false;
467 }
468
469 public static AttributeMap createEmptyMap(final String namespace,
470 final String tagName)
471 {
472 final AttributeMap map = new AttributeMap();
473 map.setAttribute(JFreeReportInfo.REPORT_NAMESPACE,
474 Element.NAMESPACE_ATTRIBUTE, namespace);
475 map.setAttribute(JFreeReportInfo.REPORT_NAMESPACE,
476 Element.TYPE_ATTRIBUTE, tagName);
477 return map;
478 }
479
480
481 public static Object performPrecompute(final int expressionPosition,
482 final PrecomputeNodeKey nodeKey,
483 final LayoutController layoutController,
484 final FlowController flowController)
485 throws ReportProcessingException, ReportDataFactoryException,
486 DataSourceException
487 {
488 final FlowController fc = flowController.createPrecomputeInstance();
489 final PrecomputedValueRegistry pcvr = fc.getPrecomputedValueRegistry();
490
491 pcvr.startElementPrecomputation(nodeKey);
492
493 final LayoutController rootLc = layoutController.createPrecomputeInstance(fc);
494 final LayoutController rootParent = rootLc.getParent();
495 final ReportTarget target = new EmptyReportTarget(fc.getReportJob(), fc.getExportDescriptor());
496
497 LayoutController lc = rootLc;
498 while (lc.isAdvanceable())
499 {
500 lc = lc.advance(target);
501 while (lc.isAdvanceable() == false && lc.getParent() != null)
502 {
503 final LayoutController parent = lc.getParent();
504 lc = parent.join(lc.getFlowController());
505 }
506 }
507
508 target.commit();
509 final PrecomputeNode precomputeNode = pcvr.currentNode();
510 final Object functionResult = precomputeNode.getFunctionResult(expressionPosition);
511 pcvr.finishElementPrecomputation(nodeKey);
512 return functionResult;
513 // throw new IllegalStateException
514 // ("Ups - we did not get to the root parent again. This is awful and we cannot continue.");
515 }
516
517
518 public static LayoutController skipInvisibleElement(final LayoutController layoutController)
519 throws ReportProcessingException, ReportDataFactoryException, DataSourceException
520 {
521 final FlowController fc = layoutController.getFlowController();
522 final ReportTarget target = new EmptyReportTarget(fc.getReportJob(), fc.getExportDescriptor());
523 final LayoutController rootParent = layoutController.getParent();
524
525 // Now start to iterate until the derived layout controller 'lc' that has this given parent
526 // wants to join.
527 LayoutController lc = layoutController;
528 while (lc.isAdvanceable())
529 {
530 lc = lc.advance(target);
531 while (lc.isAdvanceable() == false && lc.getParent() != null)
532 {
533 final LayoutController parent = lc.getParent();
534 lc = parent.join(lc.getFlowController());
535 if (parent == rootParent)
536 {
537 target.commit();
538 return lc;
539 }
540 }
541 }
542 target.commit();
543 throw new IllegalStateException
544 ("Ups - we did not get to the root parent again. This is awful and we cannot continue.");
545 // return lc;
546 }
547
548 public static Object evaluateExpression(final FlowController flowController,
549 final Object declaringParent,
550 final Expression expression)
551 throws DataSourceException
552 {
553 final ExpressionRuntime runtime =
554 getExpressionRuntime(flowController, declaringParent);
555
556 try
557 {
558 expression.setRuntime(runtime);
559 return expression.computeValue();
560 }
561 catch (DataSourceException dse)
562 {
563 throw dse;
564 }
565 catch (Exception e)
566 {
567 throw new DataSourceException("Failed to evaluate expression", e);
568 }
569 finally
570 {
571 expression.setRuntime(null);
572 }
573 }
574 }