Bump version to 6.4.0.12
[LibreOffice.git] / xmloff / source / core / attrlist.cxx
blob5db3e8c27b183ec441dee910a5e54b909ed783b6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <vector>
22 #include <osl/diagnose.h>
23 #include <xmloff/xmltoken.hxx>
24 #include <cppuhelper/implbase.hxx>
26 #include <xmloff/attrlist.hxx>
29 using namespace ::com::sun::star;
30 using namespace ::xmloff::token;
32 struct SvXMLTagAttribute_Impl
34 SvXMLTagAttribute_Impl( const OUString &rName,
35 const OUString &rValue )
36 : sName(rName),
37 sValue(rValue)
41 OUString sName;
42 OUString sValue;
45 struct SvXMLAttributeList_Impl
47 SvXMLAttributeList_Impl()
49 // performance improvement during adding
50 vecAttribute.reserve(20);
53 ::std::vector<struct SvXMLTagAttribute_Impl> vecAttribute;
54 typedef ::std::vector<struct SvXMLTagAttribute_Impl>::size_type size_type;
58 sal_Int16 SAL_CALL SvXMLAttributeList::getLength()
60 return sal::static_int_cast< sal_Int16 >(m_pImpl->vecAttribute.size());
64 SvXMLAttributeList::SvXMLAttributeList( const SvXMLAttributeList &r ) :
65 cppu::WeakImplHelper<css::xml::sax::XAttributeList, css::util::XCloneable, css::lang::XUnoTunnel>(r),
66 m_pImpl( new SvXMLAttributeList_Impl( *r.m_pImpl ) )
70 SvXMLAttributeList::SvXMLAttributeList( const uno::Reference<
71 xml::sax::XAttributeList> & rAttrList )
72 : m_pImpl( new SvXMLAttributeList_Impl),
73 sType( GetXMLToken(XML_CDATA) )
76 SvXMLAttributeList* pImpl =
77 comphelper::getUnoTunnelImplementation<SvXMLAttributeList>( rAttrList );
79 if( pImpl )
80 *m_pImpl = *(pImpl->m_pImpl);
81 else
82 AppendAttributeList( rAttrList );
85 OUString SAL_CALL SvXMLAttributeList::getNameByIndex(sal_Int16 i)
87 return ( static_cast< SvXMLAttributeList_Impl::size_type >( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sName : OUString();
91 OUString SAL_CALL SvXMLAttributeList::getTypeByIndex(sal_Int16)
93 return sType;
96 OUString SAL_CALL SvXMLAttributeList::getValueByIndex(sal_Int16 i)
98 return ( static_cast< SvXMLAttributeList_Impl::size_type >( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sValue : OUString();
101 OUString SAL_CALL SvXMLAttributeList::getTypeByName( const OUString& )
103 return sType;
106 OUString SAL_CALL SvXMLAttributeList::getValueByName(const OUString& sName)
108 auto ii = std::find_if(m_pImpl->vecAttribute.begin(), m_pImpl->vecAttribute.end(),
109 [&sName](struct SvXMLTagAttribute_Impl& rAttr) { return rAttr.sName == sName; });
111 if (ii != m_pImpl->vecAttribute.end())
112 return (*ii).sValue;
114 return OUString();
118 uno::Reference< css::util::XCloneable > SvXMLAttributeList::createClone()
120 uno::Reference< css::util::XCloneable > r = new SvXMLAttributeList( *this );
121 return r;
125 SvXMLAttributeList::SvXMLAttributeList()
126 : m_pImpl( new SvXMLAttributeList_Impl ),
127 sType( GetXMLToken(XML_CDATA) )
132 SvXMLAttributeList::~SvXMLAttributeList()
137 void SvXMLAttributeList::AddAttribute( const OUString &sName ,
138 const OUString &sValue )
140 m_pImpl->vecAttribute.emplace_back( sName , sValue );
143 void SvXMLAttributeList::Clear()
145 m_pImpl->vecAttribute.clear();
147 OSL_ASSERT( ! getLength() );
150 void SvXMLAttributeList::RemoveAttribute( const OUString& sName )
152 auto ii = std::find_if(m_pImpl->vecAttribute.begin(), m_pImpl->vecAttribute.end(),
153 [&sName](struct SvXMLTagAttribute_Impl& rAttr) { return rAttr.sName == sName; });
155 if (ii != m_pImpl->vecAttribute.end())
156 m_pImpl->vecAttribute.erase( ii );
159 void SvXMLAttributeList::AppendAttributeList( const uno::Reference< css::xml::sax::XAttributeList > &r )
161 OSL_ASSERT( r.is() );
163 sal_Int16 nMax = r->getLength();
164 SvXMLAttributeList_Impl::size_type nTotalSize =
165 m_pImpl->vecAttribute.size() + nMax;
166 m_pImpl->vecAttribute.reserve( nTotalSize );
168 for( sal_Int16 i = 0 ; i < nMax ; ++i ) {
169 m_pImpl->vecAttribute.emplace_back(
170 r->getNameByIndex( i ) ,
171 r->getValueByIndex( i ));
174 OSL_ASSERT( nTotalSize == static_cast<SvXMLAttributeList_Impl::size_type>(getLength()));
177 void SvXMLAttributeList::SetValueByIndex( sal_Int16 i,
178 const OUString& rValue )
180 if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
181 < m_pImpl->vecAttribute.size() )
183 m_pImpl->vecAttribute[i].sValue = rValue;
187 void SvXMLAttributeList::RemoveAttributeByIndex( sal_Int16 i )
189 if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
190 < m_pImpl->vecAttribute.size() )
191 m_pImpl->vecAttribute.erase( m_pImpl->vecAttribute.begin() + i );
194 void SvXMLAttributeList::RenameAttributeByIndex( sal_Int16 i,
195 const OUString& rNewName )
197 if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
198 < m_pImpl->vecAttribute.size() )
200 m_pImpl->vecAttribute[i].sName = rNewName;
204 sal_Int16 SvXMLAttributeList::GetIndexByName( const OUString& rName ) const
206 auto ii = std::find_if(m_pImpl->vecAttribute.begin(), m_pImpl->vecAttribute.end(),
207 [&rName](struct SvXMLTagAttribute_Impl& rAttr) { return rAttr.sName == rName; });
209 if (ii != m_pImpl->vecAttribute.end())
210 return static_cast<sal_Int16>(std::distance(m_pImpl->vecAttribute.begin(), ii));
212 return -1;
215 // XUnoTunnel & co
216 UNO3_GETIMPLEMENTATION_IMPL(SvXMLAttributeList)
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */