1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*dde7d3faSAndrew Rist  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19*dde7d3faSAndrew Rist  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <comphelper/attributelist.hxx>
27cdf0e10cSrcweir #include <vos/diagnose.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <vector>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace rtl;
32cdf0e10cSrcweir using namespace osl;
33cdf0e10cSrcweir using namespace com::sun::star;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace comphelper {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir struct TagAttribute_Impl
38cdf0e10cSrcweir {
TagAttribute_Implcomphelper::TagAttribute_Impl39cdf0e10cSrcweir 	TagAttribute_Impl(){}
TagAttribute_Implcomphelper::TagAttribute_Impl40cdf0e10cSrcweir 	TagAttribute_Impl( const OUString &aName, const OUString &aType,
41cdf0e10cSrcweir 						 const OUString &aValue )
42cdf0e10cSrcweir 	{
43cdf0e10cSrcweir 		this->sName 	= aName;
44cdf0e10cSrcweir 		this->sType 	= aType;
45cdf0e10cSrcweir 		this->sValue 	= aValue;
46cdf0e10cSrcweir 	}
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 	OUString sName;
49cdf0e10cSrcweir 	OUString sType;
50cdf0e10cSrcweir 	OUString sValue;
51cdf0e10cSrcweir };
52cdf0e10cSrcweir 
53cdf0e10cSrcweir struct AttributeList_Impl
54cdf0e10cSrcweir {
AttributeList_Implcomphelper::AttributeList_Impl55cdf0e10cSrcweir 	AttributeList_Impl()
56cdf0e10cSrcweir 	{
57cdf0e10cSrcweir 		// performance improvement during adding
58cdf0e10cSrcweir 		vecAttribute.reserve(20);
59cdf0e10cSrcweir 	}
60cdf0e10cSrcweir 	::std::vector<struct TagAttribute_Impl> vecAttribute;
61cdf0e10cSrcweir };
62cdf0e10cSrcweir 
getLength(void)63cdf0e10cSrcweir sal_Int16 SAL_CALL AttributeList::getLength(void) throw( ::com::sun::star::uno::RuntimeException )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir 	return (sal_Int16)(m_pImpl->vecAttribute.size());
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
getNameByIndex(sal_Int16 i)68cdf0e10cSrcweir OUString SAL_CALL AttributeList::getNameByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     return ( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size()) ) ? m_pImpl->vecAttribute[i].sName : OUString();
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
getTypeByIndex(sal_Int16 i)73cdf0e10cSrcweir OUString SAL_CALL AttributeList::getTypeByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir 	if( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size() ) ) {
76cdf0e10cSrcweir 		return m_pImpl->vecAttribute[i].sType;
77cdf0e10cSrcweir 	}
78cdf0e10cSrcweir 	return OUString();
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
getValueByIndex(sal_Int16 i)81cdf0e10cSrcweir OUString SAL_CALL  AttributeList::getValueByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     return ( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size() ) ) ? m_pImpl->vecAttribute[i].sValue : OUString();
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
getTypeByName(const OUString & sName)86cdf0e10cSrcweir OUString SAL_CALL AttributeList::getTypeByName( const OUString& sName ) throw( ::com::sun::star::uno::RuntimeException )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
91cdf0e10cSrcweir 		if( (*ii).sName == sName ) {
92cdf0e10cSrcweir 			return (*ii).sType;
93cdf0e10cSrcweir 		}
94cdf0e10cSrcweir 	}
95cdf0e10cSrcweir 	return OUString();
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
getValueByName(const OUString & sName)98cdf0e10cSrcweir OUString SAL_CALL AttributeList::getValueByName(const OUString& sName) throw( ::com::sun::star::uno::RuntimeException )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
103cdf0e10cSrcweir 		if( (*ii).sName == sName ) {
104cdf0e10cSrcweir 			return (*ii).sValue;
105cdf0e10cSrcweir 		}
106cdf0e10cSrcweir 	}
107cdf0e10cSrcweir 	return OUString();
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 
AttributeList()111cdf0e10cSrcweir AttributeList::AttributeList()
112cdf0e10cSrcweir {
113cdf0e10cSrcweir 	m_pImpl = new AttributeList_Impl;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 
~AttributeList()118cdf0e10cSrcweir AttributeList::~AttributeList()
119cdf0e10cSrcweir {
120cdf0e10cSrcweir 	delete m_pImpl;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
AddAttribute(const OUString & sName,const OUString & sType,const OUString & sValue)123cdf0e10cSrcweir void AttributeList::AddAttribute( 	const OUString &sName ,
124cdf0e10cSrcweir 										const OUString &sType ,
125cdf0e10cSrcweir 										const OUString &sValue )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	m_pImpl->vecAttribute.push_back( TagAttribute_Impl( sName , sType , sValue ) );
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
Clear()130cdf0e10cSrcweir void AttributeList::Clear()
131cdf0e10cSrcweir {
132cdf0e10cSrcweir 	m_pImpl->vecAttribute.clear();
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	VOS_ENSURE( ! getLength(), "Length > 0 after AttributeList::Clear!");
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
RemoveAttribute(const OUString sName)137cdf0e10cSrcweir void AttributeList::RemoveAttribute( const OUString sName )
138cdf0e10cSrcweir {
139cdf0e10cSrcweir 	::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
142cdf0e10cSrcweir 		if( (*ii).sName == sName ) {
143cdf0e10cSrcweir 			m_pImpl->vecAttribute.erase( ii );
144cdf0e10cSrcweir 			break;
145cdf0e10cSrcweir 		}
146cdf0e10cSrcweir 	}
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 
SetAttributeList(const uno::Reference<::com::sun::star::xml::sax::XAttributeList> & r)150cdf0e10cSrcweir void AttributeList::SetAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList >  &r )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir 	Clear();
153cdf0e10cSrcweir 	AppendAttributeList( r );
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
AppendAttributeList(const uno::Reference<::com::sun::star::xml::sax::XAttributeList> & r)156cdf0e10cSrcweir void AttributeList::AppendAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList >  &r )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir 	VOS_ENSURE( r.is(), "r isn't!" );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	sal_Int32 nMax = r->getLength();
161cdf0e10cSrcweir 	sal_Int32 nTotalSize = m_pImpl->vecAttribute.size() + nMax;
162cdf0e10cSrcweir 	m_pImpl->vecAttribute.reserve( nTotalSize );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	for( sal_Int16 i = 0 ; i < nMax ; i ++ ) {
165cdf0e10cSrcweir 		m_pImpl->vecAttribute.push_back( TagAttribute_Impl(
166cdf0e10cSrcweir 			r->getNameByIndex( i ) ,
167cdf0e10cSrcweir 			r->getTypeByIndex( i ) ,
168cdf0e10cSrcweir 			r->getValueByIndex( i )));
169cdf0e10cSrcweir 	}
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	VOS_ENSURE( nTotalSize == getLength(), "nTotalSize != getLength()");
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir } // namespace comphelper
175cdf0e10cSrcweir 
176