LibreOffice Module xmloff (master) 1
XMLEmbeddedObjectImportContext.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <string_view>
23#include <tuple>
24
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>
32#include <xmloff/xmlimp.hxx>
34#include <xmloff/xmltoken.hxx>
37
38using namespace ::com::sun::star::uno;
39using namespace ::com::sun::star::util;
40using namespace ::com::sun::star::beans;
41using namespace ::com::sun::star::lang;
42using namespace ::com::sun::star::document;
43using namespace ::com::sun::star::xml::sax;
44using namespace ::xmloff::token;
45
46namespace {
47
48class XMLEmbeddedObjectImportContext_Impl : public SvXMLImportContext
49{
50 css::uno::Reference< css::xml::sax::XFastDocumentHandler > mxFastHandler;
51
52public:
53
54 XMLEmbeddedObjectImportContext_Impl( SvXMLImport& rImport,
55 const css::uno::Reference< css::xml::sax::XFastDocumentHandler >& rHandler );
56
57 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
58 sal_Int32 nElement,
59 const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
60
61 virtual void SAL_CALL startFastElement(
62 sal_Int32 nElement,
63 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override;
64
65 virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
66
67 virtual void SAL_CALL characters( const OUString& rChars ) override;
68};
69
70}
71
72XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
73 SvXMLImport& rImport,
74 const Reference< XFastDocumentHandler >& rHandler ) :
75 SvXMLImportContext( rImport ),
76 mxFastHandler( rHandler )
77{
78 assert(mxFastHandler);
79}
80
81css::uno::Reference< css::xml::sax::XFastContextHandler > XMLEmbeddedObjectImportContext_Impl::createFastChildContext(
82 sal_Int32 ,
83 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
84{
85 return new XMLEmbeddedObjectImportContext_Impl(GetImport(), mxFastHandler);
86}
87
88void XMLEmbeddedObjectImportContext_Impl::startFastElement(
89 sal_Int32 nElement,
90 const Reference< XFastAttributeList >& xAttrList )
91{
92 mxFastHandler->startFastElement( nElement, xAttrList );
93}
94
95void XMLEmbeddedObjectImportContext_Impl::endFastElement(sal_Int32 nElement)
96{
97 mxFastHandler->endFastElement( nElement );
98}
99
100void XMLEmbeddedObjectImportContext_Impl::characters( const OUString& rChars )
101{
102 mxFastHandler->characters( rChars );
103}
104
105
106void XMLEmbeddedObjectImportContext::SetComponent( Reference< XComponent > const & rComp )
107{
108 if( !rComp.is() || sFilterService.isEmpty() )
109 return;
110
111 Sequence<Any> aArgs( 0 );
112
113 Reference< XComponentContext > xContext( GetImport().GetComponentContext() );
114
115 Reference<XInterface> xFilter =
116 xContext->getServiceManager()->createInstanceWithArgumentsAndContext(sFilterService, aArgs, xContext);
117 SAL_WARN_IF(!xFilter, "xmloff", "could not create filter " << sFilterService);
118 if( !xFilter.is() )
119 return;
120
121 assert(dynamic_cast<SvXMLImport*>(xFilter.get()));
122 SvXMLImport *pFastHandler = dynamic_cast<SvXMLImport*>(xFilter.get());
123 mxFastHandler = pFastHandler;
124
125 try
126 {
127 Reference < XModifiable2 > xModifiable2( rComp, UNO_QUERY_THROW );
128 xModifiable2->disableSetModified();
129 }
130 catch( Exception& )
131 {
132 }
133
134 Reference < XImporter > xImporter( mxFastHandler, UNO_QUERY );
135 xImporter->setTargetDocument( rComp );
136
137 xComp = rComp; // keep ref to component only if there is a handler
138
139 // #i34042: copy namespace declarations
140 // We created a new instance of XMLImport, so we need to propagate the namespace
141 // declarations to it.
142 pFastHandler->GetNamespaceMap() = GetImport().GetNamespaceMap();
143}
144
146 SvXMLImport& rImport, sal_Int32 nElement,
147 const Reference< XFastAttributeList >& xAttrList ) :
148 SvXMLImportContext( rImport )
149{
151
152 if( nElement == XML_ELEMENT(MATH, XML_MATH) )
153 {
156 }
157 else if( nElement == XML_ELEMENT(OFFICE, XML_DOCUMENT) )
158 {
159 OUString sMime;
160
161 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
162 {
163 switch (aIter.getToken())
164 {
166 sMime = aIter.toString();
167 break;
168 default:
169 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
170 }
171 }
172
173 std::u16string_view sClass;
174 static std::u16string_view const prefixes[] = {
175 u"application/vnd.oasis.openoffice.",
176 u"application/x-vnd.oasis.openoffice.",
177 u"application/vnd.oasis.opendocument.",
178 u"application/x-vnd.oasis.opendocument."};
179 for (auto const & p: prefixes)
180 {
181 if (o3tl::starts_with(sMime, p, &sClass))
182 {
183 break;
184 }
185 }
186
187 if( !sClass.empty() )
188 {
189 static const std::tuple<XMLTokenEnum, OUString, SvGUID> aServiceMap[] = {
197 };
198 for (auto const& [eClass, sMatchingFilterService, rCLASSID] : aServiceMap)
199 {
200 if (IsXMLToken(sClass, eClass))
201 {
202 sFilterService = sMatchingFilterService;
203 aName = SvGlobalName(rCLASSID);
204 break;
205 }
206 }
207 }
208 }
209
210 sCLSID = aName.GetHexName();
211}
212
214{
215}
216
217css::uno::Reference< css::xml::sax::XFastContextHandler > XMLEmbeddedObjectImportContext::createFastChildContext(
218 sal_Int32 ,
219 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
220{
221 if( mxFastHandler.is() )
222 return new XMLEmbeddedObjectImportContext_Impl( GetImport(), mxFastHandler );
223 return nullptr;
224}
225
227 sal_Int32 nElement,
228 const Reference< XFastAttributeList >& rAttrList )
229{
230 if( !mxFastHandler.is() )
231 return;
232
233 mxFastHandler->startDocument();
234 mxFastHandler->startFastElement( nElement, rAttrList );
235}
236
238{
239 if( !mxFastHandler.is() )
240 return;
241
242 mxFastHandler->endFastElement( nElement );
243 mxFastHandler->endDocument();
244
245 try
246 {
247 Reference < XModifiable2 > xModifiable2( xComp, UNO_QUERY_THROW );
248 xModifiable2->enableSetModified();
249 xModifiable2->setModified( true ); // trigger new replacement image generation
250 }
251 catch( Exception& )
252 {
253 }
254}
255
256void XMLEmbeddedObjectImportContext::characters( const OUString& rChars )
257{
258 if( mxFastHandler.is() )
259 mxFastHandler->characters( rChars );
260}
261
262/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral XML_IMPORT_FILTER_CALC
constexpr OUStringLiteral XML_IMPORT_FILTER_DRAW
constexpr OUStringLiteral XML_IMPORT_FILTER_WRITER
constexpr OUStringLiteral XML_IMPORT_FILTER_CHART
constexpr OUStringLiteral XML_IMPORT_FILTER_IMPRESS
constexpr OUStringLiteral XML_IMPORT_FILTER_MATH
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
virtual void SAL_CALL startFastElement(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:45
virtual void SAL_CALL endFastElement(sal_Int32 Element) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
Definition: xmlictxt.cxx:40
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
friend class SvXMLImport
Definition: xmlictxt.hxx:49
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:59
virtual void SAL_CALL characters(const OUString &aChars) override
This method is called for all characters that are contained in the current element.
Definition: xmlictxt.cxx:70
virtual void SAL_CALL characters(const OUString &rChars) override
This method is called for all characters that are contained in the current element.
XMLEmbeddedObjectImportContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
void SetComponent(css::uno::Reference< css::lang::XComponent > const &rComp)
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
css::uno::Reference< css::xml::sax::XFastDocumentHandler > mxFastHandler
css::uno::Reference< css::lang::XComponent > xComp
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
#define SO3_SDRAW_CLASSID
#define SO3_SWWEB_CLASSID
#define SO3_SIMPRESS_CLASSID
#define SO3_SCH_CLASSID
#define SO3_SC_CLASSID
#define SO3_SW_CLASSID
#define SO3_SM_CLASSID
float u
MATH
OUString aName
void * p
#define SAL_WARN_IF(condition, area, stream)
@ Exception
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3597
const std::pair< OUString, OUString > aServiceMap[]
Definition: xmlexp.cxx:126
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97