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