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 "ShapeContextHandler.hxx"
25 #include "oox/vml/vmldrawingfragment.hxx"
26 #include "oox/vml/vmlshape.hxx"
27 #include "oox/vml/vmlshapecontainer.hxx"
28 
29 namespace oox { namespace shape {
30 
31 using namespace ::com::sun::star;
32 using namespace core;
33 using namespace drawingml;
34 
ShapeContextHandler_getImplementationName()35 ::rtl::OUString SAL_CALL ShapeContextHandler_getImplementationName()
36 {
37     return CREATE_OUSTRING( "com.sun.star.comp.oox.ShapeContextHandler" );
38 }
39 
40 uno::Sequence< ::rtl::OUString > SAL_CALL
ShapeContextHandler_getSupportedServiceNames()41 ShapeContextHandler_getSupportedServiceNames()
42 {
43     uno::Sequence< ::rtl::OUString > s(1);
44     s[0] = CREATE_OUSTRING( "com.sun.star.xml.sax.FastShapeContextHandler" );
45     return s;
46 }
47 
48 uno::Reference< uno::XInterface > SAL_CALL
ShapeContextHandler_createInstance(const uno::Reference<uno::XComponentContext> & context)49 ShapeContextHandler_createInstance( const uno::Reference< uno::XComponentContext > & context)
50         SAL_THROW((uno::Exception))
51 {
52     return static_cast< ::cppu::OWeakObject* >( new ShapeContextHandler(context) );
53 }
54 
55 
ShapeContextHandler(uno::Reference<uno::XComponentContext> const & context)56 ShapeContextHandler::ShapeContextHandler
57 (uno::Reference< uno::XComponentContext > const & context) :
58 mnStartToken(0), m_xContext(context)
59 {
60     try
61     {
62         mxFilterBase.set( new ShapeFilterBase(context) );
63     }
64     catch( uno::Exception& )
65     {
66     }
67 }
68 
~ShapeContextHandler()69 ShapeContextHandler::~ShapeContextHandler()
70 {
71 }
72 
73 uno::Reference<xml::sax::XFastContextHandler>
getGraphicShapeContext(::sal_Int32 Element)74 ShapeContextHandler::getGraphicShapeContext(::sal_Int32 Element )
75 {
76     if (! mxGraphicShapeContext.is())
77     {
78         FragmentHandlerRef rFragmentHandler
79             (new ShapeFragmentHandler(*mxFilterBase, msRelationFragmentPath));
80         ShapePtr pMasterShape;
81 
82         switch (Element & 0xffff)
83         {
84             case XML_graphic:
85                 mpShape.reset(new Shape("com.sun.star.drawing.GraphicObjectShape" ));
86                 mxGraphicShapeContext.set
87                 (new GraphicalObjectFrameContext(*rFragmentHandler, pMasterShape, mpShape, true));
88                 break;
89             case XML_pic:
90                 mpShape.reset(new Shape("com.sun.star.drawing.GraphicObjectShape" ));
91                 mxGraphicShapeContext.set
92                 (new GraphicShapeContext(*rFragmentHandler, pMasterShape, mpShape));
93                 break;
94             default:
95                 break;
96         }
97     }
98 
99     return mxGraphicShapeContext;
100 }
101 
102 uno::Reference<xml::sax::XFastContextHandler>
getDrawingShapeContext()103 ShapeContextHandler::getDrawingShapeContext()
104 {
105     if (!mxDrawingFragmentHandler.is())
106     {
107         mpDrawing.reset( new oox::vml::Drawing( *mxFilterBase, mxDrawPage, oox::vml::VMLDRAWING_WORD ) );
108         mxDrawingFragmentHandler.set
109           (dynamic_cast<ContextHandler *>
110            (new oox::vml::DrawingFragment
111             ( *mxFilterBase, msRelationFragmentPath, *mpDrawing )));
112     }
113 
114     return mxDrawingFragmentHandler;
115 }
116 
117 uno::Reference<xml::sax::XFastContextHandler>
getContextHandler()118 ShapeContextHandler::getContextHandler()
119 {
120     uno::Reference<xml::sax::XFastContextHandler> xResult;
121 
122     switch (getNamespace( mnStartToken ))
123     {
124         case NMSP_doc:
125         case NMSP_vml:
126             xResult.set(getDrawingShapeContext());
127             break;
128         default:
129             xResult.set(getGraphicShapeContext(mnStartToken));
130             break;
131     }
132 
133     return xResult;
134 }
135 
136 // ::com::sun::star::xml::sax::XFastContextHandler:
startFastElement(::sal_Int32 Element,const uno::Reference<xml::sax::XFastAttributeList> & Attribs)137 void SAL_CALL ShapeContextHandler::startFastElement
138 (::sal_Int32 Element,
139  const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
140     throw (uno::RuntimeException, xml::sax::SAXException)
141 {
142     static const ::rtl::OUString sInputStream
143         (RTL_CONSTASCII_USTRINGPARAM ("InputStream"));
144 
145     uno::Sequence<beans::PropertyValue> aSeq(1);
146     aSeq[0].Name = sInputStream;
147     aSeq[0].Value <<= mxInputStream;
148     mxFilterBase->filter(aSeq);
149 
150     mpThemePtr.reset(new Theme());
151 
152     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
153 
154     if (xContextHandler.is())
155         xContextHandler->startFastElement(Element, Attribs);
156 }
157 
startUnknownElement(const::rtl::OUString & Namespace,const::rtl::OUString & Name,const uno::Reference<xml::sax::XFastAttributeList> & Attribs)158 void SAL_CALL ShapeContextHandler::startUnknownElement
159 (const ::rtl::OUString & Namespace, const ::rtl::OUString & Name,
160  const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
161     throw (uno::RuntimeException, xml::sax::SAXException)
162 {
163     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
164 
165     if (xContextHandler.is())
166         xContextHandler->startUnknownElement(Namespace, Name, Attribs);
167 }
168 
endFastElement(::sal_Int32 Element)169 void SAL_CALL ShapeContextHandler::endFastElement(::sal_Int32 Element)
170     throw (uno::RuntimeException, xml::sax::SAXException)
171 {
172     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
173 
174     if (xContextHandler.is())
175         xContextHandler->endFastElement(Element);
176 }
177 
endUnknownElement(const::rtl::OUString & Namespace,const::rtl::OUString & Name)178 void SAL_CALL ShapeContextHandler::endUnknownElement
179 (const ::rtl::OUString & Namespace,
180  const ::rtl::OUString & Name)
181     throw (uno::RuntimeException, xml::sax::SAXException)
182 {
183     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
184 
185     if (xContextHandler.is())
186         xContextHandler->endUnknownElement(Namespace, Name);
187 }
188 
189 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
createFastChildContext(::sal_Int32 Element,const uno::Reference<xml::sax::XFastAttributeList> & Attribs)190 ShapeContextHandler::createFastChildContext
191 (::sal_Int32 Element,
192  const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
193     throw (uno::RuntimeException, xml::sax::SAXException)
194 {
195     uno::Reference< xml::sax::XFastContextHandler > xResult;
196     uno::Reference< xml::sax::XFastContextHandler > xContextHandler(getContextHandler());
197 
198     if (xContextHandler.is())
199         xResult.set(xContextHandler->createFastChildContext
200                     (Element, Attribs));
201 
202     return xResult;
203 }
204 
205 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
createUnknownChildContext(const::rtl::OUString & Namespace,const::rtl::OUString & Name,const uno::Reference<xml::sax::XFastAttributeList> & Attribs)206 ShapeContextHandler::createUnknownChildContext
207 (const ::rtl::OUString & Namespace,
208  const ::rtl::OUString & Name,
209  const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
210     throw (uno::RuntimeException, xml::sax::SAXException)
211 {
212     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
213 
214     if (xContextHandler.is())
215         return xContextHandler->createUnknownChildContext
216             (Namespace, Name, Attribs);
217 
218     return uno::Reference< xml::sax::XFastContextHandler >();
219 }
220 
characters(const::rtl::OUString & aChars)221 void SAL_CALL ShapeContextHandler::characters(const ::rtl::OUString & aChars)
222     throw (uno::RuntimeException, xml::sax::SAXException)
223 {
224     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
225 
226     if (xContextHandler.is())
227         xContextHandler->characters(aChars);
228 }
229 
230 // ::com::sun::star::xml::sax::XFastShapeContextHandler:
231 uno::Reference< drawing::XShape > SAL_CALL
getShape()232 ShapeContextHandler::getShape() throw (uno::RuntimeException)
233 {
234     uno::Reference< drawing::XShape > xResult;
235     uno::Reference< drawing::XShapes > xShapes( mxDrawPage, uno::UNO_QUERY );
236 
237     if (mxFilterBase.is() && xShapes.is())
238     {
239         if (mpDrawing.get() != NULL)
240         {
241             mpDrawing->finalizeFragmentImport();
242             if( const ::oox::vml::ShapeBase* pShape = mpDrawing->getShapes().getFirstShape() )
243                 xResult = pShape->convertAndInsert( xShapes );
244         }
245         else if (mpShape.get() != NULL)
246         {
247             mpShape->addShape(*mxFilterBase, mpThemePtr.get(), xShapes);
248 			xResult.set(mpShape->getXShape());
249             mxGraphicShapeContext.clear( );
250 		}
251     }
252 
253     return xResult;
254 }
255 
256 css::uno::Reference< css::drawing::XDrawPage > SAL_CALL
getDrawPage()257 ShapeContextHandler::getDrawPage() throw (css::uno::RuntimeException)
258 {
259     return mxDrawPage;
260 }
261 
setDrawPage(const css::uno::Reference<css::drawing::XDrawPage> & the_value)262 void SAL_CALL ShapeContextHandler::setDrawPage
263 (const css::uno::Reference< css::drawing::XDrawPage > & the_value)
264     throw (css::uno::RuntimeException)
265 {
266     mxDrawPage = the_value;
267 }
268 
269 css::uno::Reference< css::frame::XModel > SAL_CALL
getModel()270 ShapeContextHandler::getModel() throw (css::uno::RuntimeException)
271 {
272     if( !mxFilterBase.is() )
273         throw uno::RuntimeException();
274     return mxFilterBase->getModel();
275 }
276 
setModel(const css::uno::Reference<css::frame::XModel> & the_value)277 void SAL_CALL ShapeContextHandler::setModel
278 (const css::uno::Reference< css::frame::XModel > & the_value)
279     throw (css::uno::RuntimeException)
280 {
281     if( !mxFilterBase.is() )
282         throw uno::RuntimeException();
283     uno::Reference<lang::XComponent> xComp(the_value, uno::UNO_QUERY_THROW);
284     mxFilterBase->setTargetDocument(xComp);
285 }
286 
287 uno::Reference< io::XInputStream > SAL_CALL
getInputStream()288 ShapeContextHandler::getInputStream() throw (uno::RuntimeException)
289 {
290     return mxInputStream;
291 }
292 
setInputStream(const uno::Reference<io::XInputStream> & the_value)293 void SAL_CALL ShapeContextHandler::setInputStream
294 (const uno::Reference< io::XInputStream > & the_value)
295     throw (uno::RuntimeException)
296 {
297     mxInputStream = the_value;
298 }
299 
getRelationFragmentPath()300 ::rtl::OUString SAL_CALL ShapeContextHandler::getRelationFragmentPath()
301     throw (uno::RuntimeException)
302 {
303     return msRelationFragmentPath;
304 }
305 
setRelationFragmentPath(const::rtl::OUString & the_value)306 void SAL_CALL ShapeContextHandler::setRelationFragmentPath
307 (const ::rtl::OUString & the_value)
308     throw (uno::RuntimeException)
309 {
310     msRelationFragmentPath = the_value;
311 }
312 
getStartToken()313 ::sal_Int32 SAL_CALL ShapeContextHandler::getStartToken() throw (::com::sun::star::uno::RuntimeException)
314 {
315     return mnStartToken;
316 }
317 
setStartToken(::sal_Int32 _starttoken)318 void SAL_CALL ShapeContextHandler::setStartToken( ::sal_Int32 _starttoken ) throw (::com::sun::star::uno::RuntimeException)
319 {
320     mnStartToken = _starttoken;
321 
322 
323 }
324 
getImplementationName()325 ::rtl::OUString ShapeContextHandler::getImplementationName()
326     throw (css::uno::RuntimeException)
327 {
328     return ShapeContextHandler_getImplementationName();
329 }
330 
getSupportedServiceNames()331 uno::Sequence< ::rtl::OUString > ShapeContextHandler::getSupportedServiceNames()
332     throw (css::uno::RuntimeException)
333 {
334     return ShapeContextHandler_getSupportedServiceNames();
335 }
336 
supportsService(const::rtl::OUString & ServiceName)337 ::sal_Bool SAL_CALL ShapeContextHandler::supportsService
338 (const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException)
339 {
340     uno::Sequence< ::rtl::OUString > aSeq = getSupportedServiceNames();
341 
342     if (aSeq[0].equals(ServiceName))
343         return sal_True;
344 
345     return sal_False;
346 }
347 
348 }}
349