xref: /trunk/main/sw/source/filter/xml/xmlfmte.cxx (revision efeef26f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 
28 #include <com/sun/star/text/XTextDocument.hpp>
29 #include <xmloff/xmlnmspe.hxx>
30 #include <xmloff/attrlist.hxx>
31 #include "xmlexpit.hxx"
32 #include <xmloff/nmspmap.hxx>
33 #include <xmloff/XMLTextListAutoStylePool.hxx>
34 #include <xmloff/XMLTextMasterPageExport.hxx>
35 
36 #include <xmloff/txtprmap.hxx>
37 #include <xmloff/xmlaustp.hxx>
38 #include <xmloff/families.hxx>
39 #include <format.hxx>
40 #include <fmtpdsc.hxx>
41 #include <pagedesc.hxx>
42 #include <unostyle.hxx>
43 #include <cellatr.hxx>
44 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
45 #include <com/sun/star/beans/XPropertySet.hpp>
46 #include "xmlexp.hxx"
47 #include <SwStyleNameMapper.hxx>
48 
49 
50 using ::rtl::OUString;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::text;
54 using namespace ::com::sun::star::drawing;
55 using namespace ::com::sun::star::lang;
56 using namespace ::xmloff::token;
57 
ExportFmt(const SwFmt & rFmt,enum XMLTokenEnum eFamily)58 void SwXMLExport::ExportFmt( const SwFmt& rFmt, enum XMLTokenEnum eFamily )
59 {
60 	// <style:style ...>
61 	CheckAttrList();
62 
63 	// style:family="..."
64 	DBG_ASSERT( RES_FRMFMT==rFmt.Which(), "frame format expected" );
65 	if( RES_FRMFMT != rFmt.Which() )
66 		return;
67 	DBG_ASSERT( eFamily != XML_TOKEN_INVALID, "family must be specified" );
68 	// style:name="..."
69 	sal_Bool bEncoded = sal_False;
70 	AddAttribute( XML_NAMESPACE_STYLE, XML_NAME, EncodeStyleName(
71 					rFmt.GetName(), &bEncoded ) );
72 	if( bEncoded )
73 		AddAttribute( XML_NAMESPACE_STYLE, XML_DISPLAY_NAME, rFmt.GetName() );
74 
75 
76 	if( eFamily != XML_TOKEN_INVALID )
77 		AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY, eFamily );
78 
79 #ifdef DBG_UTIL
80 	// style:parent-style-name="..." (if its not the default only)
81 	const SwFmt* pParent = rFmt.DerivedFrom();
82 	// Parent-Namen nur uebernehmen, wenn kein Default
83 	ASSERT( !pParent || pParent->IsDefault(), "unexpected parent" );
84 
85 	ASSERT( USHRT_MAX == rFmt.GetPoolFmtId(), "pool ids arent'supported" );
86 	ASSERT( USHRT_MAX == rFmt.GetPoolHelpId(), "help ids arent'supported" );
87 	ASSERT( USHRT_MAX == rFmt.GetPoolHelpId() ||
88 			UCHAR_MAX == rFmt.GetPoolHlpFileId(), "help file ids aren't supported" );
89 #endif
90 
91 	// style:master-page-name
92 	if( RES_FRMFMT == rFmt.Which() && XML_TABLE == eFamily )
93 	{
94 		const SfxPoolItem *pItem;
95 		if( SFX_ITEM_SET == rFmt.GetAttrSet().GetItemState( RES_PAGEDESC,
96 															sal_False, &pItem ) )
97 		{
98 			String sName;
99 			const SwPageDesc *pPageDesc =
100 				((const SwFmtPageDesc *)pItem)->GetPageDesc();
101 			if( pPageDesc )
102 				SwStyleNameMapper::FillProgName(
103 									pPageDesc->GetName(),
104 									sName,
105 									nsSwGetPoolIdFromName::GET_POOLID_PAGEDESC,
106 									sal_True);
107 			AddAttribute( XML_NAMESPACE_STYLE, XML_MASTER_PAGE_NAME,
108 						  EncodeStyleName( sName ) );
109 		}
110 	}
111 
112 	if( XML_TABLE_CELL == eFamily )
113 	{
114 		DBG_ASSERT(RES_FRMFMT == rFmt.Which(), "only frame format");
115 
116 		const SfxPoolItem *pItem;
117 		if( SFX_ITEM_SET ==
118 			rFmt.GetAttrSet().GetItemState( RES_BOXATR_FORMAT,
119 											sal_False, &pItem ) )
120 		{
121 			sal_Int32 nFormat = (sal_Int32)
122 				((const SwTblBoxNumFormat *)pItem)->GetValue();
123 
124 			if ( (nFormat != -1) && (nFormat != NUMBERFORMAT_TEXT) )
125 			{
126 				// if we have a format, register and then export
127 				// (Careful: here we assume that data styles will be
128 				// written after cell styles)
129 				addDataStyle(nFormat);
130                 OUString sDataStyleName = getDataStyleName(nFormat);
131                 if( sDataStyleName.getLength() > 0 )
132                     AddAttribute( XML_NAMESPACE_STYLE, XML_DATA_STYLE_NAME,
133                                   sDataStyleName );
134 			}
135 		}
136 	}
137 
138 	{
139 		SvXMLElementExport aElem( *this, XML_NAMESPACE_STYLE, XML_STYLE,
140 								  sal_True, sal_True );
141 
142 		SvXMLItemMapEntriesRef xItemMap;
143 		XMLTokenEnum ePropToken = XML_TABLE_PROPERTIES;
144 		if( XML_TABLE == eFamily )
145 		{
146 			xItemMap = xTableItemMap;
147 		}
148 		else if( XML_TABLE_ROW == eFamily )
149 		{
150 			xItemMap = xTableRowItemMap;
151 			ePropToken = XML_TABLE_ROW_PROPERTIES;
152 		}
153 		else if( XML_TABLE_CELL == eFamily )
154 		{
155 			xItemMap = xTableCellItemMap;
156 			ePropToken = XML_TABLE_CELL_PROPERTIES;
157 		}
158 
159 		if( xItemMap.Is() )
160 		{
161 			SvXMLExportItemMapper& rItemMapper = GetTableItemMapper();
162 			rItemMapper.setMapEntries( xItemMap );
163 
164 			GetTableItemMapper().exportXML( *this,
165 										   rFmt.GetAttrSet(),
166 										   GetTwipUnitConverter(),
167 										   ePropToken,
168 										   XML_EXPORT_FLAG_IGN_WS );
169 		}
170 	}
171 }
172 
173 
_ExportStyles(sal_Bool bUsed)174 void SwXMLExport::_ExportStyles( sal_Bool bUsed )
175 {
176 	SvXMLExport::_ExportStyles( bUsed );
177 
178 	// drawing defaults
179 	GetShapeExport()->ExportGraphicDefaults();
180 
181 	GetTextParagraphExport()->exportTextStyles( bUsed
182 											 ,IsShowProgress()
183 											  );
184 	//page defaults
185 	GetPageExport()->exportDefaultStyle();
186 }
187 
_ExportAutoStyles()188 void SwXMLExport::_ExportAutoStyles()
189 {
190 	// The order in which styles are collected *MUST* be the same as
191 	// the order in which they are exported. Otherwise, caching will
192 	// fail.
193 
194 	if( (getExportFlags() & (EXPORT_MASTERSTYLES|EXPORT_CONTENT)) != 0 )
195 	{
196 		if( (getExportFlags() & EXPORT_CONTENT) == 0 )
197 		{
198 			// only master pages are exported => styles for frames bound
199 			// to frames (but none for frames bound to pages) need to be
200 			// collected.
201 			// TODO: exclude PageBoundFrames on export
202 		}
203 	}
204 
205 	// exported in _ExportMasterStyles
206 	if( (getExportFlags() & EXPORT_MASTERSTYLES) != 0 )
207 		GetPageExport()->collectAutoStyles( sal_False );
208 
209     // if we don't export styles (i.e. in content stream only, but not
210     // in single-stream case), then we can save ourselves a bit of
211     // work and memory by not collecting field masters
212     if( (getExportFlags() & EXPORT_STYLES ) == 0 )
213         GetTextParagraphExport()->exportUsedDeclarations( sal_False );
214 
215 	// exported in _ExportContent
216 	if( (getExportFlags() & EXPORT_CONTENT) != 0 )
217 	{
218 		GetTextParagraphExport()->exportTrackedChanges( sal_True );
219 		Reference < XTextDocument > xTextDoc( GetModel(), UNO_QUERY );
220 		Reference < XText > xText = xTextDoc->getText();
221 
222 		// collect form autostyle
223 		// (do this before collectTextAutoStyles, 'cause the shapes need the results of the work
224 		// done by examineForms)
225 		Reference<XDrawPageSupplier> xDrawPageSupplier( GetModel(), UNO_QUERY );
226 		if (xDrawPageSupplier.is() && GetFormExport().is())
227 		{
228 			Reference<XDrawPage> xPage = xDrawPageSupplier->getDrawPage();
229 			if (xPage.is())
230 				GetFormExport()->examineForms(xPage);
231 		}
232 
233         GetTextParagraphExport()->collectTextAutoStylesOptimized( bShowProgress );
234         //GetTextParagraphExport()->collectTextAutoStyles( xText, bShowProgress, sal_True, bPortions );
235 	}
236 
237 	GetTextParagraphExport()->exportTextAutoStyles();
238 	GetShapeExport()->exportAutoStyles();
239 	if( (getExportFlags() & EXPORT_MASTERSTYLES) != 0 )
240 		GetPageExport()->exportAutoStyles();
241 
242 	// we rely on data styles being written after cell styles in the
243 	// ExportFmt() method; so be careful when changing order.
244 	exportAutoDataStyles();
245 
246 	sal_uInt16 nContentAutostyles = EXPORT_CONTENT | EXPORT_AUTOSTYLES;
247 	if ( ( getExportFlags() & nContentAutostyles ) == nContentAutostyles )
248 		GetFormExport()->exportAutoStyles();
249 }
250 
CreatePageExport()251 XMLPageExport* SwXMLExport::CreatePageExport()
252 {
253 	return new XMLTextMasterPageExport( *this );
254 }
255 
_ExportMasterStyles()256 void SwXMLExport::_ExportMasterStyles()
257 {
258 	// export master styles
259 	GetPageExport()->exportMasterStyles( sal_False );
260 }
261 
262 // ---------------------------------------------------------------------
263 class SwXMLAutoStylePoolP : public SvXMLAutoStylePoolP
264 {
265 	SvXMLExport& rExport;
266 	const OUString sListStyleName;
267 	const OUString sMasterPageName;
268 	const OUString sCDATA;
269 
270 protected:
271 
272 	virtual void exportStyleAttributes(
273 			SvXMLAttributeList& rAttrList,
274 			sal_Int32 nFamily,
275 			const ::std::vector< XMLPropertyState >& rProperties,
276 			const SvXMLExportPropertyMapper& rPropExp
277 			, const SvXMLUnitConverter& rUnitConverter,
278 			const SvXMLNamespaceMap& rNamespaceMap
279             ) const;
280 public:
281 
282 	SwXMLAutoStylePoolP( SvXMLExport& rExport );
283 	virtual ~SwXMLAutoStylePoolP();
284 };
285 
exportStyleAttributes(SvXMLAttributeList & rAttrList,sal_Int32 nFamily,const::std::vector<XMLPropertyState> & rProperties,const SvXMLExportPropertyMapper & rPropExp,const SvXMLUnitConverter & rUnitConverter,const SvXMLNamespaceMap & rNamespaceMap) const286 void SwXMLAutoStylePoolP::exportStyleAttributes(
287 			SvXMLAttributeList& rAttrList,
288 			sal_Int32 nFamily,
289 			const ::std::vector< XMLPropertyState >& rProperties,
290 			const SvXMLExportPropertyMapper& rPropExp
291 			, const SvXMLUnitConverter& rUnitConverter,
292 			const SvXMLNamespaceMap& rNamespaceMap
293             ) const
294 {
295 	SvXMLAutoStylePoolP::exportStyleAttributes( rAttrList, nFamily, rProperties, rPropExp, rUnitConverter, rNamespaceMap);
296 
297 	if( XML_STYLE_FAMILY_TEXT_PARAGRAPH == nFamily )
298 	{
299 		for( ::std::vector< XMLPropertyState >::const_iterator
300 					aProperty = rProperties.begin();
301 			 aProperty != rProperties.end();
302 		 	 aProperty++ )
303 		{
304             if (aProperty->mnIndex != -1) // #i26762#
305             {
306                 switch( rPropExp.getPropertySetMapper()->
307 						GetEntryContextId( aProperty->mnIndex ) )
308                 {
309                 case CTF_NUMBERINGSTYLENAME:
310                     {
311                         OUString sStyleName;
312                         aProperty->maValue >>= sStyleName;
313                         // --> OD 2008-11-19 #i70748#
314                         // export also empty list styles
315                         if( sStyleName.getLength() )
316                         {
317                             OUString sTmp = rExport.GetTextParagraphExport()->GetListAutoStylePool().Find( sStyleName );
318                             if( sTmp.getLength() )
319                                 sStyleName = sTmp;
320                         }
321                         GetExport().AddAttribute( XML_NAMESPACE_STYLE,
322                               sListStyleName,
323                               GetExport().EncodeStyleName( sStyleName ) );
324                         // <--
325                     }
326                     break;
327                 case CTF_PAGEDESCNAME:
328                     {
329                         OUString sStyleName;
330                         aProperty->maValue >>= sStyleName;
331                         GetExport().AddAttribute( XML_NAMESPACE_STYLE,
332                                       sMasterPageName,
333                                       GetExport().EncodeStyleName( sStyleName ) );
334                     }
335                     break;
336                 }
337 			}
338 		}
339 	}
340 }
341 
SwXMLAutoStylePoolP(SvXMLExport & rExp)342 SwXMLAutoStylePoolP::SwXMLAutoStylePoolP(SvXMLExport& rExp ) :
343 	SvXMLAutoStylePoolP( rExp ),
344 	rExport( rExp ),
345 	sListStyleName( GetXMLToken( XML_LIST_STYLE_NAME ) ),
346 	sMasterPageName( GetXMLToken( XML_MASTER_PAGE_NAME ) ),
347 	sCDATA( GetXMLToken( XML_CDATA ) )
348 {
349 }
350 
351 
~SwXMLAutoStylePoolP()352 SwXMLAutoStylePoolP::~SwXMLAutoStylePoolP()
353 {
354 }
355 
CreateAutoStylePool()356 SvXMLAutoStylePoolP* SwXMLExport::CreateAutoStylePool()
357 {
358 	return new SwXMLAutoStylePoolP( *this );
359 }
360