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.output.chart;
24 
25 import com.sun.star.report.DataSourceFactory;
26 import com.sun.star.report.ImageService;
27 import com.sun.star.report.InputRepository;
28 import com.sun.star.report.OfficeToken;
29 import com.sun.star.report.OutputRepository;
30 import com.sun.star.report.pentaho.OfficeNamespaces;
31 import com.sun.star.report.pentaho.PentahoReportEngineMetaData;
32 import com.sun.star.report.pentaho.output.OfficeDocumentReportTarget;
33 
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37 
38 import org.jfree.layouting.util.AttributeMap;
39 import org.jfree.report.DataFlags;
40 import org.jfree.report.DataSourceException;
41 import org.jfree.report.ReportProcessingException;
42 import org.jfree.report.flow.ReportJob;
43 import org.jfree.report.flow.ReportStructureRoot;
44 import org.jfree.report.flow.ReportTargetUtil;
45 
46 import org.pentaho.reporting.libraries.base.util.IOUtils;
47 import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
48 import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
49 import org.pentaho.reporting.libraries.xmlns.common.AttributeList;
50 import org.pentaho.reporting.libraries.xmlns.writer.XmlWriter;
51 import org.pentaho.reporting.libraries.xmlns.writer.XmlWriterSupport;
52 
53 /**
54  *
55  * @author Ocke Janssen
56  */
57 public class ChartRawReportTarget extends OfficeDocumentReportTarget
58 {
59 
60     private boolean inFilterElements = false;
61     private boolean tableRowsStarted = false;
62     private int tableCount = 0;
63     private int closeTags = 0;
64 
ChartRawReportTarget(final ReportJob reportJob, final ResourceManager resourceManager, final ResourceKey baseResource, final InputRepository inputRepository, final OutputRepository outputRepository, final String target, final ImageService imageService, final DataSourceFactory dataSourceFactory)65     public ChartRawReportTarget(final ReportJob reportJob,
66             final ResourceManager resourceManager,
67             final ResourceKey baseResource,
68             final InputRepository inputRepository,
69             final OutputRepository outputRepository,
70             final String target,
71             final ImageService imageService,
72             final DataSourceFactory dataSourceFactory)
73             throws ReportProcessingException
74     {
75         super(reportJob, resourceManager, baseResource, inputRepository, outputRepository, target, imageService, dataSourceFactory);
76     }
77 
getTargetMimeType()78     protected String getTargetMimeType()
79     {
80         return "application/vnd.oasis.opendocument.chart";
81     }
82 
getStartContent()83     protected String getStartContent()
84     {
85         return "chart";
86     }
87 
getExportDescriptor()88     public String getExportDescriptor()
89     {
90         return "raw/" + PentahoReportEngineMetaData.OPENDOCUMENT_CHART;
91     }
92 
startContent(final AttributeMap attrs)93     protected void startContent(final AttributeMap attrs) throws IOException, DataSourceException, ReportProcessingException
94     {
95         inFilterElements = false;
96         closeTags = 0;
97         tableCount = 0;
98         final XmlWriter xmlWriter = getXmlWriter();
99         xmlWriter.writeTag(OfficeNamespaces.OFFICE_NS, getStartContent(), null, XmlWriterSupport.OPEN);
100         writeNullDate();
101         ++closeTags;
102     }
103 
endContent(final AttributeMap attrs)104     protected void endContent(final AttributeMap attrs) throws IOException, DataSourceException, ReportProcessingException
105     {
106         final XmlWriter xmlWriter = getXmlWriter();
107         //xmlWriter.writeCloseTag();
108         while (closeTags > 0)
109         {
110             xmlWriter.writeCloseTag();
111             --closeTags;
112         }
113     }
114 
startReportSection(final AttributeMap attrs, final int role)115     protected void startReportSection(final AttributeMap attrs, final int role)
116             throws IOException, DataSourceException, ReportProcessingException
117     {
118     }
119 
endReportSection(final AttributeMap attrs, final int role)120     protected void endReportSection(final AttributeMap attrs, final int role)
121             throws IOException, DataSourceException, ReportProcessingException
122     {
123     }
124 
startOther(final AttributeMap attrs)125     protected void startOther(final AttributeMap attrs) throws IOException, DataSourceException, ReportProcessingException
126     {
127         final String namespace = ReportTargetUtil.getNamespaceFromAttribute(attrs);
128         if (!isFilteredNamespace(namespace))
129         {
130             final String elementType = ReportTargetUtil.getElemenTypeFromAttribute(attrs);
131             try
132             {
133                 processElement(attrs, namespace, elementType);
134             }
135             catch (IOException e)
136             {
137                 throw new ReportProcessingException(OfficeDocumentReportTarget.FAILED, e);
138             }
139         }
140     }
141 
isFiltered(final String elementType)142     private boolean isFiltered(final String elementType)
143     {
144         return OfficeToken.TABLE_HEADER_COLUMNS.equals(elementType) || OfficeToken.TABLE_HEADER_ROWS.equals(elementType) || OfficeToken.TABLE_COLUMNS.equals(elementType);
145     }
146 
endOther(final AttributeMap attrs)147     protected void endOther(final AttributeMap attrs) throws IOException, DataSourceException, ReportProcessingException
148     {
149         if (tableRowsStarted && getCurrentRole() == ROLE_TEMPLATE)
150         {
151             return;
152         }
153         final String namespace = ReportTargetUtil.getNamespaceFromAttribute(attrs);
154         if (!isFilteredNamespace(namespace))
155         {
156             final String elementType = ReportTargetUtil.getElemenTypeFromAttribute(attrs);
157             // if this is the report namespace, write out a table definition ..
158             if (OfficeNamespaces.TABLE_NS.equals(namespace))
159             {
160                 if (OfficeToken.TABLE.equals(elementType) || OfficeToken.TABLE_ROWS.equals(elementType))
161                 {
162                     return;
163                 }
164                 else if (isFiltered(elementType))
165                 {
166                     inFilterElements = false;
167                     if (tableCount > 1)
168                     {
169                         return;
170                     }
171                 }
172             }
173             else if (OfficeNamespaces.CHART_NS.equals(namespace) && "chart".equals(elementType))
174             {
175                 return;
176             }
177             if (inFilterElements && tableCount > 1)
178             {
179                 return;
180             }
181             final XmlWriter xmlWriter = getXmlWriter();
182             xmlWriter.writeCloseTag();
183             --closeTags;
184         }
185     }
186 
processContent(final DataFlags value)187     public void processContent(final DataFlags value)
188             throws DataSourceException, ReportProcessingException
189     {
190         if (!(tableRowsStarted && getCurrentRole() == ROLE_TEMPLATE))
191         {
192             super.processContent(value);
193         }
194     }
195 
processElement(final AttributeMap attrs, final String namespace, final String elementType)196     private void processElement(final AttributeMap attrs, final String namespace, final String elementType)
197             throws IOException, ReportProcessingException
198     {
199         if (tableRowsStarted && getCurrentRole() == ROLE_TEMPLATE)
200         {
201             return;
202         }
203         if (OfficeNamespaces.TABLE_NS.equals(namespace))
204         {
205             if (OfficeToken.TABLE.equals(elementType))
206             {
207                 tableCount += 1;
208                 if (tableCount > 1)
209                 {
210                     return;
211                 }
212             }
213             else if (OfficeToken.TABLE_ROWS.equals(elementType))
214             {
215                 if (tableCount > 1)
216                 {
217                     return;
218                 }
219                 tableRowsStarted = true;
220             }
221             else if (isFiltered(elementType))
222             {
223                 inFilterElements = true;
224                 if (tableCount > 1)
225                 {
226                     return;
227                 }
228             }
229         }
230         if (inFilterElements && tableCount > 1)
231         {
232             return;
233         }
234 
235         // All styles have to be processed or you will loose the paragraph-styles and inline text-styles.
236         // ..
237         performStyleProcessing(attrs);
238 
239         final AttributeList attrList = buildAttributeList(attrs);
240         final XmlWriter xmlWriter = getXmlWriter();
241         xmlWriter.writeTag(namespace, elementType, attrList, XmlWriter.OPEN);
242         ++closeTags;
243         // System.out.println("elementType = " + elementType);
244     }
245     // /////////////////////////////////////////////////////////////////////////
246 
processText(final String text)247     public void processText(final String text) throws DataSourceException, ReportProcessingException
248     {
249         if (inFilterElements && tableCount > 1)
250         {
251             return;
252         }
253         super.processText(text);
254     }
255 
endReport(final ReportStructureRoot report)256     public void endReport(final ReportStructureRoot report)
257             throws DataSourceException, ReportProcessingException
258     {
259         super.endReport(report);
260         copyMeta();
261     }
262 }
263