bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / text / XMLTextListAutoStylePool.cxx
blobfd976361e866352dc6cd442e4b38b8f8216f7639
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/debug.hxx>
21 #include <tools/solar.h>
22 #include <o3tl/sorted_vector.hxx>
23 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
24 #include <com/sun/star/container/XNamed.hpp>
25 #include <com/sun/star/container/XIndexReplace.hpp>
26 #include <rtl/ustrbuf.hxx>
27 #include <xmloff/xmlnume.hxx>
28 #include <xmloff/XMLTextListAutoStylePool.hxx>
29 #include <xmloff/xmlexp.hxx>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::beans;
35 using namespace ::com::sun::star::container;
36 using namespace ::com::sun::star::style;
39 class XMLTextListAutoStylePoolEntry_Impl
41 OUString sName;
42 OUString sInternalName;
43 Reference < XIndexReplace > xNumRules;
44 sal_uInt32 nPos;
45 bool bIsNamed;
48 public:
50 XMLTextListAutoStylePoolEntry_Impl(
51 sal_uInt32 nPos,
52 const Reference < XIndexReplace > & rNumRules,
53 XMLTextListAutoStylePoolNames_Impl& rNames,
54 const OUString& rPrefix,
55 sal_uInt32& rName );
57 XMLTextListAutoStylePoolEntry_Impl(
58 const Reference < XIndexReplace > & rNumRules ) :
59 xNumRules( rNumRules ),
60 nPos( 0 ),
61 bIsNamed( false )
63 Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
64 if( xNamed.is() )
66 sInternalName = xNamed->getName();
67 bIsNamed = true;
71 XMLTextListAutoStylePoolEntry_Impl(
72 const OUString& rInternalName ) :
73 sInternalName( rInternalName ),
74 nPos( 0 ),
75 bIsNamed( true )
79 const OUString& GetName() const { return sName; }
80 const OUString& GetInternalName() const { return sInternalName; }
81 const Reference < XIndexReplace > & GetNumRules() const { return xNumRules; }
82 sal_uInt32 GetPos() const { return nPos; }
83 bool IsNamed() const { return bIsNamed; }
86 XMLTextListAutoStylePoolEntry_Impl::XMLTextListAutoStylePoolEntry_Impl(
87 sal_uInt32 nP,
88 const Reference < XIndexReplace > & rNumRules,
89 XMLTextListAutoStylePoolNames_Impl& rNames,
90 const OUString& rPrefix,
91 sal_uInt32& rName ) :
92 xNumRules( rNumRules ),
93 nPos( nP ),
94 bIsNamed( false )
96 Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
97 if( xNamed.is() )
99 sInternalName = xNamed->getName();
100 bIsNamed = true;
103 // create a name that hasn't been used before. The created name has not
104 // to be added to the array, because it will never tried again
105 OUStringBuffer sBuffer( 7 );
108 rName++;
109 sBuffer.append( rPrefix );
110 sBuffer.append( (sal_Int32)rName );
111 sName = sBuffer.makeStringAndClear();
113 while (rNames.find(sName) != rNames.end());
116 struct XMLTextListAutoStylePoolEntryCmp_Impl
118 bool operator()(
119 XMLTextListAutoStylePoolEntry_Impl* const& r1,
120 XMLTextListAutoStylePoolEntry_Impl* const& r2 ) const
122 if( r1->IsNamed() )
124 if( r2->IsNamed() )
125 return r1->GetInternalName().compareTo( r2->GetInternalName() ) < 0;
126 else
127 return true;
129 else
131 if( r2->IsNamed() )
132 return false;
133 else
134 return r1->GetNumRules().get() < r2->GetNumRules().get();
138 class XMLTextListAutoStylePool_Impl : public o3tl::sorted_vector<XMLTextListAutoStylePoolEntry_Impl*, XMLTextListAutoStylePoolEntryCmp_Impl> {};
140 XMLTextListAutoStylePool::XMLTextListAutoStylePool( SvXMLExport& rExp ) :
141 rExport( rExp ),
142 sPrefix( "L" ),
143 pPool( new XMLTextListAutoStylePool_Impl ),
144 nName( 0 )
146 Reference<ucb::XAnyCompareFactory> xCompareFac( rExp.GetModel(), uno::UNO_QUERY );
147 if( xCompareFac.is() )
148 mxNumRuleCompare = xCompareFac->createAnyCompareByName( OUString( "NumberingRules" ) );
149 SvXMLExportFlags nExportFlags = rExport.getExportFlags();
150 bool bStylesOnly = (nExportFlags & SvXMLExportFlags::STYLES) && !(nExportFlags & SvXMLExportFlags::CONTENT);
151 if( bStylesOnly )
152 sPrefix = "ML";
156 XMLTextListAutoStylePool::~XMLTextListAutoStylePool()
158 // The XMLTextListAutoStylePoolEntry_Impl object in the pool need delete explicitly in dtor.
159 pPool->DeleteAndDestroyAll();
160 delete pPool;
163 void XMLTextListAutoStylePool::RegisterName( const OUString& rName )
165 m_aNames.insert(rName);
168 sal_uInt32 XMLTextListAutoStylePool::Find( XMLTextListAutoStylePoolEntry_Impl* pEntry ) const
170 if( !pEntry->IsNamed() && mxNumRuleCompare.is() )
172 const sal_uInt32 nCount = pPool->size();
174 uno::Any aAny1, aAny2;
175 aAny1 <<= pEntry->GetNumRules();
177 for( sal_uLong nPos = 0; nPos < nCount; nPos++ )
179 aAny2 <<= (*pPool)[nPos]->GetNumRules();
181 if( mxNumRuleCompare->compare( aAny1, aAny2 ) == 0 )
182 return nPos;
185 else
187 XMLTextListAutoStylePool_Impl::const_iterator it = pPool->find( pEntry );
188 if( it != pPool->end() )
189 return it - pPool->begin();
192 return (sal_uInt32)-1;
195 OUString XMLTextListAutoStylePool::Add(
196 const Reference < XIndexReplace > & rNumRules )
198 OUString sName;
199 XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
201 sal_uInt32 nPos = Find( &aTmp );
202 if( nPos != (sal_uInt32)-1 )
204 sName = (*pPool)[ nPos ]->GetName();
206 else
208 XMLTextListAutoStylePoolEntry_Impl *pEntry =
209 new XMLTextListAutoStylePoolEntry_Impl( pPool->size(),
210 rNumRules, m_aNames, sPrefix,
211 nName );
212 pPool->insert( pEntry );
213 sName = pEntry->GetName();
216 return sName;
219 OUString XMLTextListAutoStylePool::Find(
220 const Reference < XIndexReplace > & rNumRules ) const
222 OUString sName;
223 XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
225 sal_uInt32 nPos = Find( &aTmp );
226 if( nPos != (sal_uInt32)-1 )
227 sName = (*pPool)[ nPos ]->GetName();
229 return sName;
232 OUString XMLTextListAutoStylePool::Find(
233 const OUString& rInternalName ) const
235 OUString sName;
236 XMLTextListAutoStylePoolEntry_Impl aTmp( rInternalName );
237 sal_uInt32 nPos = Find( &aTmp );
238 if( nPos != (sal_uInt32)-1 )
239 sName = (*pPool)[ nPos ]->GetName();
241 return sName;
244 void XMLTextListAutoStylePool::exportXML() const
246 sal_uInt32 nCount = pPool->size();
247 if( !nCount )
248 return;
250 XMLTextListAutoStylePoolEntry_Impl **aExpEntries =
251 new XMLTextListAutoStylePoolEntry_Impl*[nCount];
253 sal_uInt32 i;
254 for( i=0; i < nCount; i++ )
256 aExpEntries[i] = 0;
258 for( i=0; i < nCount; i++ )
260 XMLTextListAutoStylePoolEntry_Impl *pEntry = (*pPool)[i];
261 DBG_ASSERT( pEntry->GetPos() < nCount, "Illegal pos" );
262 aExpEntries[pEntry->GetPos()] = pEntry;
265 SvxXMLNumRuleExport aNumRuleExp( rExport );
267 for( i=0; i < nCount; i++ )
269 XMLTextListAutoStylePoolEntry_Impl *pEntry = aExpEntries[i];
270 aNumRuleExp.exportNumberingRule( pEntry->GetName(), false,
271 pEntry->GetNumRules() );
273 delete [] aExpEntries;
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */