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.data;
24 
25 import com.sun.star.report.pentaho.OfficeNamespaces;
26 import com.sun.star.report.pentaho.model.DataStyle;
27 import com.sun.star.report.pentaho.parser.ElementReadHandler;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 import org.jfree.report.structure.Element;
33 import org.jfree.report.structure.StaticText;
34 
35 import org.pentaho.reporting.libraries.xmlns.parser.XmlReadHandler;
36 
37 import org.xml.sax.Attributes;
38 import org.xml.sax.SAXException;
39 
40 
41 public class DataStyleReadHandler extends ElementReadHandler
42 {
43 
44     private final DataStyle dataStyle;
45     private final List children;
46     private final boolean hasCData;
47 
DataStyleReadHandler(final boolean hasCData)48     public DataStyleReadHandler(final boolean hasCData)
49     {
50         this.hasCData = hasCData;
51         this.dataStyle = new DataStyle();
52         this.children = new ArrayList();
53     }
54 
55     /**
56      * Returns the handler for a child element.
57      *
58      * @param tagName the tag name.
59      * @param atts    the attributes.
60      * @return the handler or null, if the tagname is invalid.
61      * @throws org.xml.sax.SAXException if there is a parsing error.
62      */
getHandlerForChild(final String uri, final String tagName, final Attributes atts)63     protected XmlReadHandler getHandlerForChild(final String uri,
64             final String tagName,
65             final Attributes atts)
66             throws SAXException
67     {
68         if (OfficeNamespaces.DATASTYLE_NS.equals(uri) || OfficeNamespaces.STYLE_NS.equals(uri))
69         {
70             final DataStyleReadHandler xrh = new DataStyleReadHandler("text".equals(tagName) || "currency-symbol".equals(tagName) || "embedded-text".equals(tagName));
71             children.add(xrh);
72             return xrh;
73         }
74 
75         return null;
76     }
77 
78     /**
79      * This method is called to process the character data between element tags.
80      *
81      * @param ch     the character buffer.
82      * @param start  the start index.
83      * @param length the length.
84      * @throws org.xml.sax.SAXException if there is a parsing error.
85      */
characters(final char[] ch, final int start, final int length)86     public void characters(final char[] ch, final int start, final int length)
87             throws SAXException
88     {
89         if (hasCData)
90         {
91             children.add(new StaticText(new String(ch, start, length)));
92         }
93     }
94 
95     /**
96      * Done parsing.
97      *
98      * @throws org.xml.sax.SAXException if there is a parsing error.
99      */
doneParsing()100     protected void doneParsing() throws SAXException
101     {
102         for (int i = 0; i < children.size(); i++)
103         {
104             final Object o = children.get(i);
105             if (o instanceof ElementReadHandler)
106             {
107                 final ElementReadHandler handler = (ElementReadHandler) o;
108                 dataStyle.addNode(handler.getElement());
109             }
110             else if (o instanceof StaticText)
111             {
112                 dataStyle.addNode((StaticText) o);
113             }
114         }
115     }
116 
getDataStyle()117     public DataStyle getDataStyle()
118     {
119         return dataStyle;
120     }
121 
getElement()122     public Element getElement()
123     {
124         return dataStyle;
125     }
126 }
127