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 <sal/log.hxx>
22 #include <xmloff/xmltkmap.hxx>
23 #include <xmloff/namespacemap.hxx>
24 #include <xmloff/xmlnamespace.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <com/sun/star/style/TabStop.hpp>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/xmluconv.hxx>
29 #include <xmltabi.hxx>
30 
31 
32 using namespace ::com::sun::star;
33 using namespace ::xmloff::token;
34 
35 namespace {
36 
38 {
39  XML_TOK_TABSTOP_POSITION,
40  XML_TOK_TABSTOP_TYPE,
41  XML_TOK_TABSTOP_CHAR,
42  XML_TOK_TABSTOP_LEADER_STYLE,
43  XML_TOK_TABSTOP_LEADER_TEXT
44 };
45 
46 }
47 
49 {
50  { XML_NAMESPACE_STYLE, XML_POSITION, XML_TOK_TABSTOP_POSITION },
51  { XML_NAMESPACE_STYLE, XML_TYPE, XML_TOK_TABSTOP_TYPE },
52  { XML_NAMESPACE_STYLE, XML_CHAR, XML_TOK_TABSTOP_CHAR },
53  { XML_NAMESPACE_STYLE, XML_LEADER_TEXT, XML_TOK_TABSTOP_LEADER_TEXT },
54  { XML_NAMESPACE_STYLE, XML_LEADER_STYLE, XML_TOK_TABSTOP_LEADER_STYLE },
56 };
57 
58 
60 {
61 private:
62  style::TabStop aTabStop;
63 
64 public:
65 
66  SvxXMLTabStopContext_Impl( SvXMLImport& rImport, sal_Int32 nElement,
67  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList );
68 
69  const style::TabStop& getTabStop() const { return aTabStop; }
70 };
71 
72 
74  SvXMLImport& rImport, sal_Int32 /*nElement*/,
75  const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
76 : SvXMLImportContext( rImport )
77 {
78  aTabStop.Position = 0;
79  aTabStop.Alignment = style::TabAlign_LEFT;
80  aTabStop.DecimalChar = ',';
81  aTabStop.FillChar = ' ';
82  sal_Unicode cTextFillChar = 0;
83 
84  for (auto &aIter : sax_fastparser::castToFastAttributeList(xAttrList))
85  {
86  const OUString sValue = aIter.toString();
87 
88  sal_Int32 nVal;
89  switch( aIter.getToken() )
90  {
92  if (GetImport().GetMM100UnitConverter().convertMeasureToCore(
93  nVal, sValue))
94  {
95  aTabStop.Position = nVal;
96  }
97  break;
98  case XML_ELEMENT(STYLE, XML_TYPE):
99  if( IsXMLToken( sValue, XML_LEFT ) )
100  {
101  aTabStop.Alignment = style::TabAlign_LEFT;
102  }
103  else if( IsXMLToken( sValue, XML_RIGHT ) )
104  {
105  aTabStop.Alignment = style::TabAlign_RIGHT;
106  }
107  else if( IsXMLToken( sValue, XML_CENTER ) )
108  {
109  aTabStop.Alignment = style::TabAlign_CENTER;
110  }
111  else if( IsXMLToken( sValue, XML_CHAR ) )
112  {
113  aTabStop.Alignment = style::TabAlign_DECIMAL;
114  }
115  else if( IsXMLToken( sValue, XML_DEFAULT ) )
116  {
117  aTabStop.Alignment = style::TabAlign_DEFAULT;
118  }
119  break;
120  case XML_ELEMENT(STYLE, XML_CHAR):
121  if( !sValue.isEmpty() )
122  aTabStop.DecimalChar = sValue[0];
123  break;
125  if( IsXMLToken( sValue, XML_NONE ) )
126  aTabStop.FillChar = ' ';
127  else if( IsXMLToken( sValue, XML_DOTTED ) )
128  aTabStop.FillChar = '.';
129  else
130  aTabStop.FillChar = '_';
131  break;
133  if( !sValue.isEmpty() )
134  cTextFillChar = sValue[0];
135  break;
136  default:
137  SAL_WARN("xmloff", "unknown attribute " << SvXMLImport::getPrefixAndNameFromToken(aIter.getToken()) << "=" << sValue);
138  }
139  }
140 
141  if( cTextFillChar != 0 && aTabStop.FillChar != ' ' )
142  aTabStop.FillChar = cTextFillChar;
143 }
144 
145 
147  SvXMLImport& rImport, sal_Int32 nElement,
148  const XMLPropertyState& rProp,
149  ::std::vector< XMLPropertyState > &rProps )
150 : XMLElementPropertyContext( rImport, nElement, rProp, rProps )
151 {
152 }
153 
154 css::uno::Reference< css::xml::sax::XFastContextHandler > SvxXMLTabStopImportContext::createFastChildContext(
155  sal_Int32 nElement,
156  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList)
157 {
158  if( nElement == XML_ELEMENT(STYLE, XML_TAB_STOP) )
159  {
160  // create new tabstop import context
161  const rtl::Reference<SvxXMLTabStopContext_Impl> xTabStopContext{
162  new SvxXMLTabStopContext_Impl( GetImport(), nElement, xAttrList )};
163 
164  // add new tabstop to array of tabstops
165  if( !mpTabStops )
166  mpTabStops = std::make_unique<SvxXMLTabStopArray_Impl>();
167 
168  mpTabStops->push_back( xTabStopContext );
169 
170  return xTabStopContext.get();
171  }
172  else
173  SAL_WARN("xmloff", "unknown element " << SvXMLImport::getPrefixAndNameFromToken(nElement));
174 
175  return nullptr;
176 }
177 
179 {
180  sal_uInt16 nCount = mpTabStops ? mpTabStops->size() : 0;
181  uno::Sequence< style::TabStop> aSeq( nCount );
182 
183  if( mpTabStops )
184  {
185  sal_uInt16 nNewCount = 0;
186 
187  style::TabStop* pTabStops = aSeq.getArray();
188  for( sal_uInt16 i=0; i < nCount; i++ )
189  {
190  SvxXMLTabStopContext_Impl *pTabStopContext = (*mpTabStops)[i].get();
191  const style::TabStop& rTabStop = pTabStopContext->getTabStop();
192  bool bDflt = style::TabAlign_DEFAULT == rTabStop.Alignment;
193  if( !bDflt || 0==i )
194  {
195  *pTabStops++ = pTabStopContext->getTabStop();
196  nNewCount++;
197  }
198  if( bDflt && 0==i )
199  break;
200  }
201 
202  if( nCount != nNewCount )
203  aSeq.realloc( nNewCount );
204  }
205  aProp.maValue <<= aSeq;
206 
207  SetInsert( true );
209 }
210 
211 
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
const SvXMLTokenMapEntry aTabsAttributesAttrTokenMap[]
Definition: xmltabi.cxx:48
SvXMLTokenMapAttrs
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
#define XML_TOKEN_MAP_END
Definition: xmltkmap.hxx:33
sal_uInt16 sal_Unicode
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
Definition: xmltabi.cxx:178
int nCount
static OUString getPrefixAndNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:2025
SvxXMLTabStopContext_Impl(SvXMLImport &rImport, sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList > &xAttrList)
Definition: xmltabi.cxx:73
int i
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
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:125
std::unique_ptr< SvxXMLTabStopArray_Impl > mpTabStops
Definition: xmltabi.hxx:35
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
Definition: xmltabi.cxx:154
SvxXMLTabStopImportContext(SvXMLImport &rImport, sal_Int32 nElement, const XMLPropertyState &rProp,::std::vector< XMLPropertyState > &rProps)
Definition: xmltabi.cxx:146
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:94
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:122
Sequence< sal_Int8 > aSeq
style::TabStop aTabStop
Definition: xmltabi.cxx:62
const style::TabStop & getTabStop() const
Definition: xmltabi.cxx:69
#define SAL_WARN(area, stream)
constexpr sal_uInt16 XML_NAMESPACE_STYLE