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/attrlist.hxx>
36 #include <xmloff/XMLFilterServiceNames.h>
37 #include <XMLEmbeddedObjectImportContext.hxx>
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::util
;
41 using namespace ::com::sun::star::beans
;
42 using namespace ::com::sun::star::lang
;
43 using namespace ::com::sun::star::document
;
44 using namespace ::com::sun::star::xml::sax
;
45 using namespace ::xmloff::token
;
49 class XMLEmbeddedObjectImportContext_Impl
: public SvXMLImportContext
51 css::uno::Reference
< css::xml::sax::XFastDocumentHandler
> mxFastHandler
;
55 XMLEmbeddedObjectImportContext_Impl( SvXMLImport
& rImport
,
56 const css::uno::Reference
< css::xml::sax::XFastDocumentHandler
>& rHandler
);
58 virtual css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext(
60 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& AttrList
) override
;
62 virtual void SAL_CALL
startFastElement(
64 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
) override
;
66 virtual void SAL_CALL
endFastElement(sal_Int32 nElement
) override
;
68 virtual void SAL_CALL
characters( const OUString
& rChars
) override
;
73 XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
75 const Reference
< XFastDocumentHandler
>& rHandler
) :
76 SvXMLImportContext( rImport
),
77 mxFastHandler( rHandler
)
79 assert(mxFastHandler
);
82 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLEmbeddedObjectImportContext_Impl::createFastChildContext(
84 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
86 return new XMLEmbeddedObjectImportContext_Impl(GetImport(), mxFastHandler
);
89 void XMLEmbeddedObjectImportContext_Impl::startFastElement(
91 const Reference
< XFastAttributeList
>& xAttrList
)
93 mxFastHandler
->startFastElement( nElement
, xAttrList
);
96 void XMLEmbeddedObjectImportContext_Impl::endFastElement(sal_Int32 nElement
)
98 mxFastHandler
->endFastElement( nElement
);
101 void XMLEmbeddedObjectImportContext_Impl::characters( const OUString
& rChars
)
103 mxFastHandler
->characters( rChars
);
107 void XMLEmbeddedObjectImportContext::SetComponent( Reference
< XComponent
> const & rComp
)
109 if( !rComp
.is() || sFilterService
.isEmpty() )
112 Sequence
<Any
> aArgs( 0 );
114 Reference
< XComponentContext
> xContext( GetImport().GetComponentContext() );
116 Reference
<XInterface
> xFilter
=
117 xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(sFilterService
, aArgs
, xContext
);
118 SAL_WARN_IF(!xFilter
, "xmloff", "could not create filter " << sFilterService
);
122 assert(dynamic_cast<SvXMLImport
*>(xFilter
.get()));
123 SvXMLImport
*pFastHandler
= dynamic_cast<SvXMLImport
*>(xFilter
.get());
124 mxFastHandler
= pFastHandler
;
128 Reference
< XModifiable2
> xModifiable2( rComp
, UNO_QUERY_THROW
);
129 xModifiable2
->disableSetModified();
135 Reference
< XImporter
> xImporter( mxFastHandler
, UNO_QUERY
);
136 xImporter
->setTargetDocument( rComp
);
138 xComp
= rComp
; // keep ref to component only if there is a handler
140 // #i34042: copy namespace declarations
141 // We created a new instance of XMLImport, so we need to propagate the namespace
142 // declarations to it.
143 pFastHandler
->GetNamespaceMap() = GetImport().GetNamespaceMap();
146 XMLEmbeddedObjectImportContext::XMLEmbeddedObjectImportContext(
147 SvXMLImport
& rImport
, sal_Int32 nElement
,
148 const Reference
< XFastAttributeList
>& xAttrList
) :
149 SvXMLImportContext( rImport
)
153 if( nElement
== XML_ELEMENT(MATH
, XML_MATH
) )
155 sFilterService
= XML_IMPORT_FILTER_MATH
;
156 aName
= SvGlobalName(SO3_SM_CLASSID
);
158 else if( nElement
== XML_ELEMENT(OFFICE
, XML_DOCUMENT
) )
162 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
164 switch (aIter
.getToken())
166 case XML_ELEMENT(OFFICE
, XML_MIMETYPE
):
167 sMime
= aIter
.toString();
170 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
175 static std::u16string_view
const prefixes
[] = {
176 u
"application/vnd.oasis.openoffice.",
177 u
"application/x-vnd.oasis.openoffice.",
178 u
"application/vnd.oasis.opendocument.",
179 u
"application/x-vnd.oasis.opendocument."};
180 for (auto const & p
: prefixes
)
182 if (sMime
.startsWith(p
, &sClass
))
188 if( !sClass
.isEmpty() )
190 static const std::tuple
<XMLTokenEnum
, OUString
, SvGUID
> aServiceMap
[] = {
191 { XML_TEXT
, XML_IMPORT_FILTER_WRITER
, { SO3_SW_CLASSID
} },
192 { XML_ONLINE_TEXT
, XML_IMPORT_FILTER_WRITER
, { SO3_SWWEB_CLASSID
} },
193 { XML_SPREADSHEET
, XML_IMPORT_FILTER_CALC
, { SO3_SC_CLASSID
} },
194 { XML_DRAWING
, XML_IMPORT_FILTER_DRAW
, { SO3_SDRAW_CLASSID
} },
195 { XML_GRAPHICS
, XML_IMPORT_FILTER_DRAW
, { SO3_SDRAW_CLASSID
} },
196 { XML_PRESENTATION
, XML_IMPORT_FILTER_IMPRESS
, { SO3_SIMPRESS_CLASSID
} },
197 { XML_CHART
, XML_IMPORT_FILTER_CHART
, { SO3_SCH_CLASSID
} },
199 for (auto const& [eClass
, sMatchingFilterService
, rCLASSID
] : aServiceMap
)
201 if (IsXMLToken(sClass
, eClass
))
203 sFilterService
= sMatchingFilterService
;
204 aName
= SvGlobalName(rCLASSID
);
211 sCLSID
= aName
.GetHexName();
214 XMLEmbeddedObjectImportContext::~XMLEmbeddedObjectImportContext()
218 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLEmbeddedObjectImportContext::createFastChildContext(
220 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
222 if( mxFastHandler
.is() )
223 return new XMLEmbeddedObjectImportContext_Impl( GetImport(), mxFastHandler
);
227 void XMLEmbeddedObjectImportContext::startFastElement(
229 const Reference
< XFastAttributeList
>& rAttrList
)
231 if( !mxFastHandler
.is() )
234 mxFastHandler
->startDocument();
235 mxFastHandler
->startFastElement( nElement
, rAttrList
);
238 void XMLEmbeddedObjectImportContext::endFastElement(sal_Int32 nElement
)
240 if( !mxFastHandler
.is() )
243 mxFastHandler
->endFastElement( nElement
);
244 mxFastHandler
->endDocument();
248 Reference
< XModifiable2
> xModifiable2( xComp
, UNO_QUERY_THROW
);
249 xModifiable2
->enableSetModified();
250 xModifiable2
->setModified( true ); // trigger new replacement image generation
257 void XMLEmbeddedObjectImportContext::characters( const OUString
& rChars
)
259 if( mxFastHandler
.is() )
260 mxFastHandler
->characters( rChars
);
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */