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 
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>
37 
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;
45 
46 namespace {
47 
48 class XMLEmbeddedObjectImportContext_Impl : public SvXMLImportContext
49 {
50  css::uno::Reference< css::xml::sax::XFastDocumentHandler > mxFastHandler;
51 
52 public:
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 
72 XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
73  SvXMLImport& rImport,
74  const Reference< XFastDocumentHandler >& rHandler ) :
75  SvXMLImportContext( rImport ),
76  mxFastHandler( rHandler )
77 {
78  assert(mxFastHandler);
79 }
80 
81 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLEmbeddedObjectImportContext_Impl::createFastChildContext(
82  sal_Int32 ,
83  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
84 {
85  // we have no state, so avoid allocation cost, and just use a single instance
86  return this;
87 }
88 
89 void XMLEmbeddedObjectImportContext_Impl::startFastElement(
90  sal_Int32 nElement,
91  const Reference< XFastAttributeList >& xAttrList )
92 {
93  mxFastHandler->startFastElement( nElement, xAttrList );
94 }
95 
96 void XMLEmbeddedObjectImportContext_Impl::endFastElement(sal_Int32 nElement)
97 {
98  mxFastHandler->endFastElement( nElement );
99 }
100 
101 void XMLEmbeddedObjectImportContext_Impl::characters( const OUString& rChars )
102 {
103  mxFastHandler->characters( rChars );
104 }
105 
106 
107 void XMLEmbeddedObjectImportContext::SetComponent( Reference< XComponent > const & rComp )
108 {
109  if( !rComp.is() || sFilterService.isEmpty() )
110  return;
111 
112  Sequence<Any> aArgs( 0 );
113 
114  Reference< XComponentContext > xContext( GetImport().GetComponentContext() );
115 
116  Reference<XInterface> xFilter =
117  xContext->getServiceManager()->createInstanceWithArgumentsAndContext(sFilterService, aArgs, xContext);
118  SAL_WARN_IF(!xFilter, "xmloff", "could not create filter " << sFilterService);
119  if( !xFilter.is() )
120  return;
121 
122  assert(dynamic_cast<SvXMLImport*>(xFilter.get()));
123  SvXMLImport *pFastHandler = dynamic_cast<SvXMLImport*>(xFilter.get());
124  mxFastHandler = pFastHandler;
125 
126  try
127  {
128  Reference < XModifiable2 > xModifiable2( rComp, UNO_QUERY_THROW );
129  xModifiable2->disableSetModified();
130  }
131  catch( Exception& )
132  {
133  }
134 
135  Reference < XImporter > xImporter( mxFastHandler, UNO_QUERY );
136  xImporter->setTargetDocument( rComp );
137 
138  xComp = rComp; // keep ref to component only if there is a handler
139 }
140 
142  SvXMLImport& rImport, sal_Int32 nElement,
143  const Reference< XFastAttributeList >& xAttrList ) :
144  SvXMLImportContext( rImport )
145 {
147 
148  if( nElement == XML_ELEMENT(MATH, XML_MATH) )
149  {
151  aName = SvGlobalName(SO3_SM_CLASSID);
152  }
153  else if( nElement == XML_ELEMENT(OFFICE, XML_DOCUMENT) )
154  {
155  OUString sMime;
156 
157  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
158  {
159  switch (aIter.getToken())
160  {
162  sMime = aIter.toString();
163  break;
164  default:
165  XMLOFF_WARN_UNKNOWN("xmloff", aIter);
166  }
167  }
168 
169  OUString sClass;
170  static std::u16string_view const prefixes[] = {
171  u"application/vnd.oasis.openoffice.",
172  u"application/x-vnd.oasis.openoffice.",
173  u"application/vnd.oasis.opendocument.",
174  u"application/x-vnd.oasis.opendocument."};
175  for (auto const & p: prefixes)
176  {
177  if (sMime.startsWith(p, &sClass))
178  {
179  break;
180  }
181  }
182 
183  if( !sClass.isEmpty() )
184  {
185  static struct { XMLTokenEnum eClass; std::u16string_view sFilterService;
186  } const aServiceMap[] = {
187  { XML_TEXT, std::u16string_view(u"" XML_IMPORT_FILTER_WRITER) },
188  { XML_ONLINE_TEXT, std::u16string_view(u"" XML_IMPORT_FILTER_WRITER) },
189  { XML_SPREADSHEET, std::u16string_view(u"" XML_IMPORT_FILTER_CALC) },
190  { XML_DRAWING, std::u16string_view(u"" XML_IMPORT_FILTER_DRAW) },
191  { XML_GRAPHICS, std::u16string_view(u"" XML_IMPORT_FILTER_DRAW) },
192  { XML_PRESENTATION, std::u16string_view(u"" XML_IMPORT_FILTER_IMPRESS) },
193  { XML_CHART, std::u16string_view(u"" XML_IMPORT_FILTER_CHART) }};
194  for (auto const & entry: aServiceMap)
195  {
196  if( IsXMLToken( sClass, entry.eClass ) )
197  {
198  sFilterService = entry.sFilterService;
199 
200  switch( entry.eClass )
201  {
202  case XML_TEXT: aName = SvGlobalName(SO3_SW_CLASSID); break;
203  case XML_ONLINE_TEXT: aName = SvGlobalName(SO3_SWWEB_CLASSID); break;
204  case XML_SPREADSHEET: aName = SvGlobalName(SO3_SC_CLASSID); break;
205  case XML_DRAWING:
206  case XML_GRAPHICS:
207  case XML_IMAGE: aName = SvGlobalName(SO3_SDRAW_CLASSID); break;
208  case XML_PRESENTATION: aName = SvGlobalName(SO3_SIMPRESS_CLASSID); break;
209  case XML_CHART: aName = SvGlobalName(SO3_SCH_CLASSID); break;
210  default:
211  break;
212  }
213 
214  break;
215  }
216  }
217  }
218  }
219 
220  sCLSID = aName.GetHexName();
221 }
222 
224 {
225 }
226 
227 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLEmbeddedObjectImportContext::createFastChildContext(
228  sal_Int32 ,
229  const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
230 {
231  if( mxFastHandler.is() )
232  return new XMLEmbeddedObjectImportContext_Impl( GetImport(), mxFastHandler );
233  return nullptr;
234 }
235 
237  sal_Int32 nElement,
238  const Reference< XFastAttributeList >& rAttrList )
239 {
240  if( !mxFastHandler.is() )
241  return;
242 
243  mxFastHandler->startDocument();
244  mxFastHandler->startFastElement( nElement, rAttrList );
245 }
246 
248 {
249  if( !mxFastHandler.is() )
250  return;
251 
252  mxFastHandler->endFastElement( nElement );
253  mxFastHandler->endDocument();
254 
255  try
256  {
257  Reference < XModifiable2 > xModifiable2( xComp, UNO_QUERY_THROW );
258  xModifiable2->enableSetModified();
259  xModifiable2->setModified( true ); // trigger new replacement image generation
260  }
261  catch( Exception& )
262  {
263  }
264 }
265 
266 void XMLEmbeddedObjectImportContext::characters( const OUString& rChars )
267 {
268  if( mxFastHandler.is() )
269  mxFastHandler->characters( rChars );
270 }
271 
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
#define XML_IMPORT_FILTER_CALC
virtual void SAL_CALL startFastElement(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:45
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:56
#define SO3_SW_CLASSID
css::uno::Reference< css::xml::sax::XFastDocumentHandler > mxFastHandler
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3464
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
#define SO3_SM_CLASSID
#define SO3_SIMPRESS_CLASSID
XMLEmbeddedObjectImportContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
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 ...
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:110
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:49
#define XML_IMPORT_FILTER_IMPRESS
const XMLServiceMapEntry_Impl aServiceMap[]
Definition: xmlexp.cxx:138
#define XML_IMPORT_FILTER_DRAW
OUString GetHexName() const
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
MATH
float u
virtual void SAL_CALL characters(const OUString &rChars) override
This method is called for all characters that are contained in the current element.
#define SO3_SC_CLASSID
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:45
css::uno::Reference< css::lang::XComponent > xComp
#define SAL_WARN_IF(condition, area, stream)
void SetComponent(css::uno::Reference< css::lang::XComponent > const &rComp)
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
Handling of tokens in XML:
OUString aName
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
#define SO3_SCH_CLASSID
#define XML_IMPORT_FILTER_WRITER
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:96
#define XML_IMPORT_FILTER_MATH
void * p
#define SO3_SWWEB_CLASSID
#define XML_IMPORT_FILTER_CHART
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
#define SO3_SDRAW_CLASSID