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: attributelist.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_comphelper.hxx"
33 #include <comphelper/attributelist.hxx>
34 #include <vos/diagnose.hxx>
40 using namespace com::sun::star
;
42 namespace comphelper
{
44 struct TagAttribute_Impl
47 TagAttribute_Impl( const OUString
&aName
, const OUString
&aType
,
48 const OUString
&aValue
)
52 this->sValue
= aValue
;
60 struct AttributeList_Impl
64 // performance improvement during adding
65 vecAttribute
.reserve(20);
67 ::std::vector
<struct TagAttribute_Impl
> vecAttribute
;
70 sal_Int16 SAL_CALL
AttributeList::getLength(void) throw( ::com::sun::star::uno::RuntimeException
)
72 return (sal_Int16
)(m_pImpl
->vecAttribute
.size());
75 OUString SAL_CALL
AttributeList::getNameByIndex(sal_Int16 i
) throw( ::com::sun::star::uno::RuntimeException
)
77 return ( i
< static_cast < sal_Int16
> (m_pImpl
->vecAttribute
.size()) ) ? m_pImpl
->vecAttribute
[i
].sName
: OUString();
80 OUString SAL_CALL
AttributeList::getTypeByIndex(sal_Int16 i
) throw( ::com::sun::star::uno::RuntimeException
)
82 if( i
< static_cast < sal_Int16
> (m_pImpl
->vecAttribute
.size() ) ) {
83 return m_pImpl
->vecAttribute
[i
].sType
;
88 OUString SAL_CALL
AttributeList::getValueByIndex(sal_Int16 i
) throw( ::com::sun::star::uno::RuntimeException
)
90 return ( i
< static_cast < sal_Int16
> (m_pImpl
->vecAttribute
.size() ) ) ? m_pImpl
->vecAttribute
[i
].sValue
: OUString();
93 OUString SAL_CALL
AttributeList::getTypeByName( const OUString
& sName
) throw( ::com::sun::star::uno::RuntimeException
)
95 ::std::vector
<struct TagAttribute_Impl
>::iterator ii
= m_pImpl
->vecAttribute
.begin();
97 for( ; ii
!= m_pImpl
->vecAttribute
.end() ; ii
++ ) {
98 if( (*ii
).sName
== sName
) {
105 OUString SAL_CALL
AttributeList::getValueByName(const OUString
& sName
) throw( ::com::sun::star::uno::RuntimeException
)
107 ::std::vector
<struct TagAttribute_Impl
>::iterator ii
= m_pImpl
->vecAttribute
.begin();
109 for( ; ii
!= m_pImpl
->vecAttribute
.end() ; ii
++ ) {
110 if( (*ii
).sName
== sName
) {
118 AttributeList::AttributeList()
120 m_pImpl
= new AttributeList_Impl
;
125 AttributeList::~AttributeList()
130 void AttributeList::AddAttribute( const OUString
&sName
,
131 const OUString
&sType
,
132 const OUString
&sValue
)
134 m_pImpl
->vecAttribute
.push_back( TagAttribute_Impl( sName
, sType
, sValue
) );
137 void AttributeList::Clear()
139 m_pImpl
->vecAttribute
.clear();
141 VOS_ENSURE( ! getLength(), "Length > 0 after AttributeList::Clear!");
144 void AttributeList::RemoveAttribute( const OUString sName
)
146 ::std::vector
<struct TagAttribute_Impl
>::iterator ii
= m_pImpl
->vecAttribute
.begin();
148 for( ; ii
!= m_pImpl
->vecAttribute
.end() ; ii
++ ) {
149 if( (*ii
).sName
== sName
) {
150 m_pImpl
->vecAttribute
.erase( ii
);
157 void AttributeList::SetAttributeList( const uno::Reference
< ::com::sun::star::xml::sax::XAttributeList
> &r
)
160 AppendAttributeList( r
);
163 void AttributeList::AppendAttributeList( const uno::Reference
< ::com::sun::star::xml::sax::XAttributeList
> &r
)
165 VOS_ENSURE( r
.is(), "r isn't!" );
167 sal_Int32 nMax
= r
->getLength();
168 sal_Int32 nTotalSize
= m_pImpl
->vecAttribute
.size() + nMax
;
169 m_pImpl
->vecAttribute
.reserve( nTotalSize
);
171 for( sal_Int16 i
= 0 ; i
< nMax
; i
++ ) {
172 m_pImpl
->vecAttribute
.push_back( TagAttribute_Impl(
173 r
->getNameByIndex( i
) ,
174 r
->getTypeByIndex( i
) ,
175 r
->getValueByIndex( i
)));
178 VOS_ENSURE( nTotalSize
== getLength(), "nTotalSize != getLength()");
181 } // namespace comphelper