update dev300-m58
[ooovba.git] / xmloff / source / core / unoatrcn.cxx
blob1ae7ed7ab360213a852df14b5255f97bdfa43b4e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unoatrcn.cxx,v $
10 * $Revision: 1.10 $
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_xmloff.hxx"
33 #include <tools/debug.hxx>
34 #include <com/sun/star/xml/AttributeData.hpp>
35 #include <rtl/ustrbuf.hxx>
36 #include <rtl/uuid.h>
37 #include <rtl/memory.h>
39 #include <xmloff/xmlcnimp.hxx>
41 #include "unoatrcn.hxx"
43 using ::rtl::OUString;
44 using ::rtl::OUStringBuffer;
46 using namespace ::com::sun::star;
48 // --------------------------------------------------------------------
49 // Interface implementation
50 // --------------------------------------------------------------------
52 #define IMPL ((AttrContainerImpl*)mpData)
54 uno::Reference< uno::XInterface > SvUnoAttributeContainer_CreateInstance()
56 return *(new SvUnoAttributeContainer);
59 SvUnoAttributeContainer::SvUnoAttributeContainer( SvXMLAttrContainerData* pContainer)
60 : mpContainer( pContainer )
62 if( mpContainer == NULL )
63 mpContainer = new SvXMLAttrContainerData;
66 SvUnoAttributeContainer::~SvUnoAttributeContainer()
68 delete mpContainer;
71 // container::XElementAccess
72 uno::Type SAL_CALL SvUnoAttributeContainer::getElementType(void)
73 throw( uno::RuntimeException )
75 return ::getCppuType((const xml::AttributeData*)0);
78 sal_Bool SAL_CALL SvUnoAttributeContainer::hasElements(void)
79 throw( uno::RuntimeException )
81 return mpContainer->GetAttrCount() != 0;
84 sal_uInt16 SvUnoAttributeContainer::getIndexByName(const OUString& aName ) const
86 const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
88 sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
89 if( nPos == -1L )
91 for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
93 if( mpContainer->GetAttrLName(nAttr) == aName &&
94 mpContainer->GetAttrPrefix(nAttr).getLength() == 0L )
95 return nAttr;
98 else
100 const OUString aPrefix( aName.copy( 0L, nPos ) );
101 const OUString aLName( aName.copy( nPos+1L ) );
103 for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
105 if( mpContainer->GetAttrLName(nAttr) == aLName &&
106 mpContainer->GetAttrPrefix(nAttr) == aPrefix )
107 return nAttr;
111 return USHRT_MAX;
114 const ::com::sun::star::uno::Sequence< sal_Int8 > & SvUnoAttributeContainer::getUnoTunnelId() throw()
116 static ::com::sun::star::uno::Sequence< sal_Int8 > * pSeq = 0;
117 if( !pSeq )
119 ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() );
120 if( !pSeq )
122 static ::com::sun::star::uno::Sequence< sal_Int8 > aSeq( 16 );
123 rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True );
124 pSeq = &aSeq;
127 return *pSeq;
130 SvUnoAttributeContainer* SvUnoAttributeContainer::getImplementation( uno::Reference< uno::XInterface > xInt ) throw()
132 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xUT( xInt, ::com::sun::star::uno::UNO_QUERY );
133 if( xUT.is() )
135 return
136 reinterpret_cast<SvUnoAttributeContainer*>(
137 sal::static_int_cast<sal_IntPtr>(
138 xUT->getSomething( SvUnoAttributeContainer::getUnoTunnelId())));
140 else
141 return NULL;
144 sal_Int64 SAL_CALL SvUnoAttributeContainer::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rId ) throw(::com::sun::star::uno::RuntimeException)
146 if( rId.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
147 rId.getConstArray(), 16 ) )
149 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
151 return 0;
154 // container::XNameAccess
155 uno::Any SAL_CALL SvUnoAttributeContainer::getByName(const OUString& aName)
156 throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
158 sal_uInt16 nAttr = getIndexByName(aName );
160 if( nAttr == USHRT_MAX )
161 throw container::NoSuchElementException();
163 xml::AttributeData aData;
164 aData.Namespace = mpContainer->GetAttrNamespace(nAttr);
165 aData.Type = OUString::createFromAscii("CDATA");
166 aData.Value = mpContainer->GetAttrValue(nAttr);
168 uno::Any aAny;
169 aAny <<= aData;
170 return aAny;
173 uno::Sequence< OUString > SAL_CALL SvUnoAttributeContainer::getElementNames(void) throw( uno::RuntimeException )
175 const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
177 uno::Sequence< OUString > aElementNames( (sal_Int32)nAttrCount );
178 OUString *pNames = aElementNames.getArray();
180 for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
182 OUStringBuffer sBuffer( mpContainer->GetAttrPrefix(nAttr) );
183 if( sBuffer.getLength() != 0L )
184 sBuffer.append( (sal_Unicode)':' );
185 sBuffer.append( mpContainer->GetAttrLName(nAttr) );
186 *pNames++ = sBuffer.makeStringAndClear();
189 return aElementNames;
192 sal_Bool SAL_CALL SvUnoAttributeContainer::hasByName(const OUString& aName) throw( uno::RuntimeException )
194 return getIndexByName(aName ) != USHRT_MAX;
197 // container::XNameReplace
198 void SAL_CALL SvUnoAttributeContainer::replaceByName(const OUString& aName, const uno::Any& aElement)
199 throw( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
201 if( aElement.hasValue() && aElement.getValueType() == ::getCppuType((const xml::AttributeData*)0) )
203 sal_uInt16 nAttr = getIndexByName(aName );
204 if( nAttr == USHRT_MAX )
205 throw container::NoSuchElementException();
207 xml::AttributeData* pData = (xml::AttributeData*)aElement.getValue();
209 sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
210 if( nPos != -1L )
212 const OUString aPrefix( aName.copy( 0L, nPos ));
213 const OUString aLName( aName.copy( nPos+1L ));
215 if( pData->Namespace.getLength() == 0L )
217 if( mpContainer->SetAt( nAttr, aPrefix, aLName, pData->Value ) )
218 return;
220 else
222 if( mpContainer->SetAt( nAttr, aPrefix, pData->Namespace, aLName, pData->Value ) )
223 return;
226 else
228 if( pData->Namespace.getLength() == 0L )
230 if( mpContainer->SetAt( nAttr, aName, pData->Value ) )
231 return;
236 throw lang::IllegalArgumentException();
239 // container::XNameContainer
240 void SAL_CALL SvUnoAttributeContainer::insertByName(const OUString& aName, const uno::Any& aElement)
241 throw( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException )
243 if( !aElement.hasValue() || aElement.getValueType() != ::getCppuType((const xml::AttributeData*)0) )
244 throw lang::IllegalArgumentException();
246 sal_uInt16 nAttr = getIndexByName(aName );
247 if( nAttr != USHRT_MAX )
248 throw container::ElementExistException();
250 xml::AttributeData* pData = (xml::AttributeData*)aElement.getValue();
252 sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
253 if( nPos != -1L )
255 const OUString aPrefix( aName.copy( 0L, nPos ));
256 const OUString aLName( aName.copy( nPos+1L ));
258 if( pData->Namespace.getLength() == 0L )
260 if( mpContainer->AddAttr( aPrefix, aLName, pData->Value ) )
261 return;
263 else
265 if( mpContainer->AddAttr( aPrefix, pData->Namespace, aLName, pData->Value ) )
266 return;
269 else
271 if( pData->Namespace.getLength() == 0L )
273 if( mpContainer->AddAttr( aName, pData->Value ) )
274 return;
279 void SAL_CALL SvUnoAttributeContainer::removeByName(const OUString& Name)
280 throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
282 sal_uInt16 nAttr = getIndexByName(Name);
283 if( nAttr == USHRT_MAX )
284 throw container::NoSuchElementException();
286 mpContainer->Remove( nAttr );
289 //XServiceInfo
290 OUString SAL_CALL SvUnoAttributeContainer::getImplementationName(void) throw( uno::RuntimeException )
292 return OUString::createFromAscii( "SvUnoAttributeContainer" );
295 uno::Sequence< OUString > SvUnoAttributeContainer::getSupportedServiceNames(void)
296 throw( uno::RuntimeException )
298 OUString aSN( OUString::createFromAscii( "com.sun.star.xml.AttributeContainer" ) );
299 uno::Sequence< OUString > aNS( &aSN, 1L );
300 return aNS;
303 sal_Bool SvUnoAttributeContainer::supportsService(const OUString& ServiceName)
304 throw( uno::RuntimeException )
306 const uno::Sequence < OUString > aServiceNames( getSupportedServiceNames() );
307 const OUString* pNames = aServiceNames.getConstArray();
308 sal_Int32 nCount = aServiceNames.getLength();
309 while( nCount-- )
311 if( *pNames++ == ServiceName )
312 return sal_True;
315 return sal_False;