1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <vbahelper/collectionbase.hxx>
25 
26 #include <map>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/container/XNameAccess.hpp>
29 #include <cppuhelper/implbase2.hxx>
30 
31 namespace vbahelper {
32 
33 using namespace ::com::sun::star;
34 using namespace ::ooo::vba;
35 
36 // ============================================================================
37 
38 namespace {
39 
40 // ----------------------------------------------------------------------------
41 
42 class CollectionEnumeration : public ::cppu::WeakImplHelper1< container::XEnumeration >
43 {
44 public:
45     explicit CollectionEnumeration( const ::rtl::Reference< CollectionBase >& rxCollection );
46     virtual sal_Bool SAL_CALL hasMoreElements() throw (uno::RuntimeException);
47     virtual uno::Any SAL_CALL nextElement() throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException);
48 
49 private:
50     ::rtl::Reference< CollectionBase > mxCollection;
51     sal_Int32 mnCurrIndex;
52 };
53 
CollectionEnumeration(const::rtl::Reference<CollectionBase> & rxCollection)54 CollectionEnumeration::CollectionEnumeration( const ::rtl::Reference< CollectionBase >& rxCollection ) :
55     mxCollection( rxCollection ),
56     mnCurrIndex( 1 )    // collection expects one-based indexes
57 {
58 }
59 
hasMoreElements()60 sal_Bool SAL_CALL CollectionEnumeration::hasMoreElements() throw (uno::RuntimeException)
61 {
62     return mnCurrIndex <= mxCollection->getCount();
63 }
64 
nextElement()65 uno::Any SAL_CALL CollectionEnumeration::nextElement() throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
66 {
67     if( hasMoreElements() )
68         return mxCollection->getItemByIndex( mnCurrIndex++ );
69     throw container::NoSuchElementException();
70 }
71 
72 // ----------------------------------------------------------------------------
73 
74 struct IsLessIgnoreCase
75 {
operator ()vbahelper::__anonc8aca98e0111::IsLessIgnoreCase76     inline bool operator()( const ::rtl::OUString& rName1, const ::rtl::OUString& rName2 ) const
77         { return ::rtl_ustr_compareIgnoreAsciiCase_WithLength( rName1.getStr(), rName1.getLength(), rName2.getStr(), rName2.getLength() ) < 0; }
78 };
79 
80 // ----------------------------------------------------------------------------
81 
82 class SequenceToContainer : public ::cppu::WeakImplHelper2< container::XIndexAccess, container::XNameAccess >
83 {
84 public:
85     explicit SequenceToContainer( const ::std::vector< uno::Reference< container::XNamed > >& rElements, const uno::Type& rElementType );
86     explicit SequenceToContainer( const ::std::vector< beans::NamedValue >& rElements, const uno::Type& rElementType );
87     // XIndexAccess
88     virtual sal_Int32 SAL_CALL getCount() throw (uno::RuntimeException);
89     virtual uno::Any SAL_CALL getByIndex( sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException);
90     // XNameAccess
91     virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& rName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException);
92     virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames() throw (uno::RuntimeException);
93     virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& rName ) throw (uno::RuntimeException);
94     // XElementAccess
95     virtual uno::Type SAL_CALL getElementType() throw (uno::RuntimeException);
96     virtual sal_Bool SAL_CALL hasElements() throw (uno::RuntimeException);
97 
98 private:
99     typedef uno::Sequence< ::rtl::OUString > ElementNameSequence;
100     typedef ::std::vector< uno::Any > ElementVector;
101     typedef ::std::map< ::rtl::OUString, uno::Any, IsLessIgnoreCase > ElementMap;
102 
103     ElementNameSequence maElementNames;
104     ElementVector maElements;
105     ElementMap maElementMap;
106     uno::Type maElementType;
107 };
108 
SequenceToContainer(const::std::vector<uno::Reference<container::XNamed>> & rElements,const uno::Type & rElementType)109 SequenceToContainer::SequenceToContainer( const ::std::vector< uno::Reference< container::XNamed > >& rElements, const uno::Type& rElementType ) :
110     maElementType( rElementType )
111 {
112     maElementNames.realloc( static_cast< sal_Int32 >( rElements.size() ) );
113     maElements.reserve( rElements.size() );
114     ::rtl::OUString* pElementName = maElementNames.getArray();
115     for( ::std::vector< uno::Reference< container::XNamed > >::const_iterator aIt = rElements.begin(), aEnd = rElements.end(); aIt != aEnd; ++aIt, ++pElementName )
116     {
117         uno::Reference< container::XNamed > xNamed = *aIt;
118         *pElementName = xNamed->getName();
119         maElements.push_back( uno::Any( xNamed ) );
120         // same name may occur multiple times, VBA returns first occurance
121         if( maElementMap.count( *pElementName ) == 0 )
122             maElementMap[ *pElementName ] <<= xNamed;
123     }
124 }
125 
SequenceToContainer(const::std::vector<beans::NamedValue> & rElements,const uno::Type & rElementType)126 SequenceToContainer::SequenceToContainer( const ::std::vector< beans::NamedValue >& rElements, const uno::Type& rElementType ) :
127     maElementType( rElementType )
128 {
129     maElementNames.realloc( static_cast< sal_Int32 >( rElements.size() ) );
130     maElements.reserve( rElements.size() );
131     ::rtl::OUString* pElementName = maElementNames.getArray();
132     for( ::std::vector< beans::NamedValue >::const_iterator aIt = rElements.begin(), aEnd = rElements.end(); aIt != aEnd; ++aIt, ++pElementName )
133     {
134         *pElementName = aIt->Name;
135         maElements.push_back( aIt->Value );
136         // same name may occur multiple times, VBA returns first occurance
137         if( maElementMap.count( *pElementName ) == 0 )
138             maElementMap[ *pElementName ] = aIt->Value;
139     }
140 }
141 
getCount()142 sal_Int32 SAL_CALL SequenceToContainer::getCount() throw (uno::RuntimeException)
143 {
144     return static_cast< sal_Int32 >( maElements.size() );
145 }
146 
getByIndex(sal_Int32 nIndex)147 uno::Any SAL_CALL SequenceToContainer::getByIndex( sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
148 {
149     if( (0 <= nIndex) && (nIndex < getCount()) )
150         return maElements[ static_cast< size_t >( nIndex ) ];
151     throw lang::IndexOutOfBoundsException();
152 }
153 
getByName(const::rtl::OUString & rName)154 uno::Any SAL_CALL SequenceToContainer::getByName( const ::rtl::OUString& rName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
155 {
156     ElementMap::iterator aIt = maElementMap.find( rName );
157     if( aIt != maElementMap.end() )
158         return aIt->second;
159     throw container::NoSuchElementException();
160 }
161 
getElementNames()162 uno::Sequence< ::rtl::OUString > SAL_CALL SequenceToContainer::getElementNames() throw (uno::RuntimeException)
163 {
164     return maElementNames;
165 }
166 
hasByName(const::rtl::OUString & rName)167 sal_Bool SAL_CALL SequenceToContainer::hasByName( const ::rtl::OUString& rName ) throw (uno::RuntimeException)
168 {
169     return maElementMap.count( rName ) > 0;
170 }
171 
getElementType()172 uno::Type SAL_CALL SequenceToContainer::getElementType() throw (uno::RuntimeException)
173 {
174     return maElementType;
175 }
176 
hasElements()177 sal_Bool SAL_CALL SequenceToContainer::hasElements() throw (uno::RuntimeException)
178 {
179     return !maElements.empty();
180 }
181 
182 } // namespace
183 
184 // ============================================================================
185 
CollectionBase(const uno::Type & rElementType)186 CollectionBase::CollectionBase( const uno::Type& rElementType ) :
187     maElementType( rElementType ),
188     mbConvertOnDemand( false )
189 {
190 }
191 
getCount()192 sal_Int32 SAL_CALL CollectionBase::getCount() throw (uno::RuntimeException)
193 {
194     if( mxIndexAccess.is() )
195         return mxIndexAccess->getCount();
196     if( mxNameAccess.is() )
197         return mxNameAccess->getElementNames().getLength();
198     throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No element container set." ) ), 0 );
199 }
200 
createEnumeration()201 uno::Reference< container::XEnumeration > SAL_CALL CollectionBase::createEnumeration() throw (uno::RuntimeException)
202 {
203     return new CollectionEnumeration( this );
204 }
205 
getElementType()206 uno::Type SAL_CALL CollectionBase::getElementType() throw (uno::RuntimeException)
207 {
208     return maElementType;
209 }
210 
hasElements()211 sal_Bool SAL_CALL CollectionBase::hasElements() throw (uno::RuntimeException)
212 {
213     if( mxIndexAccess.is() )
214         return mxIndexAccess->hasElements();
215     if( mxNameAccess.is() )
216         return mxNameAccess->hasElements();
217     throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No element container set." ) ), 0 );
218 }
219 
getDefaultMethodName()220 ::rtl::OUString SAL_CALL CollectionBase::getDefaultMethodName() throw (uno::RuntimeException)
221 {
222     static ::rtl::OUString saDefMethodName( RTL_CONSTASCII_USTRINGPARAM( "Item" ) );
223     return saDefMethodName;
224 }
225 
226 // ----------------------------------------------------------------------------
227 
initContainer(const uno::Reference<container::XElementAccess> & rxElementAccess,ContainerType eContainerType)228 void CollectionBase::initContainer(
229         const uno::Reference< container::XElementAccess >& rxElementAccess,
230         ContainerType eContainerType ) throw (uno::RuntimeException)
231 {
232     mxIndexAccess.set( rxElementAccess, uno::UNO_QUERY );
233     mxNameAccess.set( rxElementAccess, uno::UNO_QUERY );
234     switch( eContainerType )
235     {
236         case CONTAINER_NATIVE_VBA:
237             mbConvertOnDemand = false;
238         break;
239         case CONTAINER_CONVERT_ON_DEMAND:
240             mbConvertOnDemand = true;
241         break;
242     }
243 }
244 
initElements(const::std::vector<uno::Reference<container::XNamed>> & rElements,ContainerType eContainerType)245 void CollectionBase::initElements( const ::std::vector< uno::Reference< container::XNamed > >& rElements, ContainerType eContainerType ) throw (uno::RuntimeException)
246 {
247     // SequenceToContainer derives twice from XElementAccess, need to resolve ambiguity
248     initContainer( static_cast< container::XIndexAccess* >( new SequenceToContainer( rElements, maElementType ) ), eContainerType );
249 }
250 
initElements(const::std::vector<beans::NamedValue> & rElements,ContainerType eContainerType)251 void CollectionBase::initElements( const ::std::vector< beans::NamedValue >& rElements, ContainerType eContainerType ) throw (uno::RuntimeException)
252 {
253     // SequenceToContainer derives twice from XElementAccess, need to resolve ambiguity
254     initContainer( static_cast< container::XIndexAccess* >( new SequenceToContainer( rElements, maElementType ) ), eContainerType );
255 }
256 
createCollectionItem(const uno::Any & rElement,const uno::Any & rIndex)257 uno::Any CollectionBase::createCollectionItem( const uno::Any& rElement, const uno::Any& rIndex ) throw (css::uno::RuntimeException)
258 {
259     uno::Any aItem = mbConvertOnDemand ? implCreateCollectionItem( rElement, rIndex ) : rElement;
260     if( aItem.hasValue() )
261         return aItem;
262     throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Invalid item." ) ), 0 );
263 }
264 
getItemByIndex(sal_Int32 nIndex)265 uno::Any CollectionBase::getItemByIndex( sal_Int32 nIndex ) throw (uno::RuntimeException)
266 {
267     if( mxIndexAccess.is() )
268     {
269         if( (1 <= nIndex) && (nIndex <= mxIndexAccess->getCount()) )
270             // createCollectionItem() will convert from container element to VBA item
271             return createCollectionItem( mxIndexAccess->getByIndex( nIndex - 1 ), uno::Any( nIndex ) );
272         throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Index out of bounds." ) ), 0 );
273     }
274     if( mxNameAccess.is() )
275     {
276         uno::Sequence< ::rtl::OUString > aElementNames = mxNameAccess->getElementNames();
277         if( (1 <= nIndex) && (nIndex <= aElementNames.getLength()) )
278             // createCollectionItem() will convert from container element to VBA item
279             return createCollectionItem( mxNameAccess->getByName( aElementNames[ nIndex - 1 ] ), uno::Any( aElementNames[ nIndex - 1 ] ) );
280         throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Index out of bounds." ) ), 0 );
281     }
282     throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No element container set." ) ), 0 );
283 }
284 
getItemByName(const::rtl::OUString & rName)285 uno::Any CollectionBase::getItemByName( const ::rtl::OUString& rName ) throw (uno::RuntimeException)
286 {
287     if( mxNameAccess.is() )
288     {
289         if( rName.getLength() > 0 )
290             // createCollectionItem() will convert from container element to VBA item
291             return createCollectionItem( mxNameAccess->getByName( rName ), uno::Any( rName ) );
292         throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Invalid item name." ) ), 0 );
293     }
294     if( mxIndexAccess.is() )
295     {
296         for( sal_Int32 nIndex = 0, nSize = mxIndexAccess->getCount(); nIndex < nSize; ++nIndex )
297         {
298             uno::Any aElement = mxIndexAccess->getByIndex( nIndex );
299             uno::Reference< container::XNamed > xNamed( aElement, uno::UNO_QUERY );
300             if( xNamed.is() && xNamed->getName().equalsIgnoreAsciiCase( rName ) )
301                 // createCollectionItem() will convert from container element to VBA item
302                 return createCollectionItem( aElement, uno::Any( nIndex ) );
303         }
304         throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Invalid item name." ) ), 0 );
305     }
306     throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No element container set." ) ), 0 );
307 }
308 
getAnyItemOrThis(const uno::Any & rIndex)309 uno::Any CollectionBase::getAnyItemOrThis( const uno::Any& rIndex ) throw (uno::RuntimeException)
310 {
311     if( !rIndex.hasValue() )
312         return uno::Any( uno::Reference< XCollectionBase >( this ) );
313     if( rIndex.has< ::rtl::OUString >() )
314         return getItemByName( rIndex.get< ::rtl::OUString >() );
315     // extractIntFromAny() throws if no index can be extracted
316     return getItemByIndex( extractIntFromAny( rIndex ) );
317 }
318 
319 // protected ------------------------------------------------------------------
320 
implCreateCollectionItem(const uno::Any &,const uno::Any &)321 uno::Any CollectionBase::implCreateCollectionItem( const uno::Any& /*rElement*/, const uno::Any& /*rIndex*/ ) throw (uno::RuntimeException)
322 {
323     throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Creation of VBA implementation object not implemented." ) ), 0 );
324 }
325 
326 // ============================================================================
327 
328 } // namespace vbahelper
329