1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/attributelist.hxx>
31 #include <vos/diagnose.hxx>
32 
33 #include <vector>
34 
35 using namespace rtl;
36 using namespace osl;
37 using namespace com::sun::star;
38 
39 namespace comphelper {
40 
41 struct TagAttribute_Impl
42 {
43 	TagAttribute_Impl(){}
44 	TagAttribute_Impl( const OUString &aName, const OUString &aType,
45 						 const OUString &aValue )
46 	{
47 		this->sName 	= aName;
48 		this->sType 	= aType;
49 		this->sValue 	= aValue;
50 	}
51 
52 	OUString sName;
53 	OUString sType;
54 	OUString sValue;
55 };
56 
57 struct AttributeList_Impl
58 {
59 	AttributeList_Impl()
60 	{
61 		// performance improvement during adding
62 		vecAttribute.reserve(20);
63 	}
64 	::std::vector<struct TagAttribute_Impl> vecAttribute;
65 };
66 
67 sal_Int16 SAL_CALL AttributeList::getLength(void) throw( ::com::sun::star::uno::RuntimeException )
68 {
69 	return (sal_Int16)(m_pImpl->vecAttribute.size());
70 }
71 
72 OUString SAL_CALL AttributeList::getNameByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
73 {
74     return ( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size()) ) ? m_pImpl->vecAttribute[i].sName : OUString();
75 }
76 
77 OUString SAL_CALL AttributeList::getTypeByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
78 {
79 	if( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size() ) ) {
80 		return m_pImpl->vecAttribute[i].sType;
81 	}
82 	return OUString();
83 }
84 
85 OUString SAL_CALL  AttributeList::getValueByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
86 {
87     return ( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size() ) ) ? m_pImpl->vecAttribute[i].sValue : OUString();
88 }
89 
90 OUString SAL_CALL AttributeList::getTypeByName( const OUString& sName ) throw( ::com::sun::star::uno::RuntimeException )
91 {
92 	::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
93 
94 	for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
95 		if( (*ii).sName == sName ) {
96 			return (*ii).sType;
97 		}
98 	}
99 	return OUString();
100 }
101 
102 OUString SAL_CALL AttributeList::getValueByName(const OUString& sName) throw( ::com::sun::star::uno::RuntimeException )
103 {
104 	::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
105 
106 	for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
107 		if( (*ii).sName == sName ) {
108 			return (*ii).sValue;
109 		}
110 	}
111 	return OUString();
112 }
113 
114 
115 AttributeList::AttributeList()
116 {
117 	m_pImpl = new AttributeList_Impl;
118 }
119 
120 
121 
122 AttributeList::~AttributeList()
123 {
124 	delete m_pImpl;
125 }
126 
127 void AttributeList::AddAttribute( 	const OUString &sName ,
128 										const OUString &sType ,
129 										const OUString &sValue )
130 {
131 	m_pImpl->vecAttribute.push_back( TagAttribute_Impl( sName , sType , sValue ) );
132 }
133 
134 void AttributeList::Clear()
135 {
136 	m_pImpl->vecAttribute.clear();
137 
138 	VOS_ENSURE( ! getLength(), "Length > 0 after AttributeList::Clear!");
139 }
140 
141 void AttributeList::RemoveAttribute( const OUString sName )
142 {
143 	::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
144 
145 	for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
146 		if( (*ii).sName == sName ) {
147 			m_pImpl->vecAttribute.erase( ii );
148 			break;
149 		}
150 	}
151 }
152 
153 
154 void AttributeList::SetAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList >  &r )
155 {
156 	Clear();
157 	AppendAttributeList( r );
158 }
159 
160 void AttributeList::AppendAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList >  &r )
161 {
162 	VOS_ENSURE( r.is(), "r isn't!" );
163 
164 	sal_Int32 nMax = r->getLength();
165 	sal_Int32 nTotalSize = m_pImpl->vecAttribute.size() + nMax;
166 	m_pImpl->vecAttribute.reserve( nTotalSize );
167 
168 	for( sal_Int16 i = 0 ; i < nMax ; i ++ ) {
169 		m_pImpl->vecAttribute.push_back( TagAttribute_Impl(
170 			r->getNameByIndex( i ) ,
171 			r->getTypeByIndex( i ) ,
172 			r->getValueByIndex( i )));
173 	}
174 
175 	VOS_ENSURE( nTotalSize == getLength(), "nTotalSize != getLength()");
176 }
177 
178 } // namespace comphelper
179 
180