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.style;
24 
25 import com.sun.star.report.pentaho.OfficeNamespaces;
26 import com.sun.star.report.pentaho.model.OfficeStyles;
27 import com.sun.star.report.pentaho.parser.ElementReadHandler;
28 import com.sun.star.report.pentaho.parser.data.DataStyleReadHandler;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 import org.jfree.report.modules.factories.report.flow.SectionReadHandler;
34 import org.jfree.report.structure.Element;
35 
36 import org.pentaho.reporting.libraries.xmlns.parser.XmlReadHandler;
37 
38 import org.xml.sax.Attributes;
39 import org.xml.sax.SAXException;
40 
41 public class OfficeStylesReadHandler extends ElementReadHandler
42 {
43 
44     private final List textStyleChilds;
45     private final List dataStyleChilds;
46     private final List otherStyleChilds;
47     private final List pageLayoutChilds;
48     private final OfficeStyles officeStyles;
49 
OfficeStylesReadHandler(final OfficeStyles officeStyles)50     public OfficeStylesReadHandler(final OfficeStyles officeStyles)
51     {
52         this.officeStyles = officeStyles;
53         this.pageLayoutChilds = new ArrayList();
54         this.dataStyleChilds = new ArrayList();
55         this.textStyleChilds = new ArrayList();
56         this.otherStyleChilds = new ArrayList();
57     }
58 
59     /**
60      * Returns the handler for a child element.
61      *
62      * @param tagName the tag name.
63      * @param atts    the attributes.
64      * @return the handler or null, if the tagname is invalid.
65      *
66      * @throws org.xml.sax.SAXException if there is a parsing error.
67      */
getHandlerForChild(final String uri, final String tagName, final Attributes atts)68     protected XmlReadHandler getHandlerForChild(final String uri, final String tagName,
69             final Attributes atts)
70             throws SAXException
71     {
72         if (OfficeNamespaces.STYLE_NS.equals(uri))
73         {
74             if ("style".equals(tagName))
75             {
76                 final OfficeStyleReadHandler xrh = new OfficeStyleReadHandler();
77                 textStyleChilds.add(xrh);
78                 return xrh;
79             }
80             else if ("page-layout".equals(tagName))
81             {
82                 final PageLayoutReadHandler prh = new PageLayoutReadHandler();
83                 pageLayoutChilds.add(prh);
84                 return prh;
85             }
86         }
87         else if (OfficeNamespaces.DATASTYLE_NS.equals(uri))
88         {
89             final DataStyleReadHandler xrh = new DataStyleReadHandler(false);
90             dataStyleChilds.add(xrh);
91             return xrh;
92         }
93 
94         final SectionReadHandler genericReadHander = new SectionReadHandler();
95         otherStyleChilds.add(genericReadHander);
96         return genericReadHander;
97     }
98 
99     /**
100      * Done parsing.
101      *
102      * @throws org.xml.sax.SAXException if there is a parsing error.
103      */
doneParsing()104     protected void doneParsing() throws SAXException
105     {
106         for (int i = 0; i < textStyleChilds.size(); i++)
107         {
108             final OfficeStyleReadHandler handler =
109                     (OfficeStyleReadHandler) textStyleChilds.get(i);
110             officeStyles.addStyle(handler.getOfficeStyle());
111         }
112 
113         for (int i = 0; i < pageLayoutChilds.size(); i++)
114         {
115             final PageLayoutReadHandler handler =
116                     (PageLayoutReadHandler) pageLayoutChilds.get(i);
117             officeStyles.addPageStyle(handler.getPageLayout());
118         }
119 
120         for (int i = 0; i < dataStyleChilds.size(); i++)
121         {
122             final DataStyleReadHandler handler =
123                     (DataStyleReadHandler) dataStyleChilds.get(i);
124             officeStyles.addDataStyle(handler.getDataStyle());
125         }
126 
127         for (int i = 0; i < otherStyleChilds.size(); i++)
128         {
129             final SectionReadHandler handler =
130                     (SectionReadHandler) otherStyleChilds.get(i);
131             officeStyles.addOtherNode((Element) handler.getNode());
132         }
133     }
134 
getElement()135     public Element getElement()
136     {
137         return officeStyles;
138     }
139 }
140