Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <tools/debug.hxx>
30 : : #include <o3tl/sorted_vector.hxx>
31 : : #include <com/sun/star/ucb/XAnyCompareFactory.hpp>
32 : : #include <com/sun/star/container/XNamed.hpp>
33 : : #include <com/sun/star/container/XIndexReplace.hpp>
34 : : #include <rtl/ustrbuf.hxx>
35 : : #include <xmloff/xmlnume.hxx>
36 : : #include "xmloff/XMLTextListAutoStylePool.hxx"
37 : : #include <xmloff/xmlexp.hxx>
38 : :
39 : : using ::rtl::OUString;
40 : : using ::rtl::OUStringBuffer;
41 : :
42 : : using namespace ::com::sun::star;
43 : : using namespace ::com::sun::star::uno;
44 : : using namespace ::com::sun::star::beans;
45 : : using namespace ::com::sun::star::container;
46 : : using namespace ::com::sun::star::style;
47 : :
48 : :
49 : 52 : class XMLTextListAutoStylePoolEntry_Impl
50 : : {
51 : : OUString sName;
52 : : OUString sInternalName;
53 : : Reference < XIndexReplace > xNumRules;
54 : : sal_uInt32 nPos;
55 : : sal_Bool bIsNamed;
56 : :
57 : :
58 : : public:
59 : :
60 : : XMLTextListAutoStylePoolEntry_Impl(
61 : : sal_uInt32 nPos,
62 : : const Reference < XIndexReplace > & rNumRules,
63 : : XMLTextListAutoStylePoolNames_Impl& rNames,
64 : : const OUString& rPrefix,
65 : : sal_uInt32& rName );
66 : :
67 : 44 : XMLTextListAutoStylePoolEntry_Impl(
68 : : const Reference < XIndexReplace > & rNumRules ) :
69 : : xNumRules( rNumRules ),
70 : : nPos( 0 ),
71 : 44 : bIsNamed( sal_False )
72 : : {
73 [ + - ]: 44 : Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
74 [ - + ]: 44 : if( xNamed.is() )
75 : : {
76 [ # # ][ # # ]: 0 : sInternalName = xNamed->getName();
77 : 0 : bIsNamed = sal_True;
78 : 44 : }
79 : 44 : }
80 : :
81 : 0 : XMLTextListAutoStylePoolEntry_Impl(
82 : : const OUString& rInternalName ) :
83 : : sInternalName( rInternalName ),
84 : : nPos( 0 ),
85 : 0 : bIsNamed( sal_True )
86 : : {
87 : 0 : }
88 : :
89 : 52 : const OUString& GetName() const { return sName; }
90 : 0 : const OUString& GetInternalName() const { return sInternalName; }
91 : 116 : const Reference < XIndexReplace > & GetNumRules() const { return xNumRules; }
92 : 8 : sal_uInt32 GetPos() const { return nPos; }
93 : 56 : sal_Bool IsNamed() const { return bIsNamed; }
94 : : };
95 : :
96 : 8 : XMLTextListAutoStylePoolEntry_Impl::XMLTextListAutoStylePoolEntry_Impl(
97 : : sal_uInt32 nP,
98 : : const Reference < XIndexReplace > & rNumRules,
99 : : XMLTextListAutoStylePoolNames_Impl& rNames,
100 : : const OUString& rPrefix,
101 : : sal_uInt32& rName ) :
102 : : xNumRules( rNumRules ),
103 : : nPos( nP ),
104 : 8 : bIsNamed( sal_False )
105 : : {
106 [ + - ]: 8 : Reference < XNamed > xNamed( xNumRules, UNO_QUERY );
107 [ - + ]: 8 : if( xNamed.is() )
108 : : {
109 [ # # ][ # # ]: 0 : sInternalName = xNamed->getName();
110 : 0 : bIsNamed = sal_True;
111 : : }
112 : :
113 : : // create a name that hasn't been used before. The created name has not
114 : : // to be added to the array, because it will never tried again
115 : 8 : OUStringBuffer sBuffer( 7 );
116 [ - + ]: 8 : do
117 : : {
118 : 8 : rName++;
119 [ + - ]: 8 : sBuffer.append( rPrefix );
120 [ + - ]: 8 : sBuffer.append( (sal_Int32)rName );
121 [ + - ]: 8 : sName = sBuffer.makeStringAndClear();
122 : : }
123 [ + - ]: 16 : while (rNames.find(sName) != rNames.end());
124 : 8 : }
125 : :
126 : : struct XMLTextListAutoStylePoolEntryCmp_Impl
127 : : {
128 : 6 : bool operator()(
129 : : XMLTextListAutoStylePoolEntry_Impl* const& r1,
130 : : XMLTextListAutoStylePoolEntry_Impl* const& r2 ) const
131 : : {
132 [ - + ]: 6 : if( r1->IsNamed() )
133 : : {
134 [ # # ]: 0 : if( r2->IsNamed() )
135 : 0 : return r1->GetInternalName().compareTo( r2->GetInternalName() ) < 0;
136 : : else
137 : 0 : return true;
138 : : }
139 : : else
140 : : {
141 [ - + ]: 6 : if( r2->IsNamed() )
142 : 0 : return false;
143 : : else
144 : 6 : return r1->GetNumRules().get() < r2->GetNumRules().get();
145 : : }
146 : : }
147 : : };
148 : 154 : class XMLTextListAutoStylePool_Impl : public o3tl::sorted_vector<XMLTextListAutoStylePoolEntry_Impl*, XMLTextListAutoStylePoolEntryCmp_Impl> {};
149 : :
150 : 77 : XMLTextListAutoStylePool::XMLTextListAutoStylePool( SvXMLExport& rExp ) :
151 : : rExport( rExp ),
152 : : sPrefix( RTL_CONSTASCII_USTRINGPARAM("L") ),
153 [ + - ]: 77 : pPool( new XMLTextListAutoStylePool_Impl ),
154 [ + - ][ + - ]: 154 : nName( 0 )
155 : : {
156 [ + - ]: 77 : Reference<ucb::XAnyCompareFactory> xCompareFac( rExp.GetModel(), uno::UNO_QUERY );
157 [ + + ]: 77 : if( xCompareFac.is() )
158 [ + - ][ + - ]: 18 : mxNumRuleCompare = xCompareFac->createAnyCompareByName( OUString( RTL_CONSTASCII_USTRINGPARAM( "NumberingRules" ) ) );
[ + - ][ + - ]
159 : 77 : sal_uInt16 nExportFlags = rExport.getExportFlags();
160 [ + + ][ + + ]: 77 : sal_Bool bStylesOnly = (nExportFlags & EXPORT_STYLES) != 0 && (nExportFlags & EXPORT_CONTENT) == 0;
161 [ + + ]: 77 : if( bStylesOnly )
162 [ + - ]: 77 : sPrefix = OUString( RTL_CONSTASCII_USTRINGPARAM("ML") );
163 : :
164 : 77 : }
165 : :
166 : 77 : XMLTextListAutoStylePool::~XMLTextListAutoStylePool()
167 : : {
168 : : // The XMLTextListAutoStylePoolEntry_Impl object in the pool need delete explicitly in dtor.
169 [ + - ]: 77 : pPool->DeleteAndDestroyAll();
170 [ + - ]: 77 : delete pPool;
171 : 77 : }
172 : :
173 : 210 : void XMLTextListAutoStylePool::RegisterName( const OUString& rName )
174 : : {
175 : 210 : m_aNames.insert(rName);
176 : 210 : }
177 : :
178 : 44 : sal_uInt32 XMLTextListAutoStylePool::Find( XMLTextListAutoStylePoolEntry_Impl* pEntry ) const
179 : : {
180 [ + - ][ + - ]: 44 : if( !pEntry->IsNamed() && mxNumRuleCompare.is() )
[ + - ]
181 : : {
182 : 44 : const sal_uInt32 nCount = pPool->size();
183 : :
184 : 44 : uno::Any aAny1, aAny2;
185 [ + - ]: 44 : aAny1 <<= pEntry->GetNumRules();
186 : :
187 [ + + ]: 60 : for( sal_uLong nPos = 0; nPos < nCount; nPos++ )
188 : : {
189 [ + - ][ + - ]: 52 : aAny2 <<= (*pPool)[nPos]->GetNumRules();
190 : :
191 [ + - ][ + - ]: 52 : if( mxNumRuleCompare->compare( aAny1, aAny2 ) == 0 )
[ + + ]
192 : 36 : return nPos;
193 [ + + ][ + + ]: 44 : }
194 : : }
195 : : else
196 : : {
197 [ # # ]: 0 : XMLTextListAutoStylePool_Impl::const_iterator it = pPool->find( pEntry );
198 [ # # ][ # # ]: 0 : if( it != pPool->end() )
199 [ # # ]: 0 : return it - pPool->begin();
200 : : }
201 : :
202 : 44 : return (sal_uInt32)-1;
203 : : }
204 : :
205 : 44 : OUString XMLTextListAutoStylePool::Add(
206 : : const Reference < XIndexReplace > & rNumRules )
207 : : {
208 : 44 : OUString sName;
209 [ + - ]: 44 : XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
210 : :
211 [ + - ]: 44 : sal_uInt32 nPos = Find( &aTmp );
212 [ + + ]: 44 : if( nPos != (sal_uInt32)-1 )
213 : : {
214 [ + - ]: 36 : sName = (*pPool)[ nPos ]->GetName();
215 : : }
216 : : else
217 : : {
218 : : XMLTextListAutoStylePoolEntry_Impl *pEntry =
219 : 8 : new XMLTextListAutoStylePoolEntry_Impl( pPool->size(),
220 : : rNumRules, m_aNames, sPrefix,
221 [ + - ][ + - ]: 8 : nName );
222 [ + - ]: 8 : pPool->insert( pEntry );
223 : 8 : sName = pEntry->GetName();
224 : : }
225 : :
226 [ + - ]: 44 : return sName;
227 : : }
228 : :
229 : 0 : ::rtl::OUString XMLTextListAutoStylePool::Find(
230 : : const Reference < XIndexReplace > & rNumRules ) const
231 : : {
232 : 0 : OUString sName;
233 [ # # ]: 0 : XMLTextListAutoStylePoolEntry_Impl aTmp( rNumRules );
234 : :
235 [ # # ]: 0 : sal_uInt32 nPos = Find( &aTmp );
236 [ # # ]: 0 : if( nPos != (sal_uInt32)-1 )
237 [ # # ]: 0 : sName = (*pPool)[ nPos ]->GetName();
238 : :
239 [ # # ]: 0 : return sName;
240 : : }
241 : :
242 : 0 : ::rtl::OUString XMLTextListAutoStylePool::Find(
243 : : const OUString& rInternalName ) const
244 : : {
245 : 0 : OUString sName;
246 [ # # ]: 0 : XMLTextListAutoStylePoolEntry_Impl aTmp( rInternalName );
247 [ # # ]: 0 : sal_uInt32 nPos = Find( &aTmp );
248 [ # # ]: 0 : if( nPos != (sal_uInt32)-1 )
249 [ # # ]: 0 : sName = (*pPool)[ nPos ]->GetName();
250 : :
251 [ # # ]: 0 : return sName;
252 : : }
253 : :
254 : 79 : void XMLTextListAutoStylePool::exportXML() const
255 : : {
256 : 79 : sal_uInt32 nCount = pPool->size();
257 [ + + ]: 79 : if( !nCount )
258 : 79 : return;
259 : :
260 : : XMLTextListAutoStylePoolEntry_Impl **aExpEntries =
261 [ + - ]: 4 : new XMLTextListAutoStylePoolEntry_Impl*[nCount];
262 : :
263 : : sal_uInt32 i;
264 [ + + ]: 12 : for( i=0; i < nCount; i++ )
265 : : {
266 : 8 : aExpEntries[i] = 0;
267 : : }
268 [ + + ]: 12 : for( i=0; i < nCount; i++ )
269 : : {
270 [ + - ]: 8 : XMLTextListAutoStylePoolEntry_Impl *pEntry = (*pPool)[i];
271 : : DBG_ASSERT( pEntry->GetPos() < nCount, "Illegal pos" );
272 : 8 : aExpEntries[pEntry->GetPos()] = pEntry;
273 : : }
274 : :
275 [ + - ]: 4 : SvxXMLNumRuleExport aNumRuleExp( rExport );
276 : :
277 [ + + ]: 12 : for( i=0; i < nCount; i++ )
278 : : {
279 : 8 : XMLTextListAutoStylePoolEntry_Impl *pEntry = aExpEntries[i];
280 : 8 : aNumRuleExp.exportNumberingRule( pEntry->GetName(),
281 [ + - ]: 16 : pEntry->GetNumRules() );
282 : : }
283 [ + - ][ + - ]: 79 : delete [] aExpEntries;
284 : : }
285 : :
286 : :
287 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|