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 .
22 #include <tools/solar.h>
23 #include <o3tl/sorted_vector.hxx>
24 #include <com/sun/star/frame/XModel.hpp>
25 #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
26 #include <com/sun/star/container/XNamed.hpp>
27 #include <com/sun/star/container/XIndexReplace.hpp>
28 #include <rtl/ustrbuf.hxx>
29 #include <sal/log.hxx>
30 #include <xmloff/xmlnume.hxx>
31 #include <xmloff/XMLTextListAutoStylePool.hxx>
32 #include <xmloff/xmlexp.hxx>
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::beans
;
38 using namespace ::com::sun::star::container
;
39 using namespace ::com::sun::star::style
;
42 class XMLTextListAutoStylePoolEntry_Impl
45 OUString sInternalName
;
46 Reference
< XIndexReplace
> xNumRules
;
53 XMLTextListAutoStylePoolEntry_Impl(
55 const Reference
< XIndexReplace
> & rNumRules
,
56 XMLTextListAutoStylePoolNames_Impl
& rNames
,
57 const OUString
& rPrefix
,
60 explicit XMLTextListAutoStylePoolEntry_Impl(
61 const Reference
< XIndexReplace
> & rNumRules
) :
62 xNumRules( rNumRules
),
66 Reference
< XNamed
> xNamed( xNumRules
, UNO_QUERY
);
69 sInternalName
= xNamed
->getName();
74 explicit XMLTextListAutoStylePoolEntry_Impl(
75 const OUString
& rInternalName
) :
76 sInternalName( rInternalName
),
82 const OUString
& GetName() const { return sName
; }
83 const OUString
& GetInternalName() const { return sInternalName
; }
84 const Reference
< XIndexReplace
> & GetNumRules() const { return xNumRules
; }
85 sal_uInt32
GetPos() const { return nPos
; }
86 bool IsNamed() const { return bIsNamed
; }
89 XMLTextListAutoStylePoolEntry_Impl::XMLTextListAutoStylePoolEntry_Impl(
91 const Reference
< XIndexReplace
> & rNumRules
,
92 XMLTextListAutoStylePoolNames_Impl
& rNames
,
93 const OUString
& rPrefix
,
95 xNumRules( rNumRules
),
99 Reference
< XNamed
> xNamed( xNumRules
, UNO_QUERY
);
102 sInternalName
= xNamed
->getName();
106 // create a name that hasn't been used before. The created name has not
107 // to be added to the array, because it will never tried again
108 OUStringBuffer
sBuffer( 7 );
112 sBuffer
.append( rPrefix
);
113 sBuffer
.append( static_cast<sal_Int32
>(rName
) );
114 sName
= sBuffer
.makeStringAndClear();
116 while (rNames
.find(sName
) != rNames
.end());
121 struct XMLTextListAutoStylePoolEntryCmp_Impl
124 std::unique_ptr
<XMLTextListAutoStylePoolEntry_Impl
> const& r1
,
125 std::unique_ptr
<XMLTextListAutoStylePoolEntry_Impl
> const& r2
) const
130 return r1
->GetInternalName().compareTo( r2
->GetInternalName() ) < 0;
139 return r1
->GetNumRules().get() < r2
->GetNumRules().get();
146 class XMLTextListAutoStylePool_Impl
: public o3tl::sorted_vector
<std::unique_ptr
<XMLTextListAutoStylePoolEntry_Impl
>, XMLTextListAutoStylePoolEntryCmp_Impl
> {};
148 XMLTextListAutoStylePool::XMLTextListAutoStylePool( SvXMLExport
& rExp
) :
151 pPool( new XMLTextListAutoStylePool_Impl
),
154 Reference
<ucb::XAnyCompareFactory
> xCompareFac( rExp
.GetModel(), uno::UNO_QUERY
);
155 if( xCompareFac
.is() )
156 mxNumRuleCompare
= xCompareFac
->createAnyCompareByName( "NumberingRules" );
157 SvXMLExportFlags nExportFlags
= rExport
.getExportFlags();
158 bool bStylesOnly
= (nExportFlags
& SvXMLExportFlags::STYLES
) && !(nExportFlags
& SvXMLExportFlags::CONTENT
);
164 XMLTextListAutoStylePool::~XMLTextListAutoStylePool()
168 void XMLTextListAutoStylePool::RegisterName( const OUString
& rName
)
170 m_aNames
.insert(rName
);
173 sal_uInt32
XMLTextListAutoStylePool::Find( const XMLTextListAutoStylePoolEntry_Impl
* pEntry
) const
175 if( !pEntry
->IsNamed() && mxNumRuleCompare
.is() )
177 const sal_uInt32 nCount
= pPool
->size();
179 uno::Any aAny1
, aAny2
;
180 aAny1
<<= pEntry
->GetNumRules();
182 for( sal_uInt32 nPos
= 0; nPos
< nCount
; nPos
++ )
184 aAny2
<<= (*pPool
)[nPos
]->GetNumRules();
186 if( mxNumRuleCompare
->compare( aAny1
, aAny2
) == 0 )
192 XMLTextListAutoStylePool_Impl::const_iterator it
= pPool
->find( pEntry
);
193 if( it
!= pPool
->end() )
194 return it
- pPool
->begin();
197 return sal_uInt32(-1);
200 OUString
XMLTextListAutoStylePool::Add(
201 const Reference
< XIndexReplace
> & rNumRules
)
204 XMLTextListAutoStylePoolEntry_Impl
aTmp( rNumRules
);
206 sal_uInt32 nPos
= Find( &aTmp
);
207 if( nPos
!= sal_uInt32(-1) )
209 sName
= (*pPool
)[ nPos
]->GetName();
213 std::unique_ptr
<XMLTextListAutoStylePoolEntry_Impl
> pEntry(
214 new XMLTextListAutoStylePoolEntry_Impl( pPool
->size(),
215 rNumRules
, m_aNames
, sPrefix
,
217 sName
= pEntry
->GetName();
218 pPool
->insert( std::move(pEntry
) );
224 OUString
XMLTextListAutoStylePool::Find(
225 const Reference
< XIndexReplace
> & rNumRules
) const
228 XMLTextListAutoStylePoolEntry_Impl
aTmp( rNumRules
);
230 sal_uInt32 nPos
= Find( &aTmp
);
231 if( nPos
!= sal_uInt32(-1) )
232 sName
= (*pPool
)[ nPos
]->GetName();
237 OUString
XMLTextListAutoStylePool::Find(
238 const OUString
& rInternalName
) const
241 XMLTextListAutoStylePoolEntry_Impl
aTmp( rInternalName
);
242 sal_uInt32 nPos
= Find( &aTmp
);
243 if( nPos
!= sal_uInt32(-1) )
244 sName
= (*pPool
)[ nPos
]->GetName();
249 void XMLTextListAutoStylePool::exportXML() const
251 sal_uInt32 nCount
= pPool
->size();
255 std::vector
<XMLTextListAutoStylePoolEntry_Impl
*> aExpEntries(nCount
);
258 for( i
=0; i
< nCount
; i
++ )
260 XMLTextListAutoStylePoolEntry_Impl
*pEntry
= (*pPool
)[i
].get();
261 SAL_WARN_IF( pEntry
->GetPos() >= nCount
, "xmloff", "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() );
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */