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.pentaho.OfficeNamespaces; 26 import com.sun.star.report.pentaho.model.OfficeReport; 27 import com.sun.star.report.pentaho.parser.ElementReadHandler; 28 import com.sun.star.report.pentaho.parser.chart.ChartReadHandler; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 import org.jfree.report.JFreeReportInfo; 34 import org.jfree.report.structure.Element; 35 import org.jfree.report.structure.Section; 36 37 import org.pentaho.reporting.libraries.xmlns.parser.XmlReadHandler; 38 39 import org.xml.sax.Attributes; 40 import org.xml.sax.SAXException; 41 42 43 public class ReportReadHandler extends ElementReadHandler 44 { 45 46 private RootTableReadHandler pageHeader; 47 private RootTableReadHandler pageFooter; 48 private RootTableReadHandler reportHeader; 49 private RootTableReadHandler reportFooter; 50 private RootTableReadHandler detail; 51 setDetail(final RootTableReadHandler detail)52 public void setDetail(final RootTableReadHandler detail) 53 { 54 this.detail = detail; 55 } 56 getDetail()57 public final RootTableReadHandler getDetail() 58 { 59 return detail; 60 } 61 private GroupReadHandler groups; 62 private final OfficeReport rootSection; 63 private final List functionHandlers; 64 private final List preBodyHandlers; 65 private final List postBodyHandlers; 66 private boolean pre = true; 67 ReportReadHandler()68 public ReportReadHandler() 69 { 70 rootSection = new OfficeReport(); 71 rootSection.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, "simple-report-structure", Boolean.TRUE); 72 functionHandlers = new ArrayList(); 73 preBodyHandlers = new ArrayList(); 74 postBodyHandlers = new ArrayList(); 75 } 76 77 /** 78 * Returns the handler for a child element. 79 * 80 * @param tagName the tag name. 81 * @param atts the attributes. 82 * @return the handler or null, if the tagname is invalid. 83 * @throws org.xml.sax.SAXException if there is a parsing error. 84 */ getHandlerForChild(final String uri, final String tagName, final Attributes atts)85 protected XmlReadHandler getHandlerForChild(final String uri, 86 final String tagName, 87 final Attributes atts) 88 throws SAXException 89 { 90 final XmlReadHandler erh; 91 if (OfficeNamespaces.CHART_NS.equals(uri)) 92 { 93 erh = new ChartReadHandler(this); 94 if (pre) 95 { 96 preBodyHandlers.add(erh); 97 } 98 else 99 { 100 postBodyHandlers.add(erh); 101 } 102 } 103 else if (OfficeNamespaces.OOREPORT_NS.equals(uri)) 104 { 105 if ("function".equals(tagName)) 106 { 107 erh = new FunctionReadHandler(); 108 functionHandlers.add(erh); 109 } 110 else if ("page-header".equals(tagName)) 111 { 112 pageHeader = new RootTableReadHandler(); 113 erh = pageHeader; 114 } 115 else if ("report-header".equals(tagName)) 116 { 117 reportHeader = new RootTableReadHandler(); 118 erh = reportHeader; 119 } 120 else if ("report-footer".equals(tagName)) 121 { 122 reportFooter = new RootTableReadHandler(); 123 erh = reportFooter; 124 } 125 else if ("page-footer".equals(tagName)) 126 { 127 pageFooter = new RootTableReadHandler(); 128 erh = pageFooter; 129 } 130 else if ("detail".equals(tagName)) 131 { 132 pre = false; 133 detail = new DetailRootTableReadHandler(); 134 erh = detail; 135 } 136 else if ("group".equals(tagName)) 137 { 138 groups = new GroupReadHandler(this); 139 erh = groups; 140 } 141 else 142 { 143 erh = null; 144 } 145 } 146 else 147 { 148 erh = null; 149 } 150 return erh; 151 } 152 153 /** 154 * Done parsing. 155 * 156 * @throws org.xml.sax.SAXException if there is a parsing error. 157 */ doneParsing()158 protected void doneParsing() throws SAXException 159 { 160 if (pageHeader != null) 161 { 162 rootSection.setPageHeader(pageHeader.getElement()); 163 } 164 if (pageFooter != null) 165 { 166 rootSection.setPageFooter(pageFooter.getElement()); 167 } 168 if (reportHeader != null) 169 { 170 rootSection.setReportHeader(reportHeader.getElement()); 171 } 172 173 final Section preBody = createSection("report-pre-body", preBodyHandlers); 174 if (preBody != null) 175 { 176 rootSection.setPreBodySection(preBody); 177 } 178 179 final Section groupBody = new Section(); 180 groupBody.setNamespace(JFreeReportInfo.REPORT_NAMESPACE); 181 groupBody.setType("report-body"); 182 rootSection.setBodySection(groupBody); 183 184 // XOR: Either the detail or the group section can be set .. 185 if (groups != null) 186 { 187 groupBody.addNode(groups.getElement()); 188 } 189 else if (detail != null) 190 { 191 groupBody.addNode(detail.getElement()); 192 } 193 194 final Section postBody = createSection("report-post-body", postBodyHandlers); 195 if (postBody != null) 196 { 197 rootSection.setPostBodySection(postBody); 198 } 199 200 if (reportFooter != null) 201 { 202 rootSection.setReportFooter(reportFooter.getElement()); 203 } 204 205 for (int i = 0; i < functionHandlers.size(); i++) 206 { 207 final FunctionReadHandler handler = 208 (FunctionReadHandler) functionHandlers.get(i); 209 rootSection.addExpression(handler.getExpression()); 210 } 211 } 212 getElement()213 public Element getElement() 214 { 215 return rootSection; 216 } 217 createSection(final String name, final List handler)218 private final Section createSection(final String name, final List handler) 219 { 220 if (!handler.isEmpty()) 221 { 222 final Section section = new Section(); 223 section.setNamespace(JFreeReportInfo.REPORT_NAMESPACE); 224 section.setType(name); 225 226 for (int i = 0; i < handler.size(); i++) 227 { 228 final ElementReadHandler erh = (ElementReadHandler) handler.get(i); 229 section.addNode(erh.getElement()); 230 } 231 return section; 232 } 233 return null; 234 } 235 } 236