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: XMLTextListAutoStylePool.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 <svtools/cntnrsrt.hxx>
35 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
36 #include <com/sun/star/container/XNamed.hpp>
37 #include <com/sun/star/container/XIndexReplace.hpp>
38 #include <rtl/ustrbuf.hxx>
39 #include <xmloff/xmlnume.hxx>
40 #include "XMLTextListAutoStylePool.hxx"
41 #include <xmloff/xmlexp.hxx>
43 using ::rtl::OUString
;
44 using ::rtl::OUStringBuffer
;
46 using namespace ::com::sun::star
;
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::beans
;
49 using namespace ::com::sun::star::container
;
50 using namespace ::com::sun::star::style
;
53 int XMLTextListAutoStylePoolNameCmp_Impl( const OUString
& r1
,
56 return (int)r1
.compareTo( r2
);
59 DECLARE_CONTAINER_SORT_DEL( XMLTextListAutoStylePoolNames_Impl
,
61 IMPL_CONTAINER_SORT( XMLTextListAutoStylePoolNames_Impl
,
63 XMLTextListAutoStylePoolNameCmp_Impl
)
65 class XMLTextListAutoStylePoolEntry_Impl
68 OUString sInternalName
;
69 Reference
< XIndexReplace
> xNumRules
;
76 XMLTextListAutoStylePoolEntry_Impl(
78 const Reference
< XIndexReplace
> & rNumRules
,
79 XMLTextListAutoStylePoolNames_Impl
& rNames
,
80 const OUString
& rPrefix
,
83 XMLTextListAutoStylePoolEntry_Impl(
84 const Reference
< XIndexReplace
> & rNumRules
) :
85 xNumRules( rNumRules
),
89 Reference
< XNamed
> xNamed( xNumRules
, UNO_QUERY
);
92 sInternalName
= xNamed
->getName();
97 XMLTextListAutoStylePoolEntry_Impl(
98 const OUString
& rInternalName
) :
99 sInternalName( rInternalName
),
105 const OUString
& GetName() const { return sName
; }
106 const OUString
& GetInternalName() const { return sInternalName
; }
107 const Reference
< XIndexReplace
> & GetNumRules() const { return xNumRules
; }
108 sal_uInt32
GetPos() const { return nPos
; }
109 sal_Bool
IsNamed() const { return bIsNamed
; }
112 XMLTextListAutoStylePoolEntry_Impl::XMLTextListAutoStylePoolEntry_Impl(
114 const Reference
< XIndexReplace
> & rNumRules
,
115 XMLTextListAutoStylePoolNames_Impl
& rNames
,
116 const OUString
& rPrefix
,
117 sal_uInt32
& rName
) :
118 xNumRules( rNumRules
),
120 bIsNamed( sal_False
)
122 Reference
< XNamed
> xNamed( xNumRules
, UNO_QUERY
);
125 sInternalName
= xNamed
->getName();
129 // create a name that hasn't been used before. The created name has not
130 // to be added to the array, because it will never tried again
131 OUStringBuffer
sBuffer( 7 );
135 sBuffer
.append( rPrefix
);
136 sBuffer
.append( (sal_Int32
)rName
);
137 sName
= sBuffer
.makeStringAndClear();
139 while( rNames
.Seek_Entry( &sName
, 0 ) );
142 int XMLTextListAutoStylePoolEntryCmp_Impl(
143 const XMLTextListAutoStylePoolEntry_Impl
& r1
,
144 const XMLTextListAutoStylePoolEntry_Impl
& r2
)
150 nRet
= (int)r1
.GetInternalName().compareTo( r2
.GetInternalName());
159 nRet
= (int)(r1
.GetNumRules().get() - r2
.GetNumRules().get());
165 typedef XMLTextListAutoStylePoolEntry_Impl
*XMLTextListAutoStylePoolEntryPtr
;
166 DECLARE_CONTAINER_SORT( XMLTextListAutoStylePool_Impl
,
167 XMLTextListAutoStylePoolEntry_Impl
)
168 IMPL_CONTAINER_SORT( XMLTextListAutoStylePool_Impl
,
169 XMLTextListAutoStylePoolEntry_Impl
,
170 XMLTextListAutoStylePoolEntryCmp_Impl
)
172 XMLTextListAutoStylePool::XMLTextListAutoStylePool( SvXMLExport
& rExp
) :
174 sPrefix( RTL_CONSTASCII_USTRINGPARAM("L") ),
175 pPool( new XMLTextListAutoStylePool_Impl( 5, 5 ) ),
176 pNames( new XMLTextListAutoStylePoolNames_Impl( 5, 5 ) ),
179 Reference
<ucb::XAnyCompareFactory
> xCompareFac( rExp
.GetModel(), uno::UNO_QUERY
);
180 if( xCompareFac
.is() )
181 mxNumRuleCompare
= xCompareFac
->createAnyCompareByName( OUString( RTL_CONSTASCII_USTRINGPARAM( "NumberingRules" ) ) );
182 sal_uInt16 nExportFlags
= rExport
.getExportFlags();
183 sal_Bool bStylesOnly
= (nExportFlags
& EXPORT_STYLES
) != 0 && (nExportFlags
& EXPORT_CONTENT
) == 0;
185 sPrefix
= OUString( RTL_CONSTASCII_USTRINGPARAM("ML") );
189 XMLTextListAutoStylePool::~XMLTextListAutoStylePool()
195 void XMLTextListAutoStylePool::RegisterName( const OUString
& rName
)
197 OUString
*pName
= new OUString( rName
);
198 if( !pNames
->Insert( pName
) )
202 sal_Bool
XMLTextListAutoStylePool::HasName( const OUString
& rName
) const
204 return pNames
->Seek_Entry( &rName
, 0 );
207 sal_uInt32
XMLTextListAutoStylePool::Find( XMLTextListAutoStylePoolEntry_Impl
* pEntry
) const
210 if( !pEntry
->IsNamed() && mxNumRuleCompare
.is() )
212 const sal_uInt32 nCount
= pPool
->Count();
214 uno::Any aAny1
, aAny2
;
215 aAny1
<<= pEntry
->GetNumRules();
217 for( nPos
= 0; nPos
< nCount
; nPos
++ )
219 aAny2
<<= pPool
->GetObject(nPos
)->GetNumRules();
221 if( mxNumRuleCompare
->compare( aAny1
, aAny2
) == 0 )
225 else if( pPool
->Seek_Entry( pEntry
, &nPos
) )
230 return (sal_uInt32
)-1;
233 OUString
XMLTextListAutoStylePool::Add(
234 const Reference
< XIndexReplace
> & rNumRules
)
237 XMLTextListAutoStylePoolEntry_Impl
aTmp( rNumRules
);
239 sal_uInt32 nPos
= Find( &aTmp
);
240 if( nPos
!= (sal_uInt32
)-1 )
242 sName
= pPool
->GetObject( nPos
)->GetName();
246 XMLTextListAutoStylePoolEntry_Impl
*pEntry
=
247 new XMLTextListAutoStylePoolEntry_Impl( pPool
->Count(),
248 rNumRules
, *pNames
, sPrefix
,
250 pPool
->Insert( pEntry
);
251 sName
= pEntry
->GetName();
257 ::rtl::OUString
XMLTextListAutoStylePool::Find(
258 const Reference
< XIndexReplace
> & rNumRules
) const
261 XMLTextListAutoStylePoolEntry_Impl
aTmp( rNumRules
);
263 sal_uInt32 nPos
= Find( &aTmp
);
264 if( nPos
!= (sal_uInt32
)-1 )
265 sName
= pPool
->GetObject( nPos
)->GetName();
270 ::rtl::OUString
XMLTextListAutoStylePool::Find(
271 const OUString
& rInternalName
) const
274 XMLTextListAutoStylePoolEntry_Impl
aTmp( rInternalName
);
275 sal_uInt32 nPos
= Find( &aTmp
);
276 if( nPos
!= (sal_uInt32
)-1 )
277 sName
= pPool
->GetObject( nPos
)->GetName();
282 void XMLTextListAutoStylePool::exportXML() const
284 sal_uInt32 nCount
= pPool
->Count();
288 XMLTextListAutoStylePoolEntry_Impl
**aExpEntries
=
289 new XMLTextListAutoStylePoolEntryPtr
[nCount
];
292 for( i
=0; i
< nCount
; i
++ )
296 for( i
=0; i
< nCount
; i
++ )
298 XMLTextListAutoStylePoolEntry_Impl
*pEntry
= pPool
->GetObject(i
);
299 DBG_ASSERT( pEntry
->GetPos() < nCount
, "Illegal pos" );
300 aExpEntries
[pEntry
->GetPos()] = pEntry
;
303 SvxXMLNumRuleExport
aNumRuleExp( rExport
);
305 for( i
=0; i
< nCount
; i
++ )
307 XMLTextListAutoStylePoolEntry_Impl
*pEntry
= aExpEntries
[i
];
308 aNumRuleExp
.exportNumberingRule( pEntry
->GetName(),
309 pEntry
->GetNumRules() );
311 delete [] aExpEntries
;