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
24 #include <iostream>
25 #include <boost/shared_ptr.hpp>
26 #ifdef DEBUG_ELEMENT
27 #include "ooxmlLoggers.hxx"
28 #include <resourcemodel/Protocol.hxx>
29 #endif
30 #include "OOXMLFastDocumentHandler.hxx"
31 #include "OOXMLFastContextHandler.hxx"
32 #include "OOXMLFastTokens.hxx"
33 #include "OOXMLFactory.hxx"
34
35 namespace writerfilter {
36 namespace ooxml
37 {
38 using namespace ::com::sun::star;
39 using namespace ::std;
40
41
OOXMLFastDocumentHandler(uno::Reference<uno::XComponentContext> const & context,Stream * pStream,OOXMLDocument * pDocument)42 OOXMLFastDocumentHandler::OOXMLFastDocumentHandler(
43 uno::Reference< uno::XComponentContext > const & context,
44 Stream* pStream,
45 OOXMLDocument* pDocument )
46 : m_xContext(context)
47 , mpStream( pStream )
48 #ifdef DEBUG_ELEMENT
49 , mpTmpStream()
50 #endif
51 , mpDocument( pDocument )
52 , mpContextHandler()
53 {
54 #ifdef DEBUG_PROTOCOL
55 if ( pStream )
56 {
57 mpTmpStream.reset( new StreamProtocol( pStream, debug_logger ) );
58 mpStream = mpTmpStream.get();
59 }
60 #endif
61 }
62
63 // ::com::sun::star::xml::sax::XFastContextHandler:
startFastElement(::sal_Int32 Element,const uno::Reference<xml::sax::XFastAttributeList> &)64 void SAL_CALL OOXMLFastDocumentHandler::startFastElement
65 (::sal_Int32
66 #ifdef DEBUG_CONTEXT_STACK
67 Element
68 #endif
69 , const uno::Reference< xml::sax::XFastAttributeList > & /*Attribs*/)
70 throw (uno::RuntimeException, xml::sax::SAXException)
71 {
72 #ifdef DEBUG_CONTEXT_STACK
73 clog << this << ":start element:"
74 << fastTokenToId(Element)
75 << endl;
76 #endif
77 }
78
startUnknownElement(const::rtl::OUString & Namespace,const::rtl::OUString & Name,const uno::Reference<xml::sax::XFastAttributeList> &)79 void SAL_CALL OOXMLFastDocumentHandler::startUnknownElement
80 (const ::rtl::OUString &
81 #ifdef DEBUG_CONTEXT_STACK
82 Namespace
83 #endif
84 , const ::rtl::OUString &
85 #ifdef DEBUG_CONTEXT_STACK
86 Name
87 #endif
88 ,
89 const uno::Reference< xml::sax::XFastAttributeList > & /*Attribs*/)
90 throw (uno::RuntimeException, xml::sax::SAXException)
91 {
92 #ifdef DEBUG_CONTEXT_STACK
93 clog << this << ":start unknown element:"
94 << OUStringToOString(Namespace, RTL_TEXTENCODING_ASCII_US).getStr()
95 << ":"
96 << OUStringToOString(Name, RTL_TEXTENCODING_ASCII_US).getStr()
97 << endl;
98 #endif
99 }
100
endFastElement(::sal_Int32 Element)101 void SAL_CALL OOXMLFastDocumentHandler::endFastElement(::sal_Int32
102 #ifdef DEBUG_CONTEXT_STACK
103 Element
104 #endif
105 )
106 throw (uno::RuntimeException, xml::sax::SAXException)
107 {
108 #ifdef DEBUG_CONTEXT_STACK
109 clog << this << ":end element:"
110 << fastTokenToId(Element)
111 << endl;
112 #endif
113 }
114
endUnknownElement(const::rtl::OUString & Namespace,const::rtl::OUString & Name)115 void SAL_CALL OOXMLFastDocumentHandler::endUnknownElement
116 (const ::rtl::OUString &
117 #ifdef DEBUG_CONTEXT_STACK
118 Namespace
119 #endif
120 , const ::rtl::OUString &
121 #ifdef DEBUG_CONTEXT_STACK
122 Name
123 #endif
124 )
125 throw (uno::RuntimeException, xml::sax::SAXException)
126 {
127 #ifdef DEBUG_CONTEXT_STACK
128 clog << this << ":end unknown element:"
129 << OUStringToOString(Namespace, RTL_TEXTENCODING_ASCII_US).getStr()
130 << ":"
131 << OUStringToOString(Name, RTL_TEXTENCODING_ASCII_US).getStr()
132 << endl;
133 #endif
134 }
135
136 OOXMLFastContextHandler::Pointer_t
getContextHandler() const137 OOXMLFastDocumentHandler::getContextHandler() const
138 {
139 if (mpContextHandler == OOXMLFastContextHandler::Pointer_t())
140 {
141 mpContextHandler.reset(
142 new OOXMLFastContextHandler(m_xContext) );
143 mpContextHandler->setStream(mpStream);
144 mpContextHandler->setDocument(mpDocument);
145 mpContextHandler->setForwardEvents(true);
146 }
147
148 return mpContextHandler;
149 }
150
151 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
createFastChildContext(::sal_Int32 Element,const uno::Reference<xml::sax::XFastAttributeList> &)152 OOXMLFastDocumentHandler::createFastChildContext
153 (::sal_Int32 Element,
154 const uno::Reference< xml::sax::XFastAttributeList > & /*Attribs*/)
155 throw (uno::RuntimeException, xml::sax::SAXException)
156 {
157 #ifdef DEBUG_CONTEXT_STACK
158 clog << this << ":createFastChildContext:"
159 << fastTokenToId(Element)
160 << endl;
161 #endif
162
163 if ( mpStream == 0 && mpDocument == 0 )
164 {
165 // document handler has been created as unknown child - see <OOXMLFastDocumentHandler::createUnknownChildContext(..)>
166 // --> do not provide a child context
167 return NULL;
168 }
169
170 return OOXMLFactory::getInstance()->createFastChildContextFromStart(getContextHandler().get(), Element);
171 }
172
173 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
createUnknownChildContext(const::rtl::OUString & Namespace,const::rtl::OUString & Name,const uno::Reference<xml::sax::XFastAttributeList> &)174 OOXMLFastDocumentHandler::createUnknownChildContext
175 (const ::rtl::OUString &
176 #ifdef DEBUG_CONTEXT_STACK
177 Namespace
178 #endif
179 ,
180 const ::rtl::OUString &
181 #ifdef DEBUG_CONTEXT_STACK
182 Name
183 #endif
184 , const uno::Reference< xml::sax::XFastAttributeList > & /*Attribs*/)
185 throw (uno::RuntimeException, xml::sax::SAXException)
186 {
187 #ifdef DEBUG_CONTEXT_STACK
188 clog << this << ":createUnknownChildContext:"
189 << OUStringToOString(Namespace, RTL_TEXTENCODING_ASCII_US).getStr()
190 << ":"
191 << OUStringToOString(Name, RTL_TEXTENCODING_ASCII_US).getStr()
192 << endl;
193 #endif
194
195 return uno::Reference< xml::sax::XFastContextHandler >
196 ( new OOXMLFastDocumentHandler( m_xContext, 0, 0 ) );
197 }
198
characters(const::rtl::OUString &)199 void SAL_CALL OOXMLFastDocumentHandler::characters(const ::rtl::OUString & /*aChars*/)
200 throw (uno::RuntimeException, xml::sax::SAXException)
201 {
202 }
203
204 // ::com::sun::star::xml::sax::XFastDocumentHandler:
startDocument()205 void SAL_CALL OOXMLFastDocumentHandler::startDocument()
206 throw (uno::RuntimeException, xml::sax::SAXException)
207 {
208 }
209
endDocument()210 void SAL_CALL OOXMLFastDocumentHandler::endDocument()
211 throw (uno::RuntimeException, xml::sax::SAXException)
212 {
213 }
214
setDocumentLocator(const uno::Reference<xml::sax::XLocator> &)215 void SAL_CALL OOXMLFastDocumentHandler::setDocumentLocator
216 (const uno::Reference< xml::sax::XLocator > & /*xLocator*/)
217 throw (uno::RuntimeException, xml::sax::SAXException)
218 {
219 }
220
setIsSubstream(bool bSubstream)221 void OOXMLFastDocumentHandler::setIsSubstream( bool bSubstream )
222 {
223 if ( mpStream != 0 && mpDocument != 0 )
224 {
225 getContextHandler( )->getParserState( )->setInSectionGroup( bSubstream );
226 }
227 }
228
229 }}
230