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