LibreOffice Module sw (master)  1
xmlfmte.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/text/XTextDocument.hpp>
21 #include <xmloff/xmlnamespace.hxx>
22 #include <xmloff/attrlist.hxx>
23 #include "xmlexpit.hxx"
24 #include <xmloff/namespacemap.hxx>
28 
29 #include <xmloff/txtprmap.hxx>
30 #include <xmloff/xmlaustp.hxx>
31 #include <xmloff/families.hxx>
32 #include <xmloff/maptype.hxx>
33 #include <format.hxx>
34 #include <fmtpdsc.hxx>
35 #include <pagedesc.hxx>
36 #include <cellatr.hxx>
37 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
38 #include "xmlexp.hxx"
39 #include <SwStyleNameMapper.hxx>
40 #include <osl/diagnose.h>
41 
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::text;
45 using namespace ::com::sun::star::drawing;
46 using namespace ::com::sun::star::lang;
47 using namespace ::xmloff::token;
48 
49 void SwXMLExport::ExportFormat( const SwFormat& rFormat, enum XMLTokenEnum eFamily )
50 {
51  // <style:style ...>
52  CheckAttrList();
53 
54  // style:family="..."
55  OSL_ENSURE( RES_FRMFMT==rFormat.Which(), "frame format expected" );
56  if( RES_FRMFMT != rFormat.Which() )
57  return;
58  OSL_ENSURE( eFamily != XML_TOKEN_INVALID, "family must be specified" );
59  // style:name="..."
60  bool bEncoded = false;
62  rFormat.GetName(), &bEncoded ) );
63  if( bEncoded )
65 
66  if( eFamily != XML_TOKEN_INVALID )
68 
69 #if OSL_DEBUG_LEVEL > 0
70  // style:parent-style-name="..." (if it's not the default only)
71  const SwFormat* pParent = rFormat.DerivedFrom();
72  // Only adopt parent name, if it's not the default
73  OSL_ENSURE( !pParent || pParent->IsDefault(), "unexpected parent" );
74 
75  OSL_ENSURE( USHRT_MAX == rFormat.GetPoolFormatId(), "pool ids aren't supported" );
76  OSL_ENSURE( USHRT_MAX == rFormat.GetPoolHelpId(), "help ids aren't supported" );
77  OSL_ENSURE( USHRT_MAX == rFormat.GetPoolHelpId() ||
78  UCHAR_MAX == rFormat.GetPoolHlpFileId(), "help file ids aren't supported" );
79 #endif
80 
81  // style:master-page-name
82  if( RES_FRMFMT == rFormat.Which() && XML_TABLE == eFamily )
83  {
84  const SfxPoolItem *pItem;
85  if( SfxItemState::SET == rFormat.GetAttrSet().GetItemState( RES_PAGEDESC,
86  false, &pItem ) )
87  {
88  OUString sName;
89  const SwPageDesc *pPageDesc =
90  static_cast<const SwFormatPageDesc *>(pItem)->GetPageDesc();
91  if( pPageDesc )
93  pPageDesc->GetName(),
94  sName,
97  EncodeStyleName( sName ) );
98  }
99  }
100 
101  if( XML_TABLE_CELL == eFamily )
102  {
103  OSL_ENSURE(RES_FRMFMT == rFormat.Which(), "only frame format");
104 
105  const SfxPoolItem *pItem;
106  if( SfxItemState::SET ==
108  false, &pItem ) )
109  {
110  sal_Int32 nFormat = static_cast<sal_Int32>(static_cast<const SwTableBoxNumFormat *>(pItem)->GetValue());
111 
112  if ( (nFormat != -1) && (nFormat != static_cast<sal_Int32>(getSwDefaultTextFormat())) )
113  {
114  // if we have a format, register and then export
115  // (Careful: here we assume that data styles will be
116  // written after cell styles)
117  addDataStyle(nFormat);
118  OUString sDataStyleName = getDataStyleName(nFormat);
119  if( !sDataStyleName.isEmpty() )
121  sDataStyleName );
122  }
123  }
124  }
125 
126  {
128  true, true );
129 
130  SvXMLItemMapEntriesRef xItemMap;
131  XMLTokenEnum ePropToken = XML_TABLE_PROPERTIES;
132  if( XML_TABLE == eFamily )
133  {
134  xItemMap = m_xTableItemMap;
135  }
136  else if( XML_TABLE_ROW == eFamily )
137  {
138  xItemMap = m_xTableRowItemMap;
139  ePropToken = XML_TABLE_ROW_PROPERTIES;
140  }
141  else if( XML_TABLE_CELL == eFamily )
142  {
143  xItemMap = m_xTableCellItemMap;
144  ePropToken = XML_TABLE_CELL_PROPERTIES;
145  }
146 
147  if( xItemMap.is() )
148  {
149  m_pTableItemMapper->setMapEntries( xItemMap );
150  m_pTableItemMapper->exportXML( *this,
151  rFormat.GetAttrSet(),
153  ePropToken );
154  }
155  }
156 }
157 
158 void SwXMLExport::ExportStyles_( bool bUsed )
159 {
161 
162  // drawing defaults
163  GetShapeExport()->ExportGraphicDefaults();
164 
165  GetTextParagraphExport()->exportTextStyles( bUsed
166  ,IsShowProgress()
167  );
168  collectDataStyles(true);
170  GetShapeExport()->GetShapeTableExport()->exportTableStyles();
171  //page defaults
172  GetPageExport()->exportDefaultStyle();
173 }
174 
176 {
178 
180  return;
181 
182  // The order in which styles are collected *MUST* be the same as
183  // the order in which they are exported. Otherwise, caching will
184  // fail.
185  if( getExportFlags() & (SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::CONTENT) )
186  {
187  if( !(getExportFlags() & SvXMLExportFlags::CONTENT) )
188  {
189  // only master pages are exported => styles for frames bound
190  // to frames (but none for frames bound to pages) need to be
191  // collected.
192  // TODO: exclude PageBoundFrames on export
193  }
194  }
195 
196  // exported in _ExportMasterStyles
197  if( getExportFlags() & SvXMLExportFlags::MASTERSTYLES )
198  GetPageExport()->collectAutoStyles( false );
199 
200 
201  // exported in ExportContent_
202  if( getExportFlags() & SvXMLExportFlags::CONTENT )
203  {
204  // collect form autostyle
205  // (do this before collectTextAutoStyles, 'cause the shapes need the results of the work
206  // done by examineForms)
207  Reference<XDrawPageSupplier> xDrawPageSupplier( GetModel(), UNO_QUERY );
208  if (xDrawPageSupplier.is() && GetFormExport().is())
209  {
210  Reference<XDrawPage> xPage = xDrawPageSupplier->getDrawPage();
211  if (xPage.is())
212  GetFormExport()->examineForms(xPage);
213  }
214 
215  GetTextParagraphExport()->collectTextAutoStylesOptimized( m_bShowProgress );
216  }
217 
218  mbAutoStylesCollected = true;
219 }
220 
222 {
224 
225  // if we don't export styles (i.e. in content stream only, but not
226  // in single-stream case), then we can save ourselves a bit of
227  // work and memory by not collecting field masters
228  if( !(getExportFlags() & SvXMLExportFlags::STYLES) )
229  GetTextParagraphExport()->exportUsedDeclarations();
230 
231  // exported in ExportContent_
232  if( getExportFlags() & SvXMLExportFlags::CONTENT )
233  {
234  GetTextParagraphExport()->exportTrackedChanges( true );
235  }
236 
237  GetTextParagraphExport()->exportTextAutoStyles();
238  GetShapeExport()->exportAutoStyles();
239  if( getExportFlags() & SvXMLExportFlags::MASTERSTYLES )
240  GetPageExport()->exportAutoStyles();
241 
242  // we rely on data styles being written after cell styles in the
243  // ExportFormat() method; so be careful when changing order.
245 
246  SvXMLExportFlags nContentAutostyles = SvXMLExportFlags::CONTENT | SvXMLExportFlags::AUTOSTYLES;
247  if ( ( getExportFlags() & nContentAutostyles ) == nContentAutostyles )
248  GetFormExport()->exportAutoStyles();
249 }
250 
252 {
253  return new XMLTextMasterPageExport( *this );
254 }
255 
257 {
258  // export master styles
259  GetPageExport()->exportMasterStyles( false );
260 }
261 
262 namespace {
263 
264 class SwXMLAutoStylePoolP : public SvXMLAutoStylePoolP
265 {
267  const OUString sListStyleName;
268  const OUString sMasterPageName;
269 
270 protected:
271 
272  virtual void exportStyleAttributes(
273  SvXMLAttributeList& rAttrList,
274  XmlStyleFamily nFamily,
275  const std::vector< XMLPropertyState >& rProperties,
276  const SvXMLExportPropertyMapper& rPropExp
277  , const SvXMLUnitConverter& rUnitConverter,
278  const SvXMLNamespaceMap& rNamespaceMap
279  ) const override;
280 public:
281 
282  explicit SwXMLAutoStylePoolP( SvXMLExport& rExport );
283 };
284 
285 }
286 
287 void SwXMLAutoStylePoolP::exportStyleAttributes(
288  SvXMLAttributeList& rAttrList,
289  XmlStyleFamily nFamily,
290  const std::vector< XMLPropertyState >& rProperties,
291  const SvXMLExportPropertyMapper& rPropExp
292  , const SvXMLUnitConverter& rUnitConverter,
293  const SvXMLNamespaceMap& rNamespaceMap
294  ) const
295 {
296  SvXMLAutoStylePoolP::exportStyleAttributes( rAttrList, nFamily, rProperties, rPropExp, rUnitConverter, rNamespaceMap);
297 
298  if( XmlStyleFamily::TEXT_PARAGRAPH != nFamily )
299  return;
300 
301  for( const auto& rProperty : rProperties )
302  {
303  if (rProperty.mnIndex != -1) // #i26762#
304  {
305  switch( rPropExp.getPropertySetMapper()->
306  GetEntryContextId( rProperty.mnIndex ) )
307  {
309  {
310  OUString sStyleName;
311  rProperty.maValue >>= sStyleName;
312  // #i70748# - export also empty list styles
313  if( !sStyleName.isEmpty() )
314  {
315  OUString sTmp = rExport.GetTextParagraphExport()->GetListAutoStylePool().Find( sStyleName );
316  if( !sTmp.isEmpty() )
317  sStyleName = sTmp;
318  }
319  GetExport().AddAttribute( XML_NAMESPACE_STYLE,
320  sListStyleName,
321  GetExport().EncodeStyleName( sStyleName ) );
322  }
323  break;
324  case CTF_PAGEDESCNAME:
325  {
326  OUString sStyleName;
327  rProperty.maValue >>= sStyleName;
328  GetExport().AddAttribute( XML_NAMESPACE_STYLE,
329  sMasterPageName,
330  GetExport().EncodeStyleName( sStyleName ) );
331  }
332  break;
333  }
334  }
335  }
336 }
337 
338 SwXMLAutoStylePoolP::SwXMLAutoStylePoolP(SvXMLExport& rExp ) :
339  SvXMLAutoStylePoolP( rExp ),
340  rExport( rExp ),
341  sListStyleName( GetXMLToken( XML_LIST_STYLE_NAME ) ),
342  sMasterPageName( GetXMLToken( XML_MASTER_PAGE_NAME ) )
343 {
344 }
345 
347 {
348  return new SwXMLAutoStylePoolP( *this );
349 }
350 
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool is() const
constexpr TypedWhichId< SwFormatPageDesc > RES_PAGEDESC(93)
XML_DISPLAY_NAME
rtl::Reference< XMLShapeExport > const & GetShapeExport()
Pagedescriptor Client of SwPageDesc that is "described" by the attribute.
Definition: fmtpdsc.hxx:35
bool IsDefault() const
Definition: format.hxx:109
XML_LIST_STYLE_NAME
virtual void exportStyleAttributes(SvXMLAttributeList &rAttrList, XmlStyleFamily nFamily, const ::std::vector< XMLPropertyState > &rProperties, const SvXMLExportPropertyMapper &rPropExp, const SvXMLUnitConverter &rUnitConverter, const SvXMLNamespaceMap &rNamespaceMap) const
constexpr TypedWhichId< SwTableBoxNumFormat > RES_BOXATR_FORMAT(RES_BOXATR_BEGIN)
std::string GetValue
XML_TABLE_PROPERTIES
virtual void exportAutoDataStyles()
XML_NAME
SvXMLItemMapEntriesRef m_xTableRowItemMap
Definition: xmlexp.hxx:55
rtl::Reference< XMLTextParagraphExport > const & GetTextParagraphExport()
void CheckAttrList()
SvXMLExportFlags
#define CTF_NUMBERINGSTYLENAME
XML_MASTER_PAGE_NAME
SvXMLItemMapEntriesRef m_xTableItemMap
Definition: xmlexp.hxx:54
SvXMLItemMapEntriesRef m_xTableCellItemMap
Definition: xmlexp.hxx:56
virtual void ExportMasterStyles_() override
Definition: xmlfmte.cxx:256
const OUString & GetName() const
Definition: pagedesc.hxx:186
virtual void ExportStyles_(bool bUsed)
XmlStyleFamily
const css::uno::Reference< css::frame::XModel > & GetModel() const
XML_TABLE_CELL_PROPERTIES
XML_STYLE
XML_TABLE_CELL
XML_TABLE
const OUString & GetName() const
Definition: format.hxx:111
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
virtual OUString getDataStyleName(const sal_Int32 nNumberFormat, bool bTimeFormat=false) const
bool m_bShowProgress
Definition: xmlexp.hxx:59
const char * sName
XMLTokenEnum
constexpr TypedWhichId< SwFrameFormat > RES_FRMFMT(153)
sal_uInt16 GetPoolFormatId() const
Get and set Pool style IDs.
Definition: format.hxx:143
XML_DATA_STYLE_NAME
Base class for various Writer styles.
Definition: format.hxx:43
bool mbAutoStylesCollected
void ExportFormat(const SwFormat &rFormat, enum::xmloff::token::XMLTokenEnum eClass)
Definition: xmlfmte.cxx:49
bool IsShowProgress() const
Definition: xmlexp.hxx:126
XML_TABLE_ROW
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
virtual void ExportStyles_(bool bUsed) override
Definition: xmlfmte.cxx:158
sal_uInt8 GetPoolHlpFileId() const
Definition: format.hxx:149
const SvXMLUnitConverter & GetTwipUnitConverter() const
Definition: xmlexp.hxx:137
XML_TOKEN_INVALID
virtual SvXMLAutoStylePoolP * CreateAutoStylePool() override
Definition: xmlfmte.cxx:346
XML_TABLE_ROW_PROPERTIES
virtual void exportDataStyles()
OUString EncodeStyleName(const OUString &rName, bool *pEncoded=nullptr) const
sal_uInt16 Which() const
for Querying of Writer-functions.
Definition: format.hxx:78
SwFormat * DerivedFrom() const
Definition: format.hxx:108
sal_uInt16 GetPoolHelpId() const
Get and set Help-IDs for document templates.
Definition: format.hxx:147
#define CTF_PAGEDESCNAME
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
void collectAutoStyles() override
Definition: xmlfmte.cxx:175
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:254
virtual void ExportAutoStyles_() override
Definition: xmlfmte.cxx:221
static void FillProgName(const OUString &rName, OUString &rFillName, SwGetPoolIdFromName)
rtl::Reference< XMLPageExport > const & GetPageExport()
rtl::Reference< xmloff::OFormLayerXMLExport > const & GetFormExport()
XML_FAMILY
void collectDataStyles(bool bFromUsedStyles)
virtual XMLPageExport * CreatePageExport() override
Definition: xmlfmte.cxx:251
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:116
virtual void addDataStyle(const sal_Int32 nNumberFormat, bool bTimeFormat=false)
css::uno::Any const SvXMLExport & rExport
SvXMLExportFlags getExportFlags() const
constexpr sal_uInt32 getSwDefaultTextFormat()
The number formatter's default locale's @ Text format.
Definition: cellatr.hxx:34
constexpr sal_uInt16 XML_NAMESPACE_STYLE
const rtl::Reference< XMLPropertySetMapper > & getPropertySetMapper() const
std::unique_ptr< SvXMLExportItemMapper > m_pTableItemMapper
Definition: xmlexp.hxx:51
virtual void collectAutoStyles()