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 $
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"
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>
43 #include <osl/diagnose.h>
44 #include <osl/mutex.hxx>
45 #include <comphelper/stl_types.hxx>
46 #include <svtools/itempool.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
;
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
>
69 sal_uInt16
* mpWhichIds
;
73 NamespaceMap( sal_uInt16
* pWhichIds
, SfxItemPool
* pPool
);
74 virtual ~NamespaceMap();
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
);
82 virtual Type SAL_CALL
getElementType( ) throw (RuntimeException
);
83 virtual sal_Bool SAL_CALL
hasElements( ) throw (RuntimeException
);
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()
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()
112 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Svx.NamespaceMap" ) );
117 class NamespaceIteratorImpl
122 sal_uInt16
* mpWhichId
;
124 sal_uInt16 mnItemCount
;
127 const SvXMLAttrContainerItem
* mpCurrentAttr
;
128 sal_uInt16 mnCurrentAttr
;
132 NamespaceIteratorImpl( sal_uInt16
* pWhichIds
, SfxItemPool
* pPool
);
134 sal_Bool
next( ::rtl::OUString
& rPrefix
, ::rtl::OUString
& rURL
);
138 using namespace ::svx
;
142 NamespaceIteratorImpl::NamespaceIteratorImpl( sal_uInt16
* pWhichIds
, SfxItemPool
* pPool
)
145 mpCurrentAttr
= NULL
;
148 mpWhichId
= pWhichIds
;
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
);
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
) ) ) )
174 // are we finished with the current whichid?
175 if( mnItem
== mnItemCount
)
179 // are we finished with the current pool?
180 if( 0 != *mpWhichId
)
183 mnItemCount
= (mpWhichId
&& (0 != *mpWhichId
) && mpPool
) ? mpPool
->GetItemCount( *mpWhichId
) : 0;
184 return next( rPrefix
, rURL
);
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
);
209 NamespaceMap::NamespaceMap( sal_uInt16
* pWhichIds
, SfxItemPool
* pPool
)
210 : mpWhichIds( pWhichIds
), mpPool( pPool
)
214 NamespaceMap::~NamespaceMap()
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
;
230 bFound
= aIter
.next( aPrefix
, aURL
);
232 while( bFound
&& (aPrefix
!= aName
) );
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
++;
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
;
277 bFound
= aIter
.next( aPrefix
, aURL
);
279 while( bFound
&& (aPrefix
!= aName
) );
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
);
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
)
313 Sequence
< ::rtl::OUString
> SAL_CALL
NamespaceMap::getSupportedServiceNames( )
314 throw(RuntimeException
)
316 return NamespaceMap_getSupportedServiceNames();