1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ShapeContextHandler.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "ShapeContextHandler.hxx"
32 #include "oox/vml/vmldrawingfragment.hxx"
33 #include "oox/vml/vmlshape.hxx"
34 #include "oox/vml/vmlshapecontainer.hxx"
36 namespace oox
{ namespace shape
{
38 using namespace ::com::sun::star
;
40 using namespace drawingml
;
42 ShapeContextHandler::ShapeContextHandler
43 (uno::Reference
< uno::XComponentContext
> const & context
) :
44 mnStartToken(0), m_xContext(context
)
48 uno::Reference
<lang::XMultiServiceFactory
>
49 xFactory(m_xContext
->getServiceManager(), uno::UNO_QUERY_THROW
);
50 mxFilterBase
.set( new ShapeFilterBase(xFactory
) );
52 catch( uno::Exception
& )
57 ShapeContextHandler::~ShapeContextHandler()
61 uno::Reference
<xml::sax::XFastContextHandler
>
62 ShapeContextHandler::getGraphicShapeContext(::sal_Int32 Element
)
64 if (! mxGraphicShapeContext
.is())
66 FragmentHandlerRef rFragmentHandler
67 (new ShapeFragmentHandler(*mxFilterBase
, msRelationFragmentPath
));
68 ShapePtr pMasterShape
;
70 switch (Element
& 0xffff)
73 mpShape
.reset(new Shape("com.sun.star.drawing.OLE2Shape" ));
74 mxGraphicShapeContext
.set
75 (new GraphicalObjectFrameContext(*rFragmentHandler
, pMasterShape
, mpShape
));
78 mpShape
.reset(new Shape("com.sun.star.drawing.GraphicObjectShape" ));
79 mxGraphicShapeContext
.set
80 (new GraphicShapeContext(*rFragmentHandler
, pMasterShape
, mpShape
));
87 return mxGraphicShapeContext
;
90 uno::Reference
<xml::sax::XFastContextHandler
>
91 ShapeContextHandler::getDrawingShapeContext()
93 if (!mxDrawingFragmentHandler
.is())
95 mpDrawing
.reset( new oox::vml::Drawing( *mxFilterBase
, mxDrawPage
, oox::vml::VMLDRAWING_WORD
) );
96 mxDrawingFragmentHandler
.set
97 (dynamic_cast<ContextHandler
*>
98 (new oox::vml::DrawingFragment
99 ( *mxFilterBase
, msRelationFragmentPath
, *mpDrawing
)));
102 return mxDrawingFragmentHandler
;
105 uno::Reference
<xml::sax::XFastContextHandler
>
106 ShapeContextHandler::getContextHandler()
108 uno::Reference
<xml::sax::XFastContextHandler
> xResult
;
110 switch (mnStartToken
& NMSP_MASK
)
113 xResult
.set(getDrawingShapeContext());
116 xResult
.set(getGraphicShapeContext(mnStartToken
));
123 // ::com::sun::star::xml::sax::XFastContextHandler:
124 void SAL_CALL
ShapeContextHandler::startFastElement
125 (::sal_Int32 Element
,
126 const uno::Reference
< xml::sax::XFastAttributeList
> & Attribs
)
127 throw (uno::RuntimeException
, xml::sax::SAXException
)
129 static const ::rtl::OUString sInputStream
130 (RTL_CONSTASCII_USTRINGPARAM ("InputStream"));
132 uno::Sequence
<beans::PropertyValue
> aSeq(1);
133 aSeq
[0].Name
= sInputStream
;
134 aSeq
[0].Value
<<= mxInputStream
;
135 mxFilterBase
->filter(aSeq
);
137 mpThemePtr
.reset(new Theme());
139 uno::Reference
<XFastContextHandler
> xContextHandler(getContextHandler());
141 if (xContextHandler
.is())
142 xContextHandler
->startFastElement(Element
, Attribs
);
145 void SAL_CALL
ShapeContextHandler::startUnknownElement
146 (const ::rtl::OUString
& Namespace
, const ::rtl::OUString
& Name
,
147 const uno::Reference
< xml::sax::XFastAttributeList
> & Attribs
)
148 throw (uno::RuntimeException
, xml::sax::SAXException
)
150 uno::Reference
<XFastContextHandler
> xContextHandler(getContextHandler());
152 if (xContextHandler
.is())
153 xContextHandler
->startUnknownElement(Namespace
, Name
, Attribs
);
156 void SAL_CALL
ShapeContextHandler::endFastElement(::sal_Int32 Element
)
157 throw (uno::RuntimeException
, xml::sax::SAXException
)
159 uno::Reference
<XFastContextHandler
> xContextHandler(getContextHandler());
161 if (xContextHandler
.is())
162 xContextHandler
->endFastElement(Element
);
165 void SAL_CALL
ShapeContextHandler::endUnknownElement
166 (const ::rtl::OUString
& Namespace
,
167 const ::rtl::OUString
& Name
)
168 throw (uno::RuntimeException
, xml::sax::SAXException
)
170 uno::Reference
<XFastContextHandler
> xContextHandler(getContextHandler());
172 if (xContextHandler
.is())
173 xContextHandler
->endUnknownElement(Namespace
, Name
);
176 uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
177 ShapeContextHandler::createFastChildContext
178 (::sal_Int32 Element
,
179 const uno::Reference
< xml::sax::XFastAttributeList
> & Attribs
)
180 throw (uno::RuntimeException
, xml::sax::SAXException
)
182 uno::Reference
< xml::sax::XFastContextHandler
> xResult
;
183 uno::Reference
< xml::sax::XFastContextHandler
> xContextHandler(getContextHandler());
185 if (xContextHandler
.is())
186 xResult
.set(xContextHandler
->createFastChildContext
192 uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
193 ShapeContextHandler::createUnknownChildContext
194 (const ::rtl::OUString
& Namespace
,
195 const ::rtl::OUString
& Name
,
196 const uno::Reference
< xml::sax::XFastAttributeList
> & Attribs
)
197 throw (uno::RuntimeException
, xml::sax::SAXException
)
199 uno::Reference
<XFastContextHandler
> xContextHandler(getContextHandler());
201 if (xContextHandler
.is())
202 return xContextHandler
->createUnknownChildContext
203 (Namespace
, Name
, Attribs
);
205 return uno::Reference
< xml::sax::XFastContextHandler
>();
208 void SAL_CALL
ShapeContextHandler::characters(const ::rtl::OUString
& aChars
)
209 throw (uno::RuntimeException
, xml::sax::SAXException
)
211 uno::Reference
<XFastContextHandler
> xContextHandler(getContextHandler());
213 if (xContextHandler
.is())
214 xContextHandler
->characters(aChars
);
217 // ::com::sun::star::xml::sax::XFastShapeContextHandler:
218 uno::Reference
< drawing::XShape
> SAL_CALL
219 ShapeContextHandler::getShape() throw (uno::RuntimeException
)
221 uno::Reference
< drawing::XShape
> xResult
;
222 uno::Reference
< drawing::XShapes
> xShapes( mxDrawPage
, uno::UNO_QUERY
);
224 if (mxFilterBase
.is() && xShapes
.is())
226 if (mpDrawing
.get() != NULL
)
228 mpDrawing
->finalizeFragmentImport();
229 if( const ::oox::vml::ShapeBase
* pShape
= mpDrawing
->getShapes().getFirstShape() )
230 xResult
= pShape
->convertAndInsert( xShapes
);
232 else if (mpShape
.get() != NULL
)
234 mpShape
->addShape(*mxFilterBase
, mpThemePtr
, xShapes
);
235 xResult
.set(mpShape
->getXShape());
242 css::uno::Reference
< css::drawing::XDrawPage
> SAL_CALL
243 ShapeContextHandler::getDrawPage() throw (css::uno::RuntimeException
)
248 void SAL_CALL
ShapeContextHandler::setDrawPage
249 (const css::uno::Reference
< css::drawing::XDrawPage
> & the_value
)
250 throw (css::uno::RuntimeException
)
252 mxDrawPage
= the_value
;
255 css::uno::Reference
< css::frame::XModel
> SAL_CALL
256 ShapeContextHandler::getModel() throw (css::uno::RuntimeException
)
258 if( !mxFilterBase
.is() )
259 throw uno::RuntimeException();
260 return mxFilterBase
->getModel();
263 void SAL_CALL
ShapeContextHandler::setModel
264 (const css::uno::Reference
< css::frame::XModel
> & the_value
)
265 throw (css::uno::RuntimeException
)
267 if( !mxFilterBase
.is() )
268 throw uno::RuntimeException();
269 uno::Reference
<lang::XComponent
> xComp(the_value
, uno::UNO_QUERY_THROW
);
270 mxFilterBase
->setTargetDocument(xComp
);
273 uno::Reference
< io::XInputStream
> SAL_CALL
274 ShapeContextHandler::getInputStream() throw (uno::RuntimeException
)
276 return mxInputStream
;
279 void SAL_CALL
ShapeContextHandler::setInputStream
280 (const uno::Reference
< io::XInputStream
> & the_value
)
281 throw (uno::RuntimeException
)
283 mxInputStream
= the_value
;
286 ::rtl::OUString SAL_CALL
ShapeContextHandler::getRelationFragmentPath()
287 throw (uno::RuntimeException
)
289 return msRelationFragmentPath
;
292 void SAL_CALL
ShapeContextHandler::setRelationFragmentPath
293 (const ::rtl::OUString
& the_value
)
294 throw (uno::RuntimeException
)
296 msRelationFragmentPath
= the_value
;
299 ::sal_Int32 SAL_CALL
ShapeContextHandler::getStartToken() throw (::com::sun::star::uno::RuntimeException
)
304 void SAL_CALL
ShapeContextHandler::setStartToken( ::sal_Int32 _starttoken
) throw (::com::sun::star::uno::RuntimeException
)
306 mnStartToken
= _starttoken
;
311 ::rtl::OUString
ShapeContextHandler::getImplementationName()
312 throw (css::uno::RuntimeException
)
314 return ShapeContextHandler_getImplementationName();
317 uno::Sequence
< ::rtl::OUString
> ShapeContextHandler::getSupportedServiceNames()
318 throw (css::uno::RuntimeException
)
320 return ShapeContextHandler_getSupportedServiceNames();
323 ::rtl::OUString SAL_CALL
ShapeContextHandler_getImplementationName() {
324 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
325 "com.sun.star.comp.oox.ShapeContextHandler"));
328 ::sal_Bool SAL_CALL
ShapeContextHandler::supportsService
329 (const ::rtl::OUString
& ServiceName
) throw (css::uno::RuntimeException
)
331 uno::Sequence
< ::rtl::OUString
> aSeq
= getSupportedServiceNames();
333 if (aSeq
[0].equals(ServiceName
))
339 uno::Sequence
< ::rtl::OUString
> SAL_CALL
340 ShapeContextHandler_getSupportedServiceNames()
342 uno::Sequence
< ::rtl::OUString
> s(1);
343 s
[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
344 "com.sun.star.xml.sax.FastShapeContextHandler"));
348 uno::Reference
< uno::XInterface
> SAL_CALL
349 ShapeContextHandler_create(
350 const uno::Reference
< uno::XComponentContext
> & context
)
351 SAL_THROW((uno::Exception
))
353 return static_cast< ::cppu::OWeakObject
* >
354 (new ShapeContextHandler(context
));
357 uno::Reference
< uno::XInterface
> SAL_CALL
358 ShapeContextHandler_createInstance
359 ( const uno::Reference
< lang::XMultiServiceFactory
> & rSMgr
)
360 throw( uno::Exception
)
362 uno::Reference
<beans::XPropertySet
>
363 xPropertySet(rSMgr
, uno::UNO_QUERY_THROW
);
364 uno::Any aDefaultContext
= xPropertySet
->getPropertyValue
365 (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")));
367 uno::Reference
<uno::XComponentContext
> xContext
;
368 aDefaultContext
>>= xContext
;
370 return ShapeContextHandler_create(xContext
);