Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerfilter / source / dmapper / TblStylePrHandler.cxx
blob13656e169ba7d09e1e6b8405e3fe5b411f4a3f74
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 "TblStylePrHandler.hxx"
21 #include "TagLogger.hxx"
22 #include "CellMarginHandler.hxx"
23 #include "PropertyMap.hxx"
24 #include "MeasureHandler.hxx"
25 #include <ooxml/resourceids.hxx>
26 #include <comphelper/sequence.hxx>
29 using namespace css;
31 namespace writerfilter::dmapper {
33 TblStylePrHandler::TblStylePrHandler( DomainMapper & rDMapper ) :
34 LoggedProperties("TblStylePrHandler"),
35 m_rDMapper( rDMapper ),
36 m_pTablePropsHandler(new TablePropertiesHandler()),
37 m_nType( TBL_STYLE_UNKNOWN ),
38 m_pProperties( new PropertyMap )
42 TblStylePrHandler::~TblStylePrHandler( )
46 OUString TblStylePrHandler::getTypeString() const
48 switch (m_nType)
50 case TBL_STYLE_WHOLETABLE: return "wholeTable";
51 case TBL_STYLE_FIRSTROW: return "firstRow";
52 case TBL_STYLE_LASTROW: return "lastRow";
53 case TBL_STYLE_FIRSTCOL: return "firstCol";
54 case TBL_STYLE_LASTCOL: return "lastCol";
55 case TBL_STYLE_BAND1VERT: return "band1Vert";
56 case TBL_STYLE_BAND2VERT: return "band2Vert";
57 case TBL_STYLE_BAND1HORZ: return "band1Horz";
58 case TBL_STYLE_BAND2HORZ: return "band2Horz";
59 case TBL_STYLE_NECELL: return "neCell";
60 case TBL_STYLE_NWCELL: return "nwCell";
61 case TBL_STYLE_SECELL: return "seCell";
62 case TBL_STYLE_SWCELL: return "swCell";
63 default: break;
65 return OUString();
68 void TblStylePrHandler::lcl_attribute(Id rName, Value & rVal)
71 switch ( rName )
73 case NS_ooxml::LN_CT_TblStyleOverrideType:
75 switch (rVal.getInt())
77 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_wholeTable:
78 m_nType = TBL_STYLE_WHOLETABLE;
79 break;
80 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_firstRow:
81 m_nType = TBL_STYLE_FIRSTROW;
82 break;
83 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_lastRow:
84 m_nType = TBL_STYLE_LASTROW;
85 break;
86 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_firstCol:
87 m_nType = TBL_STYLE_FIRSTCOL;
88 break;
89 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_lastCol:
90 m_nType = TBL_STYLE_LASTCOL;
91 break;
92 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_band1Vert:
93 m_nType = TBL_STYLE_BAND1VERT;
94 break;
95 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_band2Vert:
96 m_nType = TBL_STYLE_BAND2VERT;
97 break;
98 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_band1Horz:
99 m_nType = TBL_STYLE_BAND1HORZ;
100 break;
101 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_band2Horz:
102 m_nType = TBL_STYLE_BAND2HORZ;
103 break;
104 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_neCell:
105 m_nType = TBL_STYLE_NECELL;
106 break;
107 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_nwCell:
108 m_nType = TBL_STYLE_NWCELL;
109 break;
110 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_seCell:
111 m_nType = TBL_STYLE_SECELL;
112 break;
113 case NS_ooxml::LN_Value_ST_TblStyleOverrideType_swCell:
114 m_nType = TBL_STYLE_SWCELL;
115 break;
118 break;
122 void TblStylePrHandler::lcl_sprm(Sprm & rSprm)
124 #ifdef DBG_UTIL
125 TagLogger::getInstance().startElement("TblStylePrHandler.sprm");
126 TagLogger::getInstance().attribute("sprm", rSprm.toString());
127 #endif
129 switch ( rSprm.getId( ) )
131 case NS_ooxml::LN_CT_PPrBase:
132 case NS_ooxml::LN_EG_RPrBase:
133 case NS_ooxml::LN_CT_TblPrBase:
134 case NS_ooxml::LN_CT_TrPrBase:
135 case NS_ooxml::LN_CT_TcPrBase:
137 std::vector<beans::PropertyValue> aSavedGrabBag;
138 bool bGrabBag = rSprm.getId() == NS_ooxml::LN_CT_PPrBase ||
139 rSprm.getId() == NS_ooxml::LN_EG_RPrBase ||
140 rSprm.getId() == NS_ooxml::LN_CT_TblPrBase ||
141 rSprm.getId() == NS_ooxml::LN_CT_TrPrBase ||
142 rSprm.getId() == NS_ooxml::LN_CT_TcPrBase;
143 if (bGrabBag)
145 std::swap(aSavedGrabBag, m_aInteropGrabBag);
147 resolveSprmProps( rSprm );
148 if (bGrabBag)
150 if (rSprm.getId() == NS_ooxml::LN_CT_PPrBase)
151 aSavedGrabBag.push_back(getInteropGrabBag("pPr"));
152 else if (rSprm.getId() == NS_ooxml::LN_EG_RPrBase)
153 aSavedGrabBag.push_back(getInteropGrabBag("rPr"));
154 else if (rSprm.getId() == NS_ooxml::LN_CT_TblPrBase)
155 aSavedGrabBag.push_back(getInteropGrabBag("tblPr"));
156 else if (rSprm.getId() == NS_ooxml::LN_CT_TrPrBase)
157 aSavedGrabBag.push_back(getInteropGrabBag("trPr"));
158 else if (rSprm.getId() == NS_ooxml::LN_CT_TcPrBase)
159 aSavedGrabBag.push_back(getInteropGrabBag("tcPr"));
160 std::swap(m_aInteropGrabBag, aSavedGrabBag);
163 break;
164 case NS_ooxml::LN_CT_TrPrBase_tblHeader:
166 m_pProperties->Insert( PROP_HEADER_ROW_COUNT, uno::Any(sal_Int32(1)));
167 beans::PropertyValue aValue;
168 aValue.Name = "tblHeader";
169 aValue.Value <<= true;
170 m_aInteropGrabBag.push_back(aValue);
172 break;
173 case NS_ooxml::LN_CT_TblPrBase_tblInd:
175 //contains unit and value
176 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
177 if( pProperties )
179 MeasureHandlerPtr pMeasureHandler( new MeasureHandler );
180 pProperties->resolve(*pMeasureHandler);
181 TablePropertyMapPtr pPropMap( new TablePropertyMap );
182 pPropMap->setValue( TablePropertyMap::LEFT_MARGIN, pMeasureHandler->getMeasureValue() );
183 m_pProperties->Insert( PROP_LEFT_MARGIN, uno::Any(pMeasureHandler->getMeasureValue()) );
186 break;
187 case NS_ooxml::LN_CT_TblPrBase_tblCellMar:
189 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
190 if ( pProperties )
192 auto pCellMarginHandler = std::make_shared<CellMarginHandler>();
193 pCellMarginHandler->enableInteropGrabBag("tblCellMar");
194 pProperties->resolve( *pCellMarginHandler );
195 m_aInteropGrabBag.push_back(pCellMarginHandler->getInteropGrabBag());
197 if( pCellMarginHandler->m_bTopMarginValid )
198 m_pProperties->Insert( META_PROP_CELL_MAR_TOP, uno::Any(pCellMarginHandler->m_nTopMargin) );
199 if( pCellMarginHandler->m_bBottomMarginValid )
200 m_pProperties->Insert( META_PROP_CELL_MAR_BOTTOM, uno::Any(pCellMarginHandler->m_nBottomMargin) );
201 if( pCellMarginHandler->m_bLeftMarginValid )
202 m_pProperties->Insert( META_PROP_CELL_MAR_LEFT, uno::Any(pCellMarginHandler->m_nLeftMargin) );
203 if( pCellMarginHandler->m_bRightMarginValid )
204 m_pProperties->Insert( META_PROP_CELL_MAR_RIGHT, uno::Any(pCellMarginHandler->m_nRightMargin) );
207 break;
208 default:
209 // Tables specific properties have to handled here
210 m_pTablePropsHandler->SetProperties( m_pProperties );
211 m_pTablePropsHandler->SetInteropGrabBag(m_aInteropGrabBag);
212 bool bRet = m_pTablePropsHandler->sprm( rSprm );
214 if ( !bRet )
216 // The DomainMapper can handle some of the properties
217 m_rDMapper.PushStyleSheetProperties( m_pProperties, true );
218 // Just pass a non-empty string, the array will have a single element anyway.
219 m_rDMapper.enableInteropGrabBag("TblStylePrHandler");
220 m_rDMapper.sprm( rSprm );
221 uno::Sequence<beans::PropertyValue> aGrabBag = m_rDMapper.getInteropGrabBag().Value.get< uno::Sequence<beans::PropertyValue> >();
222 if (aGrabBag.hasElements())
223 m_aInteropGrabBag.push_back(aGrabBag[0]);
224 m_rDMapper.PopStyleSheetProperties( true );
228 #ifdef DBG_UTIL
229 TagLogger::getInstance().endElement();
230 #endif
233 void TblStylePrHandler::resolveSprmProps(Sprm & rSprm)
235 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
236 if( pProperties )
237 pProperties->resolve(*this);
240 void TblStylePrHandler::appendInteropGrabBag(const OUString& aKey, const OUString& aValue)
242 beans::PropertyValue aProperty;
243 aProperty.Name = aKey;
244 aProperty.Value <<= aValue;
245 m_aInteropGrabBag.push_back(aProperty);
248 beans::PropertyValue TblStylePrHandler::getInteropGrabBag(const OUString& aName)
250 beans::PropertyValue aRet;
251 aRet.Name = aName;
253 aRet.Value <<= comphelper::containerToSequence(m_aInteropGrabBag);
254 return aRet;
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */