LibreOffice Module xmloff (master) 1
XMLTextListAutoStylePool.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <vector>
21
22#include <tools/solar.h>
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>
32#include <xmloff/xmlexp.hxx>
33
34
35using namespace ::com::sun::star;
36using namespace ::com::sun::star::uno;
37using namespace ::com::sun::star::beans;
38using namespace ::com::sun::star::container;
39using namespace ::com::sun::star::style;
40
41
43{
44 OUString sName;
45 OUString sInternalName;
46 Reference < XIndexReplace > xNumRules;
47 sal_uInt32 nPos;
49
50
51public:
52
54 sal_uInt32 nPos,
55 const Reference < XIndexReplace > & rNumRules,
57 std::u16string_view rPrefix,
58 sal_uInt32& rName );
59
61 const Reference < XIndexReplace > & rNumRules ) :
62 xNumRules( rNumRules ),
63 nPos( 0 ),
64 bIsNamed( false )
65 {
66 Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
67 if( xNamed.is() )
68 {
69 sInternalName = xNamed->getName();
70 bIsNamed = true;
71 }
72 }
73
75 const OUString& rInternalName ) :
76 sInternalName( rInternalName ),
77 nPos( 0 ),
78 bIsNamed( true )
79 {
80 }
81
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; }
87};
88
90 sal_uInt32 nP,
91 const Reference < XIndexReplace > & rNumRules,
93 std::u16string_view rPrefix,
94 sal_uInt32& rName ) :
95 xNumRules( rNumRules ),
96 nPos( nP ),
97 bIsNamed( false )
98{
99 Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
100 if( xNamed.is() )
101 {
102 sInternalName = xNamed->getName();
103 bIsNamed = true;
104 }
105
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 );
109 do
110 {
111 rName++;
112 sBuffer.append( rPrefix );
113 sBuffer.append( static_cast<sal_Int32>(rName) );
114 sName = sBuffer.makeStringAndClear();
115 }
116 while (rNames.find(sName) != rNames.end());
117}
118
119namespace {
120
121struct XMLTextListAutoStylePoolEntryCmp_Impl
122{
123 bool operator()(
124 std::unique_ptr<XMLTextListAutoStylePoolEntry_Impl> const& r1,
125 std::unique_ptr<XMLTextListAutoStylePoolEntry_Impl> const& r2 ) const
126 {
127 if( r1->IsNamed() )
128 {
129 if( r2->IsNamed() )
130 return r1->GetInternalName().compareTo( r2->GetInternalName() ) < 0;
131 else
132 return true;
133 }
134 else
135 {
136 if( r2->IsNamed() )
137 return false;
138 else
139 return r1->GetNumRules().get() < r2->GetNumRules().get();
140 }
141 }
142};
143
144}
145
146class XMLTextListAutoStylePool_Impl : public o3tl::sorted_vector<std::unique_ptr<XMLTextListAutoStylePoolEntry_Impl>, XMLTextListAutoStylePoolEntryCmp_Impl> {};
147
149 rExport( rExp ),
150 sPrefix( "L" ),
152 nName( 0 )
153{
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";
161
162}
163
165{
166}
167
168void XMLTextListAutoStylePool::RegisterName( const OUString& rName )
169{
170 m_aNames.insert(rName);
171}
172
174{
175 if( !pEntry->IsNamed() && mxNumRuleCompare.is() )
176 {
177 const sal_uInt32 nCount = pPool->size();
178
179 uno::Any aAny1, aAny2;
180 aAny1 <<= pEntry->GetNumRules();
181
182 for( sal_uInt32 nPos = 0; nPos < nCount; nPos++ )
183 {
184 aAny2 <<= (*pPool)[nPos]->GetNumRules();
185
186 if( mxNumRuleCompare->compare( aAny1, aAny2 ) == 0 )
187 return nPos;
188 }
189 }
190 else
191 {
193 if( it != pPool->end() )
194 return it - pPool->begin();
195 }
196
197 return sal_uInt32(-1);
198}
199
201 const Reference < XIndexReplace > & rNumRules )
202{
203 OUString sName;
204 XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
205
206 sal_uInt32 nPos = Find( &aTmp );
207 if( nPos != sal_uInt32(-1) )
208 {
209 sName = (*pPool)[ nPos ]->GetName();
210 }
211 else
212 {
213 std::unique_ptr<XMLTextListAutoStylePoolEntry_Impl> pEntry(
215 rNumRules, m_aNames, sPrefix,
216 nName ));
217 sName = pEntry->GetName();
218 pPool->insert( std::move(pEntry) );
219 }
220
221 return sName;
222}
223
225 const Reference < XIndexReplace > & rNumRules ) const
226{
227 OUString sName;
228 XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
229
230 sal_uInt32 nPos = Find( &aTmp );
231 if( nPos != sal_uInt32(-1) )
232 sName = (*pPool)[ nPos ]->GetName();
233
234 return sName;
235}
236
238 const OUString& rInternalName ) const
239{
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();
245
246 return sName;
247}
248
250{
251 sal_uInt32 nCount = pPool->size();
252 if( !nCount )
253 return;
254
255 std::vector<XMLTextListAutoStylePoolEntry_Impl*> aExpEntries(nCount);
256
257 sal_uInt32 i;
258 for( i=0; i < nCount; i++ )
259 {
260 XMLTextListAutoStylePoolEntry_Impl *pEntry = (*pPool)[i].get();
261 SAL_WARN_IF( pEntry->GetPos() >= nCount, "xmloff", "Illegal pos" );
262 aExpEntries[pEntry->GetPos()] = pEntry;
263 }
264
265 SvxXMLNumRuleExport aNumRuleExp( rExport );
266
267 for( i=0; i < nCount; i++ )
268 {
269 XMLTextListAutoStylePoolEntry_Impl *pEntry = aExpEntries[i];
270 aNumRuleExp.exportNumberingRule( pEntry->GetName(), false,
271 pEntry->GetNumRules() );
272 }
273}
274
275
276/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::set< OUString > XMLTextListAutoStylePoolNames_Impl
SvXMLExportFlags getExportFlags() const
Definition: xmlexp.hxx:478
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlexp.hxx:416
void exportNumberingRule(const OUString &rName, bool bIsHidden, const css::uno::Reference< css::container::XIndexReplace > &xNumRule)
Definition: xmlnume.cxx:644
XMLTextListAutoStylePoolEntry_Impl(const Reference< XIndexReplace > &rNumRules)
const Reference< XIndexReplace > & GetNumRules() const
XMLTextListAutoStylePoolEntry_Impl(sal_uInt32 nPos, const Reference< XIndexReplace > &rNumRules, XMLTextListAutoStylePoolNames_Impl &rNames, std::u16string_view rPrefix, sal_uInt32 &rName)
XMLTextListAutoStylePoolEntry_Impl(const OUString &rInternalName)
std::unique_ptr< XMLTextListAutoStylePool_Impl > pPool
XMLTextListAutoStylePoolNames_Impl m_aNames
SAL_DLLPRIVATE sal_uInt32 Find(const XMLTextListAutoStylePoolEntry_Impl *pEntry) const
void RegisterName(const OUString &rName)
css::uno::Reference< css::ucb::XAnyCompare > mxNumRuleCompare
this is an optional NumRule compare component for applications where the NumRules don't have names
XMLTextListAutoStylePool(SvXMLExport &rExport)
OUString Add(const css::uno::Reference< css::container::XIndexReplace > &rNumRules)
int nCount
OUString sPrefix
sal_uInt16 nPos
#define SAL_WARN_IF(condition, area, stream)
const char * sName
int i
SvXMLExportFlags
Definition: xmlexp.hxx:91