LibreOffice Module reportdesign (master)  1
xmlStyleImport.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 #include "xmlStyleImport.hxx"
20 #include <xmloff/maptype.hxx>
21 #include <xmloff/namespacemap.hxx>
22 #include <xmloff/xmlimppr.hxx>
23 #include <xmloff/txtimppr.hxx>
24 #include <xmloff/families.hxx>
25 #include <xmloff/xmlnumfi.hxx>
26 #include <xmloff/xmltoken.hxx>
27 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
28 #include <com/sun/star/container/XNameContainer.hpp>
29 #include <xmloff/xmluconv.hxx>
31 #include "xmlfilter.hxx"
32 #include "xmlHelper.hxx"
33 #include <osl/diagnose.h>
34 
35 namespace rptxml
36 {
37 
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::xml::sax;
41 using namespace ::com::sun::star::style;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::container;
44 using namespace xmloff::token;
45 
46 namespace {
47 
48 class OSpecialHandleXMLImportPropertyMapper : public SvXMLImportPropertyMapper
49 {
50 public:
51  OSpecialHandleXMLImportPropertyMapper(const rtl::Reference< XMLPropertySetMapper >& rMapper,SvXMLImport& _rImport) : SvXMLImportPropertyMapper(rMapper ,_rImport)
52  {
53  }
55  virtual bool handleSpecialItem(
56  XMLPropertyState& /*rProperty*/,
57  ::std::vector< XMLPropertyState >& /*rProperties*/,
58  const OUString& /*rValue*/,
59  const SvXMLUnitConverter& /*rUnitConverter*/,
60  const SvXMLNamespaceMap& /*rNamespaceMap*/ ) const override
61  {
62  // nothing to do here
63  return true;
64  }
65 };
66 
67 }
68 
70  SvXMLStylesContext& rStyles, XmlStyleFamily nFamily ) :
71  XMLPropStyleContext( rImport, rStyles, nFamily, false/*bDefaultStyle*/ ),
72  pStyles(&rStyles),
73  m_nNumberFormat(-1),
74  m_rImport(rImport)
75 {
76 
77 }
78 
79 
81 {
82 
83 }
84 
85 
87 {
88  if ( !IsDefaultStyle() )
89  {
90  if ( GetFamily() == XmlStyleFamily::TABLE_CELL )
91  {
92  if ((m_nNumberFormat == -1) && !m_sDataStyleName.isEmpty())
93  {
95  XmlStyleFamily::DATA_STYLE, m_sDataStyleName)));
96  if ( !pStyle )
97  {
98  OReportStylesContext* pMyStyles = dynamic_cast< OReportStylesContext *>(m_rImport.GetAutoStyles());
99  if ( pMyStyles )
100  pStyle = const_cast<SvXMLNumFormatContext*>(dynamic_cast< const SvXMLNumFormatContext *>(pMyStyles->
101  FindStyleChildContext(XmlStyleFamily::DATA_STYLE, m_sDataStyleName, true)));
102  else {
103  OSL_FAIL("not possible to get style");
104  }
105  }
106  if ( pStyle )
107  {
108  m_nNumberFormat = pStyle->GetKey();
110  }
111  }
112  }
113  }
115 }
116 
118 {
119 }
120 
121 
122 void OControlStyleContext::AddProperty(const sal_Int16 nContextID, const uno::Any& rValue)
123 {
124  sal_Int32 nIndex(static_cast<OReportStylesContext *>(pStyles)->GetIndex(nContextID));
125  OSL_ENSURE(nIndex != -1, "Property not found in Map");
126  XMLPropertyState aPropState(nIndex, rValue);
127  GetProperties().push_back(aPropState); // has to be inserted in a sort order later
128 }
129 
130 void OControlStyleContext::SetAttribute( sal_Int32 nElement,
131  const OUString& rValue )
132 {
133  switch(nElement & TOKEN_MASK)
134  {
135  case XML_DATA_STYLE_NAME:
136  m_sDataStyleName = rValue;
137  break;
139  break;
140  default:
141  XMLPropStyleContext::SetAttribute( nElement, rValue );
142  }
143 }
144 
145 
146 constexpr OUStringLiteral g_sTableStyleFamilyName( u"" XML_STYLE_FAMILY_TABLE_TABLE_STYLES_NAME );
147 constexpr OUStringLiteral g_sColumnStyleFamilyName( u"" XML_STYLE_FAMILY_TABLE_COLUMN_STYLES_NAME );
148 constexpr OUStringLiteral g_sRowStyleFamilyName( u"" XML_STYLE_FAMILY_TABLE_ROW_STYLES_NAME );
149 constexpr OUStringLiteral g_sCellStyleFamilyName( u"" XML_STYLE_FAMILY_TABLE_CELL_STYLES_NAME );
150 
152  const bool bTempAutoStyles ) :
153  SvXMLStylesContext( rImport ),
154  m_rImport(rImport),
155  m_nNumberFormatIndex(-1),
156  bAutoStyles(bTempAutoStyles)
157 {
158 
159 }
160 
161 
163 {
164 
165 }
166 
167 
169 {
170  if (bAutoStyles)
171  GetImport().GetTextImport()->SetAutoStyles( this );
172  else
174 }
175 
176 
179  XmlStyleFamily nFamily ) const
180 {
182 
183  if (!xMapper.is())
184  {
185  ORptFilter& rImport = GetOwnImport();
186  switch( nFamily )
187  {
188  case XmlStyleFamily::TABLE_CELL:
189  {
190  if( !m_xCellImpPropMapper.is() )
191  {
193  new XMLTextImportPropertyMapper/*OSpecialHandleXMLImportPropertyMapper*/( rImport.GetCellStylesPropertySetMapper(), m_rImport );
194 
196  }
197  xMapper = m_xCellImpPropMapper;
198  }
199  break;
200  case XmlStyleFamily::TABLE_COLUMN:
201  {
202  if( !m_xColumnImpPropMapper.is() )
205 
206  xMapper = m_xColumnImpPropMapper;
207  }
208  break;
209  case XmlStyleFamily::TABLE_ROW:
210  {
211  if( !m_xRowImpPropMapper.is() )
212  m_xRowImpPropMapper =new OSpecialHandleXMLImportPropertyMapper( rImport.GetRowStylesPropertySetMapper(), m_rImport );
213  xMapper = m_xRowImpPropMapper;
214  }
215  break;
216  case XmlStyleFamily::TABLE_TABLE:
217  {
218  if( !m_xTableImpPropMapper.is() )
219  {
220  rtl::Reference < XMLPropertyHandlerFactory> xFac = new ::xmloff::OControlPropertyHandlerFactory();
222  }
223  xMapper = m_xTableImpPropMapper;
224  }
225  break;
226  default:
227  break;
228  }
229  }
230 
231  return xMapper;
232 }
233 
235  XmlStyleFamily nFamily, sal_Int32 /*nElement*/,
236  const uno::Reference< xml::sax::XFastAttributeList > & /*xAttrList*/ )
237 {
238  switch( nFamily )
239  {
240  case XmlStyleFamily::SD_GRAPHICS_ID:
241  // There are no writer specific defaults for graphic styles!
242  return new XMLGraphicsDefaultStyle( GetImport(), *this );
243  default:
244  return nullptr;
245  }
246 }
247 
249  XmlStyleFamily nFamily, sal_Int32 nElement,
250  const Reference< xml::sax::XFastAttributeList > & xAttrList )
251 {
253  xAttrList );
254  if (!pStyle)
255  {
256  switch( nFamily )
257  {
258  case XmlStyleFamily::TABLE_TABLE:
259  case XmlStyleFamily::TABLE_COLUMN:
260  case XmlStyleFamily::TABLE_ROW:
261  case XmlStyleFamily::TABLE_CELL:
262  pStyle = new OControlStyleContext( GetOwnImport(), *this, nFamily );
263  break;
264  default:
265  OSL_FAIL("OReportStylesContext::CreateStyleStyleChildContext: Unknown style family. Please check.");
266  break;
267  }
268  }
269 
270  return pStyle;
271 }
272 
273 Reference < XNameContainer >
275 {
276  Reference < XNameContainer > xStyles(SvXMLStylesContext::GetStylesContainer(nFamily));
277  if (!xStyles.is())
278  {
279  OUString sName;
280  switch( nFamily )
281  {
282  case XmlStyleFamily::TABLE_TABLE:
283  {
284  if( m_xTableStyles.is() )
285  xStyles.set(m_xTableStyles);
286  else
287  sName = "TableStyles";
288  }
289  break;
290  case XmlStyleFamily::TABLE_CELL:
291  {
292  if( m_xCellStyles.is() )
293  xStyles.set(m_xCellStyles);
294  else
295  sName = "CellStyles";
296  }
297  break;
298  case XmlStyleFamily::TABLE_COLUMN:
299  {
300  if( m_xColumnStyles.is() )
301  xStyles.set(m_xColumnStyles);
302  else
303  sName = "ColumnStyles";
304  }
305  break;
306  case XmlStyleFamily::TABLE_ROW:
307  {
308  if( m_xRowStyles.is() )
309  xStyles.set(m_xRowStyles);
310  else
311  sName = "RowStyles";
312  }
313  break;
314  case XmlStyleFamily::SD_GRAPHICS_ID:
315  xStyles = const_cast<SvXMLImport *>(&GetImport())->GetTextImport()->GetFrameStyles();
316  break;
317  default:
318  OSL_FAIL("OReportStylesContext::CreateStyleStyleChildContext: Unknown style family. Please check.");
319  break;
320  }
321  if( !xStyles.is() && !sName.isEmpty() && GetOwnImport().GetModel().is() )
322  {
323  Reference< XStyleFamiliesSupplier > xFamiliesSupp(
324  GetOwnImport().GetModel(), UNO_QUERY );
325  if (xFamiliesSupp.is())
326  {
327  Reference< XNameAccess > xFamilies(xFamiliesSupp->getStyleFamilies());
328 
329  xStyles.set(xFamilies->getByName( sName ), uno::UNO_QUERY);
330  switch( nFamily )
331  {
332  case XmlStyleFamily::TABLE_TABLE:
333  m_xTableStyles.set(xStyles);
334  break;
335  case XmlStyleFamily::TABLE_CELL:
336  m_xCellStyles.set(xStyles);
337  break;
338  case XmlStyleFamily::TABLE_COLUMN:
339  m_xColumnStyles.set(xStyles);
340  break;
341  case XmlStyleFamily::TABLE_ROW:
342  m_xRowStyles.set(xStyles);
343  break;
344  default:
345  break;
346  }
347  }
348  }
349  }
350 
351  return xStyles;
352 }
353 
354 
356 {
358  if (sServiceName.isEmpty())
359  {
360  switch( nFamily )
361  {
362  case XmlStyleFamily::TABLE_TABLE:
363  sServiceName = g_sTableStyleFamilyName;
364  break;
365  case XmlStyleFamily::TABLE_COLUMN:
366  sServiceName = g_sColumnStyleFamilyName;
367  break;
368  case XmlStyleFamily::TABLE_ROW:
369  sServiceName = g_sRowStyleFamilyName;
370  break;
371  case XmlStyleFamily::TABLE_CELL:
372  sServiceName = g_sCellStyleFamilyName;
373  break;
374  default:
375  break;
376  }
377  }
378  return sServiceName;
379 }
380 
381 
382 sal_Int32 OReportStylesContext::GetIndex(const sal_Int16 nContextID)
383 {
384  if ( nContextID == CTF_RPT_NUMBERFORMAT )
385  {
386  if (m_nNumberFormatIndex == -1)
388  GetImportPropertyMapper(XmlStyleFamily::TABLE_CELL)->getPropertySetMapper()->FindEntryIndex(nContextID);
389  return m_nNumberFormatIndex;
390  }
391  return -1;
392 }
393 
394 
395 } // rptxml
396 
397 
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XmlStyleFamily GetFamily() const
virtual css::uno::Reference< css::container::XNameContainer > GetStylesContainer(XmlStyleFamily nFamily) const override
rtl::Reference< SvXMLImportPropertyMapper > m_xCellImpPropMapper
sal_Int32 nIndex
virtual OUString GetServiceName(XmlStyleFamily nFamily) const override
const rtl::Reference< XMLPropertySetMapper > & GetRowStylesPropertySetMapper() const
Definition: xmlfilter.hxx:106
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
const ::std::vector< XMLPropertyState > & GetProperties() const
SvXMLImport & GetImport()
XML_MASTER_PAGE_NAME
rtl::Reference< SvXMLImportPropertyMapper > m_xColumnImpPropMapper
const rtl::Reference< XMLPropertySetMapper > & GetColumnStylesPropertySetMapper() const
Definition: xmlfilter.hxx:103
bool IsDefaultStyle() const
OReportStylesContext(const OReportStylesContext &)=delete
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
constexpr OUStringLiteral g_sCellStyleFamilyName(u""XML_STYLE_FAMILY_TABLE_CELL_STYLES_NAME)
constexpr OUStringLiteral g_sColumnStyleFamilyName(u""XML_STYLE_FAMILY_TABLE_COLUMN_STYLES_NAME)
XmlStyleFamily
constexpr OUStringLiteral g_sRowStyleFamilyName(u""XML_STYLE_FAMILY_TABLE_ROW_STYLES_NAME)
ORptFilter & GetOwnImport() const
css::uno::Reference< css::container::XNameContainer > m_xCellStyles
constexpr OUStringLiteral sServiceName
const char * sName
SvXMLStylesContext * GetStyles()
css::uno::Reference< css::container::XNameContainer > m_xTableStyles
XML_DATA_STYLE_NAME
SvXMLStylesContext * pStyles
virtual rtl::Reference< SvXMLImportPropertyMapper > GetImportPropertyMapper(XmlStyleFamily nFamily) const
sal_Int32 GetIndex(const sal_Int16 nContextID)
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
virtual void SetDefaults() override
void CopyStylesToDoc(bool bOverwrite, bool bFinish=true)
rtl::Reference< SvXMLImportPropertyMapper > m_xRowImpPropMapper
constexpr OUStringLiteral g_sTableStyleFamilyName(u""XML_STYLE_FAMILY_TABLE_TABLE_STYLES_NAME)
OControlStyleContext(const OControlStyleContext &)=delete
virtual OUString GetServiceName(XmlStyleFamily nFamily) const
virtual rtl::Reference< SvXMLImportPropertyMapper > GetImportPropertyMapper(XmlStyleFamily nFamily) const override
static const XMLPropertyMapEntry * GetTableStyleProps()
Definition: xmlHelper.cxx:154
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
const rtl::Reference< XMLPropertySetMapper > & GetCellStylesPropertySetMapper() const
Definition: xmlfilter.hxx:100
const SvXMLImport & m_rImport
virtual ~OControlStyleContext() override
virtual SvXMLStyleContext * CreateStyleStyleChildContext(XmlStyleFamily nFamily, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
const css::uno::Reference< css::frame::XModel > & GetModel() const
css::uno::Reference< css::container::XNameContainer > m_xRowStyles
const SvXMLStyleContext * FindStyleChildContext(XmlStyleFamily nFamily, const OUString &rName, bool bCreateIndex=false) const
constexpr sal_Int32 TOKEN_MASK
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
virtual SvXMLStyleContext * CreateDefaultStyleStyleChildContext(XmlStyleFamily nFamily, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
static SvXMLImportPropertyMapper * CreateParaExtPropMapper(SvXMLImport &)
virtual css::uno::Reference< css::container::XNameContainer > GetStylesContainer(XmlStyleFamily nFamily) const
virtual SvXMLStyleContext * CreateStyleStyleChildContext(XmlStyleFamily nFamily, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
SvXMLStylesContext * GetAutoStyles()
virtual ~OReportStylesContext() override
css::uno::Reference< css::container::XNameContainer > m_xColumnStyles
rtl::Reference< SvXMLImportPropertyMapper > m_xTableImpPropMapper
void AddProperty(sal_Int16 nContextID, const css::uno::Any &aValue)
#define CTF_RPT_NUMBERFORMAT
Definition: xmlHelper.hxx:29
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet) override