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.office;
24 
25 import com.sun.star.report.pentaho.OfficeNamespaces;
26 import com.sun.star.report.pentaho.model.OfficeMasterStyles;
27 import com.sun.star.report.pentaho.parser.ElementReadHandler;
28 import com.sun.star.report.pentaho.parser.style.MasterPageReadHandler;
29 import com.sun.star.report.pentaho.parser.style.StyleDefinitionReadHandler;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
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 
42 /**
43  * Todo: Document me!
44  *
45  * @author Thomas Morgner
46  * @since 13.03.2007
47  */
48 public class MasterStylesReadHandler extends ElementReadHandler
49 {
50 
51     private final OfficeMasterStyles masterStyles;
52     private final List otherHandlers;
53     private final List masterPageHandlers;
54 
MasterStylesReadHandler(final OfficeMasterStyles masterStyles)55     public MasterStylesReadHandler(final OfficeMasterStyles masterStyles)
56     {
57         this.masterStyles = masterStyles;
58         this.masterPageHandlers = new ArrayList();
59         this.otherHandlers = new ArrayList();
60     }
61 
getMasterStyles()62     public OfficeMasterStyles getMasterStyles()
63     {
64         return masterStyles;
65     }
66 
67     /**
68      * Returns the handler for a child element.
69      *
70      * @param tagName the tag name.
71      * @param atts    the attributes.
72      * @return the handler or null, if the tagname is invalid.
73      *
74      * @throws org.xml.sax.SAXException if there is a parsing error.
75      */
getHandlerForChild(final String uri, final String tagName, final Attributes atts)76     protected XmlReadHandler getHandlerForChild(final String uri,
77             final String tagName,
78             final Attributes atts)
79             throws SAXException
80     {
81         if (OfficeNamespaces.STYLE_NS.equals(uri) && "master-page".equals(tagName))
82         {
83             final MasterPageReadHandler mrh = new MasterPageReadHandler();
84             masterPageHandlers.add(mrh);
85             return mrh;
86         }
87 
88         final StyleDefinitionReadHandler readHandler =
89                 new StyleDefinitionReadHandler();
90         otherHandlers.add(readHandler);
91         return readHandler;
92     }
93 
94     /**
95      * Done parsing.
96      *
97      * @throws org.xml.sax.SAXException if there is a parsing error.
98      */
doneParsing()99     protected void doneParsing()
100             throws SAXException
101     {
102         for (int i = 0; i < otherHandlers.size(); i++)
103         {
104             final ElementReadHandler handler =
105                     (ElementReadHandler) otherHandlers.get(i);
106             masterStyles.getOtherNodes().addNode(handler.getElement());
107         }
108 
109         for (int i = 0; i < masterPageHandlers.size(); i++)
110         {
111             final MasterPageReadHandler handler =
112                     (MasterPageReadHandler) masterPageHandlers.get(i);
113             masterStyles.addMasterPage(handler.getMasterPage());
114         }
115     }
116 
getElement()117     public Element getElement()
118     {
119         return masterStyles;
120     }
121 }
122