merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / text / XMLTextListAutoStylePool.cxx
blob26a4c4309ed0411aae919f8f98894df551a15d90
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLTextListAutoStylePool.cxx,v $
10 * $Revision: 1.12 $
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,
54 const OUString& r2 )
56 return (int)r1.compareTo( r2 );
59 DECLARE_CONTAINER_SORT_DEL( XMLTextListAutoStylePoolNames_Impl,
60 OUString )
61 IMPL_CONTAINER_SORT( XMLTextListAutoStylePoolNames_Impl,
62 OUString,
63 XMLTextListAutoStylePoolNameCmp_Impl )
65 class XMLTextListAutoStylePoolEntry_Impl
67 OUString sName;
68 OUString sInternalName;
69 Reference < XIndexReplace > xNumRules;
70 sal_uInt32 nPos;
71 sal_Bool bIsNamed;
74 public:
76 XMLTextListAutoStylePoolEntry_Impl(
77 sal_uInt32 nPos,
78 const Reference < XIndexReplace > & rNumRules,
79 XMLTextListAutoStylePoolNames_Impl& rNames,
80 const OUString& rPrefix,
81 sal_uInt32& rName );
83 XMLTextListAutoStylePoolEntry_Impl(
84 const Reference < XIndexReplace > & rNumRules ) :
85 xNumRules( rNumRules ),
86 nPos( 0 ),
87 bIsNamed( sal_False )
89 Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
90 if( xNamed.is() )
92 sInternalName = xNamed->getName();
93 bIsNamed = sal_True;
97 XMLTextListAutoStylePoolEntry_Impl(
98 const OUString& rInternalName ) :
99 sInternalName( rInternalName ),
100 nPos( 0 ),
101 bIsNamed( sal_True )
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(
113 sal_uInt32 nP,
114 const Reference < XIndexReplace > & rNumRules,
115 XMLTextListAutoStylePoolNames_Impl& rNames,
116 const OUString& rPrefix,
117 sal_uInt32& rName ) :
118 xNumRules( rNumRules ),
119 nPos( nP ),
120 bIsNamed( sal_False )
122 Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
123 if( xNamed.is() )
125 sInternalName = xNamed->getName();
126 bIsNamed = sal_True;
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 );
134 rName++;
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 )
146 int nRet;
147 if( r1.IsNamed() )
149 if( r2.IsNamed() )
150 nRet = (int)r1.GetInternalName().compareTo( r2.GetInternalName());
151 else
152 nRet = -1;
154 else
156 if( r2.IsNamed() )
157 nRet = 1;
158 else
159 nRet = (int)(r1.GetNumRules().get() - r2.GetNumRules().get());
162 return nRet;
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 ) :
173 rExport( rExp ),
174 sPrefix( RTL_CONSTASCII_USTRINGPARAM("L") ),
175 pPool( new XMLTextListAutoStylePool_Impl( 5, 5 ) ),
176 pNames( new XMLTextListAutoStylePoolNames_Impl( 5, 5 ) ),
177 nName( 0 )
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;
184 if( bStylesOnly )
185 sPrefix = OUString( RTL_CONSTASCII_USTRINGPARAM("ML") );
189 XMLTextListAutoStylePool::~XMLTextListAutoStylePool()
191 delete pPool;
192 delete pNames;
195 void XMLTextListAutoStylePool::RegisterName( const OUString& rName )
197 OUString *pName = new OUString( rName );
198 if( !pNames->Insert( pName ) )
199 delete 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
209 ULONG nPos;
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 )
222 return nPos;
225 else if( pPool->Seek_Entry( pEntry, &nPos ) )
227 return nPos;
230 return (sal_uInt32)-1;
233 OUString XMLTextListAutoStylePool::Add(
234 const Reference < XIndexReplace > & rNumRules )
236 OUString sName;
237 XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
239 sal_uInt32 nPos = Find( &aTmp );
240 if( nPos != (sal_uInt32)-1 )
242 sName = pPool->GetObject( nPos )->GetName();
244 else
246 XMLTextListAutoStylePoolEntry_Impl *pEntry =
247 new XMLTextListAutoStylePoolEntry_Impl( pPool->Count(),
248 rNumRules, *pNames, sPrefix,
249 nName );
250 pPool->Insert( pEntry );
251 sName = pEntry->GetName();
254 return sName;
257 ::rtl::OUString XMLTextListAutoStylePool::Find(
258 const Reference < XIndexReplace > & rNumRules ) const
260 OUString sName;
261 XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
263 sal_uInt32 nPos = Find( &aTmp );
264 if( nPos != (sal_uInt32)-1 )
265 sName = pPool->GetObject( nPos )->GetName();
267 return sName;
270 ::rtl::OUString XMLTextListAutoStylePool::Find(
271 const OUString& rInternalName ) const
273 OUString sName;
274 XMLTextListAutoStylePoolEntry_Impl aTmp( rInternalName );
275 sal_uInt32 nPos = Find( &aTmp );
276 if( nPos != (sal_uInt32)-1 )
277 sName = pPool->GetObject( nPos )->GetName();
279 return sName;
282 void XMLTextListAutoStylePool::exportXML() const
284 sal_uInt32 nCount = pPool->Count();
285 if( !nCount )
286 return;
288 XMLTextListAutoStylePoolEntry_Impl **aExpEntries =
289 new XMLTextListAutoStylePoolEntryPtr[nCount];
291 sal_uInt32 i;
292 for( i=0; i < nCount; i++ )
294 aExpEntries[i] = 0;
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;