LibreOffice Module xmloff (master)  1
xmltabi.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <com/sun/star/style/TabAlign.hpp>
21 #include <xmloff/xmltkmap.hxx>
22 #include <xmloff/nmspmap.hxx>
23 #include <xmloff/xmlnmspe.hxx>
24 #include <xmloff/xmlimp.hxx>
25 #include <com/sun/star/style/TabStop.hpp>
26 #include <xmloff/xmltoken.hxx>
27 #include <xmloff/xmluconv.hxx>
28 #include <xmltabi.hxx>
29 
30 
31 using namespace ::com::sun::star;
32 using namespace ::xmloff::token;
33 
34 namespace {
35 
37 {
38  XML_TOK_TABSTOP_POSITION,
39  XML_TOK_TABSTOP_TYPE,
40  XML_TOK_TABSTOP_CHAR,
41  XML_TOK_TABSTOP_LEADER_STYLE,
42  XML_TOK_TABSTOP_LEADER_TEXT
43 };
44 
45 }
46 
48 {
49  { XML_NAMESPACE_STYLE, XML_POSITION, XML_TOK_TABSTOP_POSITION },
50  { XML_NAMESPACE_STYLE, XML_TYPE, XML_TOK_TABSTOP_TYPE },
51  { XML_NAMESPACE_STYLE, XML_CHAR, XML_TOK_TABSTOP_CHAR },
52  { XML_NAMESPACE_STYLE, XML_LEADER_TEXT, XML_TOK_TABSTOP_LEADER_TEXT },
53  { XML_NAMESPACE_STYLE, XML_LEADER_STYLE, XML_TOK_TABSTOP_LEADER_STYLE },
55 };
56 
57 
59 {
60 private:
61  style::TabStop aTabStop;
62 
63 public:
64 
65  SvxXMLTabStopContext_Impl( SvXMLImport& rImport, sal_uInt16 nPrfx,
66  const OUString& rLName,
67  const uno::Reference< xml::sax::XAttributeList > & xAttrList );
68 
69  const style::TabStop& getTabStop() const { return aTabStop; }
70 };
71 
72 
74  SvXMLImport& rImport, sal_uInt16 nPrfx,
75  const OUString& rLName,
76  const uno::Reference< xml::sax::XAttributeList > & xAttrList )
77 : SvXMLImportContext( rImport, nPrfx, rLName )
78 {
79  aTabStop.Position = 0;
80  aTabStop.Alignment = style::TabAlign_LEFT;
81  aTabStop.DecimalChar = ',';
82  aTabStop.FillChar = ' ';
83  sal_Unicode cTextFillChar = 0;
84 
85  static const SvXMLTokenMap aTokenMap( aTabsAttributesAttrTokenMap );
86 
87  sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
88  for( sal_Int16 i=0; i < nAttrCount; i++ )
89  {
90  const OUString& rAttrName = xAttrList->getNameByIndex( i );
91  OUString aLocalName;
92  sal_uInt16 nPrefix =
94  &aLocalName );
95  const OUString& rValue = xAttrList->getValueByIndex( i );
96 
97  sal_Int32 nVal;
98  switch( aTokenMap.Get( nPrefix, aLocalName ) )
99  {
100  case XML_TOK_TABSTOP_POSITION:
102  nVal, rValue))
103  {
104  aTabStop.Position = nVal;
105  }
106  break;
107  case XML_TOK_TABSTOP_TYPE:
108  if( IsXMLToken( rValue, XML_LEFT ) )
109  {
110  aTabStop.Alignment = style::TabAlign_LEFT;
111  }
112  else if( IsXMLToken( rValue, XML_RIGHT ) )
113  {
114  aTabStop.Alignment = style::TabAlign_RIGHT;
115  }
116  else if( IsXMLToken( rValue, XML_CENTER ) )
117  {
118  aTabStop.Alignment = style::TabAlign_CENTER;
119  }
120  else if( IsXMLToken( rValue, XML_CHAR ) )
121  {
122  aTabStop.Alignment = style::TabAlign_DECIMAL;
123  }
124  else if( IsXMLToken( rValue, XML_DEFAULT ) )
125  {
126  aTabStop.Alignment = style::TabAlign_DEFAULT;
127  }
128  break;
129  case XML_TOK_TABSTOP_CHAR:
130  if( !rValue.isEmpty() )
131  aTabStop.DecimalChar = rValue[0];
132  break;
133  case XML_TOK_TABSTOP_LEADER_STYLE:
134  if( IsXMLToken( rValue, XML_NONE ) )
135  aTabStop.FillChar = ' ';
136  else if( IsXMLToken( rValue, XML_DOTTED ) )
137  aTabStop.FillChar = '.';
138  else
139  aTabStop.FillChar = '_';
140  break;
141  case XML_TOK_TABSTOP_LEADER_TEXT:
142  if( !rValue.isEmpty() )
143  cTextFillChar = rValue[0];
144  break;
145  }
146  }
147 
148  if( cTextFillChar != 0 && aTabStop.FillChar != ' ' )
149  aTabStop.FillChar = cTextFillChar;
150 }
151 
152 
154  SvXMLImport& rImport, sal_uInt16 nPrfx,
155  const OUString& rLName,
156  const XMLPropertyState& rProp,
157  ::std::vector< XMLPropertyState > &rProps )
158 : XMLElementPropertyContext( rImport, nPrfx, rLName, rProp, rProps )
159 {
160 }
161 
163  sal_uInt16 nPrefix,
164  const OUString& rLocalName,
165  const uno::Reference< xml::sax::XAttributeList > & xAttrList )
166 {
167  SvXMLImportContext *pContext = nullptr;
168 
169  if( XML_NAMESPACE_STYLE == nPrefix && IsXMLToken( rLocalName, XML_TAB_STOP ) )
170  {
171  // create new tabstop import context
172  const rtl::Reference<SvxXMLTabStopContext_Impl> xTabStopContext{
173  new SvxXMLTabStopContext_Impl( GetImport(), nPrefix, rLocalName,
174  xAttrList )};
175 
176  // add new tabstop to array of tabstops
177  if( !mpTabStops )
178  mpTabStops = std::make_unique<SvxXMLTabStopArray_Impl>();
179 
180  mpTabStops->push_back( xTabStopContext );
181 
182  pContext = xTabStopContext.get();
183  }
184 
185  return pContext;
186 }
187 
189 {
190  sal_uInt16 nCount = mpTabStops ? mpTabStops->size() : 0;
191  uno::Sequence< style::TabStop> aSeq( nCount );
192 
193  if( mpTabStops )
194  {
195  sal_uInt16 nNewCount = 0;
196 
197  style::TabStop* pTabStops = aSeq.getArray();
198  for( sal_uInt16 i=0; i < nCount; i++ )
199  {
200  SvxXMLTabStopContext_Impl *pTabStopContext = (*mpTabStops)[i].get();
201  const style::TabStop& rTabStop = pTabStopContext->getTabStop();
202  bool bDflt = style::TabAlign_DEFAULT == rTabStop.Alignment;
203  if( !bDflt || 0==i )
204  {
205  *pTabStops++ = pTabStopContext->getTabStop();
206  nNewCount++;
207  }
208  if( bDflt && 0==i )
209  break;
210  }
211 
212  if( nCount != nNewCount )
213  aSeq.realloc( nNewCount );
214  }
215  aProp.maValue <<= aSeq;
216 
217  SetInsert( true );
219 
220 }
221 
222 
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3422
constexpr sal_uInt16 XML_NAMESPACE_STYLE
Definition: xmlnmspe.hxx:30
static XMLTokenEnum const aTokenMap[]
Definition: Oasis2OOo.cxx:1145
static const SvXMLTokenMapEntry aTabsAttributesAttrTokenMap[]
Definition: xmltabi.cxx:47
SvXMLTokenMapAttrs
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
Definition: xmltabi.cxx:162
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:397
virtual void EndElement() override
EndElement is called before a context will be destructed, but after an elements context has been pars...
Definition: xmltabi.cxx:188
sal_uInt16 GetKeyByAttrName(const OUString &rAttrName, OUString *pPrefix, OUString *pLocalName, OUString *pNamespace) const
Definition: nmspmap.cxx:437
#define XML_TOKEN_MAP_END
Definition: xmltkmap.hxx:33
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
bool convertMeasureToCore(sal_Int32 &rValue, const OUString &rString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32) const
convert string to measure with meCoreMeasureUnit, using optional min and max values ...
Definition: xmluconv.cxx:171
sal_uInt16 sal_Unicode
int nCount
SvxXMLTabStopImportContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLName, const XMLPropertyState &rProp,::std::vector< XMLPropertyState > &rProps)
Definition: xmltabi.cxx:153
SvxXMLTabStopContext_Impl(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLName, const uno::Reference< xml::sax::XAttributeList > &xAttrList)
Definition: xmltabi.cxx:73
int i
sal_uInt16 Get(sal_uInt16 nPrefix, const OUString &rLName) const
Definition: xmltkmap.cxx:93
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
css::uno::Any maValue
Definition: maptype.hxx:107
std::unique_ptr< SvxXMLTabStopArray_Impl > mpTabStops
Definition: xmltabi.hxx:35
Handling of tokens in XML:
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:104
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlimp.hxx:399
Sequence< sal_Int8 > aSeq
style::TabStop aTabStop
Definition: xmltabi.cxx:61
const style::TabStop & getTabStop() const
Definition: xmltabi.cxx:69
virtual void EndElement() override
EndElement is called before a context will be destructed, but after an elements context has been pars...