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 <sal/config.h>
22 #include <string_view>
24 #include <sal/log.hxx>
25 #include <com/sun/star/document/XImporter.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/util/XModifiable2.hpp>
28 #include <tools/globname.hxx>
29 #include <comphelper/classids.hxx>
30 #include <xmloff/namespacemap.hxx>
31 #include <xmloff/xmlimp.hxx>
32 #include <xmloff/xmlnamespace.hxx>
33 #include <xmloff/xmltoken.hxx>
34 #include <xmloff/attrlist.hxx>
35 #include <xmloff/XMLFilterServiceNames.h>
36 #include <XMLEmbeddedObjectImportContext.hxx>
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::util
;
40 using namespace ::com::sun::star::beans
;
41 using namespace ::com::sun::star::lang
;
42 using namespace ::com::sun::star::document
;
43 using namespace ::com::sun::star::xml::sax
;
44 using namespace ::xmloff::token
;
48 class XMLEmbeddedObjectImportContext_Impl
: public SvXMLImportContext
50 css::uno::Reference
< css::xml::sax::XFastDocumentHandler
> mxFastHandler
;
54 XMLEmbeddedObjectImportContext_Impl( SvXMLImport
& rImport
,
55 const css::uno::Reference
< css::xml::sax::XFastDocumentHandler
>& rHandler
);
57 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext(
59 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& AttrList
) override
;
61 virtual void SAL_CALL
startFastElement(
63 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
) override
;
65 virtual void SAL_CALL
endFastElement(sal_Int32 nElement
) override
;
67 virtual void SAL_CALL
characters( const OUString
& rChars
) override
;
72 XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
74 const Reference
< XFastDocumentHandler
>& rHandler
) :
75 SvXMLImportContext( rImport
),
76 mxFastHandler( rHandler
)
78 assert(mxFastHandler
);
81 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLEmbeddedObjectImportContext_Impl::createFastChildContext(
83 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
85 return new XMLEmbeddedObjectImportContext_Impl(GetImport(), mxFastHandler
);
88 void XMLEmbeddedObjectImportContext_Impl::startFastElement(
90 const Reference
< XFastAttributeList
>& xAttrList
)
92 mxFastHandler
->startFastElement( nElement
, xAttrList
);
95 void XMLEmbeddedObjectImportContext_Impl::endFastElement(sal_Int32 nElement
)
97 mxFastHandler
->endFastElement( nElement
);
100 void XMLEmbeddedObjectImportContext_Impl::characters( const OUString
& rChars
)
102 mxFastHandler
->characters( rChars
);
106 void XMLEmbeddedObjectImportContext::SetComponent( Reference
< XComponent
> const & rComp
)
108 if( !rComp
.is() || sFilterService
.isEmpty() )
111 Sequence
<Any
> aArgs( 0 );
113 Reference
< XComponentContext
> xContext( GetImport().GetComponentContext() );
115 Reference
<XInterface
> xFilter
=
116 xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(sFilterService
, aArgs
, xContext
);
117 SAL_WARN_IF(!xFilter
, "xmloff", "could not create filter " << sFilterService
);
121 assert(dynamic_cast<SvXMLImport
*>(xFilter
.get()));
122 SvXMLImport
*pFastHandler
= dynamic_cast<SvXMLImport
*>(xFilter
.get());
123 mxFastHandler
= pFastHandler
;
127 Reference
< XModifiable2
> xModifiable2( rComp
, UNO_QUERY_THROW
);
128 xModifiable2
->disableSetModified();
134 Reference
< XImporter
> xImporter( mxFastHandler
, UNO_QUERY
);
135 xImporter
->setTargetDocument( rComp
);
137 xComp
= rComp
; // keep ref to component only if there is a handler
139 // #i34042: copy namepspace declarations
140 // We created a new instance of XMLImport, so we need to propogate the namespace
141 // declarations to it.
142 pFastHandler
->GetNamespaceMap() = GetImport().GetNamespaceMap();
145 XMLEmbeddedObjectImportContext::XMLEmbeddedObjectImportContext(
146 SvXMLImport
& rImport
, sal_uInt16 nPrfx
, const OUString
& rLName
,
147 const Reference
< XAttributeList
>& xAttrList
) :
148 SvXMLImportContext( rImport
, nPrfx
, rLName
)
152 if( nPrfx
== XML_NAMESPACE_MATH
&&
153 IsXMLToken( rLName
, XML_MATH
) )
155 sFilterService
= XML_IMPORT_FILTER_MATH
;
156 aName
= SvGlobalName(SO3_SM_CLASSID
);
158 else if( nPrfx
== XML_NAMESPACE_OFFICE
&&
159 IsXMLToken( rLName
, XML_DOCUMENT
) )
163 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
164 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
166 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
168 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName
, &aLocalName
);
169 if( nPrefix
== XML_NAMESPACE_OFFICE
&&
170 IsXMLToken( aLocalName
, XML_MIMETYPE
) )
172 sMime
= xAttrList
->getValueByIndex( i
);
178 static std::u16string_view
const prefixes
[] = {
179 u
"application/vnd.oasis.openoffice.",
180 u
"application/x-vnd.oasis.openoffice.",
181 u
"application/vnd.oasis.opendocument.",
182 u
"application/x-vnd.oasis.opendocument."};
183 for (auto const & p
: prefixes
)
185 if (sMime
.startsWith(p
, &sClass
))
191 if( !sClass
.isEmpty() )
193 static struct { XMLTokenEnum eClass
; std::u16string_view sFilterService
;
194 } const aServiceMap
[] = {
195 { XML_TEXT
, std::u16string_view(u
"" XML_IMPORT_FILTER_WRITER
) },
196 { XML_ONLINE_TEXT
, std::u16string_view(u
"" XML_IMPORT_FILTER_WRITER
) },
197 { XML_SPREADSHEET
, std::u16string_view(u
"" XML_IMPORT_FILTER_CALC
) },
198 { XML_DRAWING
, std::u16string_view(u
"" XML_IMPORT_FILTER_DRAW
) },
199 { XML_GRAPHICS
, std::u16string_view(u
"" XML_IMPORT_FILTER_DRAW
) },
200 { XML_PRESENTATION
, std::u16string_view(u
"" XML_IMPORT_FILTER_IMPRESS
) },
201 { XML_CHART
, std::u16string_view(u
"" XML_IMPORT_FILTER_CHART
) }};
202 for (auto const & entry
: aServiceMap
)
204 if( IsXMLToken( sClass
, entry
.eClass
) )
206 sFilterService
= entry
.sFilterService
;
208 switch( entry
.eClass
)
210 case XML_TEXT
: aName
= SvGlobalName(SO3_SW_CLASSID
); break;
211 case XML_ONLINE_TEXT
: aName
= SvGlobalName(SO3_SWWEB_CLASSID
); break;
212 case XML_SPREADSHEET
: aName
= SvGlobalName(SO3_SC_CLASSID
); break;
215 case XML_IMAGE
: aName
= SvGlobalName(SO3_SDRAW_CLASSID
); break;
216 case XML_PRESENTATION
: aName
= SvGlobalName(SO3_SIMPRESS_CLASSID
); break;
217 case XML_CHART
: aName
= SvGlobalName(SO3_SCH_CLASSID
); break;
228 sCLSID
= aName
.GetHexName();
231 XMLEmbeddedObjectImportContext::~XMLEmbeddedObjectImportContext()
235 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLEmbeddedObjectImportContext::createFastChildContext(
237 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
239 if( mxFastHandler
.is() )
240 return new XMLEmbeddedObjectImportContext_Impl( GetImport(), mxFastHandler
);
244 void XMLEmbeddedObjectImportContext::startFastElement(
246 const Reference
< XFastAttributeList
>& rAttrList
)
248 if( !mxFastHandler
.is() )
251 mxFastHandler
->startDocument();
252 mxFastHandler
->startFastElement( nElement
, rAttrList
);
255 void XMLEmbeddedObjectImportContext::endFastElement(sal_Int32 nElement
)
257 if( !mxFastHandler
.is() )
260 mxFastHandler
->endFastElement( nElement
);
261 mxFastHandler
->endDocument();
265 Reference
< XModifiable2
> xModifiable2( xComp
, UNO_QUERY_THROW
);
266 xModifiable2
->enableSetModified();
267 xModifiable2
->setModified( true ); // trigger new replacement image generation
274 void XMLEmbeddedObjectImportContext::characters( const OUString
& rChars
)
276 if( mxFastHandler
.is() )
277 mxFastHandler
->characters( rChars
);
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */