1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package com.sun.star.report.pentaho.parser.table;
28 
29 import com.sun.star.report.OfficeToken;
30 import com.sun.star.report.pentaho.OfficeNamespaces;
31 import com.sun.star.report.pentaho.model.OfficeTableSection;
32 import com.sun.star.report.pentaho.parser.ElementReadHandler;
33 import com.sun.star.report.pentaho.parser.rpt.ConditionalPrintExpressionReadHandler;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 import org.jfree.report.structure.Element;
39 import org.jfree.report.structure.Section;
40 
41 import org.pentaho.reporting.libraries.xmlns.parser.XmlReadHandler;
42 
43 import org.xml.sax.Attributes;
44 import org.xml.sax.SAXException;
45 
46 
47 /**
48  * Creation-Date: 03.07.2006, 13:47:47
49  *
50  * @author Thomas Morgner
51  */
52 public class TableReadHandler extends ElementReadHandler
53 {
54 
55     private final List children;
56     private final Section table;
57 
58     public TableReadHandler()
59     {
60         children = new ArrayList();
61         table = new OfficeTableSection();
62     }
63 
64     /**
65      * Starts parsing.
66      *
67      * @param attrs the attributes.
68      * @throws org.xml.sax.SAXException if there is a parsing error.
69      */
70     protected void startParsing(final Attributes attrs)
71             throws SAXException
72     {
73         super.startParsing(attrs);
74         final String enabled = attrs.getValue(OfficeNamespaces.OOREPORT_NS, "visible");
75         if (enabled == null || OfficeToken.TRUE.equals(enabled))
76         {
77             table.setEnabled(true);
78         }
79         else
80         {
81             table.setEnabled(false);
82         }
83 
84     }
85 
86     /**
87      * Returns the handler for a child element.
88      *
89      * @param tagName the tag name.
90      * @param atts    the attributes.
91      * @return the handler or null, if the tagname is invalid.
92      * @throws org.xml.sax.SAXException if there is a parsing error.
93      */
94     protected XmlReadHandler getHandlerForChild(final String uri,
95             final String tagName,
96             final Attributes atts)
97             throws SAXException
98     {
99         if (OfficeNamespaces.OOREPORT_NS.equals(uri) && "conditional-print-expression".equals(tagName))
100         {
101             return new ConditionalPrintExpressionReadHandler(table);
102         }
103         else if (OfficeNamespaces.TABLE_NS.equals(uri))
104         {
105             if (OfficeToken.TABLE_COLUMNS.equals(tagName) || OfficeToken.TABLE_HEADER_COLUMNS.equals(tagName))
106             {
107                 final TableColumnsReadHandler columns = new TableColumnsReadHandler();
108                 children.add(columns);
109                 return columns;
110             }
111             else if (OfficeToken.TABLE_ROW.equals(tagName))
112             {
113                 final TableRowReadHandler rowHandler = new TableRowReadHandler();
114                 children.add(rowHandler);
115                 return rowHandler;
116             }
117             else if (OfficeToken.TABLE_ROWS.equals(tagName) || OfficeToken.TABLE_HEADER_ROWS.equals(tagName))
118             {
119                 final TableRowsReadHandler rowsHandler = new TableRowsReadHandler();
120                 children.add(rowsHandler);
121                 return rowsHandler;
122             }
123         }
124         return null;
125     }
126 
127     /**
128      * Done parsing.
129      *
130      * @throws org.xml.sax.SAXException if there is a parsing error.
131      */
132     protected void doneParsing() throws SAXException
133     {
134         for (int i = 0; i < children.size(); i++)
135         {
136             final ElementReadHandler handler = (ElementReadHandler) children.get(i);
137             table.addNode(handler.getElement());
138         }
139     }
140 
141     public Element getElement()
142     {
143         return table;
144     }
145 }
146