Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / text / XMLTextColumnsExport.cxx
blobec80f2ed90cc9da5de49231f651fe883b98e815e
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 <o3tl/any.hxx>
23 #include <rtl/ustrbuf.hxx>
26 #include <com/sun/star/text/XTextColumns.hpp>
27 #include <com/sun/star/text/ColumnSeparatorStyle.hpp>
28 #include <com/sun/star/text/TextColumn.hpp>
29 #include <com/sun/star/style/VerticalAlignment.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <sax/tools/converter.hxx>
34 #include <xmloff/xmltoken.hxx>
35 #include <xmloff/xmlnamespace.hxx>
36 #include <xmloff/xmluconv.hxx>
37 #include <xmloff/xmlexp.hxx>
38 #include <XMLTextColumnsExport.hxx>
40 using namespace ::com::sun::star::style;
41 using namespace ::com::sun::star::text;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::beans;
44 using namespace ::xmloff::token;
47 constexpr OUStringLiteral gsSeparatorLineIsOn(u"SeparatorLineIsOn");
48 constexpr OUStringLiteral gsSeparatorLineWidth(u"SeparatorLineWidth");
49 constexpr OUStringLiteral gsSeparatorLineColor(u"SeparatorLineColor");
50 constexpr OUStringLiteral gsSeparatorLineRelativeHeight(u"SeparatorLineRelativeHeight");
51 constexpr OUStringLiteral gsSeparatorLineVerticalAlignment(u"SeparatorLineVerticalAlignment");
52 constexpr OUStringLiteral gsIsAutomatic(u"IsAutomatic");
53 constexpr OUStringLiteral gsAutomaticDistance(u"AutomaticDistance");
54 constexpr OUStringLiteral gsSeparatorLineStyle(u"SeparatorLineStyle");
56 XMLTextColumnsExport::XMLTextColumnsExport( SvXMLExport& rExp ) :
57 rExport( rExp )
61 void XMLTextColumnsExport::exportXML( const Any& rAny )
63 Reference < XTextColumns > xColumns;
64 rAny >>= xColumns;
65 if (!xColumns)
66 return;
68 const Sequence < TextColumn > aColumns = xColumns->getColumns();
69 sal_Int32 nCount = aColumns.getLength();
71 OUStringBuffer sValue;
72 GetExport().AddAttribute( XML_NAMESPACE_FO, XML_COLUMN_COUNT,
73 OUString::number(nCount ? nCount : 1) );
75 // handle 'automatic' columns
76 Reference < XPropertySet > xPropSet( xColumns, UNO_QUERY );
77 if( xPropSet.is() )
79 Any aAny = xPropSet->getPropertyValue( gsIsAutomatic );
80 if ( *o3tl::doAccess<bool>(aAny) )
82 aAny = xPropSet->getPropertyValue( gsAutomaticDistance );
83 sal_Int32 nDistance = 0;
84 aAny >>= nDistance;
85 OUStringBuffer aBuffer;
86 GetExport().GetMM100UnitConverter().convertMeasureToXML(
87 aBuffer, nDistance );
88 GetExport().AddAttribute( XML_NAMESPACE_FO,
89 XML_COLUMN_GAP,
90 aBuffer.makeStringAndClear() );
94 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE, XML_COLUMNS,
95 true, true );
97 if( xPropSet.is() )
99 Any aAny = xPropSet->getPropertyValue( gsSeparatorLineIsOn );
100 if( *o3tl::doAccess<bool>(aAny) )
102 // style:width
103 aAny = xPropSet->getPropertyValue( gsSeparatorLineWidth );
104 sal_Int32 nWidth = 0;
105 aAny >>= nWidth;
106 GetExport().GetMM100UnitConverter().convertMeasureToXML( sValue,
107 nWidth );
108 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_WIDTH,
109 sValue.makeStringAndClear() );
111 // style:color
112 aAny = xPropSet->getPropertyValue( gsSeparatorLineColor );
113 sal_Int32 nColor = 0;
114 aAny >>= nColor;
115 ::sax::Converter::convertColor( sValue, nColor );
116 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_COLOR,
117 sValue.makeStringAndClear() );
119 // style:height
120 aAny = xPropSet->getPropertyValue( gsSeparatorLineRelativeHeight );
121 sal_Int32 nHeight = 0;
122 aAny >>= nHeight;
123 ::sax::Converter::convertPercent( sValue, nHeight );
124 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_HEIGHT,
125 sValue.makeStringAndClear() );
127 // style::style
128 aAny = xPropSet->getPropertyValue( gsSeparatorLineStyle );
129 sal_Int16 nStyle = css::text::ColumnSeparatorStyle::NONE;
130 aAny >>= nStyle;
132 enum XMLTokenEnum eStr = XML_TOKEN_INVALID;
133 switch ( nStyle )
135 case css::text::ColumnSeparatorStyle::NONE: eStr = XML_NONE; break;
136 case css::text::ColumnSeparatorStyle::SOLID: eStr = XML_SOLID; break;
137 case css::text::ColumnSeparatorStyle::DOTTED: eStr = XML_DOTTED; break;
138 case css::text::ColumnSeparatorStyle::DASHED: eStr = XML_DASHED; break;
139 default:
140 break;
142 if ( eStr != XML_TOKEN_INVALID )
143 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_STYLE, eStr );
145 // style:vertical-align
146 aAny = xPropSet->getPropertyValue( gsSeparatorLineVerticalAlignment );
147 VerticalAlignment eVertAlign;
148 aAny >>= eVertAlign;
150 eStr = XML_TOKEN_INVALID;
151 switch( eVertAlign )
153 // case VerticalAlignment_TOP: eStr = XML_TOP;
154 case VerticalAlignment_MIDDLE: eStr = XML_MIDDLE; break;
155 case VerticalAlignment_BOTTOM: eStr = XML_BOTTOM; break;
156 default:
157 break;
160 if( eStr != XML_TOKEN_INVALID)
161 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
162 XML_VERTICAL_ALIGN, eStr );
164 // style:column-sep
165 SvXMLElementExport aElement( GetExport(), XML_NAMESPACE_STYLE,
166 XML_COLUMN_SEP,
167 true, true );
171 for (const auto& rColumn : aColumns)
173 // style:rel-width
174 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_REL_WIDTH,
175 OUString::number(rColumn.Width) + "*" );
177 // fo:margin-left
178 GetExport().GetMM100UnitConverter().convertMeasureToXML( sValue,
179 rColumn.LeftMargin );
180 GetExport().AddAttribute( XML_NAMESPACE_FO, XML_START_INDENT,
181 sValue.makeStringAndClear() );
183 // fo:margin-right
184 GetExport().GetMM100UnitConverter().convertMeasureToXML( sValue,
185 rColumn.RightMargin );
186 GetExport().AddAttribute( XML_NAMESPACE_FO, XML_END_INDENT,
187 sValue.makeStringAndClear() );
189 // style:column
190 SvXMLElementExport aElement( GetExport(), XML_NAMESPACE_STYLE, XML_COLUMN,
191 true, true );
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */