merge the formfield patch from ooo-build
[ooovba.git] / svx / source / unodraw / UnoNamespaceMap.cxx
blob0296bc060f5ac0cb2811041718dd4e3a75f68aad
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: UnoNamespaceMap.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include <set>
36 #include "UnoNamespaceMap.hxx"
37 #include <com/sun/star/container/XNameAccess.hpp>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #ifndef _CPPUHELPER_IMPLBASE3_HXX_
41 #include <cppuhelper/implbase2.hxx>
42 #endif
43 #include <osl/diagnose.h>
44 #include <osl/mutex.hxx>
45 #include <comphelper/stl_types.hxx>
46 #include <svtools/itempool.hxx>
47 #include "unoapi.hxx"
48 #include "xmlcnitm.hxx"
51 using namespace ::comphelper;
52 using namespace ::osl;
53 using namespace ::cppu;
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::container;
57 using namespace ::com::sun::star::drawing;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::beans;
61 namespace svx
63 /** implements a component to export namespaces of all SvXMLAttrContainerItem inside
64 one or two pools with a variable count of which ids.
66 class NamespaceMap : public WeakImplHelper2< XNameAccess, XServiceInfo >
68 private:
69 sal_uInt16* mpWhichIds;
70 SfxItemPool* mpPool;
72 public:
73 NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool );
74 virtual ~NamespaceMap();
76 // XNameAccess
77 virtual Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException);
78 virtual Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw (RuntimeException);
79 virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (RuntimeException);
81 // XElementAccess
82 virtual Type SAL_CALL getElementType( ) throw (RuntimeException);
83 virtual sal_Bool SAL_CALL hasElements( ) throw (RuntimeException);
85 // XServiceInfo
86 virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw(RuntimeException);
87 virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(RuntimeException);
88 virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw(RuntimeException);
91 Reference< XInterface > SAL_CALL NamespaceMap_createInstance( sal_uInt16* pWhichIds, SfxItemPool* pPool1, SfxItemPool* )
93 return (XWeak*)new NamespaceMap( pWhichIds, pPool1 );
96 Reference< XInterface > SAL_CALL NamespaceMap_createInstance( sal_uInt16* pWhichIds, SfxItemPool* pPool )
98 return (XWeak*)new NamespaceMap( pWhichIds, pPool );
101 Sequence< ::rtl::OUString > SAL_CALL NamespaceMap_getSupportedServiceNames()
102 throw()
104 Sequence< ::rtl::OUString > aSupportedServiceNames( 1 );
105 aSupportedServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.NamespaceMap" ) );
106 return aSupportedServiceNames;
109 ::rtl::OUString SAL_CALL NamespaceMap_getImplementationName()
110 throw()
112 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Svx.NamespaceMap" ) );
117 class NamespaceIteratorImpl
119 private:
120 SfxItemPool* mpPool;
122 sal_uInt16* mpWhichId;
124 sal_uInt16 mnItemCount;
125 sal_uInt16 mnItem;
127 const SvXMLAttrContainerItem* mpCurrentAttr;
128 sal_uInt16 mnCurrentAttr;
130 public:
132 NamespaceIteratorImpl( sal_uInt16* pWhichIds, SfxItemPool* pPool );
134 sal_Bool next( ::rtl::OUString& rPrefix, ::rtl::OUString& rURL );
138 using namespace ::svx;
140 // -------------
142 NamespaceIteratorImpl::NamespaceIteratorImpl( sal_uInt16* pWhichIds, SfxItemPool* pPool )
144 mpPool = pPool;
145 mpCurrentAttr = NULL;
146 mnCurrentAttr = 0;
148 mpWhichId = pWhichIds;
150 mnItem = 0;
151 mnItemCount = (mpWhichId && (0 != *mpWhichId) && mpPool) ? mpPool->GetItemCount( *mpWhichId ) : 0;
154 sal_Bool NamespaceIteratorImpl::next( ::rtl::OUString& rPrefix, ::rtl::OUString& rURL )
156 // we still need to process the current attribute
157 if( mpCurrentAttr && (mnCurrentAttr != USHRT_MAX) )
159 rPrefix = mpCurrentAttr->GetPrefix( mnCurrentAttr );
160 rURL = mpCurrentAttr->GetNamespace( mnCurrentAttr );
162 mnCurrentAttr = mpCurrentAttr->GetNextNamespaceIndex( mnCurrentAttr );
163 return sal_True;
166 // we need the next namespace item
167 mpCurrentAttr = NULL;
169 const SfxPoolItem* pItem = 0;
170 // look for the next available item in the current pool
171 while( (mnItem < mnItemCount) && ( NULL == (pItem = mpPool->GetItem( *mpWhichId, mnItem ) ) ) )
172 mnItem++;
174 // are we finished with the current whichid?
175 if( mnItem == mnItemCount )
177 mpWhichId++;
179 // are we finished with the current pool?
180 if( 0 != *mpWhichId )
182 mnItem = 0;
183 mnItemCount = (mpWhichId && (0 != *mpWhichId) && mpPool) ? mpPool->GetItemCount( *mpWhichId ) : 0;
184 return next( rPrefix, rURL );
187 pItem = NULL;
190 if( pItem )
192 mnItem++;
194 // get that item and see if there namespaces inside
195 const SvXMLAttrContainerItem *pUnknown = (const SvXMLAttrContainerItem *)pItem;
196 if( (pUnknown->GetAttrCount() > 0) )
198 mpCurrentAttr = pUnknown;
199 mnCurrentAttr = pUnknown->GetFirstNamespaceIndex();
201 return next( rPrefix, rURL );
204 return false;
207 // -------------
209 NamespaceMap::NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool )
210 : mpWhichIds( pWhichIds ), mpPool( pPool )
214 NamespaceMap::~NamespaceMap()
218 // XNameAccess
219 Any SAL_CALL NamespaceMap::getByName( const ::rtl::OUString& aName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
221 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
223 ::rtl::OUString aPrefix;
224 ::rtl::OUString aURL;
226 sal_Bool bFound;
230 bFound = aIter.next( aPrefix, aURL );
232 while( bFound && (aPrefix != aName ) );
234 if( !bFound )
235 throw NoSuchElementException();
237 return makeAny( aURL );
240 Sequence< ::rtl::OUString > SAL_CALL NamespaceMap::getElementNames() throw (RuntimeException)
242 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
244 ::rtl::OUString aPrefix;
245 ::rtl::OUString aURL;
247 std::set< ::rtl::OUString, comphelper::UStringLess > aPrefixSet;
249 while( aIter.next( aPrefix, aURL ) )
250 aPrefixSet.insert( aPrefix );
252 Sequence< ::rtl::OUString > aSeq( aPrefixSet.size() );
253 ::rtl::OUString* pPrefixes = aSeq.getArray();
255 std::set< ::rtl::OUString, comphelper::UStringLess >::iterator aPrefixIter( aPrefixSet.begin() );
256 const std::set< ::rtl::OUString, comphelper::UStringLess >::iterator aEnd( aPrefixSet.end() );
258 while( aPrefixIter != aEnd )
260 *pPrefixes++ = *aPrefixIter++;
263 return aSeq;
266 sal_Bool SAL_CALL NamespaceMap::hasByName( const ::rtl::OUString& aName ) throw (RuntimeException)
268 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
270 ::rtl::OUString aPrefix;
271 ::rtl::OUString aURL;
273 sal_Bool bFound;
277 bFound = aIter.next( aPrefix, aURL );
279 while( bFound && (aPrefix != aName ) );
281 return bFound;
284 // XElementAccess
285 Type SAL_CALL NamespaceMap::getElementType() throw (RuntimeException)
287 return ::getCppuType( (const ::rtl::OUString*) 0 );
290 sal_Bool SAL_CALL NamespaceMap::hasElements() throw (RuntimeException)
292 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
294 ::rtl::OUString aPrefix;
295 ::rtl::OUString aURL;
297 return aIter.next( aPrefix, aURL );
300 // XServiceInfo
301 ::rtl::OUString SAL_CALL NamespaceMap::getImplementationName( )
302 throw(RuntimeException)
304 return NamespaceMap_getImplementationName();
307 sal_Bool SAL_CALL NamespaceMap::supportsService( const ::rtl::OUString& )
308 throw(RuntimeException)
310 return sal_True;
313 Sequence< ::rtl::OUString > SAL_CALL NamespaceMap::getSupportedServiceNames( )
314 throw(RuntimeException)
316 return NamespaceMap_getSupportedServiceNames();