Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / draw / layerimp.cxx
blobeeffae04e99748e72b13bd5ecf0c8f9cf8a12324
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 <sal/config.h>
22 #include <sal/log.hxx>
23 #include <comphelper/diagnose_ex.hxx>
24 #include <com/sun/star/drawing/XLayerManager.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/frame/XModel.hpp>
27 #include <com/sun/star/xml/sax/XAttributeList.hpp>
28 #include <com/sun/star/drawing/XLayerSupplier.hpp>
29 #include <xmloff/xmltoken.hxx>
30 #include <xmloff/xmlimp.hxx>
31 #include <xmloff/xmlnamespace.hxx>
32 #include <xmloff/namespacemap.hxx>
33 #include "layerimp.hxx"
36 #include <XMLStringBufferImportContext.hxx>
38 using namespace ::cppu;
39 using namespace ::xmloff::token;
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::xml;
42 using namespace ::com::sun::star::xml::sax;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::drawing;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::container;
49 namespace {
51 class SdXMLLayerContext : public SvXMLImportContext
53 public:
54 SdXMLLayerContext( SvXMLImport& rImport, const Reference< XFastAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager );
56 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
57 sal_Int32 nElement,
58 const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
59 virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
61 private:
62 css::uno::Reference< css::container::XNameAccess > mxLayerManager;
63 OUString msName;
64 OUStringBuffer sDescriptionBuffer;
65 OUStringBuffer sTitleBuffer;
66 OUString msDisplay;
67 OUString msProtected;
72 SdXMLLayerContext::SdXMLLayerContext( SvXMLImport& rImport, const Reference< XFastAttributeList >& xAttrList, const Reference< XNameAccess >& xLayerManager )
73 : SvXMLImportContext(rImport)
74 , mxLayerManager( xLayerManager )
76 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
78 OUString sValue = aIter.toString();
79 switch(aIter.getToken())
81 case XML_ELEMENT(DRAW, XML_NAME):
82 msName = sValue;
83 break;
84 case XML_ELEMENT(DRAW, XML_DISPLAY):
85 msDisplay = sValue;
86 break;
87 case XML_ELEMENT(DRAW, XML_PROTECTED):
88 msProtected = sValue;
89 break;
90 default:
91 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
97 css::uno::Reference< css::xml::sax::XFastContextHandler > SdXMLLayerContext::createFastChildContext(
98 sal_Int32 nElement,
99 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
101 if( nElement == XML_ELEMENT(SVG, XML_TITLE) )
103 return new XMLStringBufferImportContext( GetImport(), sTitleBuffer);
105 else if( nElement == XML_ELEMENT(SVG, XML_DESC) )
107 return new XMLStringBufferImportContext( GetImport(), sDescriptionBuffer);
109 else
110 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
111 return nullptr;
114 void SdXMLLayerContext::endFastElement(sal_Int32 )
116 SAL_WARN_IF( msName.isEmpty(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), draw:layer element without draw:name!" );
117 if( msName.isEmpty() )
118 return;
122 Reference< XPropertySet > xLayer;
124 if( mxLayerManager->hasByName( msName ) )
126 mxLayerManager->getByName( msName ) >>= xLayer;
127 SAL_WARN_IF( !xLayer.is(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), failed to get existing XLayer!" );
129 else
131 Reference< XLayerManager > xLayerManager( mxLayerManager, UNO_QUERY );
132 if( xLayerManager.is() )
133 xLayer = xLayerManager->insertNewByIndex( xLayerManager->getCount() );
134 SAL_WARN_IF( !xLayer.is(), "xmloff", "xmloff::SdXMLLayerContext::EndElement(), failed to create new XLayer!" );
136 if( xLayer.is() )
137 xLayer->setPropertyValue("Name", Any( msName ) );
140 if( xLayer.is() )
142 xLayer->setPropertyValue("Title", Any( sTitleBuffer.makeStringAndClear() ) );
143 xLayer->setPropertyValue("Description", Any( sDescriptionBuffer.makeStringAndClear() ) );
144 bool bIsVisible( true );
145 bool bIsPrintable( true );
146 if ( !msDisplay.isEmpty() )
148 bIsVisible = (msDisplay == "always") || (msDisplay == "screen");
149 bIsPrintable = (msDisplay == "always") || (msDisplay == "printer");
151 xLayer->setPropertyValue("IsVisible", Any( bIsVisible ) );
152 xLayer->setPropertyValue("IsPrintable", Any( bIsPrintable ) );
153 bool bIsLocked( false );
154 if ( !msProtected.isEmpty() )
155 bIsLocked = (msProtected == "true");
156 xLayer->setPropertyValue("IsLocked", Any( bIsLocked ) );
158 // tdf#129898 repair layer "DrawnInSlideshow", which was wrongly written
159 // in LO 6.2 to 6.4. It should always have ODF defaults.
160 if (msName == "DrawnInSlideshow")
162 xLayer->setPropertyValue("IsVisible", Any(true));
163 xLayer->setPropertyValue("IsPrintable", Any(true));
164 xLayer->setPropertyValue("IsLocked", Any(false));
168 catch( Exception& )
170 TOOLS_WARN_EXCEPTION("xmloff.draw", "");
175 SdXMLLayerSetContext::SdXMLLayerSetContext( SvXMLImport& rImport )
176 : SvXMLImportContext(rImport)
178 Reference< XLayerSupplier > xLayerSupplier( rImport.GetModel(), UNO_QUERY );
179 SAL_WARN_IF( !xLayerSupplier.is(), "xmloff", "xmloff::SdXMLLayerSetContext::SdXMLLayerSetContext(), XModel is not supporting XLayerSupplier!" );
180 if( xLayerSupplier.is() )
181 mxLayerManager = xLayerSupplier->getLayerManager();
184 SdXMLLayerSetContext::~SdXMLLayerSetContext()
188 css::uno::Reference< css::xml::sax::XFastContextHandler > SdXMLLayerSetContext::createFastChildContext(
189 sal_Int32 /*nElement*/,
190 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
192 return new SdXMLLayerContext( GetImport(), xAttrList, mxLayerManager );
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */