Line data Source code
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 <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>
29 :
30 :
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;
36 :
37 :
38 32 : class XMLTextListAutoStylePoolEntry_Impl
39 : {
40 : OUString sName;
41 : OUString sInternalName;
42 : Reference < XIndexReplace > xNumRules;
43 : sal_uInt32 nPos;
44 : sal_Bool bIsNamed;
45 :
46 :
47 : public:
48 :
49 : XMLTextListAutoStylePoolEntry_Impl(
50 : sal_uInt32 nPos,
51 : const Reference < XIndexReplace > & rNumRules,
52 : XMLTextListAutoStylePoolNames_Impl& rNames,
53 : const OUString& rPrefix,
54 : sal_uInt32& rName );
55 :
56 26 : XMLTextListAutoStylePoolEntry_Impl(
57 : const Reference < XIndexReplace > & rNumRules ) :
58 : xNumRules( rNumRules ),
59 : nPos( 0 ),
60 26 : bIsNamed( sal_False )
61 : {
62 26 : Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
63 26 : if( xNamed.is() )
64 : {
65 0 : sInternalName = xNamed->getName();
66 0 : bIsNamed = sal_True;
67 26 : }
68 26 : }
69 :
70 0 : XMLTextListAutoStylePoolEntry_Impl(
71 : const OUString& rInternalName ) :
72 : sInternalName( rInternalName ),
73 : nPos( 0 ),
74 0 : bIsNamed( sal_True )
75 : {
76 0 : }
77 :
78 30 : const OUString& GetName() const { return sName; }
79 0 : const OUString& GetInternalName() const { return sInternalName; }
80 66 : const Reference < XIndexReplace > & GetNumRules() const { return xNumRules; }
81 6 : sal_uInt32 GetPos() const { return nPos; }
82 38 : sal_Bool IsNamed() const { return bIsNamed; }
83 : };
84 :
85 6 : XMLTextListAutoStylePoolEntry_Impl::XMLTextListAutoStylePoolEntry_Impl(
86 : sal_uInt32 nP,
87 : const Reference < XIndexReplace > & rNumRules,
88 : XMLTextListAutoStylePoolNames_Impl& rNames,
89 : const OUString& rPrefix,
90 : sal_uInt32& rName ) :
91 : xNumRules( rNumRules ),
92 : nPos( nP ),
93 6 : bIsNamed( sal_False )
94 : {
95 6 : Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
96 6 : if( xNamed.is() )
97 : {
98 0 : sInternalName = xNamed->getName();
99 0 : bIsNamed = sal_True;
100 : }
101 :
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 12 : OUStringBuffer sBuffer( 7 );
105 12 : do
106 : {
107 6 : rName++;
108 6 : sBuffer.append( rPrefix );
109 6 : sBuffer.append( (sal_Int32)rName );
110 6 : sName = sBuffer.makeStringAndClear();
111 : }
112 18 : while (rNames.find(sName) != rNames.end());
113 6 : }
114 :
115 : struct XMLTextListAutoStylePoolEntryCmp_Impl
116 : {
117 6 : bool operator()(
118 : XMLTextListAutoStylePoolEntry_Impl* const& r1,
119 : XMLTextListAutoStylePoolEntry_Impl* const& r2 ) const
120 : {
121 6 : if( r1->IsNamed() )
122 : {
123 0 : if( r2->IsNamed() )
124 0 : return r1->GetInternalName().compareTo( r2->GetInternalName() ) < 0;
125 : else
126 0 : return true;
127 : }
128 : else
129 : {
130 6 : if( r2->IsNamed() )
131 0 : return false;
132 : else
133 6 : return r1->GetNumRules().get() < r2->GetNumRules().get();
134 : }
135 : }
136 : };
137 176 : class XMLTextListAutoStylePool_Impl : public o3tl::sorted_vector<XMLTextListAutoStylePoolEntry_Impl*, XMLTextListAutoStylePoolEntryCmp_Impl> {};
138 :
139 88 : XMLTextListAutoStylePool::XMLTextListAutoStylePool( SvXMLExport& rExp ) :
140 : rExport( rExp ),
141 : sPrefix( "L" ),
142 88 : pPool( new XMLTextListAutoStylePool_Impl ),
143 176 : nName( 0 )
144 : {
145 88 : Reference<ucb::XAnyCompareFactory> xCompareFac( rExp.GetModel(), uno::UNO_QUERY );
146 88 : if( xCompareFac.is() )
147 24 : mxNumRuleCompare = xCompareFac->createAnyCompareByName( OUString( "NumberingRules" ) );
148 88 : sal_uInt16 nExportFlags = rExport.getExportFlags();
149 88 : sal_Bool bStylesOnly = (nExportFlags & EXPORT_STYLES) != 0 && (nExportFlags & EXPORT_CONTENT) == 0;
150 88 : if( bStylesOnly )
151 33 : sPrefix = OUString( "ML" );
152 :
153 88 : }
154 :
155 176 : XMLTextListAutoStylePool::~XMLTextListAutoStylePool()
156 : {
157 : // The XMLTextListAutoStylePoolEntry_Impl object in the pool need delete explicitly in dtor.
158 88 : pPool->DeleteAndDestroyAll();
159 88 : delete pPool;
160 88 : }
161 :
162 196 : void XMLTextListAutoStylePool::RegisterName( const OUString& rName )
163 : {
164 196 : m_aNames.insert(rName);
165 196 : }
166 :
167 26 : sal_uInt32 XMLTextListAutoStylePool::Find( XMLTextListAutoStylePoolEntry_Impl* pEntry ) const
168 : {
169 26 : if( !pEntry->IsNamed() && mxNumRuleCompare.is() )
170 : {
171 22 : const sal_uInt32 nCount = pPool->size();
172 :
173 26 : uno::Any aAny1, aAny2;
174 22 : aAny1 <<= pEntry->GetNumRules();
175 :
176 30 : for( sal_uLong nPos = 0; nPos < nCount; nPos++ )
177 : {
178 26 : aAny2 <<= (*pPool)[nPos]->GetNumRules();
179 :
180 26 : if( mxNumRuleCompare->compare( aAny1, aAny2 ) == 0 )
181 18 : return nPos;
182 4 : }
183 : }
184 : else
185 : {
186 4 : XMLTextListAutoStylePool_Impl::const_iterator it = pPool->find( pEntry );
187 4 : if( it != pPool->end() )
188 0 : return it - pPool->begin();
189 : }
190 :
191 8 : return (sal_uInt32)-1;
192 : }
193 :
194 24 : OUString XMLTextListAutoStylePool::Add(
195 : const Reference < XIndexReplace > & rNumRules )
196 : {
197 24 : OUString sName;
198 48 : XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
199 :
200 24 : sal_uInt32 nPos = Find( &aTmp );
201 24 : if( nPos != (sal_uInt32)-1 )
202 : {
203 18 : sName = (*pPool)[ nPos ]->GetName();
204 : }
205 : else
206 : {
207 : XMLTextListAutoStylePoolEntry_Impl *pEntry =
208 6 : new XMLTextListAutoStylePoolEntry_Impl( pPool->size(),
209 : rNumRules, m_aNames, sPrefix,
210 6 : nName );
211 6 : pPool->insert( pEntry );
212 6 : sName = pEntry->GetName();
213 : }
214 :
215 48 : return sName;
216 : }
217 :
218 2 : OUString XMLTextListAutoStylePool::Find(
219 : const Reference < XIndexReplace > & rNumRules ) const
220 : {
221 2 : OUString sName;
222 4 : XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
223 :
224 2 : sal_uInt32 nPos = Find( &aTmp );
225 2 : if( nPos != (sal_uInt32)-1 )
226 0 : sName = (*pPool)[ nPos ]->GetName();
227 :
228 4 : return sName;
229 : }
230 :
231 0 : OUString XMLTextListAutoStylePool::Find(
232 : const OUString& rInternalName ) const
233 : {
234 0 : OUString sName;
235 0 : XMLTextListAutoStylePoolEntry_Impl aTmp( rInternalName );
236 0 : sal_uInt32 nPos = Find( &aTmp );
237 0 : if( nPos != (sal_uInt32)-1 )
238 0 : sName = (*pPool)[ nPos ]->GetName();
239 :
240 0 : return sName;
241 : }
242 :
243 63 : void XMLTextListAutoStylePool::exportXML() const
244 : {
245 63 : sal_uInt32 nCount = pPool->size();
246 63 : if( !nCount )
247 122 : return;
248 :
249 : XMLTextListAutoStylePoolEntry_Impl **aExpEntries =
250 4 : new XMLTextListAutoStylePoolEntry_Impl*[nCount];
251 :
252 : sal_uInt32 i;
253 10 : for( i=0; i < nCount; i++ )
254 : {
255 6 : aExpEntries[i] = 0;
256 : }
257 10 : for( i=0; i < nCount; i++ )
258 : {
259 6 : XMLTextListAutoStylePoolEntry_Impl *pEntry = (*pPool)[i];
260 : DBG_ASSERT( pEntry->GetPos() < nCount, "Illegal pos" );
261 6 : aExpEntries[pEntry->GetPos()] = pEntry;
262 : }
263 :
264 4 : SvxXMLNumRuleExport aNumRuleExp( rExport );
265 :
266 10 : for( i=0; i < nCount; i++ )
267 : {
268 6 : XMLTextListAutoStylePoolEntry_Impl *pEntry = aExpEntries[i];
269 6 : aNumRuleExp.exportNumberingRule( pEntry->GetName(), sal_False,
270 12 : pEntry->GetNumRules() );
271 : }
272 4 : delete [] aExpEntries;
273 : }
274 :
275 :
276 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|