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 <o3tl/sorted_vector.hxx>
22 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
23 #include <com/sun/star/container/XNamed.hpp>
24 #include <com/sun/star/container/XIndexReplace.hpp>
25 #include <rtl/ustrbuf.hxx>
26 #include <xmloff/xmlnume.hxx>
27 #include "xmloff/XMLTextListAutoStylePool.hxx"
28 #include <xmloff/xmlexp.hxx>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::beans
;
34 using namespace ::com::sun::star::container
;
35 using namespace ::com::sun::star::style
;
38 class XMLTextListAutoStylePoolEntry_Impl
41 OUString sInternalName
;
42 Reference
< XIndexReplace
> xNumRules
;
49 XMLTextListAutoStylePoolEntry_Impl(
51 const Reference
< XIndexReplace
> & rNumRules
,
52 XMLTextListAutoStylePoolNames_Impl
& rNames
,
53 const OUString
& rPrefix
,
56 XMLTextListAutoStylePoolEntry_Impl(
57 const Reference
< XIndexReplace
> & rNumRules
) :
58 xNumRules( rNumRules
),
62 Reference
< XNamed
> xNamed( xNumRules
, UNO_QUERY
);
65 sInternalName
= xNamed
->getName();
70 XMLTextListAutoStylePoolEntry_Impl(
71 const OUString
& rInternalName
) :
72 sInternalName( rInternalName
),
78 const OUString
& GetName() const { return sName
; }
79 const OUString
& GetInternalName() const { return sInternalName
; }
80 const Reference
< XIndexReplace
> & GetNumRules() const { return xNumRules
; }
81 sal_uInt32
GetPos() const { return nPos
; }
82 sal_Bool
IsNamed() const { return bIsNamed
; }
85 XMLTextListAutoStylePoolEntry_Impl::XMLTextListAutoStylePoolEntry_Impl(
87 const Reference
< XIndexReplace
> & rNumRules
,
88 XMLTextListAutoStylePoolNames_Impl
& rNames
,
89 const OUString
& rPrefix
,
91 xNumRules( rNumRules
),
95 Reference
< XNamed
> xNamed( xNumRules
, UNO_QUERY
);
98 sInternalName
= xNamed
->getName();
102 // create a name that hasn't been used before. The created name has not
103 // to be added to the array, because it will never tried again
104 OUStringBuffer
sBuffer( 7 );
108 sBuffer
.append( rPrefix
);
109 sBuffer
.append( (sal_Int32
)rName
);
110 sName
= sBuffer
.makeStringAndClear();
112 while (rNames
.find(sName
) != rNames
.end());
115 struct XMLTextListAutoStylePoolEntryCmp_Impl
118 XMLTextListAutoStylePoolEntry_Impl
* const& r1
,
119 XMLTextListAutoStylePoolEntry_Impl
* const& r2
) const
124 return r1
->GetInternalName().compareTo( r2
->GetInternalName() ) < 0;
133 return r1
->GetNumRules().get() < r2
->GetNumRules().get();
137 class XMLTextListAutoStylePool_Impl
: public o3tl::sorted_vector
<XMLTextListAutoStylePoolEntry_Impl
*, XMLTextListAutoStylePoolEntryCmp_Impl
> {};
139 XMLTextListAutoStylePool::XMLTextListAutoStylePool( SvXMLExport
& rExp
) :
142 pPool( new XMLTextListAutoStylePool_Impl
),
145 Reference
<ucb::XAnyCompareFactory
> xCompareFac( rExp
.GetModel(), uno::UNO_QUERY
);
146 if( xCompareFac
.is() )
147 mxNumRuleCompare
= xCompareFac
->createAnyCompareByName( OUString( "NumberingRules" ) );
148 sal_uInt16 nExportFlags
= rExport
.getExportFlags();
149 sal_Bool bStylesOnly
= (nExportFlags
& EXPORT_STYLES
) != 0 && (nExportFlags
& EXPORT_CONTENT
) == 0;
151 sPrefix
= OUString( "ML" );
155 XMLTextListAutoStylePool::~XMLTextListAutoStylePool()
157 // The XMLTextListAutoStylePoolEntry_Impl object in the pool need delete explicitly in dtor.
158 pPool
->DeleteAndDestroyAll();
162 void XMLTextListAutoStylePool::RegisterName( const OUString
& rName
)
164 m_aNames
.insert(rName
);
167 sal_uInt32
XMLTextListAutoStylePool::Find( XMLTextListAutoStylePoolEntry_Impl
* pEntry
) const
169 if( !pEntry
->IsNamed() && mxNumRuleCompare
.is() )
171 const sal_uInt32 nCount
= pPool
->size();
173 uno::Any aAny1
, aAny2
;
174 aAny1
<<= pEntry
->GetNumRules();
176 for( sal_uLong nPos
= 0; nPos
< nCount
; nPos
++ )
178 aAny2
<<= (*pPool
)[nPos
]->GetNumRules();
180 if( mxNumRuleCompare
->compare( aAny1
, aAny2
) == 0 )
186 XMLTextListAutoStylePool_Impl::const_iterator it
= pPool
->find( pEntry
);
187 if( it
!= pPool
->end() )
188 return it
- pPool
->begin();
191 return (sal_uInt32
)-1;
194 OUString
XMLTextListAutoStylePool::Add(
195 const Reference
< XIndexReplace
> & rNumRules
)
198 XMLTextListAutoStylePoolEntry_Impl
aTmp( rNumRules
);
200 sal_uInt32 nPos
= Find( &aTmp
);
201 if( nPos
!= (sal_uInt32
)-1 )
203 sName
= (*pPool
)[ nPos
]->GetName();
207 XMLTextListAutoStylePoolEntry_Impl
*pEntry
=
208 new XMLTextListAutoStylePoolEntry_Impl( pPool
->size(),
209 rNumRules
, m_aNames
, sPrefix
,
211 pPool
->insert( pEntry
);
212 sName
= pEntry
->GetName();
218 OUString
XMLTextListAutoStylePool::Find(
219 const Reference
< XIndexReplace
> & rNumRules
) const
222 XMLTextListAutoStylePoolEntry_Impl
aTmp( rNumRules
);
224 sal_uInt32 nPos
= Find( &aTmp
);
225 if( nPos
!= (sal_uInt32
)-1 )
226 sName
= (*pPool
)[ nPos
]->GetName();
231 OUString
XMLTextListAutoStylePool::Find(
232 const OUString
& rInternalName
) const
235 XMLTextListAutoStylePoolEntry_Impl
aTmp( rInternalName
);
236 sal_uInt32 nPos
= Find( &aTmp
);
237 if( nPos
!= (sal_uInt32
)-1 )
238 sName
= (*pPool
)[ nPos
]->GetName();
243 void XMLTextListAutoStylePool::exportXML() const
245 sal_uInt32 nCount
= pPool
->size();
249 XMLTextListAutoStylePoolEntry_Impl
**aExpEntries
=
250 new XMLTextListAutoStylePoolEntry_Impl
*[nCount
];
253 for( i
=0; i
< nCount
; i
++ )
257 for( i
=0; i
< nCount
; i
++ )
259 XMLTextListAutoStylePoolEntry_Impl
*pEntry
= (*pPool
)[i
];
260 DBG_ASSERT( pEntry
->GetPos() < nCount
, "Illegal pos" );
261 aExpEntries
[pEntry
->GetPos()] = pEntry
;
264 SvxXMLNumRuleExport
aNumRuleExp( rExport
);
266 for( i
=0; i
< nCount
; i
++ )
268 XMLTextListAutoStylePoolEntry_Impl
*pEntry
= aExpEntries
[i
];
269 aNumRuleExp
.exportNumberingRule( pEntry
->GetName(), sal_False
,
270 pEntry
->GetNumRules() );
272 delete [] aExpEntries
;
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */