Update ooo320-m1
[ooovba.git] / xmloff / source / core / XMLEmbeddedObjectImportContext.cxx
blob1b44a8cfe5a629478b57fadc8af74c3f9bea9ee4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLEmbeddedObjectImportContext.cxx,v $
10 * $Revision: 1.18 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include <com/sun/star/document/XImporter.hpp>
34 #include <com/sun/star/util/XModifiable.hpp>
35 #include <com/sun/star/util/XModifiable2.hpp>
36 #include <com/sun/star/frame/XStorable.hpp>
38 // #110680#
39 //#ifndef _COMPHELPER_PROCESSFACTORY_HXX_
40 //#include <comphelper/processfactory.hxx>
41 //#endif
42 #include <tools/globname.hxx>
43 #include <sot/clsids.hxx>
44 #include <tools/globname.hxx>
45 #include <sot/clsids.hxx>
46 #include <xmloff/nmspmap.hxx>
47 #include <xmloff/xmlimp.hxx>
48 #include "xmlnmspe.hxx"
49 #include <xmloff/xmltoken.hxx>
50 #include "xmlerror.hxx"
51 #include <xmloff/attrlist.hxx>
52 #include "XMLFilterServiceNames.h"
53 #include "XMLEmbeddedObjectImportContext.hxx"
55 using ::rtl::OUString;
56 using ::rtl::OUStringBuffer;
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::util;
60 using namespace ::com::sun::star::beans;
61 using namespace ::com::sun::star::lang;
62 using namespace ::com::sun::star::frame;
63 using namespace ::com::sun::star::document;
64 using namespace ::com::sun::star::xml::sax;
65 using namespace ::xmloff::token;
67 struct XMLServiceMapEntry_Impl
69 enum XMLTokenEnum eClass;
70 const sal_Char *sFilterService;
71 sal_Int32 nFilterServiceLen;
74 #define SERVICE_MAP_ENTRY( cls, app ) \
75 { XML_##cls, \
76 XML_IMPORT_FILTER_##app, sizeof(XML_IMPORT_FILTER_##app)-1}
78 const XMLServiceMapEntry_Impl aServiceMap[] =
80 SERVICE_MAP_ENTRY( TEXT, WRITER ),
81 SERVICE_MAP_ENTRY( ONLINE_TEXT, WRITER ),
82 SERVICE_MAP_ENTRY( SPREADSHEET, CALC ),
83 SERVICE_MAP_ENTRY( DRAWING, DRAW ),
84 SERVICE_MAP_ENTRY( GRAPHICS, DRAW ),
85 SERVICE_MAP_ENTRY( PRESENTATION, IMPRESS ),
86 SERVICE_MAP_ENTRY( CHART, CHART ),
87 { XML_TOKEN_INVALID, 0, 0 }
90 class XMLEmbeddedObjectImportContext_Impl : public SvXMLImportContext
92 ::com::sun::star::uno::Reference<
93 ::com::sun::star::xml::sax::XDocumentHandler > xHandler;
95 public:
96 TYPEINFO();
98 XMLEmbeddedObjectImportContext_Impl( SvXMLImport& rImport, USHORT nPrfx,
99 const ::rtl::OUString& rLName,
100 const ::com::sun::star::uno::Reference<
101 ::com::sun::star::xml::sax::XDocumentHandler >& rHandler );
103 virtual ~XMLEmbeddedObjectImportContext_Impl();
105 virtual SvXMLImportContext *CreateChildContext( USHORT nPrefix,
106 const ::rtl::OUString& rLocalName,
107 const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
109 virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
111 virtual void EndElement();
113 virtual void Characters( const ::rtl::OUString& rChars );
116 TYPEINIT1( XMLEmbeddedObjectImportContext_Impl, SvXMLImportContext );
118 XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
119 SvXMLImport& rImport, USHORT nPrfx,
120 const OUString& rLName,
121 const Reference< XDocumentHandler >& rHandler ) :
122 SvXMLImportContext( rImport, nPrfx, rLName ),
123 xHandler( rHandler )
127 XMLEmbeddedObjectImportContext_Impl::~XMLEmbeddedObjectImportContext_Impl()
131 SvXMLImportContext *XMLEmbeddedObjectImportContext_Impl::CreateChildContext(
132 USHORT nPrefix,
133 const OUString& rLocalName,
134 const Reference< XAttributeList >& )
136 return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
137 nPrefix, rLocalName,
138 xHandler );
141 void XMLEmbeddedObjectImportContext_Impl::StartElement(
142 const Reference< XAttributeList >& xAttrList )
144 xHandler->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
145 GetPrefix(), GetLocalName() ),
146 xAttrList );
149 void XMLEmbeddedObjectImportContext_Impl::EndElement()
151 xHandler->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
152 GetPrefix(), GetLocalName() ) );
155 void XMLEmbeddedObjectImportContext_Impl::Characters( const OUString& rChars )
157 xHandler->characters( rChars );
160 //-----------------------------------------------------------------------------
162 TYPEINIT1( XMLEmbeddedObjectImportContext, SvXMLImportContext );
164 sal_Bool XMLEmbeddedObjectImportContext::SetComponent(
165 Reference< XComponent >& rComp )
167 if( !rComp.is() || !sFilterService.getLength() )
168 return sal_False;
171 Sequence<Any> aArgs( 0 );
173 // #110680#
174 // Reference< XMultiServiceFactory > xServiceFactory = comphelper::getProcessServiceFactory();
175 Reference< XMultiServiceFactory > xServiceFactory = GetImport().getServiceFactory();
177 xHandler = Reference < XDocumentHandler >(
178 xServiceFactory->createInstanceWithArguments( sFilterService, aArgs),
179 UNO_QUERY);
181 if( !xHandler.is() )
182 return sal_False;
186 Reference < XModifiable2 > xModifiable2( rComp, UNO_QUERY_THROW );
187 xModifiable2->disableSetModified();
189 catch( Exception& )
193 Reference < XImporter > xImporter( xHandler, UNO_QUERY );
194 xImporter->setTargetDocument( rComp );
196 xComp = rComp; // keep ref to component only if there is a handler
198 return sal_True;
201 XMLEmbeddedObjectImportContext::XMLEmbeddedObjectImportContext(
202 SvXMLImport& rImport, USHORT nPrfx, const OUString& rLName,
203 const Reference< XAttributeList >& xAttrList ) :
204 SvXMLImportContext( rImport, nPrfx, rLName )
206 SvGlobalName aName;
208 if( nPrfx == XML_NAMESPACE_MATH &&
209 IsXMLToken( rLName, XML_MATH ) )
211 sFilterService = OUString( RTL_CONSTASCII_USTRINGPARAM(XML_IMPORT_FILTER_MATH) );
212 aName = SvGlobalName(SO3_SM_CLASSID);
214 else if( nPrfx == XML_NAMESPACE_OFFICE &&
215 IsXMLToken( rLName, XML_DOCUMENT ) )
217 OUString sMime;
219 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
220 for( sal_Int16 i=0; i < nAttrCount; i++ )
222 const OUString& rAttrName = xAttrList->getNameByIndex( i );
223 OUString aLocalName;
224 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
225 if( nPrefix == XML_NAMESPACE_OFFICE &&
226 IsXMLToken( aLocalName, XML_MIMETYPE ) )
228 sMime = xAttrList->getValueByIndex( i );
229 break;
233 OUString sClass;
234 static const char * aTmp[] =
236 "application/vnd.oasis.openoffice.",
237 "application/x-vnd.oasis.openoffice.",
238 "application/vnd.oasis.opendocument.",
239 "application/x-vnd.oasis.opendocument.",
240 NULL
242 for (int k=0; aTmp[k]; k++)
244 ::rtl::OUString sTmpString = ::rtl::OUString::createFromAscii(aTmp[k]);
245 if( sMime.matchAsciiL( aTmp[k], sTmpString.getLength() ) )
247 sClass = sMime.copy( sTmpString.getLength() );
248 break;
252 if( sClass.getLength() )
254 const XMLServiceMapEntry_Impl *pEntry = aServiceMap;
255 while( pEntry->eClass != XML_TOKEN_INVALID )
257 if( IsXMLToken( sClass, pEntry->eClass ) )
259 sFilterService = OUString( pEntry->sFilterService,
260 pEntry->nFilterServiceLen,
261 RTL_TEXTENCODING_ASCII_US );
263 switch( pEntry->eClass )
265 case XML_TEXT: aName = SvGlobalName(SO3_SW_CLASSID); break;
266 case XML_ONLINE_TEXT: aName = SvGlobalName(SO3_SWWEB_CLASSID); break;
267 case XML_SPREADSHEET: aName = SvGlobalName(SO3_SC_CLASSID); break;
268 case XML_DRAWING:
269 case XML_GRAPHICS:
270 case XML_IMAGE: aName = SvGlobalName(SO3_SDRAW_CLASSID); break;
271 case XML_PRESENTATION: aName = SvGlobalName(SO3_SIMPRESS_CLASSID); break;
272 case XML_CHART: aName = SvGlobalName(SO3_SCH_CLASSID); break;
273 default:
274 break;
277 break;
279 pEntry++;
284 sCLSID = aName.GetHexName();
287 XMLEmbeddedObjectImportContext::~XMLEmbeddedObjectImportContext()
291 SvXMLImportContext *XMLEmbeddedObjectImportContext::CreateChildContext(
292 USHORT nPrefix, const OUString& rLocalName,
293 const Reference< XAttributeList >& )
295 if( xHandler.is() )
296 return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
297 nPrefix, rLocalName,
298 xHandler );
299 else
300 return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
303 void XMLEmbeddedObjectImportContext::StartElement(
304 const Reference< XAttributeList >& rAttrList )
306 if( xHandler.is() )
308 xHandler->startDocument();
309 // #i34042: copy namepspace declarations
310 SvXMLAttributeList *pAttrList = new SvXMLAttributeList( rAttrList );
311 Reference< XAttributeList > xAttrList( pAttrList );
312 const SvXMLNamespaceMap& rNamespaceMap = GetImport().GetNamespaceMap();
313 sal_uInt16 nPos = rNamespaceMap.GetFirstKey();
314 while( USHRT_MAX != nPos )
316 OUString aAttrName( rNamespaceMap.GetAttrNameByKey( nPos ) );
317 if( 0 == xAttrList->getValueByName( aAttrName ).getLength() )
319 pAttrList->AddAttribute( aAttrName,
320 rNamespaceMap.GetNameByKey( nPos ) );
322 nPos = rNamespaceMap.GetNextKey( nPos );
324 xHandler->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
325 GetPrefix(), GetLocalName() ),
326 xAttrList );
330 void XMLEmbeddedObjectImportContext::EndElement()
332 if( xHandler.is() )
334 xHandler->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
335 GetPrefix(), GetLocalName() ) );
336 xHandler->endDocument();
339 // storing part is commented out since it should be done through the object, not the model
340 // TODO/LATER: probably an object should be provided here an be stored here
342 // // Save the object. That's required because the object should not be
343 // // modified (it has been loaded just now). Setting it to unmodified
344 // // only does not work, because it is then assumed that it has been
345 // // stored.
346 // Reference < XStorable > xStorable( xComp, UNO_QUERY );
347 // if( xStorable.is() )
348 // {
349 // try
350 // {
351 // xStorable->store();
352 // }
353 // catch( ::com::sun::star::beans::PropertyVetoException& )
354 // {
355 // Sequence<OUString> aSeq( 0 );
356 // GetImport().SetError( XMLERROR_FLAG_WARNING |
357 // XMLERROR_API,
358 // aSeq );
359 // }
360 // }
364 Reference < XModifiable2 > xModifiable2( xComp, UNO_QUERY_THROW );
365 xModifiable2->enableSetModified();
366 xModifiable2->setModified( sal_True ); // trigger new replacement image generation
368 catch( Exception& )
373 // // reset modifies state for the object since it has been imported
374 // // completly and therfor hasn't been modified.
375 // Reference < XModifiable > xModifiable( xComp, UNO_QUERY );
376 // if( xModifiable.is() )
377 // {
378 // try
379 // {
380 // xModifiable->setModified( sal_False );
381 // }
382 // catch( ::com::sun::star::beans::PropertyVetoException& e )
383 // {
384 // Sequence<OUString> aSeq( 0 );
385 // GetImport().SetError( XMLERROR_FLAG_WARNING |
386 // XMLERROR_API,
387 // aSeq );
388 // }
389 // }
393 void XMLEmbeddedObjectImportContext::Characters( const ::rtl::OUString& rChars )
395 if( xHandler.is() )
396 xHandler->characters( rChars );