1*1a37d047SAndrew Rist /**************************************************************
2*1a37d047SAndrew Rist  *
3*1a37d047SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1a37d047SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1a37d047SAndrew Rist  * distributed with this work for additional information
6*1a37d047SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1a37d047SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1a37d047SAndrew Rist  * "License"); you may not use this file except in compliance
9*1a37d047SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*1a37d047SAndrew Rist  *
11*1a37d047SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*1a37d047SAndrew Rist  *
13*1a37d047SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1a37d047SAndrew Rist  * software distributed under the License is distributed on an
15*1a37d047SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1a37d047SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1a37d047SAndrew Rist  * specific language governing permissions and limitations
18*1a37d047SAndrew Rist  * under the License.
19*1a37d047SAndrew Rist  *
20*1a37d047SAndrew Rist  *************************************************************/
21*1a37d047SAndrew Rist 
22cdf0e10cSrcweir /*
23cdf0e10cSrcweir  * Copyright (c) 2007, Your Corporation. All Rights Reserved.
24cdf0e10cSrcweir  */
25cdf0e10cSrcweir package com.sun.star.report.pentaho.parser.text;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir import com.sun.star.report.OfficeToken;
28cdf0e10cSrcweir import com.sun.star.report.pentaho.OfficeNamespaces;
29cdf0e10cSrcweir import com.sun.star.report.pentaho.parser.ElementReadHandler;
30cdf0e10cSrcweir import com.sun.star.report.pentaho.parser.rpt.FixedContentReadHandler;
31cdf0e10cSrcweir import com.sun.star.report.pentaho.parser.rpt.FormattedTextReadHandler;
32cdf0e10cSrcweir import com.sun.star.report.pentaho.parser.rpt.ImageReadHandler;
33cdf0e10cSrcweir import com.sun.star.report.pentaho.parser.rpt.SubDocumentReadHandler;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import java.util.ArrayList;
36cdf0e10cSrcweir import java.util.List;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import org.jfree.report.structure.Element;
39cdf0e10cSrcweir import org.jfree.report.structure.Section;
40cdf0e10cSrcweir import org.jfree.report.structure.StaticText;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir import org.pentaho.reporting.libraries.xmlns.parser.XmlReadHandler;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir import org.xml.sax.Attributes;
45cdf0e10cSrcweir import org.xml.sax.SAXException;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir /**
49cdf0e10cSrcweir  * This is a generic implementation that accepts all input and adds special
50cdf0e10cSrcweir  * handlers for the report-elements.
51cdf0e10cSrcweir  *
52cdf0e10cSrcweir  * @author Thomas Morgner
53cdf0e10cSrcweir  */
54cdf0e10cSrcweir public class NoCDATATextContentReadHandler extends ElementReadHandler
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     private Section section;
58cdf0e10cSrcweir     private List children;
59cdf0e10cSrcweir     private boolean copyType;
60cdf0e10cSrcweir 
NoCDATATextContentReadHandler(final Section section, final boolean copyType)61cdf0e10cSrcweir     public NoCDATATextContentReadHandler(final Section section,
62cdf0e10cSrcweir             final boolean copyType)
63cdf0e10cSrcweir     {
64cdf0e10cSrcweir         this.children = new ArrayList();
65cdf0e10cSrcweir         this.section = section;
66cdf0e10cSrcweir         this.copyType = copyType;
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir 
NoCDATATextContentReadHandler(final Section section)69cdf0e10cSrcweir     public NoCDATATextContentReadHandler(final Section section)
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         this(section, false);
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir 
NoCDATATextContentReadHandler()74cdf0e10cSrcweir     public NoCDATATextContentReadHandler()
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir         this(new Section(), true);
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     /**
80cdf0e10cSrcweir      * Starts parsing.
81cdf0e10cSrcweir      *
82cdf0e10cSrcweir      * @param attrs the attributes.
83cdf0e10cSrcweir      * @throws org.xml.sax.SAXException if there is a parsing error.
84cdf0e10cSrcweir      */
startParsing(final Attributes attrs)85cdf0e10cSrcweir     protected void startParsing(final Attributes attrs) throws SAXException
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir         super.startParsing(attrs);
88cdf0e10cSrcweir         final Element element = getElement();
89cdf0e10cSrcweir         if (copyType)
90cdf0e10cSrcweir         {
91cdf0e10cSrcweir             copyElementType(element);
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir         copyAttributes(attrs, element);
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     /**
97cdf0e10cSrcweir      * Returns the handler for a child element.
98cdf0e10cSrcweir      *
99cdf0e10cSrcweir      * @param tagName the tag name.
100cdf0e10cSrcweir      * @param atts    the attributes.
101cdf0e10cSrcweir      * @return the handler or null, if the tagname is invalid.
102cdf0e10cSrcweir      * @throws org.xml.sax.SAXException if there is a parsing error.
103cdf0e10cSrcweir      */
getHandlerForChild(final String uri, final String tagName, final Attributes atts)104cdf0e10cSrcweir     protected XmlReadHandler getHandlerForChild(final String uri,
105cdf0e10cSrcweir             final String tagName,
106cdf0e10cSrcweir             final Attributes atts)
107cdf0e10cSrcweir             throws SAXException
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         if (OfficeNamespaces.OOREPORT_NS.equals(uri))
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             if ("fixed-content".equals(tagName))
112cdf0e10cSrcweir             {
113cdf0e10cSrcweir                 final FixedContentReadHandler fixedContentReadHandler = new FixedContentReadHandler();
114cdf0e10cSrcweir                 children.add(fixedContentReadHandler);
115cdf0e10cSrcweir                 return fixedContentReadHandler;
116cdf0e10cSrcweir             }
117cdf0e10cSrcweir             if ("formatted-text".equals(tagName))
118cdf0e10cSrcweir             {
119cdf0e10cSrcweir                 final FormattedTextReadHandler formattedTextReadHandler = new FormattedTextReadHandler();
120cdf0e10cSrcweir                 children.add(formattedTextReadHandler);
121cdf0e10cSrcweir                 return formattedTextReadHandler;
122cdf0e10cSrcweir             }
123cdf0e10cSrcweir             if (OfficeToken.IMAGE.equals(tagName))
124cdf0e10cSrcweir             {
125cdf0e10cSrcweir                 final ImageReadHandler imageReadHandler = new ImageReadHandler();
126cdf0e10cSrcweir                 children.add(imageReadHandler);
127cdf0e10cSrcweir                 return imageReadHandler;
128cdf0e10cSrcweir             }
129cdf0e10cSrcweir             if ("sub-document".equals(tagName))
130cdf0e10cSrcweir             {
131cdf0e10cSrcweir                 final SubDocumentReadHandler subDocReadHandler = new SubDocumentReadHandler(section);
132cdf0e10cSrcweir                 // children.add(subDocReadHandler);
133cdf0e10cSrcweir                 return subDocReadHandler;
134cdf0e10cSrcweir             }
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir         if (OfficeNamespaces.DRAWING_NS.equals(uri))
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             final XmlReadHandler readHandler;
139cdf0e10cSrcweir             if (OfficeToken.IMAGE.equals(tagName))
140cdf0e10cSrcweir             {
141cdf0e10cSrcweir                 readHandler = new ImageReadHandler();
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir             else
144cdf0e10cSrcweir             {
145cdf0e10cSrcweir                 readHandler = new NoCDATATextContentReadHandler();
146cdf0e10cSrcweir             }
147cdf0e10cSrcweir             children.add(readHandler);
148cdf0e10cSrcweir             return readHandler;
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir         else
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir             final TextContentReadHandler readHandler = new TextContentReadHandler();
153cdf0e10cSrcweir             children.add(readHandler);
154cdf0e10cSrcweir             return readHandler;
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir 
getChildren()158cdf0e10cSrcweir     public List getChildren()
159cdf0e10cSrcweir     {
160cdf0e10cSrcweir         return children;
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     /**
164cdf0e10cSrcweir      * Done parsing.
165cdf0e10cSrcweir      *
166cdf0e10cSrcweir      * @throws org.xml.sax.SAXException if there is a parsing error.
167cdf0e10cSrcweir      */
doneParsing()168cdf0e10cSrcweir     protected void doneParsing() throws SAXException
169cdf0e10cSrcweir     {
170cdf0e10cSrcweir         for (int i = 0; i < children.size(); i++)
171cdf0e10cSrcweir         {
172cdf0e10cSrcweir             final Object o = children.get(i);
173cdf0e10cSrcweir             if (o instanceof ElementReadHandler)
174cdf0e10cSrcweir             {
175cdf0e10cSrcweir                 final ElementReadHandler handler = (ElementReadHandler) o;
176cdf0e10cSrcweir                 section.addNode(handler.getElement());
177cdf0e10cSrcweir             }
178cdf0e10cSrcweir             else if (o instanceof StaticText)
179cdf0e10cSrcweir             {
180cdf0e10cSrcweir                 section.addNode((StaticText) o);
181cdf0e10cSrcweir             }
182cdf0e10cSrcweir         }
183cdf0e10cSrcweir     }
184cdf0e10cSrcweir 
getElement()185cdf0e10cSrcweir     public Element getElement()
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         return section;
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir }
190