nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / text / XMLTextListAutoStylePool.cxx
blob2779e41d6ba9aae429830cd6cdb74353f515adc2
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 <vector>
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
44 OUString sName;
45 OUString sInternalName;
46 Reference < XIndexReplace > xNumRules;
47 sal_uInt32 nPos;
48 bool bIsNamed;
51 public:
53 XMLTextListAutoStylePoolEntry_Impl(
54 sal_uInt32 nPos,
55 const Reference < XIndexReplace > & rNumRules,
56 XMLTextListAutoStylePoolNames_Impl& rNames,
57 const OUString& rPrefix,
58 sal_uInt32& rName );
60 explicit XMLTextListAutoStylePoolEntry_Impl(
61 const Reference < XIndexReplace > & rNumRules ) :
62 xNumRules( rNumRules ),
63 nPos( 0 ),
64 bIsNamed( false )
66 Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
67 if( xNamed.is() )
69 sInternalName = xNamed->getName();
70 bIsNamed = true;
74 explicit XMLTextListAutoStylePoolEntry_Impl(
75 const OUString& rInternalName ) :
76 sInternalName( rInternalName ),
77 nPos( 0 ),
78 bIsNamed( true )
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(
90 sal_uInt32 nP,
91 const Reference < XIndexReplace > & rNumRules,
92 XMLTextListAutoStylePoolNames_Impl& rNames,
93 const OUString& rPrefix,
94 sal_uInt32& rName ) :
95 xNumRules( rNumRules ),
96 nPos( nP ),
97 bIsNamed( false )
99 Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
100 if( xNamed.is() )
102 sInternalName = xNamed->getName();
103 bIsNamed = true;
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 );
111 rName++;
112 sBuffer.append( rPrefix );
113 sBuffer.append( static_cast<sal_Int32>(rName) );
114 sName = sBuffer.makeStringAndClear();
116 while (rNames.find(sName) != rNames.end());
119 namespace {
121 struct XMLTextListAutoStylePoolEntryCmp_Impl
123 bool operator()(
124 std::unique_ptr<XMLTextListAutoStylePoolEntry_Impl> const& r1,
125 std::unique_ptr<XMLTextListAutoStylePoolEntry_Impl> const& r2 ) const
127 if( r1->IsNamed() )
129 if( r2->IsNamed() )
130 return r1->GetInternalName().compareTo( r2->GetInternalName() ) < 0;
131 else
132 return true;
134 else
136 if( r2->IsNamed() )
137 return false;
138 else
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 ) :
149 rExport( rExp ),
150 sPrefix( "L" ),
151 pPool( new XMLTextListAutoStylePool_Impl ),
152 nName( 0 )
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);
159 if( bStylesOnly )
160 sPrefix = "ML";
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 )
187 return nPos;
190 else
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 )
203 OUString sName;
204 XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
206 sal_uInt32 nPos = Find( &aTmp );
207 if( nPos != sal_uInt32(-1) )
209 sName = (*pPool)[ nPos ]->GetName();
211 else
213 std::unique_ptr<XMLTextListAutoStylePoolEntry_Impl> pEntry(
214 new XMLTextListAutoStylePoolEntry_Impl( pPool->size(),
215 rNumRules, m_aNames, sPrefix,
216 nName ));
217 sName = pEntry->GetName();
218 pPool->insert( std::move(pEntry) );
221 return sName;
224 OUString XMLTextListAutoStylePool::Find(
225 const Reference < XIndexReplace > & rNumRules ) const
227 OUString sName;
228 XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
230 sal_uInt32 nPos = Find( &aTmp );
231 if( nPos != sal_uInt32(-1) )
232 sName = (*pPool)[ nPos ]->GetName();
234 return sName;
237 OUString XMLTextListAutoStylePool::Find(
238 const OUString& rInternalName ) const
240 OUString sName;
241 XMLTextListAutoStylePoolEntry_Impl aTmp( rInternalName );
242 sal_uInt32 nPos = Find( &aTmp );
243 if( nPos != sal_uInt32(-1) )
244 sName = (*pPool)[ nPos ]->GetName();
246 return sName;
249 void XMLTextListAutoStylePool::exportXML() const
251 sal_uInt32 nCount = pPool->size();
252 if( !nCount )
253 return;
255 std::vector<XMLTextListAutoStylePoolEntry_Impl*> aExpEntries(nCount);
257 sal_uInt32 i;
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: */