Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / chart / XMLSymbolTypePropertyHdl.cxx
blobdd24c134052d7a862202bc4bc8eda8520c626d99
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 "XMLSymbolTypePropertyHdl.hxx"
21 #include <xmloff/xmluconv.hxx>
22 #include <rtl/ustrbuf.hxx>
24 using namespace ::xmloff::token;
26 namespace
28 struct SvXMLSignedEnumMapEntry
30 ::xmloff::token::XMLTokenEnum eToken;
31 sal_Int32 nValue;
34 const SvXMLSignedEnumMapEntry aXMLChartSymbolTypeEnumMap[] =
36 { XML_NONE, -3 },
37 { XML_AUTOMATIC, -2 },
38 { XML_IMAGE, -1 },
39 { XML_TOKEN_INVALID, 0 }
42 const SvXMLSignedEnumMapEntry aXMLChartSymbolNameMap[] =
44 { XML_GRADIENTSTYLE_SQUARE, 0 }, // "square"
45 { XML_DIAMOND, 1 },
46 { XML_ARROW_DOWN, 2 },
47 { XML_ARROW_UP, 3 },
48 { XML_ARROW_RIGHT, 4 },
49 { XML_ARROW_LEFT, 5 },
50 { XML_BOW_TIE, 6 },
51 { XML_HOURGLASS, 7 },
52 { XML_CIRCLE, 8 },
53 { XML_STAR, 9 },
54 { XML_X, 10 },
55 { XML_PLUS, 11 },
56 { XML_ASTERISK, 12 },
57 { XML_HORIZONTAL_BAR, 13 },
58 { XML_VERTICAL_BAR, 14 },
59 { XML_TOKEN_INVALID, 0 }
62 bool lcl_convertEnum(
63 OUStringBuffer & rBuffer,
64 sal_Int32 nValue,
65 const SvXMLSignedEnumMapEntry *pMap )
67 enum XMLTokenEnum eTok = XML_TOKEN_INVALID;
69 while( pMap->eToken != XML_TOKEN_INVALID )
71 if( pMap->nValue == nValue )
73 eTok = pMap->eToken;
74 break;
76 pMap++;
79 if( eTok != XML_TOKEN_INVALID )
80 rBuffer.append( GetXMLToken(eTok) );
82 return (eTok != XML_TOKEN_INVALID);
85 bool lcl_convertEnum(
86 sal_Int32 & rEnum,
87 std::u16string_view rValue,
88 const SvXMLSignedEnumMapEntry *pMap )
90 while( pMap->eToken != XML_TOKEN_INVALID )
92 if( IsXMLToken( rValue, pMap->eToken ) )
94 rEnum = pMap->nValue;
95 return true;
97 pMap++;
99 return false;
102 } // anonymous namespace
104 using namespace com::sun::star;
106 XMLSymbolTypePropertyHdl::XMLSymbolTypePropertyHdl( bool bIsNamedSymbol )
107 : m_bIsNamedSymbol( bIsNamedSymbol )
110 XMLSymbolTypePropertyHdl::~XMLSymbolTypePropertyHdl()
113 bool XMLSymbolTypePropertyHdl::importXML( const OUString& rStrImpValue,
114 uno::Any& rValue, const SvXMLUnitConverter& /*rUnitConverter*/ ) const
116 bool bResult = false;
118 if( m_bIsNamedSymbol )
120 sal_Int32 nValue = -3; // NONE
121 bResult = lcl_convertEnum( nValue, rStrImpValue, aXMLChartSymbolNameMap );
122 rValue <<= nValue;
124 else
126 sal_Int32 nValue = -3; // NONE
127 bResult = lcl_convertEnum( nValue, rStrImpValue, aXMLChartSymbolTypeEnumMap );
128 rValue <<= nValue;
131 return bResult;
134 bool XMLSymbolTypePropertyHdl::exportXML( OUString& rStrExpValue,
135 const uno::Any& rValue, const SvXMLUnitConverter& /*rUnitConverter*/ ) const
137 bool bResult = false;
139 sal_Int32 nType = -3; // NONE
140 rValue >>= nType;
142 if( m_bIsNamedSymbol )
144 OUStringBuffer aBuf;
145 bResult = lcl_convertEnum( aBuf, nType, aXMLChartSymbolNameMap );
146 rStrExpValue = aBuf.makeStringAndClear();
148 else
150 if( nType < 0 )
152 OUStringBuffer aBuf;
153 bResult = lcl_convertEnum( aBuf, nType, aXMLChartSymbolTypeEnumMap );
154 rStrExpValue = aBuf.makeStringAndClear();
156 else
158 bResult = true;
159 rStrExpValue = GetXMLToken( XML_NAMED_SYMBOL );
163 return bResult;
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */