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