update credits
[LibreOffice.git] / svx / source / unodraw / UnoNamespaceMap.cxx
blobaf77a5269b1fe31e3d492f93ae032277e7efa595
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <set>
23 #include "svx/UnoNamespaceMap.hxx"
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <cppuhelper/implbase2.hxx>
28 #include <osl/diagnose.h>
29 #include <osl/mutex.hxx>
30 #include <comphelper/stl_types.hxx>
31 #include <svl/itempool.hxx>
32 #include "svx/unoapi.hxx"
33 #include "editeng/xmlcnitm.hxx"
36 using namespace ::comphelper;
37 using namespace ::osl;
38 using namespace ::cppu;
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::drawing;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::beans;
46 namespace svx
48 /** implements a component to export namespaces of all SvXMLAttrContainerItem inside
49 one or two pools with a variable count of which ids.
51 class NamespaceMap : public WeakImplHelper2< XNameAccess, XServiceInfo >
53 private:
54 sal_uInt16* mpWhichIds;
55 SfxItemPool* mpPool;
57 public:
58 NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool );
59 virtual ~NamespaceMap();
61 // XNameAccess
62 virtual Any SAL_CALL getByName( const OUString& aName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException);
63 virtual Sequence< OUString > SAL_CALL getElementNames( ) throw (RuntimeException);
64 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (RuntimeException);
66 // XElementAccess
67 virtual Type SAL_CALL getElementType( ) throw (RuntimeException);
68 virtual sal_Bool SAL_CALL hasElements( ) throw (RuntimeException);
70 // XServiceInfo
71 virtual OUString SAL_CALL getImplementationName( ) throw(RuntimeException);
72 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
73 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw(RuntimeException);
76 Reference< XInterface > SAL_CALL NamespaceMap_createInstance( sal_uInt16* pWhichIds, SfxItemPool* pPool )
78 return (XWeak*)new NamespaceMap( pWhichIds, pPool );
81 Sequence< OUString > SAL_CALL NamespaceMap_getSupportedServiceNames()
82 throw()
84 Sequence< OUString > aSupportedServiceNames( 1 );
85 aSupportedServiceNames[0] = OUString( "com.sun.star.xml.NamespaceMap" );
86 return aSupportedServiceNames;
89 OUString SAL_CALL NamespaceMap_getImplementationName()
90 throw()
92 return OUString( "com.sun.star.comp.Svx.NamespaceMap" );
97 class NamespaceIteratorImpl
99 private:
100 SfxItemPool* mpPool;
102 sal_uInt16* mpWhichId;
104 sal_uInt32 mnItemCount;
105 sal_uInt32 mnItem;
107 const SvXMLAttrContainerItem* mpCurrentAttr;
108 sal_uInt16 mnCurrentAttr;
110 public:
112 NamespaceIteratorImpl( sal_uInt16* pWhichIds, SfxItemPool* pPool );
114 sal_Bool next( OUString& rPrefix, OUString& rURL );
118 using namespace ::svx;
120 // -------------
122 NamespaceIteratorImpl::NamespaceIteratorImpl( sal_uInt16* pWhichIds, SfxItemPool* pPool )
124 mpPool = pPool;
125 mpCurrentAttr = NULL;
126 mnCurrentAttr = 0;
128 mpWhichId = pWhichIds;
130 mnItem = 0;
131 mnItemCount = (mpWhichId && (0 != *mpWhichId) && mpPool) ? mpPool->GetItemCount2( *mpWhichId ) : 0;
134 sal_Bool NamespaceIteratorImpl::next( OUString& rPrefix, OUString& rURL )
136 // we still need to process the current attribute
137 if( mpCurrentAttr && (mnCurrentAttr != USHRT_MAX) )
139 rPrefix = mpCurrentAttr->GetPrefix( mnCurrentAttr );
140 rURL = mpCurrentAttr->GetNamespace( mnCurrentAttr );
142 mnCurrentAttr = mpCurrentAttr->GetNextNamespaceIndex( mnCurrentAttr );
143 return sal_True;
146 // we need the next namespace item
147 mpCurrentAttr = NULL;
149 const SfxPoolItem* pItem = 0;
150 // look for the next available item in the current pool
151 while( (mnItem < mnItemCount) && ( NULL == (pItem = mpPool->GetItem2( *mpWhichId, mnItem ) ) ) )
152 mnItem++;
154 // are we finished with the current whichid?
155 if( mnItem == mnItemCount )
157 mpWhichId++;
159 // are we finished with the current pool?
160 if( 0 != *mpWhichId )
162 mnItem = 0;
163 mnItemCount = (mpWhichId && (0 != *mpWhichId) && mpPool) ? mpPool->GetItemCount2( *mpWhichId ) : 0;
164 return next( rPrefix, rURL );
167 pItem = NULL;
170 if( pItem )
172 mnItem++;
174 // get that item and see if there namespaces inside
175 const SvXMLAttrContainerItem *pUnknown = (const SvXMLAttrContainerItem *)pItem;
176 if( (pUnknown->GetAttrCount() > 0) )
178 mpCurrentAttr = pUnknown;
179 mnCurrentAttr = pUnknown->GetFirstNamespaceIndex();
181 return next( rPrefix, rURL );
184 return false;
187 // -------------
189 NamespaceMap::NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool )
190 : mpWhichIds( pWhichIds ), mpPool( pPool )
194 NamespaceMap::~NamespaceMap()
198 // XNameAccess
199 Any SAL_CALL NamespaceMap::getByName( const OUString& aName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
201 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
203 OUString aPrefix;
204 OUString aURL;
206 sal_Bool bFound;
210 bFound = aIter.next( aPrefix, aURL );
212 while( bFound && (aPrefix != aName ) );
214 if( !bFound )
215 throw NoSuchElementException();
217 return makeAny( aURL );
220 Sequence< OUString > SAL_CALL NamespaceMap::getElementNames() throw (RuntimeException)
222 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
224 OUString aPrefix;
225 OUString aURL;
227 std::set< OUString, comphelper::UStringLess > aPrefixSet;
229 while( aIter.next( aPrefix, aURL ) )
230 aPrefixSet.insert( aPrefix );
232 Sequence< OUString > aSeq( aPrefixSet.size() );
233 OUString* pPrefixes = aSeq.getArray();
235 std::set< OUString, comphelper::UStringLess >::iterator aPrefixIter( aPrefixSet.begin() );
236 const std::set< OUString, comphelper::UStringLess >::iterator aEnd( aPrefixSet.end() );
238 while( aPrefixIter != aEnd )
240 *pPrefixes++ = *aPrefixIter++;
243 return aSeq;
246 sal_Bool SAL_CALL NamespaceMap::hasByName( const OUString& aName ) throw (RuntimeException)
248 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
250 OUString aPrefix;
251 OUString aURL;
253 sal_Bool bFound;
257 bFound = aIter.next( aPrefix, aURL );
259 while( bFound && (aPrefix != aName ) );
261 return bFound;
264 // XElementAccess
265 Type SAL_CALL NamespaceMap::getElementType() throw (RuntimeException)
267 return ::getCppuType( (const OUString*) 0 );
270 sal_Bool SAL_CALL NamespaceMap::hasElements() throw (RuntimeException)
272 NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
274 OUString aPrefix;
275 OUString aURL;
277 return aIter.next( aPrefix, aURL );
280 // XServiceInfo
281 OUString SAL_CALL NamespaceMap::getImplementationName( )
282 throw(RuntimeException)
284 return NamespaceMap_getImplementationName();
287 sal_Bool SAL_CALL NamespaceMap::supportsService( const OUString& )
288 throw(RuntimeException)
290 return sal_True;
293 Sequence< OUString > SAL_CALL NamespaceMap::getSupportedServiceNames( )
294 throw(RuntimeException)
296 return NamespaceMap_getSupportedServiceNames();
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */