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: XMLEmbeddedObjectImportContext.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 // 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>
39 //#ifndef _COMPHELPER_PROCESSFACTORY_HXX_
40 //#include <comphelper/processfactory.hxx>
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 ) \
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
;
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
),
127 XMLEmbeddedObjectImportContext_Impl::~XMLEmbeddedObjectImportContext_Impl()
131 SvXMLImportContext
*XMLEmbeddedObjectImportContext_Impl::CreateChildContext(
133 const OUString
& rLocalName
,
134 const Reference
< XAttributeList
>& )
136 return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
141 void XMLEmbeddedObjectImportContext_Impl::StartElement(
142 const Reference
< XAttributeList
>& xAttrList
)
144 xHandler
->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
145 GetPrefix(), GetLocalName() ),
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() )
171 Sequence
<Any
> aArgs( 0 );
174 // Reference< XMultiServiceFactory > xServiceFactory = comphelper::getProcessServiceFactory();
175 Reference
< XMultiServiceFactory
> xServiceFactory
= GetImport().getServiceFactory();
177 xHandler
= Reference
< XDocumentHandler
>(
178 xServiceFactory
->createInstanceWithArguments( sFilterService
, aArgs
),
186 Reference
< XModifiable2
> xModifiable2( rComp
, UNO_QUERY_THROW
);
187 xModifiable2
->disableSetModified();
193 Reference
< XImporter
> xImporter( xHandler
, UNO_QUERY
);
194 xImporter
->setTargetDocument( rComp
);
196 xComp
= rComp
; // keep ref to component only if there is a handler
201 XMLEmbeddedObjectImportContext::XMLEmbeddedObjectImportContext(
202 SvXMLImport
& rImport
, USHORT nPrfx
, const OUString
& rLName
,
203 const Reference
< XAttributeList
>& xAttrList
) :
204 SvXMLImportContext( rImport
, nPrfx
, rLName
)
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
) )
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
);
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
);
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.",
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() );
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;
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;
284 sCLSID
= aName
.GetHexName();
287 XMLEmbeddedObjectImportContext::~XMLEmbeddedObjectImportContext()
291 SvXMLImportContext
*XMLEmbeddedObjectImportContext::CreateChildContext(
292 USHORT nPrefix
, const OUString
& rLocalName
,
293 const Reference
< XAttributeList
>& )
296 return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
300 return new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
303 void XMLEmbeddedObjectImportContext::StartElement(
304 const Reference
< XAttributeList
>& rAttrList
)
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() ),
330 void XMLEmbeddedObjectImportContext::EndElement()
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
346 // Reference < XStorable > xStorable( xComp, UNO_QUERY );
347 // if( xStorable.is() )
351 // xStorable->store();
353 // catch( ::com::sun::star::beans::PropertyVetoException& )
355 // Sequence<OUString> aSeq( 0 );
356 // GetImport().SetError( XMLERROR_FLAG_WARNING |
364 Reference
< XModifiable2
> xModifiable2( xComp
, UNO_QUERY_THROW
);
365 xModifiable2
->enableSetModified();
366 xModifiable2
->setModified( sal_True
); // trigger new replacement image generation
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() )
380 // xModifiable->setModified( sal_False );
382 // catch( ::com::sun::star::beans::PropertyVetoException& e )
384 // Sequence<OUString> aSeq( 0 );
385 // GetImport().SetError( XMLERROR_FLAG_WARNING |
393 void XMLEmbeddedObjectImportContext::Characters( const ::rtl::OUString
& rChars
)
396 xHandler
->characters( rChars
);