summaryrefslogtreecommitdiff
path: root/reportbuilder/java/com/sun/star/report/pentaho/output/spreadsheet/SpreadsheetRawReportTarget.java
blob: d2c4ebd96e3c97dd1a517b7ec45939d776b42cfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
package com.sun.star.report.pentaho.output.spreadsheet;

import com.sun.star.report.DataSourceFactory;
import com.sun.star.report.ImageService;
import com.sun.star.report.InputRepository;
import com.sun.star.report.OfficeToken;
import com.sun.star.report.OutputRepository;
import com.sun.star.report.pentaho.OfficeNamespaces;
import com.sun.star.report.pentaho.PentahoReportEngineMetaData;
import com.sun.star.report.pentaho.model.OfficeMasterPage;
import com.sun.star.report.pentaho.model.OfficeMasterStyles;
import com.sun.star.report.pentaho.model.OfficeStyle;
import com.sun.star.report.pentaho.model.OfficeStyles;
import com.sun.star.report.pentaho.model.OfficeStylesCollection;
import com.sun.star.report.pentaho.model.PageSection;
import com.sun.star.report.pentaho.output.OfficeDocumentReportTarget;
import com.sun.star.report.pentaho.output.StyleUtilities;
import com.sun.star.report.pentaho.output.text.MasterPageFactory;
import com.sun.star.report.pentaho.styles.LengthCalculator;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jfree.layouting.input.style.values.CSSNumericType;
import org.jfree.layouting.input.style.values.CSSNumericValue;
import org.jfree.layouting.util.AttributeMap;
import org.jfree.report.DataFlags;
import org.jfree.report.DataSourceException;
import org.jfree.report.JFreeReportInfo;
import org.jfree.report.ReportProcessingException;
import org.jfree.report.flow.ReportJob;
import org.jfree.report.flow.ReportStructureRoot;
import org.jfree.report.flow.ReportTargetUtil;
import org.jfree.report.structure.Element;
import org.jfree.report.structure.Section;
import org.jfree.report.util.IntegerCache;

import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
import org.pentaho.reporting.libraries.xmlns.common.AttributeList;
import org.pentaho.reporting.libraries.xmlns.writer.XmlWriter;
import org.pentaho.reporting.libraries.xmlns.writer.XmlWriterSupport;


/**
 * Creation-Date: 03.11.2007
 *
 * @author Michael D'Amour
 */
public class SpreadsheetRawReportTarget extends OfficeDocumentReportTarget
{

    private static final String[] FOPROPS = new String[]
    {
        "letter-spacing", "font-variant", "text-transform"
    };
    private static final String NUMBERCOLUMNSSPANNED = "number-columns-spanned";
    private static final String[] STYLEPROPS = new String[]
    {
        "text-combine", "font-pitch-complex", "text-rotation-angle", "font-name", "text-blinking", "letter-kerning", "text-combine-start-char", "text-combine-end-char", "text-position", "text-scale"
    };
    private static final int CELL_WIDTH_FACTOR = 10000;
    private static final String TRANSPARENT = "transparent";
    private boolean paragraphFound = false;
    private boolean paragraphHandled = false;

    /**
     * This class represents a column boundary, not in width, but it's actual boundary location. One of the motivations
     * for creating this class was to be able to record the boundaries for each incoming table while consuming as few
     * objects/memory as possible.
     */
    private static class ColumnBoundary implements Comparable
    {

        private final Set tableIndices;
        private final long boundary;

        private ColumnBoundary(final long boundary)
        {
            this.tableIndices = new HashSet();
            this.boundary = boundary;
        }

        public void addTableIndex(final int table)
        {
            tableIndices.add(IntegerCache.getInteger(table));
        }

        public float getBoundary()
        {
            return boundary;
        }

        public boolean isContainedByTable(final int table)
        {
            final Integer index = IntegerCache.getInteger(table);
            return tableIndices.contains(index);
        }

        public int compareTo(final Object arg0)
        {
            if (arg0.equals(this))
            {
                return 0;
            }
            if (arg0 instanceof ColumnBoundary)
            {
                if (boundary > ((ColumnBoundary) arg0).boundary)
                {
                    return 1;
                }
                else
                {
                    return -1;
                }
            }
            return 1;
        }

        public boolean equals(final Object obj)
        {
            return obj instanceof ColumnBoundary && ((ColumnBoundary) obj).boundary == boundary;
        }

        public int hashCode()
        {
            assert false : "hashCode not designed";
            return 42; // any arbitrary constant will do
        }
    }
    private String tableBackgroundColor; // null means transparent ...
    private static final ColumnBoundary[] EMPTY_COLBOUNDS = new ColumnBoundary[0];
    private boolean elementBoundaryCollectionPass;
    private boolean oleHandled;
    private final List columnBoundaryList;
    private long currentRowBoundaryMarker;
    private ColumnBoundary[] sortedBoundaryArray;
    private ColumnBoundary[] boundariesForTableArray;
    private int tableCounter;
    private int columnCounter;
    private int columnSpanCounter;
    private int currentSpan = 0;
    private String unitsOfMeasure;
    final private List shapes;
    final private List ole;
    final private List rowHeights;

    public SpreadsheetRawReportTarget(final ReportJob reportJob,
            final ResourceManager resourceManager,
            final ResourceKey baseResource,
            final InputRepository inputRepository,
            final OutputRepository outputRepository,
            final String target,
            final ImageService imageService,
            final DataSourceFactory dataSourceFactory)
            throws ReportProcessingException
    {
        super(reportJob, resourceManager, baseResource, inputRepository, outputRepository, target, imageService, dataSourceFactory);
        columnBoundaryList = new ArrayList();
        elementBoundaryCollectionPass = true;
        rowHeights = new ArrayList();
        shapes = new ArrayList();
        ole = new ArrayList();
        oleHandled = false;
    }

    public void startOther(final AttributeMap attrs) throws DataSourceException, ReportProcessingException
    {
        if (ReportTargetUtil.isElementOfType(JFreeReportInfo.REPORT_NAMESPACE, OfficeToken.OBJECT_OLE, attrs))
        {
            if (isElementBoundaryCollectionPass() && getCurrentRole() != ROLE_TEMPLATE)
            {
                ole.add(attrs);
            }
            oleHandled = true;
            return;
        }
        final String namespace = ReportTargetUtil.getNamespaceFromAttribute(attrs);
        if (isRepeatingSection() || isFilteredNamespace(namespace))
        {
            return;
        }

        final String elementType = ReportTargetUtil.getElemenTypeFromAttribute(attrs);
        if (OfficeNamespaces.TEXT_NS.equals(namespace) && OfficeToken.P.equals(elementType) && !paragraphHandled)
        {
            paragraphFound = true;
            return;
        }

        if (OfficeNamespaces.DRAWING_NS.equals(namespace) && OfficeToken.FRAME.equals(elementType))
        {
            if (isElementBoundaryCollectionPass() && getCurrentRole() != ROLE_TEMPLATE)
            {
                final LengthCalculator len = new LengthCalculator();
                for (int i = 0; i < rowHeights.size(); i++)
                {
                    len.add((CSSNumericValue) rowHeights.get(i));
                    // val += ((CSSNumericValue)rowHeights.get(i)).getValue();
                }

                rowHeights.clear();
                final CSSNumericValue currentRowHeight = len.getResult();
                rowHeights.add(currentRowHeight);
                attrs.setAttribute(OfficeNamespaces.DRAWING_NS, "z-index", String.valueOf(shapes.size()));
                final String y = (String) attrs.getAttribute(OfficeNamespaces.SVG_NS, "y");
                if (y != null)
                {
                    len.add(parseLength(y));
                    final CSSNumericValue currentY = len.getResult();
                    attrs.setAttribute(OfficeNamespaces.SVG_NS, "y", currentY.getValue() + currentY.getType().getType());
                }
                shapes.add(attrs);
            }
            return;
        }
        if (oleHandled)
        {
            if (isElementBoundaryCollectionPass() && getCurrentRole() != ROLE_TEMPLATE)
            {
                ole.add(attrs);
            }
            return;
        }

        // if this is the report namespace, write out a table definition ..
        if (OfficeNamespaces.TABLE_NS.equals(namespace) && OfficeToken.TABLE.equals(elementType))
        {
            // whenever we see a new table, we increment our tableCounter
            // this is used to keep tracked of the boundary conditions per table
            tableCounter++;
        }

        if (isElementBoundaryCollectionPass())
        {
            collectBoundaryForElement(attrs);
        }
        else
        // if (!isElementBoundaryCollectionPass())
        {
            try
            {
                processElement(attrs, namespace, elementType);
            }
            catch (IOException e)
            {
                throw new ReportProcessingException(OfficeDocumentReportTarget.FAILED, e);
            }
        }
    }

    protected void startReportSection(final AttributeMap attrs, final int role) throws IOException, DataSourceException, ReportProcessingException
    {
        if ((role == OfficeDocumentReportTarget.ROLE_SPREADSHEET_PAGE_HEADER || role == OfficeDocumentReportTarget.ROLE_SPREADSHEET_PAGE_FOOTER) && (!PageSection.isPrintWithReportHeader(attrs) || !PageSection.isPrintWithReportFooter(attrs)))
        {
            startBuffering(new OfficeStylesCollection(), true);
        }
        else
        {
            super.startReportSection(attrs, role);
        }
    }

    protected void endReportSection(final AttributeMap attrs, final int role) throws IOException, DataSourceException, ReportProcessingException
    {
        if ((role == OfficeDocumentReportTarget.ROLE_SPREADSHEET_PAGE_HEADER || role == OfficeDocumentReportTarget.ROLE_SPREADSHEET_PAGE_FOOTER) && (!PageSection.isPrintWithReportHeader(attrs) || !PageSection.isPrintWithReportFooter(attrs)))
        {
            finishBuffering();
        }
        else
        {
            super.endReportSection(attrs, role);
        }
    }

    private void handleParagraph()
    {
        if (paragraphFound)
        {
            try
            {
                final XmlWriter xmlWriter = getXmlWriter();
                xmlWriter.writeTag(OfficeNamespaces.TEXT_NS, OfficeToken.P, null, XmlWriterSupport.OPEN);
                paragraphHandled = true;
                paragraphFound = false;
            }
            catch (IOException ex)
            {
                LOGGER.error("ReportProcessing failed", ex);
            }
        }
    }

    private void processElement(final AttributeMap attrs, final String namespace, final String elementType)
            throws IOException, ReportProcessingException
    {
        final XmlWriter xmlWriter = getXmlWriter();

        if (ReportTargetUtil.isElementOfType(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE, attrs))
        {
            // a new table means we must clear our "calculated" table boundary array cache
            boundariesForTableArray = null;

            final String tableStyle = (String) attrs.getAttribute(OfficeNamespaces.TABLE_NS, OfficeToken.STYLE_NAME);
            if (tableStyle == null)
            {
                tableBackgroundColor = null;
            }
            else
            {
                final Object raw = StyleUtilities.queryStyle(getPredefinedStylesCollection(), OfficeToken.TABLE, tableStyle,
                        "table-properties", OfficeNamespaces.FO_NS, OfficeToken.BACKGROUND_COLOR);
                if (raw == null || TRANSPARENT.equals(raw))
                {
                    tableBackgroundColor = null;
                }
                else
                {
                    tableBackgroundColor = String.valueOf(raw);
                }
            }
            return;
        }

        if (ReportTargetUtil.isElementOfType(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE_COLUMN, attrs) || ReportTargetUtil.isElementOfType(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE_COLUMNS, attrs))
        {
            return;
        }

        // covered-table-cell elements may appear in the input from row or column spans. In the event that we hit a
        // column-span we simply ignore these elements because we are going to adjust the span to fit the uniform table.
        if (ReportTargetUtil.isElementOfType(OfficeNamespaces.TABLE_NS, OfficeToken.COVERED_TABLE_CELL, attrs))
        {
            if (columnSpanCounter > 0)
            {
                columnSpanCounter--;
            }

            if (columnSpanCounter == 0)
            {
                // if we weren't expecting a covered-table-cell, let's use it, it's probably from a row-span
                columnCounter++;
                final int span = getColumnSpanForCell(tableCounter, columnCounter, 1);
                // use the calculated span for the column in the uniform table to create any additional covered-table-cell
                // elements
                for (int i = 0; i < span; i++)
                {
                    xmlWriter.writeTag(namespace, OfficeToken.COVERED_TABLE_CELL, null, XmlWriter.CLOSE);
                }
            }
            return;
        }

        if (ReportTargetUtil.isElementOfType(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE_ROW, attrs))
        {
            // a new row means our column counter gets reset
            columnCounter = 0;
            // Lets make sure the color of the table is ok ..
            if (tableBackgroundColor != null)
            {
                final String styleName = (String) attrs.getAttribute(OfficeNamespaces.TABLE_NS, OfficeToken.STYLE_NAME);
                final OfficeStyle style = deriveStyle(OfficeToken.TABLE_ROW, styleName);
                Element tableRowProperties = style.getTableRowProperties();
                if (tableRowProperties == null)
                {
                    tableRowProperties = new Section();
                    tableRowProperties.setNamespace(OfficeNamespaces.STYLE_NS);
                    tableRowProperties.setType("table-row-properties");
                    tableRowProperties.setAttribute(OfficeNamespaces.FO_NS, OfficeToken.BACKGROUND_COLOR, tableBackgroundColor);
                    style.addNode(tableRowProperties);
                }
                else
                {
                    final Object oldValue = tableRowProperties.getAttribute(OfficeNamespaces.FO_NS, OfficeToken.BACKGROUND_COLOR);
                    if (oldValue == null || TRANSPARENT.equals(oldValue))
                    {
                        tableRowProperties.setAttribute(OfficeNamespaces.FO_NS, OfficeToken.BACKGROUND_COLOR, tableBackgroundColor);
                    }
                }
                attrs.setAttribute(OfficeNamespaces.TABLE_NS, OfficeToken.STYLE_NAME, style.getStyleName());
            }
        }
        else if (ReportTargetUtil.isElementOfType(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE_CELL, attrs))
        {
            columnCounter++;
            final String styleName = (String) attrs.getAttribute(OfficeNamespaces.TABLE_NS, OfficeToken.STYLE_NAME);
            if (styleName != null)
            {
                final OfficeStyle cellStyle = getPredefinedStylesCollection().getStyle(OfficeToken.TABLE_CELL, styleName);
                if (cellStyle != null)
                {
                    final Section textProperties = (Section) cellStyle.getTextProperties();
                    if (textProperties != null)
                    {
                        for (String i : FOPROPS)
                        {
                            textProperties.setAttribute(OfficeNamespaces.FO_NS, i, null);
                        }
                        textProperties.setAttribute(OfficeNamespaces.TEXT_NS, "display", null);
                        for (String i : STYLEPROPS)
                        {
                            textProperties.setAttribute(OfficeNamespaces.STYLE_NS, i, null);
                        }
                    }
                    final Section props = (Section) cellStyle.getTableCellProperties();
                    if (props != null)
                    {
                        final Object raw = props.getAttribute(OfficeNamespaces.FO_NS, OfficeToken.BACKGROUND_COLOR);
                        if (TRANSPARENT.equals(raw))
                        {
                            props.setAttribute(OfficeNamespaces.FO_NS, OfficeToken.BACKGROUND_COLOR, null);
                            // cellStyle.removeNode(props);
                        }
                    }
                }
                attrs.setAttribute(OfficeNamespaces.TABLE_NS, OfficeToken.STYLE_NAME, styleName);
            }

            final String numColSpanStr = (String) attrs.getAttribute(namespace, NUMBERCOLUMNSSPANNED);
            int initialColumnSpan = columnSpanCounter = 1;
            if (numColSpanStr != null)
            {
                initialColumnSpan = Integer.parseInt(numColSpanStr);
                columnSpanCounter = initialColumnSpan;
            }
            final int span = getColumnSpanForCell(tableCounter, columnCounter, initialColumnSpan);
            if (initialColumnSpan > 1)
            {
                // add the initial column span to our column counter index (subtract 1, since it is counted by default)
                columnCounter += initialColumnSpan - 1;
            }

            // if (span < initialColumnSpan)
            // {
            // // ColumnBoundary cbs[] = getBoundariesForTable(tableCounter);
            // // for (int i = 0; i < cbs.length; i++)
            // // {
            // // System.out.print(cbs[i].getBoundary() + " ");
            // // }
            // // System.out.println();
            //
            // LOGGER.error("A cell cannot span less than the declared columns: Declared=" + initialColumnSpan + " Computed="
            // + span);
            // }

            // there's no point to create number-columns-spanned attributes if we only span 1 column
            if (span > 1)
            {
                attrs.setAttribute(namespace, NUMBERCOLUMNSSPANNED, "" + span);
                currentSpan = span;
            }
            // we must also generate "covered-table-cell" elements for each column spanned
            // but we'll do this in the endElement, after we close this OfficeToken.TABLE_CELL
        }

        // All styles have to be processed or you will loose the paragraph-styles and inline text-styles.
        // ..
        performStyleProcessing(attrs);

        final AttributeList attrList = buildAttributeList(attrs);
        xmlWriter.writeTag(namespace, elementType, attrList, XmlWriter.OPEN);
        // System.out.println("elementType = " + elementType);
    }

    private void collectBoundaryForElement(final AttributeMap attrs)
    {
        if (ReportTargetUtil.isElementOfType(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE_COLUMNS, attrs))
        {
            // A table row resets the column counter.
            resetCurrentRowBoundaryMarker();
        }
        else if (ReportTargetUtil.isElementOfType(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE_COLUMN, attrs))
        {
            final String styleName = (String) attrs.getAttribute(OfficeNamespaces.TABLE_NS, OfficeToken.STYLE_NAME);
            if (styleName == null)
            {
                // This should not happen, but if it does, we will ignore that cell.
                return;
            }

            final OfficeStyle style = getPredefinedStylesCollection().getStyle(OfficeToken.TABLE_COLUMN, styleName);
            if (style == null)
            {
                // Now this is very bad. It means that there is no style defined with the given name.
                return;
            }

            final Element tableColumnProperties = style.getTableColumnProperties();
            String widthStr = (String) tableColumnProperties.getAttribute("column-width");
            widthStr = widthStr.substring(0, widthStr.indexOf(getUnitsOfMeasure(widthStr)));
            final float val = Float.parseFloat(widthStr) * CELL_WIDTH_FACTOR;
            addColumnWidthToRowBoundaryMarker((long) val);
            ColumnBoundary currentRowBoundary = new ColumnBoundary(getCurrentRowBoundaryMarker());
            final List columnBoundaryList_ = getColumnBoundaryList();
            final int idx = columnBoundaryList_.indexOf(currentRowBoundary);
            if (idx == -1)
            {
                columnBoundaryList_.add(currentRowBoundary);
            }
            else
            {
                currentRowBoundary = (ColumnBoundary) columnBoundaryList_.get(idx);
            }
            currentRowBoundary.addTableIndex(tableCounter);
        }
    }

    private String getUnitsOfMeasure(final String str)
    {
        if (unitsOfMeasure == null || "".equals(unitsOfMeasure))
        {
            if (str == null || "".equals(str))
            {
                unitsOfMeasure = "cm";
                return unitsOfMeasure;
            }

            // build units of measure, set it
            int i = str.length() - 1;
            for (; i >= 0; i--)
            {
                final char c = str.charAt(i);
                if (Character.isDigit(c) || c == '.' || c == ',')
                {
                    break;
                }
            }
            unitsOfMeasure = str.substring(i + 1);
        }
        return unitsOfMeasure;
    }

    private void createTableShapes() throws ReportProcessingException
    {
        if (!shapes.isEmpty())
        {
            try
            {
                final XmlWriter xmlWriter = getXmlWriter();
                // at this point we need to generate the table-columns section based on our boundary table
                // <table:shapes>
                // <draw:frame />
                // ..
                // </table:shapes>
                xmlWriter.writeTag(OfficeNamespaces.TABLE_NS, OfficeToken.SHAPES, null, XmlWriterSupport.OPEN);


                for (int i = 0; i < shapes.size(); i++)
                {
                    final AttributeMap attrs = (AttributeMap) shapes.get(i);
                    final AttributeList attrList = buildAttributeList(attrs);
                    attrList.removeAttribute(OfficeNamespaces.DRAWING_NS, OfficeToken.STYLE_NAME);
                    xmlWriter.writeTag(OfficeNamespaces.DRAWING_NS, OfficeToken.FRAME, attrList, XmlWriterSupport.OPEN);
                    startChartProcessing((AttributeMap) ole.get(i));

                    xmlWriter.writeCloseTag();
                }
                xmlWriter.writeCloseTag();
            }
            catch (IOException e)
            {
                throw new ReportProcessingException(OfficeDocumentReportTarget.FAILED, e);
            }
        }
    }

    private void createTableColumns() throws ReportProcessingException
    {
        try
        {
            final XmlWriter xmlWriter = getXmlWriter();
            // at this point we need to generate the table-columns section based on our boundary table
            // <table-columns>
            // <table-column style-name="coX"/>
            // ..
            // </table-columns>
            // the first boundary is '0' which is a placeholder so we will ignore it
            xmlWriter.writeTag(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE_COLUMNS, null, XmlWriterSupport.OPEN);

            // blow away current column styles
            // start processing at i=1 because we added a boundary for "0" which is virtual
            final ColumnBoundary[] cba = getSortedColumnBoundaryArray();
            for (int i = 1; i < cba.length; i++)
            {
                final ColumnBoundary cb = cba[i];
                float columnWidth = cb.getBoundary();
                if (i > 1)
                {
                    columnWidth -= cba[i - 1].getBoundary();
                }
                columnWidth = columnWidth / CELL_WIDTH_FACTOR;
                final OfficeStyle style = deriveStyle(OfficeToken.TABLE_COLUMN, ("co" + i + "_"));
                final Section tableColumnProperties = new Section();
                tableColumnProperties.setType("table-column-properties");
                tableColumnProperties.setNamespace(style.getNamespace());
                final String width = String.format("%f", columnWidth);
                tableColumnProperties.setAttribute(style.getNamespace(),
                        "column-width", width + getUnitsOfMeasure(null));
                style.addNode(tableColumnProperties);

                final AttributeList myAttrList = new AttributeList();
                myAttrList.setAttribute(OfficeNamespaces.TABLE_NS, OfficeToken.STYLE_NAME, style.getStyleName());
                xmlWriter.writeTag(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE_COLUMN, myAttrList, XmlWriterSupport.CLOSE);
            }
            xmlWriter.writeCloseTag();
        }
        catch (IOException e)
        {
            throw new ReportProcessingException(OfficeDocumentReportTarget.FAILED, e);
        }
    }

    protected void endOther(final AttributeMap attrs) throws DataSourceException, ReportProcessingException
    {
        if (ReportTargetUtil.isElementOfType(JFreeReportInfo.REPORT_NAMESPACE, OfficeToken.OBJECT_OLE, attrs) || oleHandled)
        {
            oleHandled = false;
            return;
        }

        if (ReportTargetUtil.isElementOfType(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE_ROW, attrs) && isElementBoundaryCollectionPass() && getCurrentRole() != ROLE_TEMPLATE)
        {
            final String styleName = (String) attrs.getAttribute(OfficeNamespaces.TABLE_NS, OfficeToken.STYLE_NAME);
            rowHeights.add(computeRowHeight(styleName));
        }

        if (isRepeatingSection() || isElementBoundaryCollectionPass())
        {
            return;
        }

        final String namespace = ReportTargetUtil.getNamespaceFromAttribute(attrs);
        if (isFilteredNamespace(namespace))
        {
            return;
        }
        final String elementType = ReportTargetUtil.getElemenTypeFromAttribute(attrs);
        if (OfficeNamespaces.DRAWING_NS.equals(namespace) && OfficeToken.FRAME.equals(elementType))
        {
            return;
        }

        // if this is the report namespace, write out a table definition ..
        if (OfficeNamespaces.TABLE_NS.equals(namespace) && (OfficeToken.TABLE.equals(elementType) || OfficeToken.COVERED_TABLE_CELL.equals(elementType) || OfficeToken.TABLE_COLUMN.equals(elementType) || OfficeToken.TABLE_COLUMNS.equals(elementType)))
        {
            return;
        }

        if (!paragraphHandled && OfficeNamespaces.TEXT_NS.equals(namespace) && OfficeToken.P.equals(elementType))
        {
            if (!paragraphHandled)
            {
                return;
            }

            paragraphHandled = false;
        }
        try
        {
            final XmlWriter xmlWriter = getXmlWriter();
            xmlWriter.writeCloseTag();
            // table-cell elements may have a number-columns-spanned attribute which indicates how many
            // 'covered-table-cell' elements we need to generate
            generateCoveredTableCells(attrs);
        }
        catch (IOException e)
        {
            throw new ReportProcessingException(OfficeDocumentReportTarget.FAILED, e);
        }
    }

    private void generateCoveredTableCells(final AttributeMap attrs) throws IOException
    {
        if (!ReportTargetUtil.isElementOfType(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE_CELL, attrs))
        {
            return;
        }

        // do this after we close the tag
        final XmlWriter xmlWriter = getXmlWriter();
        // final Object attribute = attrs.getAttribute(OfficeNamespaces.TABLE_NS,NUMBERCOLUMNSSPANNED);
        // final int span = TextUtilities.parseInt((String) attribute, 0);
        final int span = currentSpan;
        currentSpan = 0;
        for (int i = 1; i < span; i++)
        {
            xmlWriter.writeTag(OfficeNamespaces.TABLE_NS, OfficeToken.COVERED_TABLE_CELL, null, XmlWriter.CLOSE);
        }
    }

    public String getExportDescriptor()
    {
        return "raw/" + PentahoReportEngineMetaData.OPENDOCUMENT_SPREADSHEET;
    }

    // /////////////////////////////////////////////////////////////////////////
    public void processText(final String text) throws DataSourceException, ReportProcessingException
    {
        if (!(isRepeatingSection() || isElementBoundaryCollectionPass()))
        {
            handleParagraph();
            super.processText(text);
        }
    }

    public void processContent(final DataFlags value) throws DataSourceException, ReportProcessingException
    {
        if (!(isRepeatingSection() || isElementBoundaryCollectionPass()))
        {
            handleParagraph();
            super.processContent(value);
        }
    }

    protected String getStartContent()
    {
        return "spreadsheet";
    }

    protected void startContent(final AttributeMap attrs) throws IOException, DataSourceException,
            ReportProcessingException
    {
        if (!isElementBoundaryCollectionPass())
        {
            final XmlWriter xmlWriter = getXmlWriter();
            xmlWriter.writeTag(OfficeNamespaces.OFFICE_NS, getStartContent(), null, XmlWriterSupport.OPEN);

            writeNullDate();

            final AttributeMap tableAttributes = new AttributeMap();
            tableAttributes.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, Element.NAMESPACE_ATTRIBUTE, OfficeNamespaces.TABLE_NS);
            tableAttributes.setAttribute(JFreeReportInfo.REPORT_NAMESPACE, Element.TYPE_ATTRIBUTE, OfficeToken.TABLE);
            tableAttributes.setAttribute(OfficeNamespaces.TABLE_NS, OfficeToken.STYLE_NAME, generateInitialTableStyle());
            tableAttributes.setAttribute(OfficeNamespaces.TABLE_NS, "name", "Report");

            performStyleProcessing(tableAttributes);

            xmlWriter.writeTag(OfficeNamespaces.TABLE_NS, OfficeToken.TABLE, buildAttributeList(tableAttributes), XmlWriterSupport.OPEN);
            createTableShapes();
            createTableColumns();
        }
    }

    private String generateInitialTableStyle() throws ReportProcessingException
    {
        final OfficeStylesCollection predefStyles = getPredefinedStylesCollection();
        final OfficeStyles commonStyles = predefStyles.getAutomaticStyles();
        if (!commonStyles.containsStyle(OfficeToken.TABLE, "Initial_Table"))
        {
            final String masterPageName = createMasterPage();

            final OfficeStyle tableStyle = new OfficeStyle();
            tableStyle.setStyleFamily(OfficeToken.TABLE);
            tableStyle.setStyleName("Initial_Table");
            tableStyle.setAttribute(OfficeNamespaces.STYLE_NS, "master-page-name", masterPageName);
            final Element tableProperties = produceFirstChild(tableStyle, OfficeNamespaces.STYLE_NS, "table-properties");
            tableProperties.setAttribute(OfficeNamespaces.FO_NS, OfficeToken.BACKGROUND_COLOR, TRANSPARENT);
            commonStyles.addStyle(tableStyle);
        }
        return "Initial_Table";
    }

    private String createMasterPage() throws ReportProcessingException
    {
        final OfficeStylesCollection predefStyles = getPredefinedStylesCollection();
        final MasterPageFactory masterPageFactory = new MasterPageFactory(predefStyles.getMasterStyles());
        final OfficeMasterPage masterPage;
        if (!masterPageFactory.containsMasterPage("Standard", null, null))
        {
            masterPage = masterPageFactory.createMasterPage("Standard", null, null);

            final CSSNumericValue zeroLength = CSSNumericValue.createValue(CSSNumericType.CM, 0);
            final String pageLayoutTemplate = masterPage.getPageLayout();
            if (pageLayoutTemplate == null)
            {
                // there is no pagelayout. Create one ..
                final String derivedLayout = masterPageFactory.createPageStyle(getGlobalStylesCollection().getAutomaticStyles(), zeroLength, zeroLength);
                masterPage.setPageLayout(derivedLayout);
            }
            else
            {
                final String derivedLayout = masterPageFactory.derivePageStyle(pageLayoutTemplate,
                        getPredefinedStylesCollection().getAutomaticStyles(),
                        getGlobalStylesCollection().getAutomaticStyles(), zeroLength, zeroLength);
                masterPage.setPageLayout(derivedLayout);
            }

            final OfficeStylesCollection officeStylesCollection = getGlobalStylesCollection();
            final OfficeMasterStyles officeMasterStyles = officeStylesCollection.getMasterStyles();
            officeMasterStyles.addMasterPage(masterPage);
        }
        else
        {
            masterPage = masterPageFactory.getMasterPage("Standard", null, null);
        }
        return masterPage.getStyleName();
    }

    protected void endContent(final AttributeMap attrs) throws IOException, DataSourceException,
            ReportProcessingException
    {
        // todo
        if (!isElementBoundaryCollectionPass())
        {
            final XmlWriter xmlWriter = getXmlWriter();
            xmlWriter.writeCloseTag();
            xmlWriter.writeCloseTag();
        }
    }

    public void endReport(final ReportStructureRoot report) throws DataSourceException, ReportProcessingException
    {
        super.endReport(report);
        setElementBoundaryCollectionPass(false);
        resetTableCounter();
        columnCounter = 0;
    }

    private boolean isElementBoundaryCollectionPass()
    {
        return elementBoundaryCollectionPass;
    }

    private void setElementBoundaryCollectionPass(final boolean elementBoundaryCollectionPass)
    {
        this.elementBoundaryCollectionPass = elementBoundaryCollectionPass;
    }

    private ColumnBoundary[] getSortedColumnBoundaryArray()
    {
        if (sortedBoundaryArray == null)
        {
            getColumnBoundaryList().add(new ColumnBoundary(0));
            sortedBoundaryArray = (ColumnBoundary[]) getColumnBoundaryList().toArray(new ColumnBoundary[getColumnBoundaryList().size()]);
            Arrays.sort(sortedBoundaryArray);
        }
        return sortedBoundaryArray;
    }

    private List getColumnBoundaryList()
    {
        return columnBoundaryList;
    }

    private void addColumnWidthToRowBoundaryMarker(final long width)
    {
        currentRowBoundaryMarker += width;
    }

    private long getCurrentRowBoundaryMarker()
    {
        return currentRowBoundaryMarker;
    }

    private void resetTableCounter()
    {
        tableCounter = 0;
    }

    private void resetCurrentRowBoundaryMarker()
    {
        currentRowBoundaryMarker = 0;
    }

    private ColumnBoundary[] getBoundariesForTable(final int table)
    {
        if (boundariesForTableArray == null)
        {
            final List boundariesForTable = new ArrayList();
            final List boundaryList = getColumnBoundaryList();
            for (int i = 0; i < boundaryList.size(); i++)
            {
                final ColumnBoundary b = (ColumnBoundary) boundaryList.get(i);
                if (b.isContainedByTable(table))
                {
                    boundariesForTable.add(b);
                }
            }
            boundariesForTableArray = (ColumnBoundary[]) boundariesForTable.toArray(new ColumnBoundary[boundariesForTable.size()]);
            Arrays.sort(boundariesForTableArray);
        }
        return boundariesForTableArray;
    }

    private int getColumnSpanForCell(final int table, final int col, final int initialColumnSpan)
    {
        final ColumnBoundary[] globalBoundaries = getSortedColumnBoundaryArray();
        final ColumnBoundary[] tableBoundaries = getBoundariesForTable(table);
        // how many column boundaries in the globalBoundaries list fall between the currentRowWidth and the next boundary
        // for the current row

        float cellBoundary = tableBoundaries[col - 1].getBoundary();
        float cellWidth = tableBoundaries[col - 1].getBoundary();

        if (col > 1)
        {
            cellWidth = cellWidth - tableBoundaries[col - 2].getBoundary();
        }

        if (initialColumnSpan > 1)
        {
            // ok we've got some additional spanning specified on the input
            final int index = (col - 1) + (initialColumnSpan - 1);
            cellWidth += tableBoundaries[index].getBoundary() - tableBoundaries[col - 1].getBoundary();
            cellBoundary = tableBoundaries[index].getBoundary();
        }

        int beginBoundaryIndex = 0;
        int endBoundaryIndex = globalBoundaries.length - 1;
        for (int i = 0; i < globalBoundaries.length; i++)
        {
            // find beginning boundary
            if (globalBoundaries[i].getBoundary() <= cellBoundary - cellWidth)
            {
                beginBoundaryIndex = i;
            }
            if (globalBoundaries[i].getBoundary() <= cellBoundary)
            {
                endBoundaryIndex = i;
            }
        }
        final int span = endBoundaryIndex - beginBoundaryIndex;
        // span will be zero for the first column, so we adjust it to 1
        if (span == 0)
        {
            return 1;
        }
        // System.out.println("table = " + table + " col = " + col + " rowBoundaries.length = " + tableBoundaries.length + "
        // cellWidth = " + cellWidth + " span = " + span);
        return span;
    }

    protected String getTargetMimeType()
    {
        return "application/vnd.oasis.opendocument.spreadsheet";
    }
}