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 0 : 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 0 : XMLTextListAutoStylePoolEntry_Impl(
57 : const Reference < XIndexReplace > & rNumRules ) :
58 : xNumRules( rNumRules ),
59 : nPos( 0 ),
60 0 : bIsNamed( sal_False )
61 : {
62 0 : Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
63 0 : if( xNamed.is() )
64 : {
65 0 : sInternalName = xNamed->getName();
66 0 : bIsNamed = sal_True;
67 0 : }
68 0 : }
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 0 : const OUString& GetName() const { return sName; }
79 0 : const OUString& GetInternalName() const { return sInternalName; }
80 0 : const Reference < XIndexReplace > & GetNumRules() const { return xNumRules; }
81 0 : sal_uInt32 GetPos() const { return nPos; }
82 0 : sal_Bool IsNamed() const { return bIsNamed; }
83 : };
84 :
85 0 : 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 0 : bIsNamed( sal_False )
94 : {
95 0 : Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
96 0 : 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 0 : OUStringBuffer sBuffer( 7 );
105 0 : do
106 : {
107 0 : rName++;
108 0 : sBuffer.append( rPrefix );
109 0 : sBuffer.append( (sal_Int32)rName );
110 0 : sName = sBuffer.makeStringAndClear();
111 : }
112 0 : while (rNames.find(sName) != rNames.end());
113 0 : }
114 :
115 : struct XMLTextListAutoStylePoolEntryCmp_Impl
116 : {
117 0 : bool operator()(
118 : XMLTextListAutoStylePoolEntry_Impl* const& r1,
119 : XMLTextListAutoStylePoolEntry_Impl* const& r2 ) const
120 : {
121 0 : 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 0 : if( r2->IsNamed() )
131 0 : return false;
132 : else
133 0 : return r1->GetNumRules().get() < r2->GetNumRules().get();
134 : }
135 : }
136 : };
137 0 : class XMLTextListAutoStylePool_Impl : public o3tl::sorted_vector<XMLTextListAutoStylePoolEntry_Impl*, XMLTextListAutoStylePoolEntryCmp_Impl> {};
138 :
139 0 : XMLTextListAutoStylePool::XMLTextListAutoStylePool( SvXMLExport& rExp ) :
140 : rExport( rExp ),
141 : sPrefix( "L" ),
142 0 : pPool( new XMLTextListAutoStylePool_Impl ),
143 0 : nName( 0 )
144 : {
145 0 : Reference<ucb::XAnyCompareFactory> xCompareFac( rExp.GetModel(), uno::UNO_QUERY );
146 0 : if( xCompareFac.is() )
147 0 : mxNumRuleCompare = xCompareFac->createAnyCompareByName( OUString( "NumberingRules" ) );
148 0 : sal_uInt16 nExportFlags = rExport.getExportFlags();
149 0 : sal_Bool bStylesOnly = (nExportFlags & EXPORT_STYLES) != 0 && (nExportFlags & EXPORT_CONTENT) == 0;
150 0 : if( bStylesOnly )
151 0 : sPrefix = "ML";
152 :
153 0 : }
154 :
155 0 : XMLTextListAutoStylePool::~XMLTextListAutoStylePool()
156 : {
157 : // The XMLTextListAutoStylePoolEntry_Impl object in the pool need delete explicitly in dtor.
158 0 : pPool->DeleteAndDestroyAll();
159 0 : delete pPool;
160 0 : }
161 :
162 0 : void XMLTextListAutoStylePool::RegisterName( const OUString& rName )
163 : {
164 0 : m_aNames.insert(rName);
165 0 : }
166 :
167 0 : sal_uInt32 XMLTextListAutoStylePool::Find( XMLTextListAutoStylePoolEntry_Impl* pEntry ) const
168 : {
169 0 : if( !pEntry->IsNamed() && mxNumRuleCompare.is() )
170 : {
171 0 : const sal_uInt32 nCount = pPool->size();
172 :
173 0 : uno::Any aAny1, aAny2;
174 0 : aAny1 <<= pEntry->GetNumRules();
175 :
176 0 : for( sal_uLong nPos = 0; nPos < nCount; nPos++ )
177 : {
178 0 : aAny2 <<= (*pPool)[nPos]->GetNumRules();
179 :
180 0 : if( mxNumRuleCompare->compare( aAny1, aAny2 ) == 0 )
181 0 : return nPos;
182 0 : }
183 : }
184 : else
185 : {
186 0 : XMLTextListAutoStylePool_Impl::const_iterator it = pPool->find( pEntry );
187 0 : if( it != pPool->end() )
188 0 : return it - pPool->begin();
189 : }
190 :
191 0 : return (sal_uInt32)-1;
192 : }
193 :
194 0 : OUString XMLTextListAutoStylePool::Add(
195 : const Reference < XIndexReplace > & rNumRules )
196 : {
197 0 : OUString sName;
198 0 : XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
199 :
200 0 : sal_uInt32 nPos = Find( &aTmp );
201 0 : if( nPos != (sal_uInt32)-1 )
202 : {
203 0 : sName = (*pPool)[ nPos ]->GetName();
204 : }
205 : else
206 : {
207 : XMLTextListAutoStylePoolEntry_Impl *pEntry =
208 0 : new XMLTextListAutoStylePoolEntry_Impl( pPool->size(),
209 : rNumRules, m_aNames, sPrefix,
210 0 : nName );
211 0 : pPool->insert( pEntry );
212 0 : sName = pEntry->GetName();
213 : }
214 :
215 0 : return sName;
216 : }
217 :
218 0 : OUString XMLTextListAutoStylePool::Find(
219 : const Reference < XIndexReplace > & rNumRules ) const
220 : {
221 0 : OUString sName;
222 0 : XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
223 :
224 0 : sal_uInt32 nPos = Find( &aTmp );
225 0 : if( nPos != (sal_uInt32)-1 )
226 0 : sName = (*pPool)[ nPos ]->GetName();
227 :
228 0 : 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 0 : void XMLTextListAutoStylePool::exportXML() const
244 : {
245 0 : sal_uInt32 nCount = pPool->size();
246 0 : if( !nCount )
247 0 : return;
248 :
249 : XMLTextListAutoStylePoolEntry_Impl **aExpEntries =
250 0 : new XMLTextListAutoStylePoolEntry_Impl*[nCount];
251 :
252 : sal_uInt32 i;
253 0 : for( i=0; i < nCount; i++ )
254 : {
255 0 : aExpEntries[i] = 0;
256 : }
257 0 : for( i=0; i < nCount; i++ )
258 : {
259 0 : XMLTextListAutoStylePoolEntry_Impl *pEntry = (*pPool)[i];
260 : DBG_ASSERT( pEntry->GetPos() < nCount, "Illegal pos" );
261 0 : aExpEntries[pEntry->GetPos()] = pEntry;
262 : }
263 :
264 0 : SvxXMLNumRuleExport aNumRuleExp( rExport );
265 :
266 0 : for( i=0; i < nCount; i++ )
267 : {
268 0 : XMLTextListAutoStylePoolEntry_Impl *pEntry = aExpEntries[i];
269 0 : aNumRuleExp.exportNumberingRule( pEntry->GetName(), false,
270 0 : pEntry->GetNumRules() );
271 : }
272 0 : delete [] aExpEntries;
273 : }
274 :
275 :
276 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|