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.table;
24 
25 import com.sun.star.report.OfficeToken;
26 import com.sun.star.report.pentaho.OfficeNamespaces;
27 import com.sun.star.report.pentaho.model.OfficeTableSection;
28 import com.sun.star.report.pentaho.parser.ElementReadHandler;
29 import com.sun.star.report.pentaho.parser.rpt.ConditionalPrintExpressionReadHandler;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
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 /**
44  * Creation-Date: 03.07.2006, 13:47:47
45  *
46  * @author Thomas Morgner
47  */
48 public class TableReadHandler extends ElementReadHandler
49 {
50 
51     private final List children;
52     private final Section table;
53 
TableReadHandler()54     public TableReadHandler()
55     {
56         children = new ArrayList();
57         table = new OfficeTableSection();
58     }
59 
60     /**
61      * Starts parsing.
62      *
63      * @param attrs the attributes.
64      * @throws org.xml.sax.SAXException if there is a parsing error.
65      */
startParsing(final Attributes attrs)66     protected void startParsing(final Attributes attrs)
67             throws SAXException
68     {
69         super.startParsing(attrs);
70         final String enabled = attrs.getValue(OfficeNamespaces.OOREPORT_NS, "visible");
71         if (enabled == null || OfficeToken.TRUE.equals(enabled))
72         {
73             table.setEnabled(true);
74         }
75         else
76         {
77             table.setEnabled(false);
78         }
79 
80     }
81 
82     /**
83      * Returns the handler for a child element.
84      *
85      * @param tagName the tag name.
86      * @param atts    the attributes.
87      * @return the handler or null, if the tagname is invalid.
88      * @throws org.xml.sax.SAXException if there is a parsing error.
89      */
getHandlerForChild(final String uri, final String tagName, final Attributes atts)90     protected XmlReadHandler getHandlerForChild(final String uri,
91             final String tagName,
92             final Attributes atts)
93             throws SAXException
94     {
95         if (OfficeNamespaces.OOREPORT_NS.equals(uri) && "conditional-print-expression".equals(tagName))
96         {
97             return new ConditionalPrintExpressionReadHandler(table);
98         }
99         else if (OfficeNamespaces.TABLE_NS.equals(uri))
100         {
101             if (OfficeToken.TABLE_COLUMNS.equals(tagName) || OfficeToken.TABLE_HEADER_COLUMNS.equals(tagName))
102             {
103                 final TableColumnsReadHandler columns = new TableColumnsReadHandler();
104                 children.add(columns);
105                 return columns;
106             }
107             else if (OfficeToken.TABLE_ROW.equals(tagName))
108             {
109                 final TableRowReadHandler rowHandler = new TableRowReadHandler();
110                 children.add(rowHandler);
111                 return rowHandler;
112             }
113             else if (OfficeToken.TABLE_ROWS.equals(tagName) || OfficeToken.TABLE_HEADER_ROWS.equals(tagName))
114             {
115                 final TableRowsReadHandler rowsHandler = new TableRowsReadHandler();
116                 children.add(rowsHandler);
117                 return rowsHandler;
118             }
119         }
120         return null;
121     }
122 
123     /**
124      * Done parsing.
125      *
126      * @throws org.xml.sax.SAXException if there is a parsing error.
127      */
doneParsing()128     protected void doneParsing() throws SAXException
129     {
130         for (int i = 0; i < children.size(); i++)
131         {
132             final ElementReadHandler handler = (ElementReadHandler) children.get(i);
133             table.addNode(handler.getElement());
134         }
135     }
136 
getElement()137     public Element getElement()
138     {
139         return table;
140     }
141 }
142