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: attrlist.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_xmloff.hxx"
35 #include <osl/mutex.hxx>
36 #include <xmloff/xmltoken.hxx>
38 #include <rtl/memory.h>
40 #if OSL_DEBUG_LEVEL == 0
47 #include <xmloff/attrlist.hxx>
49 using ::rtl::OUString
;
51 using namespace ::osl
;
52 using namespace ::com::sun::star
;
53 using namespace ::xmloff::token
;
55 struct SvXMLTagAttribute_Impl
57 SvXMLTagAttribute_Impl(){}
58 SvXMLTagAttribute_Impl( const OUString
&rName
,
59 const OUString
&rValue
)
65 SvXMLTagAttribute_Impl( const SvXMLTagAttribute_Impl
& r
) :
75 struct SvXMLAttributeList_Impl
77 SvXMLAttributeList_Impl()
79 // performance improvement during adding
80 vecAttribute
.reserve(20);
83 SvXMLAttributeList_Impl( const SvXMLAttributeList_Impl
& r
) :
84 vecAttribute( r
.vecAttribute
)
88 ::std::vector
<struct SvXMLTagAttribute_Impl
> vecAttribute
;
89 typedef ::std::vector
<struct SvXMLTagAttribute_Impl
>::size_type size_type
;
94 sal_Int16 SAL_CALL
SvXMLAttributeList::getLength(void) throw( ::com::sun::star::uno::RuntimeException
)
96 return sal::static_int_cast
< sal_Int16
>(m_pImpl
->vecAttribute
.size());
100 SvXMLAttributeList::SvXMLAttributeList( const SvXMLAttributeList
&r
) :
101 cppu::WeakImplHelper3
<com::sun::star::xml::sax::XAttributeList
, com::sun::star::util::XCloneable
, com::sun::star::lang::XUnoTunnel
>(r
),
102 m_pImpl( new SvXMLAttributeList_Impl( *r
.m_pImpl
) )
106 SvXMLAttributeList::SvXMLAttributeList( const uno::Reference
<
107 xml::sax::XAttributeList
> & rAttrList
)
108 : sType( GetXMLToken(XML_CDATA
) )
110 m_pImpl
= new SvXMLAttributeList_Impl
;
112 SvXMLAttributeList
* pImpl
=
113 SvXMLAttributeList::getImplementation( rAttrList
);
116 *m_pImpl
= *(pImpl
->m_pImpl
);
118 AppendAttributeList( rAttrList
);
121 OUString SAL_CALL
SvXMLAttributeList::getNameByIndex(sal_Int16 i
) throw( ::com::sun::star::uno::RuntimeException
)
123 return ( static_cast< SvXMLAttributeList_Impl::size_type
>( i
) < m_pImpl
->vecAttribute
.size() ) ? m_pImpl
->vecAttribute
[i
].sName
: OUString();
127 OUString SAL_CALL
SvXMLAttributeList::getTypeByIndex(sal_Int16
) throw( ::com::sun::star::uno::RuntimeException
)
132 OUString SAL_CALL
SvXMLAttributeList::getValueByIndex(sal_Int16 i
) throw( ::com::sun::star::uno::RuntimeException
)
134 return ( static_cast< SvXMLAttributeList_Impl::size_type
>( i
) < m_pImpl
->vecAttribute
.size() ) ? m_pImpl
->vecAttribute
[i
].sValue
: OUString();
137 OUString SAL_CALL
SvXMLAttributeList::getTypeByName( const OUString
& ) throw( ::com::sun::star::uno::RuntimeException
)
142 OUString SAL_CALL
SvXMLAttributeList::getValueByName(const OUString
& sName
) throw( ::com::sun::star::uno::RuntimeException
)
144 ::std::vector
<struct SvXMLTagAttribute_Impl
>::iterator ii
= m_pImpl
->vecAttribute
.begin();
146 for( ; ii
!= m_pImpl
->vecAttribute
.end() ; ++ii
) {
147 if( (*ii
).sName
== sName
) {
155 uno::Reference
< ::com::sun::star::util::XCloneable
> SvXMLAttributeList::createClone() throw( ::com::sun::star::uno::RuntimeException
)
157 uno::Reference
< ::com::sun::star::util::XCloneable
> r
= new SvXMLAttributeList( *this );
162 SvXMLAttributeList::SvXMLAttributeList()
163 : sType( GetXMLToken(XML_CDATA
) )
165 m_pImpl
= new SvXMLAttributeList_Impl
;
170 SvXMLAttributeList::~SvXMLAttributeList()
176 void SvXMLAttributeList::AddAttribute( const OUString
&sName
,
177 const OUString
&sValue
)
179 m_pImpl
->vecAttribute
.push_back( SvXMLTagAttribute_Impl( sName
, sValue
) );
182 void SvXMLAttributeList::Clear()
184 m_pImpl
->vecAttribute
.clear();
186 assert( ! getLength() );
189 void SvXMLAttributeList::RemoveAttribute( const OUString sName
)
191 ::std::vector
<struct SvXMLTagAttribute_Impl
>::iterator ii
= m_pImpl
->vecAttribute
.begin();
193 for( ; ii
!= m_pImpl
->vecAttribute
.end() ; ++ii
) {
194 if( (*ii
).sName
== sName
) {
195 m_pImpl
->vecAttribute
.erase( ii
);
202 void SvXMLAttributeList::SetAttributeList( const uno::Reference
< ::com::sun::star::xml::sax::XAttributeList
> &r
)
205 AppendAttributeList( r
);
208 void SvXMLAttributeList::AppendAttributeList( const uno::Reference
< ::com::sun::star::xml::sax::XAttributeList
> &r
)
212 sal_Int16 nMax
= r
->getLength();
213 SvXMLAttributeList_Impl::size_type nTotalSize
=
214 m_pImpl
->vecAttribute
.size() + nMax
;
215 m_pImpl
->vecAttribute
.reserve( nTotalSize
);
217 for( sal_Int16 i
= 0 ; i
< nMax
; ++i
) {
218 m_pImpl
->vecAttribute
.push_back( SvXMLTagAttribute_Impl(
219 r
->getNameByIndex( i
) ,
220 r
->getValueByIndex( i
)));
223 assert( nTotalSize
== (SvXMLAttributeList_Impl::size_type
)getLength());
226 void SvXMLAttributeList::SetValueByIndex( sal_Int16 i
,
227 const ::rtl::OUString
& rValue
)
229 if( static_cast< SvXMLAttributeList_Impl::size_type
>( i
)
230 < m_pImpl
->vecAttribute
.size() )
232 m_pImpl
->vecAttribute
[i
].sValue
= rValue
;
236 void SvXMLAttributeList::RemoveAttributeByIndex( sal_Int16 i
)
238 if( static_cast< SvXMLAttributeList_Impl::size_type
>( i
)
239 < m_pImpl
->vecAttribute
.size() )
240 m_pImpl
->vecAttribute
.erase( m_pImpl
->vecAttribute
.begin() + i
);
243 void SvXMLAttributeList::RenameAttributeByIndex( sal_Int16 i
,
244 const OUString
& rNewName
)
246 if( static_cast< SvXMLAttributeList_Impl::size_type
>( i
)
247 < m_pImpl
->vecAttribute
.size() )
249 m_pImpl
->vecAttribute
[i
].sName
= rNewName
;
253 sal_Int16
SvXMLAttributeList::GetIndexByName( const OUString
& rName
) const
255 ::std::vector
<struct SvXMLTagAttribute_Impl
>::iterator ii
=
256 m_pImpl
->vecAttribute
.begin();
258 for( sal_Int16 nIndex
=0; ii
!=m_pImpl
->vecAttribute
.end(); ++ii
, ++nIndex
)
260 if( (*ii
).sName
== rName
)
269 const uno::Sequence
< sal_Int8
> & SvXMLAttributeList::getUnoTunnelId() throw()
271 static uno::Sequence
< sal_Int8
> * pSeq
= 0;
274 Guard
< Mutex
> aGuard( Mutex::getGlobalMutex() );
277 static uno::Sequence
< sal_Int8
> aSeq( 16 );
278 rtl_createUuid( (sal_uInt8
*)aSeq
.getArray(), 0, sal_True
);
285 SvXMLAttributeList
* SvXMLAttributeList::getImplementation( uno::Reference
< uno::XInterface
> xInt
) throw()
287 uno::Reference
< lang::XUnoTunnel
> xUT( xInt
, uno::UNO_QUERY
);
291 reinterpret_cast<SvXMLAttributeList
*>(
292 sal::static_int_cast
<sal_IntPtr
>(
293 xUT
->getSomething( SvXMLAttributeList::getUnoTunnelId())));
300 sal_Int64 SAL_CALL
SvXMLAttributeList::getSomething( const uno::Sequence
< sal_Int8
>& rId
)
301 throw( uno::RuntimeException
)
303 if( rId
.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
304 rId
.getConstArray(), 16 ) )
306 return sal::static_int_cast
<sal_Int64
>(reinterpret_cast<sal_uIntPtr
>(this));