1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: impastp4.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include <tools/debug.hxx>
34 #include <xmloff/xmlaustp.hxx>
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/nmspmap.hxx>
37 #include "xmlnmspe.hxx"
38 #include <xmloff/attrlist.hxx>
39 #include "impastpl.hxx"
40 #include <xmloff/xmlexppr.hxx>
41 #include <xmloff/xmlexp.hxx>
42 #include <xmloff/families.hxx>
43 #ifndef _XMLOFF_PAGEMASTERSTYLEMAP_HXX
44 #include <xmloff/PageMasterStyleMap.hxx>
47 using namespace ::std
;
48 using ::rtl::OUString
;
49 using ::rtl::OUStringBuffer
;
51 using namespace ::com::sun::star
;
52 using namespace ::xmloff::token
;
54 //#############################################################################
56 // Class SvXMLAutoStylePool_Impl
59 ///////////////////////////////////////////////////////////////////////////////
61 // ctor/dtor class SvXMLAutoStylePool_Impl
64 SvXMLAutoStylePoolP_Impl::SvXMLAutoStylePoolP_Impl( SvXMLExport
& rExp
)
70 SvXMLAutoStylePoolP_Impl::~SvXMLAutoStylePoolP_Impl()
73 XMLFamilyData_Impl
* pData
= maFamilyList
.Remove( ULONG(0) );
81 ///////////////////////////////////////////////////////////////////////////////
83 // Adds stylefamily-informations to sorted list
86 void SvXMLAutoStylePoolP_Impl::AddFamily(
88 const OUString
& rStrName
,
89 const UniReference
< SvXMLExportPropertyMapper
> & rMapper
,
90 const OUString
& rStrPrefix
,
93 // store family in a list if not already stored
96 sal_uInt16 nExportFlags
= GetExport().getExportFlags();
97 sal_Bool bStylesOnly
= (nExportFlags
& EXPORT_STYLES
) != 0 && (nExportFlags
& EXPORT_CONTENT
) == 0;
99 OUString
aPrefix( rStrPrefix
);
102 aPrefix
= OUString( 'M' );
103 aPrefix
+= rStrPrefix
;
106 XMLFamilyData_Impl
*pFamily
= new XMLFamilyData_Impl( nFamily
, rStrName
, rMapper
, aPrefix
, bAsFamily
);
107 if( !maFamilyList
.Seek_Entry( pFamily
, &nPos
) )
108 maFamilyList
.Insert( pFamily
);
113 ///////////////////////////////////////////////////////////////////////////////
115 // Adds a name to list
118 void SvXMLAutoStylePoolP_Impl::RegisterName( sal_Int32 nFamily
, const OUString
& rName
)
120 SvXMLAutoStylePoolNamesP_Impl
*pNames
= 0;
123 XMLFamilyData_Impl
aTmp( nFamily
);
124 if( maFamilyList
.Seek_Entry( &aTmp
, &nPos
) )
125 pNames
= maFamilyList
.GetObject( nPos
)->mpNameList
;
128 "SvXMLAutoStylePool_Impl::RegisterName: unknown family" );
131 OUString
*pName
= new OUString( rName
);
132 if( !pNames
->Insert( pName
) )
137 ///////////////////////////////////////////////////////////////////////////////
139 // Retrieve the list of registered names
142 void SvXMLAutoStylePoolP_Impl::GetRegisteredNames(
143 uno::Sequence
<sal_Int32
>& rFamilies
,
144 uno::Sequence
<OUString
>& rNames
)
146 // collect registered names + families
147 vector
<sal_Int32
> aFamilies
;
148 vector
<OUString
> aNames
;
150 // iterate over families
151 sal_uInt32 nCount
= maFamilyList
.Count();
152 for( sal_uInt32 i
= 0; i
< nCount
; i
++ )
154 XMLFamilyData_Impl
* pFamily
= maFamilyList
.GetObject( i
);
156 // iterate over names
157 SvXMLAutoStylePoolNamesP_Impl
* pNames
= pFamily
->mpNameList
;
158 sal_uInt32 nNames
= ( pNames
!= NULL
) ? pNames
->Count() : 0;
159 for( sal_uInt32 j
= 0; j
< nNames
; j
++ )
161 aFamilies
.push_back( pFamily
->mnFamily
);
162 aNames
.push_back( *pNames
->GetObject( j
) );
166 // copy the families + names into the sequence types
167 DBG_ASSERT( aFamilies
.size() == aNames
.size(), "families != names" );
169 rFamilies
.realloc( aFamilies
.size() );
170 std::copy( aFamilies
.begin(), aFamilies
.end(), rFamilies
.getArray() );
172 rNames
.realloc( aNames
.size() );
173 std::copy( aNames
.begin(), aNames
.end(), rNames
.getArray() );
176 ///////////////////////////////////////////////////////////////////////////////
178 // Adds a array of XMLPropertyState ( vector< XMLPropertyState > ) to list
179 // if not added, yet.
182 /*OUString SvXMLAutoStylePoolP_Impl::Add( sal_Int32 nFamily,
183 const OUString& rParent,
184 const vector< XMLPropertyState >& rProperties,
186 sal_Bool
SvXMLAutoStylePoolP_Impl::Add(OUString
& rName
, sal_Int32 nFamily
,
187 const OUString
& rParent
,
188 const ::std::vector
< XMLPropertyState
>& rProperties
,
192 sal_Bool
bRet(sal_False
);
195 XMLFamilyData_Impl
*pFamily
= 0;
196 XMLFamilyData_Impl
aTemporary( nFamily
);
197 if( maFamilyList
.Seek_Entry( &aTemporary
, &nPos
) )
199 pFamily
= maFamilyList
.GetObject( nPos
);
202 DBG_ASSERT( pFamily
, "SvXMLAutoStylePool_Impl::Add: unknown family" );
205 SvXMLAutoStylePoolParentP_Impl
aTmp( rParent
);
206 SvXMLAutoStylePoolParentP_Impl
*pParent
= 0;
208 SvXMLAutoStylePoolParentsP_Impl
*pParents
= pFamily
->mpParentList
;
209 if( pParents
->Seek_Entry( &aTmp
, &nPos
) )
211 pParent
= pParents
->GetObject( nPos
);
215 pParent
= new SvXMLAutoStylePoolParentP_Impl( rParent
);
216 pParents
->Insert( pParent
);
219 if( pParent
->Add( pFamily
, rProperties
, rName
, bDontSeek
) )
227 if( !pFamily
->pCache
)
228 pFamily
->pCache
= new SvXMLAutoStylePoolCache_Impl( 256, 256 );
229 if( pFamily
->pCache
->Count() < MAX_CACHE_SIZE
)
230 pFamily
->pCache
->Insert( new OUString( rName
),
231 pFamily
->pCache
->Count() );
238 sal_Bool
SvXMLAutoStylePoolP_Impl::AddNamed(const OUString
& rName
, sal_Int32 nFamily
,
239 const OUString
& rParent
, const ::std::vector
< XMLPropertyState
>& rProperties
)
241 // get family and parent the same way as in Add()
242 sal_Bool
bRet(sal_False
);
245 XMLFamilyData_Impl
*pFamily
= 0;
246 XMLFamilyData_Impl
aTemporary( nFamily
);
247 if( maFamilyList
.Seek_Entry( &aTemporary
, &nPos
) )
249 pFamily
= maFamilyList
.GetObject( nPos
);
252 DBG_ASSERT( pFamily
, "SvXMLAutoStylePool_Impl::Add: unknown family" );
255 SvXMLAutoStylePoolParentP_Impl
aTmp( rParent
);
256 SvXMLAutoStylePoolParentP_Impl
*pParent
= 0;
258 SvXMLAutoStylePoolParentsP_Impl
*pParents
= pFamily
->mpParentList
;
259 if( pParents
->Seek_Entry( &aTmp
, &nPos
) )
261 pParent
= pParents
->GetObject( nPos
);
265 pParent
= new SvXMLAutoStylePoolParentP_Impl( rParent
);
266 pParents
->Insert( pParent
);
269 if( pParent
->AddNamed( pFamily
, rProperties
, rName
) )
279 OUString
SvXMLAutoStylePoolP_Impl::AddToCache( sal_Int32 nFamily
,
280 const OUString
& rParent
)
284 XMLFamilyData_Impl
*pFamily
= 0;
285 XMLFamilyData_Impl
aTmp( nFamily
);
286 if( maFamilyList
.Seek_Entry( &aTmp
, &nPos
) )
288 pFamily
= maFamilyList
.GetObject( nPos
);
291 DBG_ASSERT( pFamily
, "SvXMLAutoStylePool_Impl::Add: unknown family" );
294 if( !pFamily
->pCache
)
295 pFamily
->pCache
= new SvXMLAutoStylePoolCache_Impl( 256, 256 );
296 if( pFamily
->pCache
->Count() < MAX_CACHE_SIZE
)
297 pFamily
->pCache
->Insert( new OUString( rParent
),
298 pFamily
->pCache
->Count() );
303 ///////////////////////////////////////////////////////////////////////////////
305 // Search for a array of XMLPropertyState ( vector< XMLPropertyState > ) in list
308 OUString
SvXMLAutoStylePoolP_Impl::Find( sal_Int32 nFamily
,
309 const OUString
& rParent
,
310 const vector
< XMLPropertyState
>& rProperties
) const
315 XMLFamilyData_Impl
aTemporary( nFamily
);
316 XMLFamilyData_Impl
*pFamily
= 0;
317 if( maFamilyList
.Seek_Entry( &aTemporary
, &nPos
) )
319 pFamily
= maFamilyList
.GetObject( nPos
);
322 DBG_ASSERT( pFamily
, "SvXMLAutoStylePool_Impl::Find: unknown family" );
326 SvXMLAutoStylePoolParentP_Impl
aTmp( rParent
);
328 const SvXMLAutoStylePoolParentsP_Impl
* pParents
=
329 pFamily
->mpParentList
;
330 if( pParents
->Seek_Entry( &aTmp
, &nPos
) )
331 sName
= pParents
->GetObject( nPos
)->Find( pFamily
, rProperties
);
337 OUString
SvXMLAutoStylePoolP_Impl::FindAndRemoveCached( sal_Int32 nFamily
) const
342 XMLFamilyData_Impl
aTmp( nFamily
);
343 XMLFamilyData_Impl
*pFamily
= 0;
344 if( maFamilyList
.Seek_Entry( &aTmp
, &nPos
) )
346 pFamily
= maFamilyList
.GetObject( nPos
);
349 DBG_ASSERT( pFamily
, "SvXMLAutoStylePool_Impl::Find: unknown family" );
353 DBG_ASSERT( pFamily
->pCache
, "family doesn't have a cache" );
355 // The cache may be empty already. This happens if it was filled
357 if( pFamily
->pCache
&& pFamily
->pCache
->Count() )
359 OUString
*pName
= pFamily
->pCache
->Remove( 0UL );
368 ///////////////////////////////////////////////////////////////////////////////
373 void SvXMLAutoStylePoolP_Impl::exportXML(
375 const uno::Reference
< ::com::sun::star::xml::sax::XDocumentHandler
> &,
376 const SvXMLUnitConverter
&,
377 const SvXMLNamespaceMap
&,
378 const SvXMLAutoStylePoolP
*pAntiImpl
) const
380 sal_uInt32 nCount
= 0;
382 // Get list of parents for current family (nFamily)
384 XMLFamilyData_Impl
aTmp( nFamily
);
385 XMLFamilyData_Impl
*pFamily
= 0;
386 if( maFamilyList
.Seek_Entry( &aTmp
, &nPos
) )
388 pFamily
= maFamilyList
.GetObject( nPos
);
389 nCount
= pFamily
->mnCount
;
393 "SvXMLAutoStylePool_Impl::exportXML: unknown family" );
394 if( pFamily
&& nCount
> 0 )
396 /////////////////////////////////////////////////////////////////////////////////////
397 // create, initialize and fill helper-structure (SvXMLAutoStylePoolProperties_Impl)
398 // wich contains a parent-name and a SvXMLAutoStylePoolProperties_Impl
400 const SvXMLAutoStylePoolParentsP_Impl
*pParents
=
401 pFamily
->mpParentList
;
403 SvXMLAutoStylePoolPExport_Impl
* aExpStyles
=
404 new SvXMLAutoStylePoolPExport_Impl
[nCount
];
407 for( i
=0; i
< nCount
; i
++ )
409 aExpStyles
[i
].mpParent
= 0;
410 aExpStyles
[i
].mpProperties
= 0;
413 sal_uInt32 nParents
= pParents
->Count();
414 for( i
=0; i
< nParents
; i
++ )
416 const SvXMLAutoStylePoolParentP_Impl
* pParent
=
417 pParents
->GetObject( i
);
418 sal_uInt32 nProperties
= pParent
->GetPropertiesList().Count();
419 for( sal_uInt32 j
=0; j
< nProperties
; j
++ )
421 const SvXMLAutoStylePoolPropertiesP_Impl
*pProperties
=
422 pParent
->GetPropertiesList().GetObject( j
);
423 nPos
= pProperties
->GetPos();
424 DBG_ASSERT( nPos
< nCount
,
425 "SvXMLAutoStylePool_Impl::exportXML: wrong position" );
428 DBG_ASSERT( !aExpStyles
[nPos
].mpProperties
,
429 "SvXMLAutoStylePool_Impl::exportXML: double position" );
430 aExpStyles
[nPos
].mpProperties
= pProperties
;
431 aExpStyles
[nPos
].mpParent
= &pParent
->GetParent();
436 /////////////////////////////////////////////////////////////////////////////////////
438 // create string to export for each XML-style. That means for each property-list
440 OUString aStrFamilyName
= pFamily
->maStrFamilyName
;
442 for( i
=0; i
<nCount
; i
++ )
444 DBG_ASSERT( aExpStyles
[i
].mpProperties
,
445 "SvXMLAutoStylePool_Impl::exportXML: empty position" );
447 if( aExpStyles
[i
].mpProperties
)
449 GetExport().AddAttribute(
450 XML_NAMESPACE_STYLE
, XML_NAME
,
451 aExpStyles
[i
].mpProperties
->GetName() );
453 if( pFamily
->bAsFamily
)
455 GetExport().AddAttribute(
456 XML_NAMESPACE_STYLE
, XML_FAMILY
, aStrFamilyName
);
459 if( aExpStyles
[i
].mpParent
->getLength() )
461 GetExport().AddAttribute(
462 XML_NAMESPACE_STYLE
, XML_PARENT_STYLE_NAME
,
463 GetExport().EncodeStyleName(
464 *aExpStyles
[i
].mpParent
) );
468 if( pFamily
->bAsFamily
)
469 sName
= GetXMLToken(XML_STYLE
);
471 sName
= pFamily
->maStrFamilyName
;
473 pAntiImpl
->exportStyleAttributes(
474 GetExport().GetAttrList(),
476 aExpStyles
[i
].mpProperties
->GetProperties(),
477 *pFamily
->mxMapper
.get()
478 , GetExport().GetMM100UnitConverter(),
479 GetExport().GetNamespaceMap()
482 SvXMLElementExport
aElem( GetExport(),
483 XML_NAMESPACE_STYLE
, sName
,
484 sal_True
, sal_True
);
486 sal_Int32
nStart(-1);
488 if (nFamily
== XML_STYLE_FAMILY_PAGE_MASTER
)
491 sal_Int32 nIndex
= 0;
492 UniReference
< XMLPropertySetMapper
> aPropMapper
=
493 pFamily
->mxMapper
->getPropertySetMapper();
494 sal_Int16 nContextID
;
495 while(nIndex
< aPropMapper
->GetEntryCount() && nEnd
== -1)
497 nContextID
= aPropMapper
->GetEntryContextId( nIndex
);
498 if (nContextID
&& ((nContextID
& CTF_PM_FLAGMASK
) != XML_PM_CTF_START
))
506 pFamily
->mxMapper
->exportXML(
508 aExpStyles
[i
].mpProperties
->GetProperties(),
509 nStart
, nEnd
, XML_EXPORT_FLAG_IGN_WS
);
511 pAntiImpl
->exportStyleContent(
512 GetExport().GetDocHandler(),
514 aExpStyles
[i
].mpProperties
->GetProperties(),
515 *pFamily
->mxMapper
.get(),
516 GetExport().GetMM100UnitConverter(),
517 GetExport().GetNamespaceMap()
526 void SvXMLAutoStylePoolP_Impl::ClearEntries()
528 for(sal_uInt32 a
= 0L; a
< maFamilyList
.Count(); a
++)
529 maFamilyList
[a
]->ClearEntries();