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  if( const SwFormatPageDesc* pItem = rFormat.GetAttrSet().GetItemIfSet( RES_PAGEDESC,
85  false ) )
86  {
87  OUString sName;
88  const SwPageDesc *pPageDesc = pItem->GetPageDesc();
89  if( pPageDesc )
91  pPageDesc->GetName(),
92  sName,
95  EncodeStyleName( sName ) );
96  }
97  }
98 
99  if( XML_TABLE_CELL == eFamily )
100  {
101  OSL_ENSURE(RES_FRMFMT == rFormat.Which(), "only frame format");
102 
103  if( const SwTableBoxNumFormat *pItem =
104  rFormat.GetAttrSet().GetItemIfSet( RES_BOXATR_FORMAT, false ) )
105  {
106  sal_Int32 nFormat = static_cast<sal_Int32>(pItem->GetValue());
107 
108  if ( (nFormat != -1) && (nFormat != static_cast<sal_Int32>(getSwDefaultTextFormat())) )
109  {
110  // if we have a format, register and then export
111  // (Careful: here we assume that data styles will be
112  // written after cell styles)
113  addDataStyle(nFormat);
114  OUString sDataStyleName = getDataStyleName(nFormat);
115  if( !sDataStyleName.isEmpty() )
117  sDataStyleName );
118  }
119  }
120  }
121 
122  {
124  true, true );
125 
126  SvXMLItemMapEntriesRef xItemMap;
127  XMLTokenEnum ePropToken = XML_TABLE_PROPERTIES;
128  if( XML_TABLE == eFamily )
129  {
130  xItemMap = m_xTableItemMap;
131  }
132  else if( XML_TABLE_ROW == eFamily )
133  {
134  xItemMap = m_xTableRowItemMap;
135  ePropToken = XML_TABLE_ROW_PROPERTIES;
136  }
137  else if( XML_TABLE_CELL == eFamily )
138  {
139  xItemMap = m_xTableCellItemMap;
140  ePropToken = XML_TABLE_CELL_PROPERTIES;
141  }
142 
143  if( xItemMap.is() )
144  {
145  m_pTableItemMapper->setMapEntries( xItemMap );
146  m_pTableItemMapper->exportXML( *this,
147  rFormat.GetAttrSet(),
149  ePropToken );
150  }
151  }
152 }
153 
154 void SwXMLExport::ExportStyles_( bool bUsed )
155 {
157 
158  // drawing defaults
159  GetShapeExport()->ExportGraphicDefaults();
160 
161  GetTextParagraphExport()->exportTextStyles( bUsed
162  ,IsShowProgress()
163  );
164  collectDataStyles(true);
166  GetShapeExport()->GetShapeTableExport()->exportTableStyles();
167  //page defaults
168  GetPageExport()->exportDefaultStyle();
169 }
170 
172 {
174 
176  return;
177 
178  // The order in which styles are collected *MUST* be the same as
179  // the order in which they are exported. Otherwise, caching will
180  // fail.
181  if( getExportFlags() & (SvXMLExportFlags::MASTERSTYLES|SvXMLExportFlags::CONTENT) )
182  {
183  if( !(getExportFlags() & SvXMLExportFlags::CONTENT) )
184  {
185  // only master pages are exported => styles for frames bound
186  // to frames (but none for frames bound to pages) need to be
187  // collected.
188  // TODO: exclude PageBoundFrames on export
189  }
190  }
191 
192  // exported in _ExportMasterStyles
193  if( getExportFlags() & SvXMLExportFlags::MASTERSTYLES )
194  GetPageExport()->collectAutoStyles( false );
195 
196 
197  // exported in ExportContent_
198  if( getExportFlags() & SvXMLExportFlags::CONTENT )
199  {
200  // collect form autostyle
201  // (do this before collectTextAutoStyles, 'cause the shapes need the results of the work
202  // done by examineForms)
203  Reference<XDrawPageSupplier> xDrawPageSupplier( GetModel(), UNO_QUERY );
204  if (xDrawPageSupplier.is() && GetFormExport().is())
205  {
206  Reference<XDrawPage> xPage = xDrawPageSupplier->getDrawPage();
207  if (xPage.is())
208  GetFormExport()->examineForms(xPage);
209  }
210 
211  GetTextParagraphExport()->collectTextAutoStylesOptimized( m_bShowProgress );
212  }
213 
214  mbAutoStylesCollected = true;
215 }
216 
218 {
220 
221  // if we don't export styles (i.e. in content stream only, but not
222  // in single-stream case), then we can save ourselves a bit of
223  // work and memory by not collecting field masters
224  if( !(getExportFlags() & SvXMLExportFlags::STYLES) )
225  GetTextParagraphExport()->exportUsedDeclarations();
226 
227  // exported in ExportContent_
228  if( getExportFlags() & SvXMLExportFlags::CONTENT )
229  {
230  GetTextParagraphExport()->exportTrackedChanges( true );
231  }
232 
233  GetTextParagraphExport()->exportTextAutoStyles();
234  GetShapeExport()->exportAutoStyles();
235  if( getExportFlags() & SvXMLExportFlags::MASTERSTYLES )
236  GetPageExport()->exportAutoStyles();
237 
238  // we rely on data styles being written after cell styles in the
239  // ExportFormat() method; so be careful when changing order.
241 
242  SvXMLExportFlags nContentAutostyles = SvXMLExportFlags::CONTENT | SvXMLExportFlags::AUTOSTYLES;
243  if ( ( getExportFlags() & nContentAutostyles ) == nContentAutostyles )
244  GetFormExport()->exportAutoStyles();
245 }
246 
248 {
249  return new XMLTextMasterPageExport( *this );
250 }
251 
253 {
254  // export master styles
255  GetPageExport()->exportMasterStyles( false );
256 }
257 
258 namespace {
259 
260 class SwXMLAutoStylePoolP : public SvXMLAutoStylePoolP
261 {
262  SvXMLExport& m_rExport;
263  const OUString m_sListStyleName;
264  const OUString m_sMasterPageName;
265 
266 protected:
267 
268  virtual void exportStyleAttributes(
269  SvXMLAttributeList& rAttrList,
270  XmlStyleFamily nFamily,
271  const std::vector< XMLPropertyState >& rProperties,
272  const SvXMLExportPropertyMapper& rPropExp
273  , const SvXMLUnitConverter& rUnitConverter,
274  const SvXMLNamespaceMap& rNamespaceMap
275  ) const override;
276 public:
277 
278  explicit SwXMLAutoStylePoolP( SvXMLExport& rExport );
279 };
280 
281 }
282 
283 void SwXMLAutoStylePoolP::exportStyleAttributes(
284  SvXMLAttributeList& rAttrList,
285  XmlStyleFamily nFamily,
286  const std::vector< XMLPropertyState >& rProperties,
287  const SvXMLExportPropertyMapper& rPropExp
288  , const SvXMLUnitConverter& rUnitConverter,
289  const SvXMLNamespaceMap& rNamespaceMap
290  ) const
291 {
292  SvXMLAutoStylePoolP::exportStyleAttributes( rAttrList, nFamily, rProperties, rPropExp, rUnitConverter, rNamespaceMap);
293 
294  if( XmlStyleFamily::TEXT_PARAGRAPH != nFamily )
295  return;
296 
297  for( const auto& rProperty : rProperties )
298  {
299  if (rProperty.mnIndex != -1) // #i26762#
300  {
301  switch( rPropExp.getPropertySetMapper()->
302  GetEntryContextId( rProperty.mnIndex ) )
303  {
305  {
306  OUString sStyleName;
307  rProperty.maValue >>= sStyleName;
308  // #i70748# - export also empty list styles
309  if( !sStyleName.isEmpty() )
310  {
311  OUString sTmp = m_rExport.GetTextParagraphExport()->GetListAutoStylePool().Find( sStyleName );
312  if( !sTmp.isEmpty() )
313  sStyleName = sTmp;
314  }
315  GetExport().AddAttribute( XML_NAMESPACE_STYLE,
316  m_sListStyleName,
317  GetExport().EncodeStyleName( sStyleName ) );
318  }
319  break;
320  case CTF_PAGEDESCNAME:
321  {
322  OUString sStyleName;
323  rProperty.maValue >>= sStyleName;
324  GetExport().AddAttribute( XML_NAMESPACE_STYLE,
325  m_sMasterPageName,
326  GetExport().EncodeStyleName( sStyleName ) );
327  }
328  break;
329  }
330  }
331  }
332 }
333 
334 SwXMLAutoStylePoolP::SwXMLAutoStylePoolP(SvXMLExport& rExp ) :
335  SvXMLAutoStylePoolP( rExp ),
336  m_rExport( rExp ),
337  m_sListStyleName( GetXMLToken( XML_LIST_STYLE_NAME ) ),
338  m_sMasterPageName( GetXMLToken( XML_MASTER_PAGE_NAME ) )
339 {
340 }
341 
343 {
344  return new SwXMLAutoStylePoolP( *this );
345 }
346 
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool is() const
constexpr TypedWhichId< SwFormatPageDesc > RES_PAGEDESC(93)
const T * GetItemIfSet(TypedWhichId< T > nWhich, bool bSrchInParent=true) const
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:129
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)
virtual void exportAutoDataStyles()
SvXMLItemMapEntriesRef m_xTableRowItemMap
Definition: xmlexp.hxx:57
rtl::Reference< XMLTextParagraphExport > const & GetTextParagraphExport()
void CheckAttrList()
SvXMLExportFlags
#define CTF_NUMBERINGSTYLENAME
XML_TABLE_CELL
XML_DATA_STYLE_NAME
SvXMLItemMapEntriesRef m_xTableItemMap
Definition: xmlexp.hxx:56
SvXMLItemMapEntriesRef m_xTableCellItemMap
Definition: xmlexp.hxx:58
virtual void ExportMasterStyles_() override
Definition: xmlfmte.cxx:252
const OUString & GetName() const
Definition: pagedesc.hxx:196
virtual void ExportStyles_(bool bUsed)
XmlStyleFamily
const css::uno::Reference< css::frame::XModel > & GetModel() const
const OUString & GetName() const
Definition: format.hxx:131
void AddAttribute(sal_uInt16 nPrefix, const OUString &rName, const OUString &rValue)
constexpr TypedWhichId< SwFrameFormat > RES_FRMFMT(155)
XML_DISPLAY_NAME
virtual OUString getDataStyleName(const sal_Int32 nNumberFormat, bool bTimeFormat=false) const
XML_TOKEN_INVALID
bool m_bShowProgress
Definition: xmlexp.hxx:61
const char * sName
sal_uInt16 GetPoolFormatId() const
Get and set Pool style IDs.
Definition: format.hxx:163
Base class for various Writer styles.
Definition: format.hxx:46
bool mbAutoStylesCollected
void ExportFormat(const SwFormat &rFormat, enum::xmloff::token::XMLTokenEnum eClass)
Definition: xmlfmte.cxx:49
XML_TABLE_ROW
bool IsShowProgress() const
Definition: xmlexp.hxx:128
XML_TABLE
virtual void ExportStyles_(bool bUsed) override
Definition: xmlfmte.cxx:154
sal_uInt8 GetPoolHlpFileId() const
Definition: format.hxx:169
const SvXMLUnitConverter & GetTwipUnitConverter() const
Definition: xmlexp.hxx:139
XML_TABLE_ROW_PROPERTIES
virtual SvXMLAutoStylePoolP * CreateAutoStylePool() override
Definition: xmlfmte.cxx:342
XML_FAMILY
virtual void exportDataStyles()
OUString EncodeStyleName(const OUString &rName, bool *pEncoded=nullptr) const
sal_uInt16 Which() const
for Querying of Writer-functions.
Definition: format.hxx:82
SwFormat * DerivedFrom() const
Definition: format.hxx:128
sal_uInt16 GetPoolHelpId() const
Get and set Help-IDs for document templates.
Definition: format.hxx:167
#define CTF_PAGEDESCNAME
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
void collectAutoStyles() override
Definition: xmlfmte.cxx:171
virtual void ExportAutoStyles_() override
Definition: xmlfmte.cxx:217
static void FillProgName(const OUString &rName, OUString &rFillName, SwGetPoolIdFromName)
XML_MASTER_PAGE_NAME
XML_TABLE_PROPERTIES
XMLTokenEnum
XML_LIST_STYLE_NAME
rtl::Reference< XMLPageExport > const & GetPageExport()
rtl::Reference< xmloff::OFormLayerXMLExport > const & GetFormExport()
void collectDataStyles(bool bFromUsedStyles)
virtual XMLPageExport * CreatePageExport() override
Definition: xmlfmte.cxx:247
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:136
XML_TABLE_CELL_PROPERTIES
virtual void addDataStyle(const sal_Int32 nNumberFormat, bool bTimeFormat=false)
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
XML_STYLE
const rtl::Reference< XMLPropertySetMapper > & getPropertySetMapper() const
std::unique_ptr< SvXMLExportItemMapper > m_pTableItemMapper
Definition: xmlexp.hxx:53
XML_NAME
virtual void collectAutoStyles()