1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.report.pentaho.parser.rpt;
24 
25 import com.sun.star.report.OfficeToken;
26 import com.sun.star.report.pentaho.OfficeNamespaces;
27 import com.sun.star.report.pentaho.model.OfficeGroup;
28 import com.sun.star.report.pentaho.model.OfficeGroupInstanceSection;
29 import com.sun.star.report.pentaho.parser.ElementReadHandler;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 import org.jfree.report.JFreeReportInfo;
35 import org.jfree.report.expressions.FormulaExpression;
36 import org.jfree.report.structure.Element;
37 import org.jfree.report.structure.Section;
38 
39 import org.pentaho.reporting.libraries.xmlns.parser.XmlReadHandler;
40 
41 import org.xml.sax.Attributes;
42 import org.xml.sax.SAXException;
43 
44 
45 public class GroupReadHandler extends ElementReadHandler
46 {
47 
48     private GroupSectionReadHandler groupHeader;
49     private GroupSectionReadHandler groupFooter;
50     private GroupReadHandler childGroup;
51     private RootTableReadHandler detailSection;
52     private final OfficeGroup group;
53     private final OfficeGroupInstanceSection groupInstanceSection;
54     private final List functionHandlers;
55     private final ReportReadHandler rh;
56 
GroupReadHandler(final ReportReadHandler _rh)57     public GroupReadHandler(final ReportReadHandler _rh)
58     {
59         rh = _rh;
60         group = new OfficeGroup();
61         groupInstanceSection = new OfficeGroupInstanceSection();
62         groupInstanceSection.setNamespace(JFreeReportInfo.REPORT_NAMESPACE);
63         groupInstanceSection.setType("group-instance");
64         group.addNode(groupInstanceSection);
65         functionHandlers = new ArrayList();
66     }
67 
68     /**
69      * Starts parsing.
70      *
71      * @param attrs the attributes.
72      * @throws org.xml.sax.SAXException if there is a parsing error.
73      */
startParsing(final Attributes attrs)74     protected void startParsing(final Attributes attrs) throws SAXException
75     {
76         super.startParsing(attrs);
77 
78         final String groupExpr = attrs.getValue(OfficeNamespaces.OOREPORT_NS, "group-expression");
79         if (groupExpr != null && !"".equals(groupExpr))
80         {
81             final FormulaExpression function = new FormulaExpression();
82             function.setFormula(groupExpr);
83             groupInstanceSection.setGroupingExpression(function);
84         }
85     }
86 
87     /**
88      * Returns the handler for a child element.
89      *
90      * @param tagName the tag name.
91      * @param atts    the attributes.
92      * @return the handler or null, if the tagname is invalid.
93      * @throws org.xml.sax.SAXException if there is a parsing error.
94      */
getHandlerForChild(final String uri, final String tagName, final Attributes atts)95     protected XmlReadHandler getHandlerForChild(final String uri,
96             final String tagName,
97             final Attributes atts)
98             throws SAXException
99     {
100         if (!OfficeNamespaces.OOREPORT_NS.equals(uri))
101         {
102             return null;
103         }
104         if ("function".equals(tagName))
105         {
106             final FunctionReadHandler erh = new FunctionReadHandler();
107             functionHandlers.add(erh);
108             return erh;
109         }
110         if ("group-header".equals(tagName))
111         {
112             groupHeader = new GroupSectionReadHandler();
113             return groupHeader;
114         }
115         if ("group".equals(tagName))
116         {
117             childGroup = new GroupReadHandler(rh);
118             return childGroup;
119         }
120         if ("detail".equals(tagName))
121         {
122             detailSection = new DetailRootTableReadHandler();
123             rh.setDetail(detailSection);
124             return detailSection;
125         }
126         if ("group-footer".equals(tagName))
127         {
128             ((Element) ((Section) rh.getDetail().getElement()).getNode(0)).setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "has-group-footer", OfficeToken.TRUE);
129             groupFooter = new GroupSectionReadHandler();
130             return groupFooter;
131         }
132         return null;
133     }
134 
135     /**
136      * Done parsing.
137      *
138      * @throws org.xml.sax.SAXException if there is a parsing error.
139      */
doneParsing()140     protected void doneParsing() throws SAXException
141     {
142         for (int i = 0; i < functionHandlers.size(); i++)
143         {
144             final FunctionReadHandler handler =
145                     (FunctionReadHandler) functionHandlers.get(i);
146             groupInstanceSection.addExpression(handler.getExpression());
147         }
148 
149         if (groupHeader != null)
150         {
151             groupInstanceSection.addNode(groupHeader.getElement());
152         }
153 
154         final Section groupBody = new Section();
155         groupBody.setNamespace(JFreeReportInfo.REPORT_NAMESPACE);
156         groupBody.setType("group-body");
157         groupInstanceSection.addNode(groupBody);
158         // XOR: Either the detail or the group section can be set ..
159         if (detailSection != null)
160         {
161             groupBody.addNode(detailSection.getElement());
162         }
163         else if (childGroup != null)
164         {
165             groupBody.addNode(childGroup.getElement());
166         }
167 
168         if (groupFooter != null)
169         {
170             groupInstanceSection.addNode(groupFooter.getElement());
171         }
172     }
173 
getElement()174     public Element getElement()
175     {
176         return group;
177     }
178 }
179