xref: /trunk/main/xmloff/source/style/xmlaustp.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 
31 
32 #include <com/sun/star/container/XIndexReplace.hpp>
33 #include <tools/debug.hxx>
34 #include "impastpl.hxx"
35 #include <xmloff/xmlaustp.hxx>
36 #include <xmloff/families.hxx>
37 #include "xmloff/xmlnmspe.hxx"
38 #include <xmloff/xmltoken.hxx>
39 #include <xmloff/xmlexp.hxx>
40 #include "xmloff/XMLTextListAutoStylePool.hxx"
41 
42 #ifndef _XMLOFF_PAGEMASTERSTYLEMAP_HXX
43 #include <xmloff/PageMasterStyleMap.hxx>
44 #endif
45 #include "PageMasterExportPropMapper.hxx"
46 #include "XMLBackgroundImageExport.hxx"
47 
48 
49 using namespace ::std;
50 using ::rtl::OUString;
51 using ::rtl::OUStringBuffer;
52 
53 using namespace ::com::sun::star;
54 using namespace ::xmloff::token;
55 
56 
57 namespace
58 {
59 	static void lcl_exportDataStyle( SvXMLExport& _rExport, const UniReference< XMLPropertySetMapper >& _rxMapper,
60 		const XMLPropertyState& _rProperty )
61 	{
62 		DBG_ASSERT( _rxMapper.is(), "xmloff::lcl_exportDataStyle: invalid property mapper!" );
63 		// obtain the data style name
64 		::rtl::OUString sDataStyleName;
65 		_rProperty.maValue >>= sDataStyleName;
66 		DBG_ASSERT( sDataStyleName.getLength(), "xmloff::lcl_exportDataStyle: invalid property value for the data style name!" );
67 
68 		// add the attribute
69 		_rExport.AddAttribute(
70 			_rxMapper->GetEntryNameSpace( _rProperty.mnIndex ),
71 			_rxMapper->GetEntryXMLName( _rProperty.mnIndex ),
72 			sDataStyleName );
73 	}
74 }
75 
76 void SvXMLAutoStylePoolP::exportStyleAttributes(
77 		SvXMLAttributeList&,
78 		sal_Int32 nFamily,
79 		const vector< XMLPropertyState >& rProperties,
80 		const SvXMLExportPropertyMapper& rPropExp,
81         const SvXMLUnitConverter&,
82 		const SvXMLNamespaceMap&
83         ) const
84 {
85 	if ( XML_STYLE_FAMILY_CONTROL_ID == nFamily )
86 	{	// it's a control-related style
87 		UniReference< XMLPropertySetMapper > aPropertyMapper = rPropExp.getPropertySetMapper();
88 
89 		for	(	vector< XMLPropertyState >::const_iterator pProp = rProperties.begin();
90 				pProp != rProperties.end();
91 				++pProp
92 			)
93 		{
94 			if	(	( pProp->mnIndex > -1 )
95 				&&	( CTF_FORMS_DATA_STYLE == aPropertyMapper->GetEntryContextId( pProp->mnIndex ) )
96 				)
97 			{	// it's the data-style for a grid column
98 				lcl_exportDataStyle( GetExport(), aPropertyMapper, *pProp );
99 			}
100 		}
101 	}
102 
103 	if( (XML_STYLE_FAMILY_SD_GRAPHICS_ID == nFamily) || (XML_STYLE_FAMILY_SD_PRESENTATION_ID == nFamily) )
104 	{	// it's a graphics style
105 		UniReference< XMLPropertySetMapper > aPropertyMapper = rPropExp.getPropertySetMapper();
106 		DBG_ASSERT(aPropertyMapper.is(), "SvXMLAutoStylePoolP::exportStyleAttributes: invalid property set mapper!");
107 
108 		sal_Bool bFoundControlShapeDataStyle = sal_False;
109 		sal_Bool bFoundNumberingRulesName = sal_False;
110 
111 		for	(	vector< XMLPropertyState >::const_iterator pProp = rProperties.begin();
112 				pProp != rProperties.end();
113 				++pProp
114 			)
115 		{
116 			if (pProp->mnIndex > -1)
117 			{	// it's a valid property
118 				switch( aPropertyMapper->GetEntryContextId(pProp->mnIndex) )
119 				{
120 				case CTF_SD_CONTROL_SHAPE_DATA_STYLE:
121 					{	// it's the control shape data style property
122 
123 						if (bFoundControlShapeDataStyle)
124 						{
125 							DBG_ERROR("SvXMLAutoStylePoolP::exportStyleAttributes: found two properties with the ControlShapeDataStyle context id!");
126 							// already added the attribute for the first occurence
127 							break;
128 						}
129 
130 						lcl_exportDataStyle( GetExport(), aPropertyMapper, *pProp );
131 
132 						// check if there is another property with the special context id we're handling here
133 						bFoundControlShapeDataStyle = sal_True;
134 						break;
135 					}
136 				case CTF_SD_NUMBERINGRULES_NAME:
137 					{
138 						if (bFoundNumberingRulesName)
139 						{
140 							DBG_ERROR("SvXMLAutoStylePoolP::exportStyleAttributes: found two properties with the numbering rules name context id!");
141 							// already added the attribute for the first occurence
142 							break;
143 						}
144 
145 						uno::Reference< container::XIndexReplace > xNumRule;
146 						pProp->maValue >>= xNumRule;
147 						if( xNumRule.is() && (xNumRule->getCount() > 0 ) )
148 						{
149 							const OUString sName(((XMLTextListAutoStylePool*)&GetExport().GetTextParagraphExport()->GetListAutoStylePool())->Add( xNumRule ));
150 
151 							GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_LIST_STYLE_NAME, GetExport().EncodeStyleName( sName ) );
152 						}
153 
154 						bFoundNumberingRulesName = sal_True;
155 						break;
156 					}
157 				}
158 			}
159 		}
160 	}
161 
162 	if( nFamily == XML_STYLE_FAMILY_PAGE_MASTER )
163 	{
164 		for( vector< XMLPropertyState >::const_iterator pProp = rProperties.begin(); pProp != rProperties.end(); pProp++ )
165 		{
166 			if (pProp->mnIndex > -1)
167 			{
168 				UniReference< XMLPropertySetMapper > aPropMapper = rPropExp.getPropertySetMapper();
169 				sal_Int32 nIndex = pProp->mnIndex;
170 				sal_Int16 nContextID = aPropMapper->GetEntryContextId( nIndex );
171 				switch( nContextID )
172 				{
173 					case CTF_PM_PAGEUSAGE:
174 					{
175 						OUString sValue;
176 						const XMLPropertyHandler* pPropHdl = aPropMapper->GetPropertyHandler( nIndex );
177 						if( pPropHdl &&
178                             pPropHdl->exportXML( sValue, pProp->maValue,
179                                                  GetExport().GetMM100UnitConverter() ) &&
180 							( ! IsXMLToken( sValue, XML_ALL ) ) )
181                         {
182 							GetExport().AddAttribute( aPropMapper->GetEntryNameSpace( nIndex ), aPropMapper->GetEntryXMLName( nIndex ), sValue );
183                         }
184 					}
185 					break;
186 				}
187 			}
188 		}
189 	}
190 }
191 
192 void SvXMLAutoStylePoolP::exportStyleContent(
193 		const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler > &,
194 		sal_Int32 nFamily,
195 		const vector< XMLPropertyState >& rProperties,
196 		const SvXMLExportPropertyMapper& rPropExp,
197 		const SvXMLUnitConverter&,
198 		const SvXMLNamespaceMap&
199         ) const
200 {
201 	if( nFamily == XML_STYLE_FAMILY_PAGE_MASTER )
202 	{
203 		OUString sWS( GetXMLToken(XML_WS) );
204 
205 		sal_Int32		nHeaderStartIndex(-1);
206 		sal_Int32		nHeaderEndIndex(-1);
207 		sal_Int32		nFooterStartIndex(-1);
208 		sal_Int32		nFooterEndIndex(-1);
209 		sal_Bool		bHeaderStartIndex(sal_False);
210 		sal_Bool		bHeaderEndIndex(sal_False);
211 		sal_Bool		bFooterStartIndex(sal_False);
212 		sal_Bool		bFooterEndIndex(sal_False);
213 
214 		UniReference< XMLPropertySetMapper > aPropMapper = rPropExp.getPropertySetMapper();
215 
216 		sal_Int32 nIndex(0);
217 		while(nIndex < aPropMapper->GetEntryCount())
218 		{
219 			switch( aPropMapper->GetEntryContextId( nIndex ) & CTF_PM_FLAGMASK )
220 			{
221 				case CTF_PM_HEADERFLAG:
222 				{
223 					if (!bHeaderStartIndex)
224 					{
225 						nHeaderStartIndex = nIndex;
226 						bHeaderStartIndex = sal_True;
227 					}
228 					if (bFooterStartIndex && !bFooterEndIndex)
229 					{
230 						nFooterEndIndex = nIndex;
231 						bFooterEndIndex = sal_True;
232 					}
233 				}
234 				break;
235 				case CTF_PM_FOOTERFLAG:
236 				{
237 					if (!bFooterStartIndex)
238 					{
239 						nFooterStartIndex = nIndex;
240 						bFooterStartIndex = sal_True;
241 					}
242 					if (bHeaderStartIndex && !bHeaderEndIndex)
243 					{
244 						nHeaderEndIndex = nIndex;
245 						bHeaderEndIndex = sal_True;
246 					}
247 				}
248 				break;
249 			}
250 			nIndex++;
251 		}
252 		if (!bHeaderEndIndex)
253 			nHeaderEndIndex = nIndex;
254 		if (!bFooterEndIndex)
255 			nFooterEndIndex = nIndex;
256 
257         // export header style element
258         {
259             SvXMLElementExport aElem(
260                 GetExport(), XML_NAMESPACE_STYLE, XML_HEADER_STYLE,
261                 sal_True, sal_True );
262 
263             rPropExp.exportXML(
264                 GetExport(), rProperties,
265                 nHeaderStartIndex, nHeaderEndIndex, XML_EXPORT_FLAG_IGN_WS);
266         }
267 
268         // export footer style
269         {
270             SvXMLElementExport aElem(
271                 GetExport(), XML_NAMESPACE_STYLE, XML_FOOTER_STYLE,
272                 sal_True, sal_True );
273 
274             rPropExp.exportXML(
275                 GetExport(), rProperties,
276                 nFooterStartIndex, nFooterEndIndex, XML_EXPORT_FLAG_IGN_WS);
277         }
278 	}
279 }
280 
281 SvXMLAutoStylePoolP::SvXMLAutoStylePoolP()
282 {
283     DBG_ERROR("This constuctor is obsoleted and should not be used!");
284 	pImpl = NULL;
285 }
286 
287 SvXMLAutoStylePoolP::SvXMLAutoStylePoolP( SvXMLExport& rExport )
288 {
289 	pImpl = new SvXMLAutoStylePoolP_Impl( rExport );
290 }
291 
292 SvXMLAutoStylePoolP::~SvXMLAutoStylePoolP()
293 {
294 	delete pImpl;
295 }
296 
297 SvXMLExport& SvXMLAutoStylePoolP::GetExport() const
298 {
299     return pImpl->GetExport();
300 }
301 
302 // TODO: romove this
303 void SvXMLAutoStylePoolP::AddFamily(
304 		sal_Int32 nFamily,
305 		const OUString& rStrName,
306 		SvXMLExportPropertyMapper* pMapper,
307 		OUString aStrPrefix,
308 		sal_Bool bAsFamily )
309 {
310 	UniReference <SvXMLExportPropertyMapper> xTmp = pMapper;
311 	AddFamily( nFamily, rStrName, xTmp, aStrPrefix, bAsFamily );
312 }
313 
314 void SvXMLAutoStylePoolP::AddFamily(
315 		sal_Int32 nFamily,
316 		const OUString& rStrName,
317 		const UniReference < SvXMLExportPropertyMapper > & rMapper,
318 		const OUString& rStrPrefix,
319 		sal_Bool bAsFamily )
320 {
321 	pImpl->AddFamily( nFamily, rStrName, rMapper, rStrPrefix, bAsFamily );
322 }
323 
324 void SvXMLAutoStylePoolP::RegisterName( sal_Int32 nFamily,
325 								  	   const OUString& rName )
326 {
327 	pImpl->RegisterName( nFamily, rName );
328 }
329 
330 void SvXMLAutoStylePoolP::GetRegisteredNames(
331     uno::Sequence<sal_Int32>& rFamilies,
332     uno::Sequence<OUString>& rNames )
333 {
334     pImpl->GetRegisteredNames( rFamilies, rNames );
335 }
336 
337 void SvXMLAutoStylePoolP::RegisterNames(
338     uno::Sequence<sal_Int32>& aFamilies,
339     uno::Sequence<OUString>& aNames )
340 {
341     DBG_ASSERT( aFamilies.getLength() == aNames.getLength(),
342                 "aFamilies != aNames" );
343 
344     // iterate over sequence(s) and call RegisterName(..) for each pair
345     const sal_Int32* pFamilies = aFamilies.getConstArray();
346     const OUString* pNames = aNames.getConstArray();
347     sal_Int32 nCount = min( aFamilies.getLength(), aNames.getLength() );
348     for( sal_Int32 n = 0; n < nCount; n++ )
349         RegisterName( pFamilies[n], pNames[n] );
350 }
351 
352 OUString SvXMLAutoStylePoolP::Add( sal_Int32 nFamily,
353 								  const vector< XMLPropertyState >& rProperties )
354 {
355 	OUString sEmpty;
356 	OUString sName;
357 	pImpl->Add(sName, nFamily, sEmpty, rProperties );
358 	return sName;
359 }
360 
361 OUString SvXMLAutoStylePoolP::Add( sal_Int32 nFamily,
362 								  const OUString& rParent,
363 								  const vector< XMLPropertyState >& rProperties, bool bDontSeek )
364 {
365 	OUString sName;
366 	pImpl->Add(sName, nFamily, rParent, rProperties, sal_False, bDontSeek );
367 	return sName;
368 }
369 
370 sal_Bool SvXMLAutoStylePoolP::Add(OUString& rName, sal_Int32 nFamily, const ::std::vector< XMLPropertyState >& rProperties )
371 {
372 	OUString sEmpty;
373 	return pImpl->Add(rName, nFamily, sEmpty, rProperties);
374 }
375 
376 sal_Bool SvXMLAutoStylePoolP::Add(OUString& rName, sal_Int32 nFamily, const OUString& rParent, const ::std::vector< XMLPropertyState >& rProperties )
377 {
378 	return pImpl->Add(rName, nFamily, rParent, rProperties);
379 }
380 
381 sal_Bool SvXMLAutoStylePoolP::AddNamed( const OUString& rName, sal_Int32 nFamily, const OUString& rParent,
382                                         const ::std::vector< XMLPropertyState >& rProperties )
383 
384 {
385     return pImpl->AddNamed(rName, nFamily, rParent, rProperties);
386 }
387 
388 OUString SvXMLAutoStylePoolP::AddAndCache( sal_Int32 nFamily,
389 								  const vector< XMLPropertyState >& rProperties )
390 {
391 	OUString sEmpty;
392 	OUString sName;
393 	pImpl->Add(sName, nFamily, sEmpty, rProperties, sal_True );
394 	return sName;
395 }
396 
397 OUString SvXMLAutoStylePoolP::AddAndCache( sal_Int32 nFamily,
398 								  const OUString& rParent,
399 								  const vector< XMLPropertyState >& rProperties )
400 {
401 	OUString sName;
402 	pImpl->Add(sName, nFamily, rParent, rProperties, sal_True );
403 	return sName;
404 }
405 
406 OUString SvXMLAutoStylePoolP::AddAndCache( sal_Int32 nFamily,
407 								  const OUString& rParent )
408 {
409 	return pImpl->AddToCache( nFamily, rParent );
410 }
411 
412 OUString SvXMLAutoStylePoolP::Find( sal_Int32 nFamily,
413 							       const vector< XMLPropertyState >& rProperties ) const
414 {
415 	OUString sEmpty;
416 	return pImpl->Find( nFamily, sEmpty, rProperties );
417 }
418 
419 OUString SvXMLAutoStylePoolP::Find( sal_Int32 nFamily,
420 		                           const OUString& rParent,
421 							       const vector< XMLPropertyState >& rProperties ) const
422 {
423 	return pImpl->Find( nFamily, rParent, rProperties );
424 }
425 
426 OUString SvXMLAutoStylePoolP::FindAndRemoveCached( sal_Int32 nFamily ) const
427 {
428 	return pImpl->FindAndRemoveCached( nFamily );
429 }
430 
431 
432 void SvXMLAutoStylePoolP::exportXML( sal_Int32 nFamily,
433     const uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler > &,
434 	const SvXMLUnitConverter&,
435 	const SvXMLNamespaceMap&
436     ) const
437 {
438 	pImpl->exportXML( nFamily,
439                       GetExport().GetDocHandler(),
440                       GetExport().GetMM100UnitConverter(),
441 					  GetExport().GetNamespaceMap(),
442                       this);
443 }
444 
445 void SvXMLAutoStylePoolP::ClearEntries()
446 {
447 	pImpl->ClearEntries();
448 }
449