1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "ShapeContextHandler.hxx"
29*cdf0e10cSrcweir #include "oox/vml/vmldrawingfragment.hxx"
30*cdf0e10cSrcweir #include "oox/vml/vmlshape.hxx"
31*cdf0e10cSrcweir #include "oox/vml/vmlshapecontainer.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir namespace oox { namespace shape {
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir using namespace ::com::sun::star;
36*cdf0e10cSrcweir using namespace core;
37*cdf0e10cSrcweir using namespace drawingml;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir ::rtl::OUString SAL_CALL ShapeContextHandler_getImplementationName()
40*cdf0e10cSrcweir {
41*cdf0e10cSrcweir     return CREATE_OUSTRING( "com.sun.star.comp.oox.ShapeContextHandler" );
42*cdf0e10cSrcweir }
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL
45*cdf0e10cSrcweir ShapeContextHandler_getSupportedServiceNames()
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > s(1);
48*cdf0e10cSrcweir     s[0] = CREATE_OUSTRING( "com.sun.star.xml.sax.FastShapeContextHandler" );
49*cdf0e10cSrcweir     return s;
50*cdf0e10cSrcweir }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL
53*cdf0e10cSrcweir ShapeContextHandler_createInstance( const uno::Reference< uno::XComponentContext > & context)
54*cdf0e10cSrcweir         SAL_THROW((uno::Exception))
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject* >( new ShapeContextHandler(context) );
57*cdf0e10cSrcweir }
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir ShapeContextHandler::ShapeContextHandler
61*cdf0e10cSrcweir (uno::Reference< uno::XComponentContext > const & context) :
62*cdf0e10cSrcweir mnStartToken(0), m_xContext(context)
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir     try
65*cdf0e10cSrcweir     {
66*cdf0e10cSrcweir         mxFilterBase.set( new ShapeFilterBase(context) );
67*cdf0e10cSrcweir     }
68*cdf0e10cSrcweir     catch( uno::Exception& )
69*cdf0e10cSrcweir     {
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir ShapeContextHandler::~ShapeContextHandler()
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir uno::Reference<xml::sax::XFastContextHandler>
78*cdf0e10cSrcweir ShapeContextHandler::getGraphicShapeContext(::sal_Int32 Element )
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir     if (! mxGraphicShapeContext.is())
81*cdf0e10cSrcweir     {
82*cdf0e10cSrcweir         FragmentHandlerRef rFragmentHandler
83*cdf0e10cSrcweir             (new ShapeFragmentHandler(*mxFilterBase, msRelationFragmentPath));
84*cdf0e10cSrcweir         ShapePtr pMasterShape;
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir         switch (Element & 0xffff)
87*cdf0e10cSrcweir         {
88*cdf0e10cSrcweir             case XML_graphic:
89*cdf0e10cSrcweir                 mpShape.reset(new Shape("com.sun.star.drawing.GraphicObjectShape" ));
90*cdf0e10cSrcweir                 mxGraphicShapeContext.set
91*cdf0e10cSrcweir                 (new GraphicalObjectFrameContext(*rFragmentHandler, pMasterShape, mpShape, true));
92*cdf0e10cSrcweir                 break;
93*cdf0e10cSrcweir             case XML_pic:
94*cdf0e10cSrcweir                 mpShape.reset(new Shape("com.sun.star.drawing.GraphicObjectShape" ));
95*cdf0e10cSrcweir                 mxGraphicShapeContext.set
96*cdf0e10cSrcweir                 (new GraphicShapeContext(*rFragmentHandler, pMasterShape, mpShape));
97*cdf0e10cSrcweir                 break;
98*cdf0e10cSrcweir             default:
99*cdf0e10cSrcweir                 break;
100*cdf0e10cSrcweir         }
101*cdf0e10cSrcweir     }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     return mxGraphicShapeContext;
104*cdf0e10cSrcweir }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir uno::Reference<xml::sax::XFastContextHandler>
107*cdf0e10cSrcweir ShapeContextHandler::getDrawingShapeContext()
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir     if (!mxDrawingFragmentHandler.is())
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir         mpDrawing.reset( new oox::vml::Drawing( *mxFilterBase, mxDrawPage, oox::vml::VMLDRAWING_WORD ) );
112*cdf0e10cSrcweir         mxDrawingFragmentHandler.set
113*cdf0e10cSrcweir           (dynamic_cast<ContextHandler *>
114*cdf0e10cSrcweir            (new oox::vml::DrawingFragment
115*cdf0e10cSrcweir             ( *mxFilterBase, msRelationFragmentPath, *mpDrawing )));
116*cdf0e10cSrcweir     }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     return mxDrawingFragmentHandler;
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir uno::Reference<xml::sax::XFastContextHandler>
122*cdf0e10cSrcweir ShapeContextHandler::getContextHandler()
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir     uno::Reference<xml::sax::XFastContextHandler> xResult;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     switch (getNamespace( mnStartToken ))
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir         case NMSP_doc:
129*cdf0e10cSrcweir         case NMSP_vml:
130*cdf0e10cSrcweir             xResult.set(getDrawingShapeContext());
131*cdf0e10cSrcweir             break;
132*cdf0e10cSrcweir         default:
133*cdf0e10cSrcweir             xResult.set(getGraphicShapeContext(mnStartToken));
134*cdf0e10cSrcweir             break;
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     return xResult;
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir // ::com::sun::star::xml::sax::XFastContextHandler:
141*cdf0e10cSrcweir void SAL_CALL ShapeContextHandler::startFastElement
142*cdf0e10cSrcweir (::sal_Int32 Element,
143*cdf0e10cSrcweir  const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
144*cdf0e10cSrcweir     throw (uno::RuntimeException, xml::sax::SAXException)
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir     static const ::rtl::OUString sInputStream
147*cdf0e10cSrcweir         (RTL_CONSTASCII_USTRINGPARAM ("InputStream"));
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     uno::Sequence<beans::PropertyValue> aSeq(1);
150*cdf0e10cSrcweir     aSeq[0].Name = sInputStream;
151*cdf0e10cSrcweir     aSeq[0].Value <<= mxInputStream;
152*cdf0e10cSrcweir     mxFilterBase->filter(aSeq);
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     mpThemePtr.reset(new Theme());
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir     if (xContextHandler.is())
159*cdf0e10cSrcweir         xContextHandler->startFastElement(Element, Attribs);
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir void SAL_CALL ShapeContextHandler::startUnknownElement
163*cdf0e10cSrcweir (const ::rtl::OUString & Namespace, const ::rtl::OUString & Name,
164*cdf0e10cSrcweir  const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
165*cdf0e10cSrcweir     throw (uno::RuntimeException, xml::sax::SAXException)
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     if (xContextHandler.is())
170*cdf0e10cSrcweir         xContextHandler->startUnknownElement(Namespace, Name, Attribs);
171*cdf0e10cSrcweir }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir void SAL_CALL ShapeContextHandler::endFastElement(::sal_Int32 Element)
174*cdf0e10cSrcweir     throw (uno::RuntimeException, xml::sax::SAXException)
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir     if (xContextHandler.is())
179*cdf0e10cSrcweir         xContextHandler->endFastElement(Element);
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir void SAL_CALL ShapeContextHandler::endUnknownElement
183*cdf0e10cSrcweir (const ::rtl::OUString & Namespace,
184*cdf0e10cSrcweir  const ::rtl::OUString & Name)
185*cdf0e10cSrcweir     throw (uno::RuntimeException, xml::sax::SAXException)
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     if (xContextHandler.is())
190*cdf0e10cSrcweir         xContextHandler->endUnknownElement(Namespace, Name);
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
194*cdf0e10cSrcweir ShapeContextHandler::createFastChildContext
195*cdf0e10cSrcweir (::sal_Int32 Element,
196*cdf0e10cSrcweir  const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
197*cdf0e10cSrcweir     throw (uno::RuntimeException, xml::sax::SAXException)
198*cdf0e10cSrcweir {
199*cdf0e10cSrcweir     uno::Reference< xml::sax::XFastContextHandler > xResult;
200*cdf0e10cSrcweir     uno::Reference< xml::sax::XFastContextHandler > xContextHandler(getContextHandler());
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     if (xContextHandler.is())
203*cdf0e10cSrcweir         xResult.set(xContextHandler->createFastChildContext
204*cdf0e10cSrcweir                     (Element, Attribs));
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     return xResult;
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir uno::Reference< xml::sax::XFastContextHandler > SAL_CALL
210*cdf0e10cSrcweir ShapeContextHandler::createUnknownChildContext
211*cdf0e10cSrcweir (const ::rtl::OUString & Namespace,
212*cdf0e10cSrcweir  const ::rtl::OUString & Name,
213*cdf0e10cSrcweir  const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
214*cdf0e10cSrcweir     throw (uno::RuntimeException, xml::sax::SAXException)
215*cdf0e10cSrcweir {
216*cdf0e10cSrcweir     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     if (xContextHandler.is())
219*cdf0e10cSrcweir         return xContextHandler->createUnknownChildContext
220*cdf0e10cSrcweir             (Namespace, Name, Attribs);
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir     return uno::Reference< xml::sax::XFastContextHandler >();
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir void SAL_CALL ShapeContextHandler::characters(const ::rtl::OUString & aChars)
226*cdf0e10cSrcweir     throw (uno::RuntimeException, xml::sax::SAXException)
227*cdf0e10cSrcweir {
228*cdf0e10cSrcweir     uno::Reference<XFastContextHandler> xContextHandler(getContextHandler());
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir     if (xContextHandler.is())
231*cdf0e10cSrcweir         xContextHandler->characters(aChars);
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir // ::com::sun::star::xml::sax::XFastShapeContextHandler:
235*cdf0e10cSrcweir uno::Reference< drawing::XShape > SAL_CALL
236*cdf0e10cSrcweir ShapeContextHandler::getShape() throw (uno::RuntimeException)
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir     uno::Reference< drawing::XShape > xResult;
239*cdf0e10cSrcweir     uno::Reference< drawing::XShapes > xShapes( mxDrawPage, uno::UNO_QUERY );
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir     if (mxFilterBase.is() && xShapes.is())
242*cdf0e10cSrcweir     {
243*cdf0e10cSrcweir         if (mpDrawing.get() != NULL)
244*cdf0e10cSrcweir         {
245*cdf0e10cSrcweir             mpDrawing->finalizeFragmentImport();
246*cdf0e10cSrcweir             if( const ::oox::vml::ShapeBase* pShape = mpDrawing->getShapes().getFirstShape() )
247*cdf0e10cSrcweir                 xResult = pShape->convertAndInsert( xShapes );
248*cdf0e10cSrcweir         }
249*cdf0e10cSrcweir         else if (mpShape.get() != NULL)
250*cdf0e10cSrcweir         {
251*cdf0e10cSrcweir             mpShape->addShape(*mxFilterBase, mpThemePtr.get(), xShapes);
252*cdf0e10cSrcweir 			xResult.set(mpShape->getXShape());
253*cdf0e10cSrcweir             mxGraphicShapeContext.clear( );
254*cdf0e10cSrcweir 		}
255*cdf0e10cSrcweir     }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir     return xResult;
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir css::uno::Reference< css::drawing::XDrawPage > SAL_CALL
261*cdf0e10cSrcweir ShapeContextHandler::getDrawPage() throw (css::uno::RuntimeException)
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir     return mxDrawPage;
264*cdf0e10cSrcweir }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir void SAL_CALL ShapeContextHandler::setDrawPage
267*cdf0e10cSrcweir (const css::uno::Reference< css::drawing::XDrawPage > & the_value)
268*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
269*cdf0e10cSrcweir {
270*cdf0e10cSrcweir     mxDrawPage = the_value;
271*cdf0e10cSrcweir }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir css::uno::Reference< css::frame::XModel > SAL_CALL
274*cdf0e10cSrcweir ShapeContextHandler::getModel() throw (css::uno::RuntimeException)
275*cdf0e10cSrcweir {
276*cdf0e10cSrcweir     if( !mxFilterBase.is() )
277*cdf0e10cSrcweir         throw uno::RuntimeException();
278*cdf0e10cSrcweir     return mxFilterBase->getModel();
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir void SAL_CALL ShapeContextHandler::setModel
282*cdf0e10cSrcweir (const css::uno::Reference< css::frame::XModel > & the_value)
283*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
284*cdf0e10cSrcweir {
285*cdf0e10cSrcweir     if( !mxFilterBase.is() )
286*cdf0e10cSrcweir         throw uno::RuntimeException();
287*cdf0e10cSrcweir     uno::Reference<lang::XComponent> xComp(the_value, uno::UNO_QUERY_THROW);
288*cdf0e10cSrcweir     mxFilterBase->setTargetDocument(xComp);
289*cdf0e10cSrcweir }
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir uno::Reference< io::XInputStream > SAL_CALL
292*cdf0e10cSrcweir ShapeContextHandler::getInputStream() throw (uno::RuntimeException)
293*cdf0e10cSrcweir {
294*cdf0e10cSrcweir     return mxInputStream;
295*cdf0e10cSrcweir }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir void SAL_CALL ShapeContextHandler::setInputStream
298*cdf0e10cSrcweir (const uno::Reference< io::XInputStream > & the_value)
299*cdf0e10cSrcweir     throw (uno::RuntimeException)
300*cdf0e10cSrcweir {
301*cdf0e10cSrcweir     mxInputStream = the_value;
302*cdf0e10cSrcweir }
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir ::rtl::OUString SAL_CALL ShapeContextHandler::getRelationFragmentPath()
305*cdf0e10cSrcweir     throw (uno::RuntimeException)
306*cdf0e10cSrcweir {
307*cdf0e10cSrcweir     return msRelationFragmentPath;
308*cdf0e10cSrcweir }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir void SAL_CALL ShapeContextHandler::setRelationFragmentPath
311*cdf0e10cSrcweir (const ::rtl::OUString & the_value)
312*cdf0e10cSrcweir     throw (uno::RuntimeException)
313*cdf0e10cSrcweir {
314*cdf0e10cSrcweir     msRelationFragmentPath = the_value;
315*cdf0e10cSrcweir }
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir ::sal_Int32 SAL_CALL ShapeContextHandler::getStartToken() throw (::com::sun::star::uno::RuntimeException)
318*cdf0e10cSrcweir {
319*cdf0e10cSrcweir     return mnStartToken;
320*cdf0e10cSrcweir }
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir void SAL_CALL ShapeContextHandler::setStartToken( ::sal_Int32 _starttoken ) throw (::com::sun::star::uno::RuntimeException)
323*cdf0e10cSrcweir {
324*cdf0e10cSrcweir     mnStartToken = _starttoken;
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir }
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir ::rtl::OUString ShapeContextHandler::getImplementationName()
330*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
331*cdf0e10cSrcweir {
332*cdf0e10cSrcweir     return ShapeContextHandler_getImplementationName();
333*cdf0e10cSrcweir }
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > ShapeContextHandler::getSupportedServiceNames()
336*cdf0e10cSrcweir     throw (css::uno::RuntimeException)
337*cdf0e10cSrcweir {
338*cdf0e10cSrcweir     return ShapeContextHandler_getSupportedServiceNames();
339*cdf0e10cSrcweir }
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir ::sal_Bool SAL_CALL ShapeContextHandler::supportsService
342*cdf0e10cSrcweir (const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException)
343*cdf0e10cSrcweir {
344*cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > aSeq = getSupportedServiceNames();
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir     if (aSeq[0].equals(ServiceName))
347*cdf0e10cSrcweir         return sal_True;
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir     return sal_False;
350*cdf0e10cSrcweir }
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir }}
353