1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
42 OUString sInternalName
;
43 Reference
< XIndexReplace
> xNumRules
;
50 XMLTextListAutoStylePoolEntry_Impl(
52 const Reference
< XIndexReplace
> & rNumRules
,
53 XMLTextListAutoStylePoolNames_Impl
& rNames
,
54 const OUString
& rPrefix
,
57 XMLTextListAutoStylePoolEntry_Impl(
58 const Reference
< XIndexReplace
> & rNumRules
) :
59 xNumRules( rNumRules
),
63 Reference
< XNamed
> xNamed( xNumRules
, UNO_QUERY
);
66 sInternalName
= xNamed
->getName();
71 XMLTextListAutoStylePoolEntry_Impl(
72 const OUString
& rInternalName
) :
73 sInternalName( rInternalName
),
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(
88 const Reference
< XIndexReplace
> & rNumRules
,
89 XMLTextListAutoStylePoolNames_Impl
& rNames
,
90 const OUString
& rPrefix
,
92 xNumRules( rNumRules
),
96 Reference
< XNamed
> xNamed( xNumRules
, UNO_QUERY
);
99 sInternalName
= xNamed
->getName();
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 );
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
119 XMLTextListAutoStylePoolEntry_Impl
* const& r1
,
120 XMLTextListAutoStylePoolEntry_Impl
* const& r2
) const
125 return r1
->GetInternalName().compareTo( r2
->GetInternalName() ) < 0;
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
) :
143 pPool( new XMLTextListAutoStylePool_Impl
),
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
);
156 XMLTextListAutoStylePool::~XMLTextListAutoStylePool()
158 // The XMLTextListAutoStylePoolEntry_Impl object in the pool need delete explicitly in dtor.
159 pPool
->DeleteAndDestroyAll();
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 )
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
)
199 XMLTextListAutoStylePoolEntry_Impl
aTmp( rNumRules
);
201 sal_uInt32 nPos
= Find( &aTmp
);
202 if( nPos
!= (sal_uInt32
)-1 )
204 sName
= (*pPool
)[ nPos
]->GetName();
208 XMLTextListAutoStylePoolEntry_Impl
*pEntry
=
209 new XMLTextListAutoStylePoolEntry_Impl( pPool
->size(),
210 rNumRules
, m_aNames
, sPrefix
,
212 pPool
->insert( pEntry
);
213 sName
= pEntry
->GetName();
219 OUString
XMLTextListAutoStylePool::Find(
220 const Reference
< XIndexReplace
> & rNumRules
) const
223 XMLTextListAutoStylePoolEntry_Impl
aTmp( rNumRules
);
225 sal_uInt32 nPos
= Find( &aTmp
);
226 if( nPos
!= (sal_uInt32
)-1 )
227 sName
= (*pPool
)[ nPos
]->GetName();
232 OUString
XMLTextListAutoStylePool::Find(
233 const OUString
& rInternalName
) const
236 XMLTextListAutoStylePoolEntry_Impl
aTmp( rInternalName
);
237 sal_uInt32 nPos
= Find( &aTmp
);
238 if( nPos
!= (sal_uInt32
)-1 )
239 sName
= (*pPool
)[ nPos
]->GetName();
244 void XMLTextListAutoStylePool::exportXML() const
246 sal_uInt32 nCount
= pPool
->size();
250 XMLTextListAutoStylePoolEntry_Impl
**aExpEntries
=
251 new XMLTextListAutoStylePoolEntry_Impl
*[nCount
];
254 for( i
=0; i
< nCount
; i
++ )
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: */