Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / text / XMLTextNumRuleInfo.cxx
blob5f9a5e2b7a546017d249c646e434c1f7e7672e8e
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 .
21 #include <osl/diagnose.h>
22 #include <sal/log.hxx>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/beans/XPropertyState.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/container/XIndexReplace.hpp>
27 #include <com/sun/star/style/NumberingType.hpp>
28 #include <com/sun/star/text/XTextContent.hpp>
29 #include <com/sun/star/container/XNamed.hpp>
30 #include "XMLTextNumRuleInfo.hxx"
31 #include <xmloff/XMLTextListAutoStylePool.hxx>
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::container;
37 using namespace ::com::sun::star::style;
39 // Complete refactoring of the class and enhancement of the class for lists.
40 XMLTextNumRuleInfo::XMLTextNumRuleInfo()
41 : mbListIdIsDefault(false)
42 , mnListStartValue( -1 )
43 , mnListLevel( 0 )
44 , mbIsNumbered( false )
45 , mbIsRestart( false )
46 , mnListLevelStartValue( -1 )
47 , mbOutlineStyleAsNormalListStyle( false )
49 Reset();
52 // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
53 void XMLTextNumRuleInfo::Set(
54 const css::uno::Reference < css::text::XTextContent > & xTextContent,
55 const bool bOutlineStyleAsNormalListStyle,
56 const XMLTextListAutoStylePool& rListAutoPool,
57 const bool bExportTextNumberElement,
58 const bool bListIdIsDefault )
60 Reset();
61 // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
62 mbOutlineStyleAsNormalListStyle = bOutlineStyleAsNormalListStyle;
64 Reference< XPropertySet > xPropSet( xTextContent, UNO_QUERY );
65 Reference< XPropertySetInfo > xPropSetInfo = xPropSet->getPropertySetInfo();
67 // check if this paragraph supports a numbering
68 if( !xPropSetInfo->hasPropertyByName( "NumberingLevel" ) )
69 return;
71 if( xPropSet->getPropertyValue( "NumberingLevel" ) >>= mnListLevel )
73 if( xPropSetInfo->hasPropertyByName( "NumberingRules" ) )
75 xPropSet->getPropertyValue( "NumberingRules" ) >>= mxNumRules;
78 else
80 // in applications using the outliner we always have a numbering rule,
81 // so a void property no numbering
82 mnListLevel = 0;
85 // Assertion saving writer document (#i97312#)
86 if ( mxNumRules.is() && mxNumRules->getCount() < 1 )
88 SAL_WARN("xmloff",
89 "<XMLTextNumRuleInfo::Set(..)> - numbering rules instance does not contain any numbering rule" );
90 Reset();
91 return;
94 if ( mnListLevel < 0 )
96 SAL_WARN("xmloff",
97 "<XMLTextNumRuleInfo::Set(..)> - unexpected numbering level" );
98 Reset();
99 return;
102 // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
103 bool bSuppressListStyle( false );
104 if ( mxNumRules.is() )
106 if ( !mbOutlineStyleAsNormalListStyle )
108 Reference<XPropertySet> xNumRulesProps(mxNumRules, UNO_QUERY);
109 if ( xNumRulesProps.is() &&
110 xNumRulesProps->getPropertySetInfo()->
111 hasPropertyByName( "NumberingIsOutline" ) )
113 bool bIsOutline = false;
114 xNumRulesProps->getPropertyValue( "NumberingIsOutline" ) >>= bIsOutline;
115 bSuppressListStyle = bIsOutline;
120 if( mxNumRules.is() && !bSuppressListStyle )
122 // First try to find the numbering rules in the list auto style pool.
123 // If not found, the numbering rules instance has to be named.
124 msNumRulesName = rListAutoPool.Find( mxNumRules );
125 if ( msNumRulesName.isEmpty() )
127 Reference < XNamed > xNamed( mxNumRules, UNO_QUERY );
128 SAL_WARN_IF( !xNamed.is(), "xmloff",
129 "<XMLTextNumRuleInfo::Set(..)> - numbering rules instance have to be named. Serious defect." );
130 if( xNamed.is() )
132 msNumRulesName = xNamed->getName();
135 SAL_WARN_IF( msNumRulesName.isEmpty(), "xmloff",
136 "<XMLTextNumRuleInfo::Set(..)> - no name found for numbering rules instance. Serious defect." );
138 if( xPropSetInfo->hasPropertyByName( "ListId" ) )
140 xPropSet->getPropertyValue( "ListId" ) >>= msListId;
143 mbListIdIsDefault = bListIdIsDefault;
145 mbContinueingPreviousSubTree = false;
146 if( xPropSetInfo->hasPropertyByName( "ContinueingPreviousSubTree" ) )
148 xPropSet->getPropertyValue( "ContinueingPreviousSubTree" ) >>= mbContinueingPreviousSubTree;
151 mbIsNumbered = true;
152 if( xPropSetInfo->hasPropertyByName( "NumberingIsNumber" ) )
154 if( !(xPropSet->getPropertyValue( "NumberingIsNumber" ) >>= mbIsNumbered ) )
156 OSL_FAIL( "numbered paragraph without number info" );
157 mbIsNumbered = false;
161 if( mbIsNumbered )
163 if( xPropSetInfo->hasPropertyByName( "ParaIsNumberingRestart" ) )
165 xPropSet->getPropertyValue( "ParaIsNumberingRestart" ) >>= mbIsRestart;
167 if( xPropSetInfo->hasPropertyByName( "NumberingStartValue" ) )
169 xPropSet->getPropertyValue( "NumberingStartValue" ) >>= mnListStartValue;
173 OSL_ENSURE( mnListLevel < mxNumRules->getCount(), "wrong num rule level" );
174 if( mnListLevel >= mxNumRules->getCount() )
176 Reset();
177 return;
180 Sequence<PropertyValue> aProps;
181 mxNumRules->getByIndex( mnListLevel ) >>= aProps;
183 auto pProp = std::find_if(std::cbegin(aProps), std::cend(aProps),
184 [](const PropertyValue& rProp) { return rProp.Name == "StartWith"; });
185 if (pProp != std::cend(aProps))
187 pProp->Value >>= mnListLevelStartValue;
190 msListLabelString.clear();
191 if ( bExportTextNumberElement &&
192 xPropSetInfo->hasPropertyByName( "ListLabelString" ) )
194 xPropSet->getPropertyValue( "ListLabelString" ) >>= msListLabelString;
197 // paragraph's list level range is [0..9] representing list levels [1..10]
198 ++mnListLevel;
200 else
202 mnListLevel = 0;
206 bool XMLTextNumRuleInfo::BelongsToSameList( const XMLTextNumRuleInfo& rCmp ) const
208 bool bRet( true );
209 // Currently only the text documents support <ListId>.
210 if ( !rCmp.msListId.isEmpty() || !msListId.isEmpty() )
212 bRet = rCmp.msListId == msListId;
214 else
216 bRet = rCmp.msNumRulesName == msNumRulesName;
219 return bRet;
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */