Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / draw / XMLReplacementImageContext.cxx
blob5321d55788b68b10ee3fc40a583edae04fa3ef5d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <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/xmlnamespace.hxx>
26 #include <xmloff/namespacemap.hxx>
27 #include <xmloff/XMLBase64ImportContext.hxx>
28 #include <XMLReplacementImageContext.hxx>
29 #include <sal/log.hxx>
31 using ::com::sun::star::uno::Reference;
32 using namespace ::com::sun::star::xml::sax;
33 using namespace ::com::sun::star::beans;
34 using namespace css;
35 using namespace ::xmloff::token;
37 XMLReplacementImageContext::XMLReplacementImageContext(
38 SvXMLImport& rImport,
39 sal_Int32 /*nElement*/,
40 const Reference< XFastAttributeList > & rAttrList,
41 const Reference< XPropertySet > & rPropSet ) :
42 SvXMLImportContext( rImport ),
43 m_xPropSet( rPropSet )
45 m_sHRef = rAttrList->getOptionalValue(XML_ELEMENT(XLINK, XML_HREF));
48 XMLReplacementImageContext::~XMLReplacementImageContext()
52 void XMLReplacementImageContext::endFastElement(sal_Int32 )
54 OSL_ENSURE( !m_sHRef.isEmpty() || m_xBase64Stream.is(),
55 "neither URL nor base64 image data given" );
56 uno::Reference<graphic::XGraphic> xGraphic;
58 try
60 if( !m_sHRef.isEmpty() )
62 xGraphic = GetImport().loadGraphicByURL(m_sHRef);
64 else if( m_xBase64Stream.is() )
66 xGraphic = GetImport().loadGraphicFromBase64(m_xBase64Stream);
67 m_xBase64Stream = nullptr;
70 catch (uno::Exception const &)
73 Reference < XPropertySetInfo > xPropSetInfo = m_xPropSet->getPropertySetInfo();
75 if (xGraphic.is() && xPropSetInfo->hasPropertyByName("Graphic"))
77 m_xPropSet->setPropertyValue("Graphic", uno::Any(xGraphic));
81 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLReplacementImageContext::createFastChildContext(
82 sal_Int32 nElement,
83 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
85 if( nElement == XML_ELEMENT(OFFICE, XML_BINARY_DATA) &&
86 !m_xBase64Stream.is() )
88 m_xBase64Stream = GetImport().GetStreamForGraphicObjectURLFromBase64();
89 if( m_xBase64Stream.is() )
90 return new XMLBase64ImportContext( GetImport(),
91 m_xBase64Stream );
94 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
95 return nullptr;
99 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */