1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svx.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <set>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "svx/UnoNamespaceMap.hxx"
34*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #ifndef _CPPUHELPER_IMPLBASE3_HXX_
38*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
39*cdf0e10cSrcweir #endif
40*cdf0e10cSrcweir #include <osl/diagnose.h>
41*cdf0e10cSrcweir #include <osl/mutex.hxx>
42*cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
43*cdf0e10cSrcweir #include <svl/itempool.hxx>
44*cdf0e10cSrcweir #include "svx/unoapi.hxx"
45*cdf0e10cSrcweir #include "editeng/xmlcnitm.hxx"
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using namespace ::comphelper;
49*cdf0e10cSrcweir using namespace ::osl;
50*cdf0e10cSrcweir using namespace ::cppu;
51*cdf0e10cSrcweir using namespace ::com::sun::star;
52*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
53*cdf0e10cSrcweir using namespace ::com::sun::star::container;
54*cdf0e10cSrcweir using namespace ::com::sun::star::drawing;
55*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
56*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir namespace svx
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 	/**	implements a component to export namespaces of all SvXMLAttrContainerItem inside
61*cdf0e10cSrcweir 		one or two pools with a variable count of which ids.
62*cdf0e10cSrcweir 	*/
63*cdf0e10cSrcweir 	class NamespaceMap : public WeakImplHelper2< XNameAccess, XServiceInfo >
64*cdf0e10cSrcweir 	{
65*cdf0e10cSrcweir 	private:
66*cdf0e10cSrcweir 		sal_uInt16* mpWhichIds;
67*cdf0e10cSrcweir 		SfxItemPool* mpPool;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	public:
70*cdf0e10cSrcweir 		NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool );
71*cdf0e10cSrcweir 		virtual ~NamespaceMap();
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 		// XNameAccess
74*cdf0e10cSrcweir 		virtual Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException);
75*cdf0e10cSrcweir 		virtual Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw (RuntimeException);
76*cdf0e10cSrcweir 		virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (RuntimeException);
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 		// XElementAccess
79*cdf0e10cSrcweir 		virtual Type SAL_CALL getElementType(  ) throw (RuntimeException);
80*cdf0e10cSrcweir 		virtual sal_Bool SAL_CALL hasElements(  ) throw (RuntimeException);
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 		// XServiceInfo
83*cdf0e10cSrcweir 		virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
84*cdf0e10cSrcweir 		virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(RuntimeException);
85*cdf0e10cSrcweir 		virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
86*cdf0e10cSrcweir 	};
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	Reference< XInterface > SAL_CALL NamespaceMap_createInstance( sal_uInt16* pWhichIds, SfxItemPool* pPool1, SfxItemPool* )
89*cdf0e10cSrcweir 	{
90*cdf0e10cSrcweir 		return (XWeak*)new NamespaceMap( pWhichIds, pPool1 );
91*cdf0e10cSrcweir 	}
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	Reference< XInterface > SAL_CALL NamespaceMap_createInstance( sal_uInt16* pWhichIds, SfxItemPool* pPool )
94*cdf0e10cSrcweir 	{
95*cdf0e10cSrcweir 		return (XWeak*)new NamespaceMap( pWhichIds, pPool );
96*cdf0e10cSrcweir 	}
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > SAL_CALL NamespaceMap_getSupportedServiceNames()
99*cdf0e10cSrcweir 		throw()
100*cdf0e10cSrcweir 	{
101*cdf0e10cSrcweir 		Sequence< ::rtl::OUString > aSupportedServiceNames( 1 );
102*cdf0e10cSrcweir 		aSupportedServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.NamespaceMap" ) );
103*cdf0e10cSrcweir 		return aSupportedServiceNames;
104*cdf0e10cSrcweir 	}
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 	::rtl::OUString SAL_CALL NamespaceMap_getImplementationName()
107*cdf0e10cSrcweir 		throw()
108*cdf0e10cSrcweir 	{
109*cdf0e10cSrcweir 		return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Svx.NamespaceMap" ) );
110*cdf0e10cSrcweir 	}
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	class NamespaceIteratorImpl
115*cdf0e10cSrcweir 	{
116*cdf0e10cSrcweir 	private:
117*cdf0e10cSrcweir 		SfxItemPool* mpPool;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 		sal_uInt16* mpWhichId;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 		sal_uInt32 mnItemCount;
122*cdf0e10cSrcweir 		sal_uInt32 mnItem;
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 		const SvXMLAttrContainerItem* mpCurrentAttr;
125*cdf0e10cSrcweir 		sal_uInt16 mnCurrentAttr;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 	public:
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 		NamespaceIteratorImpl( sal_uInt16* pWhichIds, SfxItemPool* pPool );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		sal_Bool next( ::rtl::OUString& rPrefix, ::rtl::OUString& rURL );
132*cdf0e10cSrcweir 	};
133*cdf0e10cSrcweir }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir using namespace ::svx;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir // -------------
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir NamespaceIteratorImpl::NamespaceIteratorImpl( sal_uInt16* pWhichIds, SfxItemPool* pPool )
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir 	mpPool = pPool;
142*cdf0e10cSrcweir 	mpCurrentAttr = NULL;
143*cdf0e10cSrcweir 	mnCurrentAttr = 0;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	mpWhichId = pWhichIds;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	mnItem = 0;
148*cdf0e10cSrcweir 	mnItemCount = (mpWhichId && (0 != *mpWhichId) && mpPool) ? mpPool->GetItemCount2( *mpWhichId ) : 0;
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir sal_Bool NamespaceIteratorImpl::next( ::rtl::OUString& rPrefix, ::rtl::OUString& rURL )
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	// we still need to process the current attribute
154*cdf0e10cSrcweir 	if( mpCurrentAttr && (mnCurrentAttr != USHRT_MAX) )
155*cdf0e10cSrcweir 	{
156*cdf0e10cSrcweir 		rPrefix = mpCurrentAttr->GetPrefix( mnCurrentAttr );
157*cdf0e10cSrcweir 		rURL = mpCurrentAttr->GetNamespace( mnCurrentAttr );
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 		mnCurrentAttr = mpCurrentAttr->GetNextNamespaceIndex( mnCurrentAttr );
160*cdf0e10cSrcweir 		return sal_True;
161*cdf0e10cSrcweir 	}
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	// we need the next namespace item
164*cdf0e10cSrcweir 	mpCurrentAttr = NULL;
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	const SfxPoolItem* pItem = 0;
167*cdf0e10cSrcweir 	// look for the next available item in the current pool
168*cdf0e10cSrcweir 	while( (mnItem < mnItemCount) && ( NULL == (pItem = mpPool->GetItem2( *mpWhichId, mnItem ) ) ) )
169*cdf0e10cSrcweir 		mnItem++;
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 	// are we finished with the current whichid?
172*cdf0e10cSrcweir 	if( mnItem == mnItemCount )
173*cdf0e10cSrcweir 	{
174*cdf0e10cSrcweir 		mpWhichId++;
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 		// are we finished with the current pool?
177*cdf0e10cSrcweir 		if( 0 != *mpWhichId )
178*cdf0e10cSrcweir 		{
179*cdf0e10cSrcweir 			mnItem = 0;
180*cdf0e10cSrcweir 			mnItemCount = (mpWhichId && (0 != *mpWhichId) && mpPool) ? mpPool->GetItemCount2( *mpWhichId ) : 0;
181*cdf0e10cSrcweir 			return next( rPrefix, rURL );
182*cdf0e10cSrcweir 		}
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 		pItem = NULL;
185*cdf0e10cSrcweir 	}
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	if( pItem )
188*cdf0e10cSrcweir 	{
189*cdf0e10cSrcweir 		mnItem++;
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 		// get that item and see if there namespaces inside
192*cdf0e10cSrcweir 		const SvXMLAttrContainerItem *pUnknown = (const SvXMLAttrContainerItem *)pItem;
193*cdf0e10cSrcweir 		if( (pUnknown->GetAttrCount() > 0) )
194*cdf0e10cSrcweir 		{
195*cdf0e10cSrcweir 			mpCurrentAttr = pUnknown;
196*cdf0e10cSrcweir 			mnCurrentAttr = pUnknown->GetFirstNamespaceIndex();
197*cdf0e10cSrcweir 		}
198*cdf0e10cSrcweir 		return next( rPrefix, rURL );
199*cdf0e10cSrcweir 	}
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	return false;
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir // -------------
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir NamespaceMap::NamespaceMap( sal_uInt16* pWhichIds, SfxItemPool* pPool )
207*cdf0e10cSrcweir : mpWhichIds( pWhichIds ), mpPool( pPool )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir NamespaceMap::~NamespaceMap()
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir // XNameAccess
216*cdf0e10cSrcweir Any SAL_CALL NamespaceMap::getByName( const ::rtl::OUString& aName ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
217*cdf0e10cSrcweir {
218*cdf0e10cSrcweir 	NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 	::rtl::OUString aPrefix;
221*cdf0e10cSrcweir 	::rtl::OUString aURL;
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 	sal_Bool bFound;
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 	do
226*cdf0e10cSrcweir 	{
227*cdf0e10cSrcweir 		bFound = aIter.next( aPrefix, aURL );
228*cdf0e10cSrcweir 	}
229*cdf0e10cSrcweir 	while( bFound && (aPrefix != aName ) );
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 	if( !bFound )
232*cdf0e10cSrcweir 		throw NoSuchElementException();
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 	return makeAny( aURL );
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL NamespaceMap::getElementNames() throw (RuntimeException)
238*cdf0e10cSrcweir {
239*cdf0e10cSrcweir 	NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 	::rtl::OUString aPrefix;
242*cdf0e10cSrcweir 	::rtl::OUString aURL;
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 	std::set< ::rtl::OUString, comphelper::UStringLess > aPrefixSet;
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir 	while( aIter.next( aPrefix, aURL ) )
247*cdf0e10cSrcweir 		aPrefixSet.insert( aPrefix );
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aSeq( aPrefixSet.size() );
250*cdf0e10cSrcweir 	::rtl::OUString* pPrefixes = aSeq.getArray();
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 	std::set< ::rtl::OUString, comphelper::UStringLess >::iterator aPrefixIter( aPrefixSet.begin() );
253*cdf0e10cSrcweir 	const std::set< ::rtl::OUString, comphelper::UStringLess >::iterator aEnd( aPrefixSet.end() );
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 	while( aPrefixIter != aEnd )
256*cdf0e10cSrcweir 	{
257*cdf0e10cSrcweir 		*pPrefixes++ = *aPrefixIter++;
258*cdf0e10cSrcweir 	}
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 	return aSeq;
261*cdf0e10cSrcweir }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir sal_Bool SAL_CALL NamespaceMap::hasByName( const ::rtl::OUString& aName ) throw (RuntimeException)
264*cdf0e10cSrcweir {
265*cdf0e10cSrcweir 	NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 	::rtl::OUString aPrefix;
268*cdf0e10cSrcweir 	::rtl::OUString aURL;
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir 	sal_Bool bFound;
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 	do
273*cdf0e10cSrcweir 	{
274*cdf0e10cSrcweir 		bFound = aIter.next( aPrefix, aURL );
275*cdf0e10cSrcweir 	}
276*cdf0e10cSrcweir 	while( bFound && (aPrefix != aName ) );
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 	return bFound;
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir // XElementAccess
282*cdf0e10cSrcweir Type SAL_CALL NamespaceMap::getElementType() throw (RuntimeException)
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir 	return ::getCppuType( (const ::rtl::OUString*) 0 );
285*cdf0e10cSrcweir }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir sal_Bool SAL_CALL NamespaceMap::hasElements() throw (RuntimeException)
288*cdf0e10cSrcweir {
289*cdf0e10cSrcweir 	NamespaceIteratorImpl aIter( mpWhichIds, mpPool );
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir 	::rtl::OUString aPrefix;
292*cdf0e10cSrcweir 	::rtl::OUString aURL;
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir 	return aIter.next( aPrefix, aURL );
295*cdf0e10cSrcweir }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir // XServiceInfo
298*cdf0e10cSrcweir ::rtl::OUString SAL_CALL NamespaceMap::getImplementationName(  )
299*cdf0e10cSrcweir 	throw(RuntimeException)
300*cdf0e10cSrcweir {
301*cdf0e10cSrcweir 	return NamespaceMap_getImplementationName();
302*cdf0e10cSrcweir }
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir sal_Bool SAL_CALL NamespaceMap::supportsService( const ::rtl::OUString& )
305*cdf0e10cSrcweir 	throw(RuntimeException)
306*cdf0e10cSrcweir {
307*cdf0e10cSrcweir 	return sal_True;
308*cdf0e10cSrcweir }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL NamespaceMap::getSupportedServiceNames(  )
311*cdf0e10cSrcweir 	throw(RuntimeException)
312*cdf0e10cSrcweir {
313*cdf0e10cSrcweir 	return NamespaceMap_getSupportedServiceNames();
314*cdf0e10cSrcweir }
315