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 : :
30 : : #include <set>
31 : :
32 : : #include "svx/UnoNamespaceMap.hxx"
33 : : #include <com/sun/star/container/XNameAccess.hpp>
34 : : #include <com/sun/star/lang/XServiceInfo.hpp>
35 : :
36 : : #include <cppuhelper/implbase2.hxx>
37 : : #include <osl/diagnose.h>
38 : : #include <osl/mutex.hxx>
39 : : #include <comphelper/stl_types.hxx>
40 : : #include <svl/itempool.hxx>
41 : : #include "svx/unoapi.hxx"
42 : : #include "editeng/xmlcnitm.hxx"
43 : :
44 : :
45 : : using namespace ::comphelper;
46 : : using namespace ::osl;
47 : : using namespace ::cppu;
48 : : using namespace ::com::sun::star;
49 : : using namespace ::com::sun::star::uno;
50 : : using namespace ::com::sun::star::container;
51 : : using namespace ::com::sun::star::drawing;
52 : : using namespace ::com::sun::star::lang;
53 : : using namespace ::com::sun::star::beans;
54 : :
55 : : namespace svx
56 : : {
57 : : /** implements a component to export namespaces of all SvXMLAttrContainerItem inside
58 : : one or two pools with a variable count of which ids.
59 : : */
60 : : class NamespaceMap : public WeakImplHelper2< XNameAccess, XServiceInfo >
61 : : {
62 : : private:
63 : : sal_uInt16* mpWhichIds;
64 : : SfxItemPool* mpPool;
65 : :
66 : : public:
67 : : NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool );
68 : : virtual ~NamespaceMap();
69 : :
70 : : // XNameAccess
71 : : virtual Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException);
72 : : virtual Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw (RuntimeException);
73 : : virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (RuntimeException);
74 : :
75 : : // XElementAccess
76 : : virtual Type SAL_CALL getElementType( ) throw (RuntimeException);
77 : : virtual sal_Bool SAL_CALL hasElements( ) throw (RuntimeException);
78 : :
79 : : // XServiceInfo
80 : : virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw(RuntimeException);
81 : : virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(RuntimeException);
82 : : virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw(RuntimeException);
83 : : };
84 : :
85 : 58 : Reference< XInterface > SAL_CALL NamespaceMap_createInstance( sal_uInt16* pWhichIds, SfxItemPool* pPool )
86 : : {
87 [ + - ]: 58 : return (XWeak*)new NamespaceMap( pWhichIds, pPool );
88 : : }
89 : :
90 : 0 : Sequence< ::rtl::OUString > SAL_CALL NamespaceMap_getSupportedServiceNames()
91 : : throw()
92 : : {
93 : 0 : Sequence< ::rtl::OUString > aSupportedServiceNames( 1 );
94 [ # # ][ # # ]: 0 : aSupportedServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.NamespaceMap" ) );
95 : 0 : return aSupportedServiceNames;
96 : : }
97 : :
98 : 0 : ::rtl::OUString SAL_CALL NamespaceMap_getImplementationName()
99 : : throw()
100 : : {
101 : 0 : return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Svx.NamespaceMap" ) );
102 : : }
103 : :
104 : :
105 : :
106 : : class NamespaceIteratorImpl
107 : : {
108 : : private:
109 : : SfxItemPool* mpPool;
110 : :
111 : : sal_uInt16* mpWhichId;
112 : :
113 : : sal_uInt32 mnItemCount;
114 : : sal_uInt32 mnItem;
115 : :
116 : : const SvXMLAttrContainerItem* mpCurrentAttr;
117 : : sal_uInt16 mnCurrentAttr;
118 : :
119 : : public:
120 : :
121 : : NamespaceIteratorImpl( sal_uInt16* pWhichIds, SfxItemPool* pPool );
122 : :
123 : : sal_Bool next( ::rtl::OUString& rPrefix, ::rtl::OUString& rURL );
124 : : };
125 : : }
126 : :
127 : : using namespace ::svx;
128 : :
129 : : // -------------
130 : :
131 : 58 : NamespaceIteratorImpl::NamespaceIteratorImpl( sal_uInt16* pWhichIds, SfxItemPool* pPool )
132 : : {
133 : 58 : mpPool = pPool;
134 : 58 : mpCurrentAttr = NULL;
135 : 58 : mnCurrentAttr = 0;
136 : :
137 : 58 : mpWhichId = pWhichIds;
138 : :
139 : 58 : mnItem = 0;
140 [ + - ][ + - ]: 58 : mnItemCount = (mpWhichId && (0 != *mpWhichId) && mpPool) ? mpPool->GetItemCount2( *mpWhichId ) : 0;
[ + - ]
141 : 58 : }
142 : :
143 : 196 : sal_Bool NamespaceIteratorImpl::next( ::rtl::OUString& rPrefix, ::rtl::OUString& rURL )
144 : : {
145 : : // we still need to process the current attribute
146 [ - + ][ # # ]: 196 : if( mpCurrentAttr && (mnCurrentAttr != USHRT_MAX) )
147 : : {
148 : 0 : rPrefix = mpCurrentAttr->GetPrefix( mnCurrentAttr );
149 : 0 : rURL = mpCurrentAttr->GetNamespace( mnCurrentAttr );
150 : :
151 : 0 : mnCurrentAttr = mpCurrentAttr->GetNextNamespaceIndex( mnCurrentAttr );
152 : 0 : return sal_True;
153 : : }
154 : :
155 : : // we need the next namespace item
156 : 196 : mpCurrentAttr = NULL;
157 : :
158 : 196 : const SfxPoolItem* pItem = 0;
159 : : // look for the next available item in the current pool
160 [ + + ][ + + ]: 218 : while( (mnItem < mnItemCount) && ( NULL == (pItem = mpPool->GetItem2( *mpWhichId, mnItem ) ) ) )
[ + + ]
161 : 22 : mnItem++;
162 : :
163 : : // are we finished with the current whichid?
164 [ + + ]: 196 : if( mnItem == mnItemCount )
165 : : {
166 : 174 : mpWhichId++;
167 : :
168 : : // are we finished with the current pool?
169 [ + + ]: 174 : if( 0 != *mpWhichId )
170 : : {
171 : 116 : mnItem = 0;
172 [ + - ][ + - ]: 116 : mnItemCount = (mpWhichId && (0 != *mpWhichId) && mpPool) ? mpPool->GetItemCount2( *mpWhichId ) : 0;
[ + - ]
173 : 116 : return next( rPrefix, rURL );
174 : : }
175 : :
176 : 58 : pItem = NULL;
177 : : }
178 : :
179 [ + + ]: 80 : if( pItem )
180 : : {
181 : 22 : mnItem++;
182 : :
183 : : // get that item and see if there namespaces inside
184 : 22 : const SvXMLAttrContainerItem *pUnknown = (const SvXMLAttrContainerItem *)pItem;
185 [ - + ]: 22 : if( (pUnknown->GetAttrCount() > 0) )
186 : : {
187 : 0 : mpCurrentAttr = pUnknown;
188 : 0 : mnCurrentAttr = pUnknown->GetFirstNamespaceIndex();
189 : : }
190 : 22 : return next( rPrefix, rURL );
191 : : }
192 : :
193 : 196 : return false;
194 : : }
195 : :
196 : : // -------------
197 : :
198 : 58 : NamespaceMap::NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool )
199 : 58 : : mpWhichIds( pWhichIds ), mpPool( pPool )
200 : : {
201 : 58 : }
202 : :
203 : 58 : NamespaceMap::~NamespaceMap()
204 : : {
205 [ - + ]: 116 : }
206 : :
207 : : // XNameAccess
208 : 0 : Any SAL_CALL NamespaceMap::getByName( const ::rtl::OUString& aName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
209 : : {
210 [ # # ]: 0 : NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
211 : :
212 : 0 : ::rtl::OUString aPrefix;
213 : 0 : ::rtl::OUString aURL;
214 : :
215 : : sal_Bool bFound;
216 : :
217 [ # # # # ]: 0 : do
[ # # ]
218 : : {
219 [ # # ]: 0 : bFound = aIter.next( aPrefix, aURL );
220 : : }
221 : 0 : while( bFound && (aPrefix != aName ) );
222 : :
223 [ # # ]: 0 : if( !bFound )
224 [ # # ]: 0 : throw NoSuchElementException();
225 : :
226 [ # # ]: 0 : return makeAny( aURL );
227 : : }
228 : :
229 : 58 : Sequence< ::rtl::OUString > SAL_CALL NamespaceMap::getElementNames() throw (RuntimeException)
230 : : {
231 [ + - ]: 58 : NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
232 : :
233 : 58 : ::rtl::OUString aPrefix;
234 : 58 : ::rtl::OUString aURL;
235 : :
236 [ + - ]: 58 : std::set< ::rtl::OUString, comphelper::UStringLess > aPrefixSet;
237 : :
238 [ + - ][ - + ]: 58 : while( aIter.next( aPrefix, aURL ) )
239 [ # # ]: 0 : aPrefixSet.insert( aPrefix );
240 : :
241 [ + - ]: 58 : Sequence< ::rtl::OUString > aSeq( aPrefixSet.size() );
242 [ + - ]: 58 : ::rtl::OUString* pPrefixes = aSeq.getArray();
243 : :
244 : 58 : std::set< ::rtl::OUString, comphelper::UStringLess >::iterator aPrefixIter( aPrefixSet.begin() );
245 : 58 : const std::set< ::rtl::OUString, comphelper::UStringLess >::iterator aEnd( aPrefixSet.end() );
246 : :
247 [ - + ]: 58 : while( aPrefixIter != aEnd )
248 : : {
249 : 0 : *pPrefixes++ = *aPrefixIter++;
250 : : }
251 : :
252 : 58 : return aSeq;
253 : : }
254 : :
255 : 0 : sal_Bool SAL_CALL NamespaceMap::hasByName( const ::rtl::OUString& aName ) throw (RuntimeException)
256 : : {
257 [ # # ]: 0 : NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
258 : :
259 : 0 : ::rtl::OUString aPrefix;
260 : 0 : ::rtl::OUString aURL;
261 : :
262 : : sal_Bool bFound;
263 : :
264 [ # # # # ]: 0 : do
[ # # ]
265 : : {
266 [ # # ]: 0 : bFound = aIter.next( aPrefix, aURL );
267 : : }
268 : 0 : while( bFound && (aPrefix != aName ) );
269 : :
270 : 0 : return bFound;
271 : : }
272 : :
273 : : // XElementAccess
274 : 0 : Type SAL_CALL NamespaceMap::getElementType() throw (RuntimeException)
275 : : {
276 : 0 : return ::getCppuType( (const ::rtl::OUString*) 0 );
277 : : }
278 : :
279 : 0 : sal_Bool SAL_CALL NamespaceMap::hasElements() throw (RuntimeException)
280 : : {
281 [ # # ]: 0 : NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
282 : :
283 : 0 : ::rtl::OUString aPrefix;
284 : 0 : ::rtl::OUString aURL;
285 : :
286 [ # # ]: 0 : return aIter.next( aPrefix, aURL );
287 : : }
288 : :
289 : : // XServiceInfo
290 : 0 : ::rtl::OUString SAL_CALL NamespaceMap::getImplementationName( )
291 : : throw(RuntimeException)
292 : : {
293 : 0 : return NamespaceMap_getImplementationName();
294 : : }
295 : :
296 : 0 : sal_Bool SAL_CALL NamespaceMap::supportsService( const ::rtl::OUString& )
297 : : throw(RuntimeException)
298 : : {
299 : 0 : return sal_True;
300 : : }
301 : :
302 : 0 : Sequence< ::rtl::OUString > SAL_CALL NamespaceMap::getSupportedServiceNames( )
303 : : throw(RuntimeException)
304 : : {
305 : 0 : return NamespaceMap_getSupportedServiceNames();
306 : : }
307 : :
308 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|