1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
46 struct XMLServiceMapEntry_Impl
48 enum XMLTokenEnum eClass
;
49 const sal_Char
*sFilterService
;
50 sal_Int32 nFilterServiceLen
;
55 #define SERVICE_MAP_ENTRY( cls, app ) \
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
, nullptr, 0 }
71 class XMLEmbeddedObjectImportContext_Impl
: public SvXMLImportContext
73 css::uno::Reference
< css::xml::sax::XDocumentHandler
> xHandler
;
77 XMLEmbeddedObjectImportContext_Impl( SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
78 const OUString
& rLName
,
79 const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& rHandler
);
81 virtual ~XMLEmbeddedObjectImportContext_Impl();
83 virtual SvXMLImportContext
*CreateChildContext( sal_uInt16 nPrefix
,
84 const OUString
& rLocalName
,
85 const css::uno::Reference
< css::xml::sax::XAttributeList
>& xAttrList
) override
;
87 virtual void StartElement( const css::uno::Reference
< css::xml::sax::XAttributeList
>& xAttrList
) override
;
89 virtual void EndElement() override
;
91 virtual void Characters( const OUString
& rChars
) override
;
95 XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
96 SvXMLImport
& rImport
, sal_uInt16 nPrfx
,
97 const OUString
& rLName
,
98 const Reference
< XDocumentHandler
>& rHandler
) :
99 SvXMLImportContext( rImport
, nPrfx
, rLName
),
104 XMLEmbeddedObjectImportContext_Impl::~XMLEmbeddedObjectImportContext_Impl()
108 SvXMLImportContext
*XMLEmbeddedObjectImportContext_Impl::CreateChildContext(
110 const OUString
& rLocalName
,
111 const Reference
< XAttributeList
>& )
113 return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
118 void XMLEmbeddedObjectImportContext_Impl::StartElement(
119 const Reference
< XAttributeList
>& xAttrList
)
121 xHandler
->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
122 GetPrefix(), GetLocalName() ),
126 void XMLEmbeddedObjectImportContext_Impl::EndElement()
128 xHandler
->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
129 GetPrefix(), GetLocalName() ) );
132 void XMLEmbeddedObjectImportContext_Impl::Characters( const OUString
& rChars
)
134 xHandler
->characters( rChars
);
138 void XMLEmbeddedObjectImportContext::SetComponent(
139 Reference
< XComponent
>& rComp
)
141 if( !rComp
.is() || sFilterService
.isEmpty() )
144 Sequence
<Any
> aArgs( 0 );
146 Reference
< XComponentContext
> xContext( GetImport().GetComponentContext() );
149 xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(sFilterService
, aArgs
, xContext
),
157 Reference
< XModifiable2
> xModifiable2( rComp
, UNO_QUERY_THROW
);
158 xModifiable2
->disableSetModified();
164 Reference
< XImporter
> xImporter( xHandler
, UNO_QUERY
);
165 xImporter
->setTargetDocument( rComp
);
167 xComp
= rComp
; // keep ref to component only if there is a handler
170 XMLEmbeddedObjectImportContext::XMLEmbeddedObjectImportContext(
171 SvXMLImport
& rImport
, sal_uInt16 nPrfx
, const OUString
& rLName
,
172 const Reference
< XAttributeList
>& xAttrList
) :
173 SvXMLImportContext( rImport
, nPrfx
, rLName
)
177 if( nPrfx
== XML_NAMESPACE_MATH
&&
178 IsXMLToken( rLName
, XML_MATH
) )
180 sFilterService
= XML_IMPORT_FILTER_MATH
;
181 aName
= SvGlobalName(SO3_SM_CLASSID
);
183 else if( nPrfx
== XML_NAMESPACE_OFFICE
&&
184 IsXMLToken( rLName
, XML_DOCUMENT
) )
188 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
189 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
191 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
193 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName
, &aLocalName
);
194 if( nPrefix
== XML_NAMESPACE_OFFICE
&&
195 IsXMLToken( aLocalName
, XML_MIMETYPE
) )
197 sMime
= xAttrList
->getValueByIndex( i
);
203 static const char * aTmp
[] =
205 "application/vnd.oasis.openoffice.",
206 "application/x-vnd.oasis.openoffice.",
207 "application/vnd.oasis.opendocument.",
208 "application/x-vnd.oasis.opendocument.",
211 for (int k
=0; aTmp
[k
]; k
++)
213 OUString sTmpString
= OUString::createFromAscii(aTmp
[k
]);
214 if( sMime
.matchAsciiL( aTmp
[k
], sTmpString
.getLength() ) )
216 sClass
= sMime
.copy( sTmpString
.getLength() );
221 if( !sClass
.isEmpty() )
223 const XMLServiceMapEntry_Impl
*pEntry
= aServiceMap
;
224 while( pEntry
->eClass
!= XML_TOKEN_INVALID
)
226 if( IsXMLToken( sClass
, pEntry
->eClass
) )
228 sFilterService
= OUString( pEntry
->sFilterService
,
229 pEntry
->nFilterServiceLen
,
230 RTL_TEXTENCODING_ASCII_US
);
232 switch( pEntry
->eClass
)
234 case XML_TEXT
: aName
= SvGlobalName(SO3_SW_CLASSID
); break;
235 case XML_ONLINE_TEXT
: aName
= SvGlobalName(SO3_SWWEB_CLASSID
); break;
236 case XML_SPREADSHEET
: aName
= SvGlobalName(SO3_SC_CLASSID
); break;
239 case XML_IMAGE
: aName
= SvGlobalName(SO3_SDRAW_CLASSID
); break;
240 case XML_PRESENTATION
: aName
= SvGlobalName(SO3_SIMPRESS_CLASSID
); break;
241 case XML_CHART
: aName
= SvGlobalName(SO3_SCH_CLASSID
); break;
253 sCLSID
= aName
.GetHexName();
256 XMLEmbeddedObjectImportContext::~XMLEmbeddedObjectImportContext()
260 SvXMLImportContext
*XMLEmbeddedObjectImportContext::CreateChildContext(
261 sal_uInt16 nPrefix
, const OUString
& rLocalName
,
262 const Reference
< XAttributeList
>& )
265 return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
269 return new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
272 void XMLEmbeddedObjectImportContext::StartElement(
273 const Reference
< XAttributeList
>& rAttrList
)
277 xHandler
->startDocument();
278 // #i34042: copy namepspace declarations
279 SvXMLAttributeList
*pAttrList
= new SvXMLAttributeList( rAttrList
);
280 Reference
< XAttributeList
> xAttrList( pAttrList
);
281 const SvXMLNamespaceMap
& rNamespaceMap
= GetImport().GetNamespaceMap();
282 sal_uInt16 nPos
= rNamespaceMap
.GetFirstKey();
283 while( USHRT_MAX
!= nPos
)
285 OUString
aAttrName( rNamespaceMap
.GetAttrNameByKey( nPos
) );
286 if( xAttrList
->getValueByName( aAttrName
).isEmpty() )
288 pAttrList
->AddAttribute( aAttrName
,
289 rNamespaceMap
.GetNameByKey( nPos
) );
291 nPos
= rNamespaceMap
.GetNextKey( nPos
);
293 xHandler
->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
294 GetPrefix(), GetLocalName() ),
299 void XMLEmbeddedObjectImportContext::EndElement()
303 xHandler
->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
304 GetPrefix(), GetLocalName() ) );
305 xHandler
->endDocument();
309 Reference
< XModifiable2
> xModifiable2( xComp
, UNO_QUERY_THROW
);
310 xModifiable2
->enableSetModified();
311 xModifiable2
->setModified( true ); // trigger new replacement image generation
319 void XMLEmbeddedObjectImportContext::Characters( const OUString
& rChars
)
322 xHandler
->characters( rChars
);
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */