Update to m13
[ooovba.git] / comphelper / source / xml / attributelist.cxx
blob2d789bc57a7dc88a2be0f324497336869f61f46b
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: attributelist.cxx,v $
10 * $Revision: 1.4 $
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>
36 #include <vector>
38 using namespace rtl;
39 using namespace osl;
40 using namespace com::sun::star;
42 namespace comphelper {
44 struct TagAttribute_Impl
46 TagAttribute_Impl(){}
47 TagAttribute_Impl( const OUString &aName, const OUString &aType,
48 const OUString &aValue )
50 this->sName = aName;
51 this->sType = aType;
52 this->sValue = aValue;
55 OUString sName;
56 OUString sType;
57 OUString sValue;
60 struct AttributeList_Impl
62 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 if( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size()) ) {
78 return m_pImpl->vecAttribute[i].sName;
80 return OUString();
83 OUString SAL_CALL AttributeList::getTypeByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
85 if( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size() ) ) {
86 return m_pImpl->vecAttribute[i].sType;
88 return OUString();
91 OUString SAL_CALL AttributeList::getValueByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
93 if( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size() ) ) {
94 return m_pImpl->vecAttribute[i].sValue;
96 return OUString();
100 OUString SAL_CALL AttributeList::getTypeByName( const OUString& sName ) throw( ::com::sun::star::uno::RuntimeException )
102 ::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
104 for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
105 if( (*ii).sName == sName ) {
106 return (*ii).sType;
109 return OUString();
112 OUString SAL_CALL AttributeList::getValueByName(const OUString& sName) throw( ::com::sun::star::uno::RuntimeException )
114 ::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
116 for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
117 if( (*ii).sName == sName ) {
118 return (*ii).sValue;
121 return OUString();
125 AttributeList::AttributeList()
127 m_pImpl = new AttributeList_Impl;
132 AttributeList::~AttributeList()
134 delete m_pImpl;
137 void AttributeList::AddAttribute( const OUString &sName ,
138 const OUString &sType ,
139 const OUString &sValue )
141 m_pImpl->vecAttribute.push_back( TagAttribute_Impl( sName , sType , sValue ) );
144 void AttributeList::Clear()
146 m_pImpl->vecAttribute.clear();
148 VOS_ENSURE( ! getLength(), "Length > 0 after AttributeList::Clear!");
151 void AttributeList::RemoveAttribute( const OUString sName )
153 ::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
155 for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
156 if( (*ii).sName == sName ) {
157 m_pImpl->vecAttribute.erase( ii );
158 break;
164 void AttributeList::SetAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList > &r )
166 Clear();
167 AppendAttributeList( r );
170 void AttributeList::AppendAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList > &r )
172 VOS_ENSURE( r.is(), "r isn't!" );
174 sal_Int32 nMax = r->getLength();
175 sal_Int32 nTotalSize = m_pImpl->vecAttribute.size() + nMax;
176 m_pImpl->vecAttribute.reserve( nTotalSize );
178 for( sal_Int16 i = 0 ; i < nMax ; i ++ ) {
179 m_pImpl->vecAttribute.push_back( TagAttribute_Impl(
180 r->getNameByIndex( i ) ,
181 r->getTypeByIndex( i ) ,
182 r->getValueByIndex( i )));
185 VOS_ENSURE( nTotalSize == getLength(), "nTotalSize != getLength()");
188 } // namespace comphelper