LibreOffice Module xmloff (master)  1
XMLReplacementImageContext.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 <com/sun/star/beans/XPropertySet.hpp>
21 #include <com/sun/star/graphic/XGraphic.hpp>
22 #include <osl/diagnose.h>
23 #include <xmloff/xmlimp.hxx>
24 #include <xmloff/xmltoken.hxx>
25 #include <xmloff/xmlnmspe.hxx>
26 #include <xmloff/nmspmap.hxx>
29 
30 using ::com::sun::star::uno::Reference;
31 using ::com::sun::star::uno::makeAny;
32 using namespace ::com::sun::star::xml::sax;
33 using namespace ::com::sun::star::beans;
34 using namespace css;
35 
37  SvXMLImport& rImport,
38  sal_uInt16 nPrfx, const OUString& rLName,
39  const Reference< XAttributeList > & rAttrList,
40  const Reference< XPropertySet > & rPropSet ) :
41  SvXMLImportContext( rImport, nPrfx, rLName ),
42  m_xPropSet( rPropSet )
43 {
46  const SvXMLTokenMap& rTokenMap =
47  xTxtImport->GetTextFrameAttrTokenMap();
48 
49  sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
50  for( sal_Int16 i=0; i < nAttrCount; i++ )
51  {
52  const OUString& rAttrName = rAttrList->getNameByIndex( i );
53  const OUString& rValue = rAttrList->getValueByIndex( i );
54 
55  OUString aLocalName;
56  sal_uInt16 nPrefix =
58  &aLocalName );
59  switch( rTokenMap.Get( nPrefix, aLocalName ) )
60  {
62  m_sHRef = rValue;
63  break;
64  }
65  }
66 }
67 
69 {
70 }
71 
73 {
74  OSL_ENSURE( !m_sHRef.isEmpty() || m_xBase64Stream.is(),
75  "neither URL nor base64 image data given" );
76  uno::Reference<graphic::XGraphic> xGraphic;
77 
78  try
79  {
80  if( !m_sHRef.isEmpty() )
81  {
82  xGraphic = GetImport().loadGraphicByURL(m_sHRef);
83  }
84  else if( m_xBase64Stream.is() )
85  {
87  m_xBase64Stream = nullptr;
88  }
89  }
90  catch (uno::Exception const &)
91  {}
92 
93  Reference < XPropertySetInfo > xPropSetInfo = m_xPropSet->getPropertySetInfo();
94 
95  if (xGraphic.is() && xPropSetInfo->hasPropertyByName("Graphic"))
96  {
97  m_xPropSet->setPropertyValue("Graphic", uno::makeAny(xGraphic));
98  }
99 }
100 
102  sal_uInt16 nPrefix,
103  const OUString& rLocalName,
104  const Reference< XAttributeList > & xAttrList )
105 {
106  SvXMLImportContext *pContext = nullptr;
107 
108  if( XML_NAMESPACE_OFFICE == nPrefix &&
109  IsXMLToken( rLocalName, ::xmloff::token::XML_BINARY_DATA ) &&
110  !m_xBase64Stream.is() )
111  {
113  if( m_xBase64Stream.is() )
114  pContext = new XMLBase64ImportContext( GetImport(), nPrefix,
115  rLocalName, xAttrList,
116  m_xBase64Stream );
117  }
118 
119  return pContext;
120 }
121 
122 
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3422
css::uno::Reference< css::graphic::XGraphic > loadGraphicFromBase64(css::uno::Reference< css::io::XOutputStream > const &rxOutputStream)
Definition: xmlimp.cxx:1378
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:397
css::uno::Reference< css::graphic::XGraphic > loadGraphicByURL(OUString const &rURL)
Definition: xmlimp.cxx:1356
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:594
sal_uInt16 GetKeyByAttrName(const OUString &rAttrName, OUString *pPrefix, OUString *pLocalName, OUString *pNamespace) const
Definition: nmspmap.cxx:437
XMLReplacementImageContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLName, const css::uno::Reference< css::xml::sax::XAttributeList > &rAttrList, const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
virtual void EndElement() override
EndElement is called before a context will be destructed, but after an elements context has been pars...
css::uno::Reference< css::io::XOutputStream > GetStreamForGraphicObjectURLFromBase64() const
Definition: xmlimp.cxx:1390
int i
sal_uInt16 Get(sal_uInt16 nPrefix, const OUString &rLName) const
Definition: xmltkmap.cxx:93
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
css::uno::Reference< css::io::XOutputStream > m_xBase64Stream
css::uno::Reference< css::beans::XPropertySet > m_xPropSet
Reference< XGraphic > xGraphic
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
Definition: xmlnmspe.hxx:29