1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include <com/sun/star/text/XTextColumns.hpp>
27 #include <com/sun/star/text/TextColumn.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/style/VerticalAlignment.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <xmloff/xmltkmap.hxx>
32 #include <xmloff/xmluconv.hxx>
33 #include <xmloff/nmspmap.hxx>
34 #include "xmloff/xmlnmspe.hxx"
35 #include <xmloff/xmlimp.hxx>
36 #include <xmloff/xmltoken.hxx>
37 #include "XMLTextColumnsContext.hxx"
38 #define _SVSTDARR_USHORTS
39 #include <svl/svstdarr.hxx>
40
41 using ::rtl::OUString;
42 using ::rtl::OUStringBuffer;
43
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::text;
48 using namespace ::com::sun::star::style;
49 using namespace ::com::sun::star::beans;
50 using namespace ::xmloff::token;
51
52 enum SvXMLTokenMapAttrs
53 {
54 XML_TOK_COLUMN_WIDTH,
55 XML_TOK_COLUMN_MARGIN_LEFT,
56 XML_TOK_COLUMN_MARGIN_RIGHT,
57 XML_TOK_COLUMN_END=XML_TOK_UNKNOWN
58 };
59
60 enum SvXMLSepTokenMapAttrs
61 {
62 XML_TOK_COLUMN_SEP_WIDTH,
63 XML_TOK_COLUMN_SEP_HEIGHT,
64 XML_TOK_COLUMN_SEP_COLOR,
65 XML_TOK_COLUMN_SEP_ALIGN,
66 XML_TOK_COLUMN_SEP_END=XML_TOK_UNKNOWN
67 };
68
69 static __FAR_DATA SvXMLTokenMapEntry aColAttrTokenMap[] =
70 {
71 { XML_NAMESPACE_STYLE, XML_REL_WIDTH, XML_TOK_COLUMN_WIDTH },
72 { XML_NAMESPACE_FO, XML_START_INDENT, XML_TOK_COLUMN_MARGIN_LEFT },
73 { XML_NAMESPACE_FO, XML_END_INDENT, XML_TOK_COLUMN_MARGIN_RIGHT },
74 XML_TOKEN_MAP_END
75 };
76
77 static __FAR_DATA SvXMLTokenMapEntry aColSepAttrTokenMap[] =
78 {
79 { XML_NAMESPACE_STYLE, XML_WIDTH, XML_TOK_COLUMN_SEP_WIDTH },
80 { XML_NAMESPACE_STYLE, XML_COLOR, XML_TOK_COLUMN_SEP_COLOR },
81 { XML_NAMESPACE_STYLE, XML_HEIGHT, XML_TOK_COLUMN_SEP_HEIGHT },
82 { XML_NAMESPACE_STYLE, XML_VERTICAL_ALIGN, XML_TOK_COLUMN_SEP_ALIGN },
83 XML_TOKEN_MAP_END
84 };
85
86 SvXMLEnumMapEntry __READONLY_DATA pXML_Sep_Align_Enum[] =
87 {
88 { XML_TOP, VerticalAlignment_TOP },
89 { XML_MIDDLE, VerticalAlignment_MIDDLE },
90 { XML_BOTTOM, VerticalAlignment_BOTTOM },
91 { XML_TOKEN_INVALID, 0 }
92 };
93
94 class XMLTextColumnContext_Impl: public SvXMLImportContext
95 {
96 text::TextColumn aColumn;
97
98 public:
99 TYPEINFO();
100
101 XMLTextColumnContext_Impl( SvXMLImport& rImport, sal_uInt16 nPrfx,
102 const OUString& rLName,
103 const uno::Reference<
104 xml::sax::XAttributeList > & xAttrList,
105 const SvXMLTokenMap& rTokenMap );
106
107 virtual ~XMLTextColumnContext_Impl();
108
getTextColumn()109 text::TextColumn& getTextColumn() { return aColumn; }
110 };
111
112 TYPEINIT1( XMLTextColumnContext_Impl, SvXMLImportContext );
113
XMLTextColumnContext_Impl(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const uno::Reference<xml::sax::XAttributeList> & xAttrList,const SvXMLTokenMap & rTokenMap)114 XMLTextColumnContext_Impl::XMLTextColumnContext_Impl(
115 SvXMLImport& rImport, sal_uInt16 nPrfx,
116 const OUString& rLName,
117 const uno::Reference<
118 xml::sax::XAttributeList > & xAttrList,
119 const SvXMLTokenMap& rTokenMap ) :
120 SvXMLImportContext( rImport, nPrfx, rLName )
121 {
122 aColumn.Width = 0;
123 aColumn.LeftMargin = 0;
124 aColumn.RightMargin = 0;
125
126 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
127 for( sal_Int16 i=0; i < nAttrCount; i++ )
128 {
129 const OUString& rAttrName = xAttrList->getNameByIndex( i );
130 OUString aLocalName;
131 sal_uInt16 nPrefix =
132 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
133 &aLocalName );
134 const OUString& rValue = xAttrList->getValueByIndex( i );
135
136 sal_Int32 nVal;
137 switch( rTokenMap.Get( nPrefix, aLocalName ) )
138 {
139 case XML_TOK_COLUMN_WIDTH:
140 {
141 sal_Int32 nPos = rValue.indexOf( (sal_Unicode)'*' );
142 if( nPos != -1 && nPos+1 == rValue.getLength() )
143 {
144 OUString sTmp( rValue.copy( 0, nPos ) );
145 if( GetImport().GetMM100UnitConverter().
146 convertNumber( nVal, sTmp, 0, USHRT_MAX ) )
147 aColumn.Width = nVal;
148 }
149 }
150 break;
151 case XML_TOK_COLUMN_MARGIN_LEFT:
152 if( GetImport().GetMM100UnitConverter().
153 convertMeasure( nVal, rValue ) )
154 aColumn.LeftMargin = nVal;
155 break;
156 case XML_TOK_COLUMN_MARGIN_RIGHT:
157
158 if( GetImport().GetMM100UnitConverter().
159 convertMeasure( nVal, rValue ) )
160 aColumn.RightMargin = nVal;
161 break;
162 default:
163 break;
164 }
165 }
166 }
167
~XMLTextColumnContext_Impl()168 XMLTextColumnContext_Impl::~XMLTextColumnContext_Impl()
169 {
170 }
171
172 // --------------------------------------------------------------------------
173
174 class XMLTextColumnSepContext_Impl: public SvXMLImportContext
175 {
176 sal_Int32 nWidth;
177 sal_Int32 nColor;
178 sal_Int8 nHeight;
179 VerticalAlignment eVertAlign;
180
181
182 public:
183 TYPEINFO();
184
185 XMLTextColumnSepContext_Impl( SvXMLImport& rImport, sal_uInt16 nPrfx,
186 const OUString& rLName,
187 const uno::Reference<
188 xml::sax::XAttributeList > & xAttrList,
189 const SvXMLTokenMap& rTokenMap );
190
191 virtual ~XMLTextColumnSepContext_Impl();
192
GetWidth() const193 sal_Int32 GetWidth() const { return nWidth; }
GetColor() const194 sal_Int32 GetColor() const { return nColor; }
GetHeight() const195 sal_Int8 GetHeight() const { return nHeight; }
GetVertAlign() const196 VerticalAlignment GetVertAlign() const { return eVertAlign; }
197 };
198
199
200 TYPEINIT1( XMLTextColumnSepContext_Impl, SvXMLImportContext );
201
XMLTextColumnSepContext_Impl(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const uno::Reference<xml::sax::XAttributeList> & xAttrList,const SvXMLTokenMap & rTokenMap)202 XMLTextColumnSepContext_Impl::XMLTextColumnSepContext_Impl(
203 SvXMLImport& rImport, sal_uInt16 nPrfx,
204 const OUString& rLName,
205 const uno::Reference<
206 xml::sax::XAttributeList > & xAttrList,
207 const SvXMLTokenMap& rTokenMap ) :
208 SvXMLImportContext( rImport, nPrfx, rLName ),
209 nWidth( 2 ),
210 nColor( 0 ),
211 nHeight( 100 ),
212 eVertAlign( VerticalAlignment_TOP )
213 {
214 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
215 for( sal_Int16 i=0; i < nAttrCount; i++ )
216 {
217 const OUString& rAttrName = xAttrList->getNameByIndex( i );
218 OUString aLocalName;
219 sal_uInt16 nPrefix =
220 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
221 &aLocalName );
222 const OUString& rValue = xAttrList->getValueByIndex( i );
223
224 sal_Int32 nVal;
225 switch( rTokenMap.Get( nPrefix, aLocalName ) )
226 {
227 case XML_TOK_COLUMN_SEP_WIDTH:
228 if( GetImport().GetMM100UnitConverter().
229 convertMeasure( nVal, rValue ) )
230 nWidth = nVal;
231 break;
232 case XML_TOK_COLUMN_SEP_HEIGHT:
233 if( GetImport().GetMM100UnitConverter().
234 convertPercent( nVal, rValue ) &&
235 nVal >=1 && nVal <= 100 )
236 nHeight = (sal_Int8)nVal;
237 break;
238 case XML_TOK_COLUMN_SEP_COLOR:
239 {
240 Color aColor;
241 if( GetImport().GetMM100UnitConverter().
242 convertColor( aColor, rValue ) )
243 nColor = (sal_Int32)aColor.GetColor();
244 }
245 break;
246 case XML_TOK_COLUMN_SEP_ALIGN:
247 {
248 sal_uInt16 nAlign;
249 if( GetImport().GetMM100UnitConverter().
250 convertEnum( nAlign, rValue,
251 pXML_Sep_Align_Enum ) )
252 eVertAlign = (VerticalAlignment)nAlign;
253 }
254 break;
255 }
256 }
257 }
258
~XMLTextColumnSepContext_Impl()259 XMLTextColumnSepContext_Impl::~XMLTextColumnSepContext_Impl()
260 {
261 }
262
263 // --------------------------------------------------------------------------
264
265 typedef XMLTextColumnContext_Impl *XMLTextColumnContext_ImplPtr;
266 SV_DECL_PTRARR( XMLTextColumnsArray_Impl, XMLTextColumnContext_ImplPtr, 5, 5 )
267
268 TYPEINIT1( XMLTextColumnsContext, XMLElementPropertyContext );
269
XMLTextColumnsContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const Reference<xml::sax::XAttributeList> & xAttrList,const XMLPropertyState & rProp,::std::vector<XMLPropertyState> & rProps)270 XMLTextColumnsContext::XMLTextColumnsContext(
271 SvXMLImport& rImport, sal_uInt16 nPrfx,
272 const OUString& rLName,
273 const Reference< xml::sax::XAttributeList >&
274 xAttrList,
275 const XMLPropertyState& rProp,
276 ::std::vector< XMLPropertyState > &rProps )
277 : XMLElementPropertyContext( rImport, nPrfx, rLName, rProp, rProps )
278 , sSeparatorLineIsOn(RTL_CONSTASCII_USTRINGPARAM("SeparatorLineIsOn"))
279 , sSeparatorLineWidth(RTL_CONSTASCII_USTRINGPARAM("SeparatorLineWidth"))
280 , sSeparatorLineColor(RTL_CONSTASCII_USTRINGPARAM("SeparatorLineColor"))
281 , sSeparatorLineRelativeHeight(RTL_CONSTASCII_USTRINGPARAM("SeparatorLineRelativeHeight"))
282 , sSeparatorLineVerticalAlignment(RTL_CONSTASCII_USTRINGPARAM("SeparatorLineVerticalAlignment"))
283 , sIsAutomatic(RTL_CONSTASCII_USTRINGPARAM("IsAutomatic"))
284 , sAutomaticDistance(RTL_CONSTASCII_USTRINGPARAM("AutomaticDistance"))
285 , pColumns( 0 )
286 , pColumnSep( 0 )
287 , pColumnAttrTokenMap( new SvXMLTokenMap(aColAttrTokenMap) )
288 , pColumnSepAttrTokenMap( new SvXMLTokenMap(aColSepAttrTokenMap) )
289 , nCount( 0 )
290 , bAutomatic( sal_False )
291 , nAutomaticDistance( 0 )
292 {
293 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
294 sal_Int32 nVal;
295 for( sal_Int16 i=0; i < nAttrCount; i++ )
296 {
297 const OUString& rAttrName = xAttrList->getNameByIndex( i );
298 OUString aLocalName;
299 sal_uInt16 nPrefix =
300 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
301 &aLocalName );
302 const OUString& rValue = xAttrList->getValueByIndex( i );
303 if( XML_NAMESPACE_FO == nPrefix )
304 {
305 if( IsXMLToken( aLocalName, XML_COLUMN_COUNT ) &&
306 GetImport().GetMM100UnitConverter().
307 convertNumber( nVal, rValue, 0, SHRT_MAX ) )
308 {
309 nCount = (sal_Int16)nVal;
310 }
311 else if( IsXMLToken( aLocalName, XML_COLUMN_GAP ) )
312 {
313 bAutomatic = GetImport().GetMM100UnitConverter().
314 convertMeasure( nAutomaticDistance, rValue );
315 }
316 }
317 }
318 }
319
~XMLTextColumnsContext()320 XMLTextColumnsContext::~XMLTextColumnsContext()
321 {
322 if( pColumns )
323 {
324 sal_uInt16 nColCount = pColumns->Count();
325 while( nColCount )
326 {
327 nColCount--;
328 XMLTextColumnContext_Impl *pColumn = (*pColumns)[nColCount];
329 pColumns->Remove( nColCount, 1 );
330 pColumn->ReleaseRef();
331 }
332 }
333 if( pColumnSep )
334 pColumnSep->ReleaseRef();
335
336 delete pColumns;
337 delete pColumnAttrTokenMap;
338 delete pColumnSepAttrTokenMap;
339 }
340
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const uno::Reference<xml::sax::XAttributeList> & xAttrList)341 SvXMLImportContext *XMLTextColumnsContext::CreateChildContext(
342 sal_uInt16 nPrefix,
343 const OUString& rLocalName,
344 const uno::Reference< xml::sax::XAttributeList > & xAttrList )
345 {
346 SvXMLImportContext *pContext = 0;
347
348 if( XML_NAMESPACE_STYLE == nPrefix &&
349 IsXMLToken( rLocalName, XML_COLUMN ) )
350 {
351 XMLTextColumnContext_Impl *pColumn =
352 new XMLTextColumnContext_Impl( GetImport(), nPrefix, rLocalName,
353 xAttrList, *pColumnAttrTokenMap );
354
355 // add new tabstop to array of tabstops
356 if( !pColumns )
357 pColumns = new XMLTextColumnsArray_Impl;
358
359 pColumns->Insert( pColumn, pColumns->Count() );
360 pColumn->AddRef();
361
362 pContext = pColumn;
363 }
364 else if( XML_NAMESPACE_STYLE == nPrefix &&
365 IsXMLToken( rLocalName, XML_COLUMN_SEP ) )
366 {
367 pColumnSep =
368 new XMLTextColumnSepContext_Impl( GetImport(), nPrefix, rLocalName,
369 xAttrList, *pColumnSepAttrTokenMap );
370 pColumnSep->AddRef();
371
372 pContext = pColumnSep;
373 }
374 else
375 {
376 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
377 }
378
379 return pContext;
380 }
381
EndElement()382 void XMLTextColumnsContext::EndElement( )
383 {
384 Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
385 if( !xFactory.is() )
386 return;
387
388 Reference<XInterface> xIfc = xFactory->createInstance(
389 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.TextColumns")));
390 if( !xIfc.is() )
391 return;
392
393 Reference< XTextColumns > xColumns( xIfc, UNO_QUERY );
394 if ( 0 == nCount )
395 {
396 // zero columns = no columns -> 1 column
397 xColumns->setColumnCount( 1 );
398 }
399 else if( !bAutomatic && pColumns &&
400 pColumns->Count() == (sal_uInt16)nCount )
401 {
402 // if we have column descriptions, one per column, and we don't use
403 // automatic width, then set the column widths
404
405 sal_Int32 nRelWidth = 0;
406 sal_uInt16 nColumnsWithWidth = 0;
407 sal_Int16 i;
408
409 for( i = 0; i < nCount; i++ )
410 {
411 const TextColumn& rColumn =
412 (*pColumns)[(sal_uInt16)i]->getTextColumn();
413 if( rColumn.Width > 0 )
414 {
415 nRelWidth += rColumn.Width;
416 nColumnsWithWidth++;
417 }
418 }
419 if( nColumnsWithWidth < nCount )
420 {
421 sal_Int32 nColWidth = 0==nRelWidth
422 ? USHRT_MAX / nCount
423 : nRelWidth / nColumnsWithWidth;
424
425 for( i=0; i < nCount; i++ )
426 {
427 TextColumn& rColumn =
428 (*pColumns)[(sal_uInt16)i]->getTextColumn();
429 if( rColumn.Width == 0 )
430 {
431 rColumn.Width = nColWidth;
432 nRelWidth += rColumn.Width;
433 if( 0 == --nColumnsWithWidth )
434 break;
435 }
436 }
437 }
438
439 Sequence< TextColumn > aColumns( (sal_Int32)nCount );
440 TextColumn *pTextColumns = aColumns.getArray();
441 for( i=0; i < nCount; i++ )
442 *pTextColumns++ = (*pColumns)[(sal_uInt16)i]->getTextColumn();
443
444 xColumns->setColumns( aColumns );
445 }
446 else
447 {
448 // only set column count (and let the columns be distributed
449 // automatically)
450
451 xColumns->setColumnCount( nCount );
452 }
453
454 Reference < XPropertySet > xPropSet( xColumns, UNO_QUERY );
455 if( xPropSet.is() )
456 {
457 Any aAny;
458 sal_Bool bOn = pColumnSep != 0;
459
460 aAny.setValue( &bOn, ::getBooleanCppuType() );
461 xPropSet->setPropertyValue( sSeparatorLineIsOn, aAny );
462
463 if( pColumnSep )
464 {
465 if( pColumnSep->GetWidth() )
466 {
467 aAny <<= pColumnSep->GetWidth();
468 xPropSet->setPropertyValue( sSeparatorLineWidth, aAny );
469 }
470 if( pColumnSep->GetHeight() )
471 {
472 aAny <<= pColumnSep->GetHeight();
473 xPropSet->setPropertyValue( sSeparatorLineRelativeHeight,
474 aAny );
475 }
476
477
478 aAny <<= pColumnSep->GetColor();
479 xPropSet->setPropertyValue( sSeparatorLineColor, aAny );
480
481
482 aAny <<= pColumnSep->GetVertAlign();
483 xPropSet->setPropertyValue( sSeparatorLineVerticalAlignment, aAny );
484 }
485
486 // handle 'automatic columns': column distance
487 if( bAutomatic )
488 {
489 aAny <<= nAutomaticDistance;
490 xPropSet->setPropertyValue( sAutomaticDistance, aAny );
491 }
492 }
493
494 aProp.maValue <<= xColumns;
495
496 SetInsert( sal_True );
497 XMLElementPropertyContext::EndElement();
498
499 }
500