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>
25 #include <sal/log.hxx>
26 #include <com/sun/star/document/XImporter.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <com/sun/star/util/XModifiable2.hpp>
29 #include <tools/globname.hxx>
30 #include <comphelper/classids.hxx>
31 #include <xmloff/namespacemap.hxx>
32 #include <xmloff/xmlimp.hxx>
33 #include <xmloff/xmlnamespace.hxx>
34 #include <xmloff/xmltoken.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::lang
;
41 using namespace ::com::sun::star::document
;
42 using namespace ::com::sun::star::xml::sax
;
43 using namespace ::xmloff::token
;
47 class XMLEmbeddedObjectImportContext_Impl
: public SvXMLImportContext
49 css::uno::Reference
< css::xml::sax::XFastDocumentHandler
> mxFastHandler
;
53 XMLEmbeddedObjectImportContext_Impl( SvXMLImport
& rImport
,
54 const css::uno::Reference
< css::xml::sax::XFastDocumentHandler
>& rHandler
);
56 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext(
58 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& AttrList
) override
;
60 virtual void SAL_CALL
startFastElement(
62 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
) override
;
64 virtual void SAL_CALL
endFastElement(sal_Int32 nElement
) override
;
66 virtual void SAL_CALL
characters( const OUString
& rChars
) override
;
71 XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
73 const Reference
< XFastDocumentHandler
>& rHandler
) :
74 SvXMLImportContext( rImport
),
75 mxFastHandler( rHandler
)
77 assert(mxFastHandler
);
80 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLEmbeddedObjectImportContext_Impl::createFastChildContext(
82 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
84 return new XMLEmbeddedObjectImportContext_Impl(GetImport(), mxFastHandler
);
87 void XMLEmbeddedObjectImportContext_Impl::startFastElement(
89 const Reference
< XFastAttributeList
>& xAttrList
)
91 mxFastHandler
->startFastElement( nElement
, xAttrList
);
94 void XMLEmbeddedObjectImportContext_Impl::endFastElement(sal_Int32 nElement
)
96 mxFastHandler
->endFastElement( nElement
);
99 void XMLEmbeddedObjectImportContext_Impl::characters( const OUString
& rChars
)
101 mxFastHandler
->characters( rChars
);
105 void XMLEmbeddedObjectImportContext::SetComponent( Reference
< XComponent
> const & rComp
)
107 if( !rComp
.is() || sFilterService
.isEmpty() )
110 Sequence
<Any
> aArgs( 0 );
112 Reference
< XComponentContext
> xContext( GetImport().GetComponentContext() );
114 Reference
<XInterface
> xFilter
=
115 xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(sFilterService
, aArgs
, xContext
);
116 SAL_WARN_IF(!xFilter
, "xmloff", "could not create filter " << sFilterService
);
120 assert(dynamic_cast<SvXMLImport
*>(xFilter
.get()));
121 SvXMLImport
& rFastHandler
= dynamic_cast<SvXMLImport
&>(*xFilter
);
122 mxFastHandler
= &rFastHandler
;
126 Reference
< XModifiable2
> xModifiable2( rComp
, UNO_QUERY
);
128 xModifiable2
->disableSetModified();
134 mxFastHandler
->setTargetDocument( rComp
);
136 xComp
= rComp
; // keep ref to component only if there is a handler
138 // #i34042: copy namespace declarations
139 // We created a new instance of XMLImport, so we need to propagate the namespace
140 // declarations to it.
141 rFastHandler
.GetNamespaceMap() = GetImport().GetNamespaceMap();
144 XMLEmbeddedObjectImportContext::XMLEmbeddedObjectImportContext(
145 SvXMLImport
& rImport
, sal_Int32 nElement
,
146 const Reference
< XFastAttributeList
>& xAttrList
) :
147 SvXMLImportContext( rImport
)
151 if( nElement
== XML_ELEMENT(MATH
, XML_MATH
) )
153 sFilterService
= XML_IMPORT_FILTER_MATH
;
154 aName
= SvGlobalName(SO3_SM_CLASSID
);
156 else if( nElement
== XML_ELEMENT(OFFICE
, XML_DOCUMENT
) )
160 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
162 switch (aIter
.getToken())
164 case XML_ELEMENT(OFFICE
, XML_MIMETYPE
):
165 sMime
= aIter
.toString();
168 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
172 std::u16string_view sClass
;
173 static std::u16string_view
const prefixes
[] = {
174 u
"application/vnd.oasis.openoffice.",
175 u
"application/x-vnd.oasis.openoffice.",
176 u
"application/vnd.oasis.opendocument.",
177 u
"application/x-vnd.oasis.opendocument."};
178 for (auto const & p
: prefixes
)
180 if (o3tl::starts_with(sMime
, p
, &sClass
))
186 if( !sClass
.empty() )
188 static const std::tuple
<XMLTokenEnum
, OUString
, SvGUID
> aServiceMap
[] = {
189 { XML_TEXT
, XML_IMPORT_FILTER_WRITER
, { SO3_SW_CLASSID
} },
190 { XML_ONLINE_TEXT
, XML_IMPORT_FILTER_WRITER
, { SO3_SWWEB_CLASSID
} },
191 { XML_SPREADSHEET
, XML_IMPORT_FILTER_CALC
, { SO3_SC_CLASSID
} },
192 { XML_DRAWING
, XML_IMPORT_FILTER_DRAW
, { SO3_SDRAW_CLASSID
} },
193 { XML_GRAPHICS
, XML_IMPORT_FILTER_DRAW
, { SO3_SDRAW_CLASSID
} },
194 { XML_PRESENTATION
, XML_IMPORT_FILTER_IMPRESS
, { SO3_SIMPRESS_CLASSID
} },
195 { XML_CHART
, XML_IMPORT_FILTER_CHART
, { SO3_SCH_CLASSID
} },
197 for (auto const& [eClass
, sMatchingFilterService
, rCLASSID
] : aServiceMap
)
199 if (IsXMLToken(sClass
, eClass
))
201 sFilterService
= sMatchingFilterService
;
202 aName
= SvGlobalName(rCLASSID
);
209 sCLSID
= aName
.GetHexName();
212 XMLEmbeddedObjectImportContext::~XMLEmbeddedObjectImportContext()
216 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLEmbeddedObjectImportContext::createFastChildContext(
218 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
220 if( mxFastHandler
.is() )
221 return new XMLEmbeddedObjectImportContext_Impl( GetImport(), mxFastHandler
);
225 void XMLEmbeddedObjectImportContext::startFastElement(
227 const Reference
< XFastAttributeList
>& rAttrList
)
229 if( !mxFastHandler
.is() )
232 mxFastHandler
->startDocument();
233 mxFastHandler
->startFastElement( nElement
, rAttrList
);
236 void XMLEmbeddedObjectImportContext::endFastElement(sal_Int32 nElement
)
238 if( !mxFastHandler
.is() )
241 mxFastHandler
->endFastElement( nElement
);
242 mxFastHandler
->endDocument();
246 Reference
< XModifiable2
> xModifiable2( xComp
, UNO_QUERY
);
249 xModifiable2
->enableSetModified();
250 xModifiable2
->setModified( true ); // trigger new replacement image generation
258 void XMLEmbeddedObjectImportContext::characters( const OUString
& rChars
)
260 if( mxFastHandler
.is() )
261 mxFastHandler
->characters( rChars
);
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */