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  * Copyright (c) 2007, Your Corporation. All Rights Reserved.
24  */
25 package com.sun.star.report.pentaho.parser.text;
26 
27 import com.sun.star.report.OfficeToken;
28 import com.sun.star.report.pentaho.OfficeNamespaces;
29 import com.sun.star.report.pentaho.parser.ElementReadHandler;
30 import com.sun.star.report.pentaho.parser.rpt.FixedContentReadHandler;
31 import com.sun.star.report.pentaho.parser.rpt.FormattedTextReadHandler;
32 import com.sun.star.report.pentaho.parser.rpt.ImageReadHandler;
33 import com.sun.star.report.pentaho.parser.rpt.SubDocumentReadHandler;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 import org.jfree.report.structure.Element;
39 import org.jfree.report.structure.Section;
40 import org.jfree.report.structure.StaticText;
41 
42 import org.pentaho.reporting.libraries.xmlns.parser.XmlReadHandler;
43 
44 import org.xml.sax.Attributes;
45 import org.xml.sax.SAXException;
46 
47 
48 /**
49  * This is a generic implementation that accepts all input and adds special
50  * handlers for the report-elements.
51  *
52  * @author Thomas Morgner
53  */
54 public class NoCDATATextContentReadHandler extends ElementReadHandler
55 {
56 
57     private Section section;
58     private List children;
59     private boolean copyType;
60 
NoCDATATextContentReadHandler(final Section section, final boolean copyType)61     public NoCDATATextContentReadHandler(final Section section,
62             final boolean copyType)
63     {
64         this.children = new ArrayList();
65         this.section = section;
66         this.copyType = copyType;
67     }
68 
NoCDATATextContentReadHandler(final Section section)69     public NoCDATATextContentReadHandler(final Section section)
70     {
71         this(section, false);
72     }
73 
NoCDATATextContentReadHandler()74     public NoCDATATextContentReadHandler()
75     {
76         this(new Section(), true);
77     }
78 
79     /**
80      * Starts parsing.
81      *
82      * @param attrs the attributes.
83      * @throws org.xml.sax.SAXException if there is a parsing error.
84      */
startParsing(final Attributes attrs)85     protected void startParsing(final Attributes attrs) throws SAXException
86     {
87         super.startParsing(attrs);
88         final Element element = getElement();
89         if (copyType)
90         {
91             copyElementType(element);
92         }
93         copyAttributes(attrs, element);
94     }
95 
96     /**
97      * Returns the handler for a child element.
98      *
99      * @param tagName the tag name.
100      * @param atts    the attributes.
101      * @return the handler or null, if the tagname is invalid.
102      * @throws org.xml.sax.SAXException if there is a parsing error.
103      */
getHandlerForChild(final String uri, final String tagName, final Attributes atts)104     protected XmlReadHandler getHandlerForChild(final String uri,
105             final String tagName,
106             final Attributes atts)
107             throws SAXException
108     {
109         if (OfficeNamespaces.OOREPORT_NS.equals(uri))
110         {
111             if ("fixed-content".equals(tagName))
112             {
113                 final FixedContentReadHandler fixedContentReadHandler = new FixedContentReadHandler();
114                 children.add(fixedContentReadHandler);
115                 return fixedContentReadHandler;
116             }
117             if ("formatted-text".equals(tagName))
118             {
119                 final FormattedTextReadHandler formattedTextReadHandler = new FormattedTextReadHandler();
120                 children.add(formattedTextReadHandler);
121                 return formattedTextReadHandler;
122             }
123             if (OfficeToken.IMAGE.equals(tagName))
124             {
125                 final ImageReadHandler imageReadHandler = new ImageReadHandler();
126                 children.add(imageReadHandler);
127                 return imageReadHandler;
128             }
129             if ("sub-document".equals(tagName))
130             {
131                 final SubDocumentReadHandler subDocReadHandler = new SubDocumentReadHandler(section);
132                 // children.add(subDocReadHandler);
133                 return subDocReadHandler;
134             }
135         }
136         if (OfficeNamespaces.DRAWING_NS.equals(uri))
137         {
138             final XmlReadHandler readHandler;
139             if (OfficeToken.IMAGE.equals(tagName))
140             {
141                 readHandler = new ImageReadHandler();
142             }
143             else
144             {
145                 readHandler = new NoCDATATextContentReadHandler();
146             }
147             children.add(readHandler);
148             return readHandler;
149         }
150         else
151         {
152             final TextContentReadHandler readHandler = new TextContentReadHandler();
153             children.add(readHandler);
154             return readHandler;
155         }
156     }
157 
getChildren()158     public List getChildren()
159     {
160         return children;
161     }
162 
163     /**
164      * Done parsing.
165      *
166      * @throws org.xml.sax.SAXException if there is a parsing error.
167      */
doneParsing()168     protected void doneParsing() throws SAXException
169     {
170         for (int i = 0; i < children.size(); i++)
171         {
172             final Object o = children.get(i);
173             if (o instanceof ElementReadHandler)
174             {
175                 final ElementReadHandler handler = (ElementReadHandler) o;
176                 section.addNode(handler.getElement());
177             }
178             else if (o instanceof StaticText)
179             {
180                 section.addNode((StaticText) o);
181             }
182         }
183     }
184 
getElement()185     public Element getElement()
186     {
187         return section;
188     }
189 }
190