bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / core / XMLEmbeddedObjectImportContext.cxx
blob807804a52f90efe93300de14d322c87f3fa20658
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/document/XImporter.hpp>
21 #include <com/sun/star/util/XModifiable.hpp>
22 #include <com/sun/star/util/XModifiable2.hpp>
23 #include <com/sun/star/frame/XStorable.hpp>
24 #include <tools/globname.hxx>
25 #include <comphelper/classids.hxx>
26 #include <xmloff/nmspmap.hxx>
27 #include <xmloff/xmlimp.hxx>
28 #include <xmloff/xmlnmspe.hxx>
29 #include <xmloff/xmltoken.hxx>
30 #include <xmloff/xmlerror.hxx>
31 #include <xmloff/attrlist.hxx>
32 #include <xmloff/XMLFilterServiceNames.h>
33 #include "XMLEmbeddedObjectImportContext.hxx"
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::util;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::frame;
40 using namespace ::com::sun::star::document;
41 using namespace ::com::sun::star::xml::sax;
42 using namespace ::xmloff::token;
44 namespace {
46 struct XMLServiceMapEntry_Impl
48 enum XMLTokenEnum eClass;
49 const sal_Char *sFilterService;
50 sal_Int32 nFilterServiceLen;
55 #define SERVICE_MAP_ENTRY( cls, app ) \
56 { XML_##cls, \
57 XML_IMPORT_FILTER_##app, sizeof(XML_IMPORT_FILTER_##app)-1}
59 const XMLServiceMapEntry_Impl aServiceMap[] =
61 SERVICE_MAP_ENTRY( TEXT, WRITER ),
62 SERVICE_MAP_ENTRY( ONLINE_TEXT, WRITER ),
63 SERVICE_MAP_ENTRY( SPREADSHEET, CALC ),
64 SERVICE_MAP_ENTRY( DRAWING, DRAW ),
65 SERVICE_MAP_ENTRY( GRAPHICS, DRAW ),
66 SERVICE_MAP_ENTRY( PRESENTATION, IMPRESS ),
67 SERVICE_MAP_ENTRY( CHART, CHART ),
68 { XML_TOKEN_INVALID, 0, 0 }
71 class XMLEmbeddedObjectImportContext_Impl : public SvXMLImportContext
73 ::com::sun::star::uno::Reference<
74 ::com::sun::star::xml::sax::XDocumentHandler > xHandler;
76 public:
77 TYPEINFO_OVERRIDE();
79 XMLEmbeddedObjectImportContext_Impl( SvXMLImport& rImport, sal_uInt16 nPrfx,
80 const OUString& rLName,
81 const ::com::sun::star::uno::Reference<
82 ::com::sun::star::xml::sax::XDocumentHandler >& rHandler );
84 virtual ~XMLEmbeddedObjectImportContext_Impl();
86 virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
87 const OUString& rLocalName,
88 const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList ) SAL_OVERRIDE;
90 virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList ) SAL_OVERRIDE;
92 virtual void EndElement() SAL_OVERRIDE;
94 virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
97 TYPEINIT1( XMLEmbeddedObjectImportContext_Impl, SvXMLImportContext );
99 XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
100 SvXMLImport& rImport, sal_uInt16 nPrfx,
101 const OUString& rLName,
102 const Reference< XDocumentHandler >& rHandler ) :
103 SvXMLImportContext( rImport, nPrfx, rLName ),
104 xHandler( rHandler )
108 XMLEmbeddedObjectImportContext_Impl::~XMLEmbeddedObjectImportContext_Impl()
112 SvXMLImportContext *XMLEmbeddedObjectImportContext_Impl::CreateChildContext(
113 sal_uInt16 nPrefix,
114 const OUString& rLocalName,
115 const Reference< XAttributeList >& )
117 return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
118 nPrefix, rLocalName,
119 xHandler );
122 void XMLEmbeddedObjectImportContext_Impl::StartElement(
123 const Reference< XAttributeList >& xAttrList )
125 xHandler->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
126 GetPrefix(), GetLocalName() ),
127 xAttrList );
130 void XMLEmbeddedObjectImportContext_Impl::EndElement()
132 xHandler->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
133 GetPrefix(), GetLocalName() ) );
136 void XMLEmbeddedObjectImportContext_Impl::Characters( const OUString& rChars )
138 xHandler->characters( rChars );
141 TYPEINIT1( XMLEmbeddedObjectImportContext, SvXMLImportContext );
143 bool XMLEmbeddedObjectImportContext::SetComponent(
144 Reference< XComponent >& rComp )
146 if( !rComp.is() || sFilterService.isEmpty() )
147 return false;
149 Sequence<Any> aArgs( 0 );
151 Reference< XComponentContext > xContext( GetImport().GetComponentContext() );
153 xHandler = Reference < XDocumentHandler >(
154 xContext->getServiceManager()->createInstanceWithArgumentsAndContext(sFilterService, aArgs, xContext),
155 UNO_QUERY);
157 if( !xHandler.is() )
158 return false;
162 Reference < XModifiable2 > xModifiable2( rComp, UNO_QUERY_THROW );
163 xModifiable2->disableSetModified();
165 catch( Exception& )
169 Reference < XImporter > xImporter( xHandler, UNO_QUERY );
170 xImporter->setTargetDocument( rComp );
172 xComp = rComp; // keep ref to component only if there is a handler
174 return true;
177 XMLEmbeddedObjectImportContext::XMLEmbeddedObjectImportContext(
178 SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
179 const Reference< XAttributeList >& xAttrList ) :
180 SvXMLImportContext( rImport, nPrfx, rLName )
182 SvGlobalName aName;
184 if( nPrfx == XML_NAMESPACE_MATH &&
185 IsXMLToken( rLName, XML_MATH ) )
187 sFilterService = XML_IMPORT_FILTER_MATH;
188 aName = SvGlobalName(SO3_SM_CLASSID);
190 else if( nPrfx == XML_NAMESPACE_OFFICE &&
191 IsXMLToken( rLName, XML_DOCUMENT ) )
193 OUString sMime;
195 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
196 for( sal_Int16 i=0; i < nAttrCount; i++ )
198 const OUString& rAttrName = xAttrList->getNameByIndex( i );
199 OUString aLocalName;
200 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
201 if( nPrefix == XML_NAMESPACE_OFFICE &&
202 IsXMLToken( aLocalName, XML_MIMETYPE ) )
204 sMime = xAttrList->getValueByIndex( i );
205 break;
209 OUString sClass;
210 static const char * aTmp[] =
212 "application/vnd.oasis.openoffice.",
213 "application/x-vnd.oasis.openoffice.",
214 "application/vnd.oasis.opendocument.",
215 "application/x-vnd.oasis.opendocument.",
216 NULL
218 for (int k=0; aTmp[k]; k++)
220 OUString sTmpString = OUString::createFromAscii(aTmp[k]);
221 if( sMime.matchAsciiL( aTmp[k], sTmpString.getLength() ) )
223 sClass = sMime.copy( sTmpString.getLength() );
224 break;
228 if( !sClass.isEmpty() )
230 const XMLServiceMapEntry_Impl *pEntry = aServiceMap;
231 while( pEntry->eClass != XML_TOKEN_INVALID )
233 if( IsXMLToken( sClass, pEntry->eClass ) )
235 sFilterService = OUString( pEntry->sFilterService,
236 pEntry->nFilterServiceLen,
237 RTL_TEXTENCODING_ASCII_US );
239 switch( pEntry->eClass )
241 case XML_TEXT: aName = SvGlobalName(SO3_SW_CLASSID); break;
242 case XML_ONLINE_TEXT: aName = SvGlobalName(SO3_SWWEB_CLASSID); break;
243 case XML_SPREADSHEET: aName = SvGlobalName(SO3_SC_CLASSID); break;
244 case XML_DRAWING:
245 case XML_GRAPHICS:
246 case XML_IMAGE: aName = SvGlobalName(SO3_SDRAW_CLASSID); break;
247 case XML_PRESENTATION: aName = SvGlobalName(SO3_SIMPRESS_CLASSID); break;
248 case XML_CHART: aName = SvGlobalName(SO3_SCH_CLASSID); break;
249 default:
250 break;
253 break;
255 pEntry++;
260 sCLSID = aName.GetHexName();
263 XMLEmbeddedObjectImportContext::~XMLEmbeddedObjectImportContext()
267 SvXMLImportContext *XMLEmbeddedObjectImportContext::CreateChildContext(
268 sal_uInt16 nPrefix, const OUString& rLocalName,
269 const Reference< XAttributeList >& )
271 if( xHandler.is() )
272 return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
273 nPrefix, rLocalName,
274 xHandler );
275 else
276 return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
279 void XMLEmbeddedObjectImportContext::StartElement(
280 const Reference< XAttributeList >& rAttrList )
282 if( xHandler.is() )
284 xHandler->startDocument();
285 // #i34042: copy namepspace declarations
286 SvXMLAttributeList *pAttrList = new SvXMLAttributeList( rAttrList );
287 Reference< XAttributeList > xAttrList( pAttrList );
288 const SvXMLNamespaceMap& rNamespaceMap = GetImport().GetNamespaceMap();
289 sal_uInt16 nPos = rNamespaceMap.GetFirstKey();
290 while( USHRT_MAX != nPos )
292 OUString aAttrName( rNamespaceMap.GetAttrNameByKey( nPos ) );
293 if( xAttrList->getValueByName( aAttrName ).isEmpty() )
295 pAttrList->AddAttribute( aAttrName,
296 rNamespaceMap.GetNameByKey( nPos ) );
298 nPos = rNamespaceMap.GetNextKey( nPos );
300 xHandler->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
301 GetPrefix(), GetLocalName() ),
302 xAttrList );
306 void XMLEmbeddedObjectImportContext::EndElement()
308 if( xHandler.is() )
310 xHandler->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
311 GetPrefix(), GetLocalName() ) );
312 xHandler->endDocument();
316 Reference < XModifiable2 > xModifiable2( xComp, UNO_QUERY_THROW );
317 xModifiable2->enableSetModified();
318 xModifiable2->setModified( sal_True ); // trigger new replacement image generation
320 catch( Exception& )
326 void XMLEmbeddedObjectImportContext::Characters( const OUString& rChars )
328 if( xHandler.is() )
329 xHandler->characters( rChars );
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */