Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / xmloff / source / core / attrlist.cxx
blobf01dc416da5059908673c29fbae325b391391486
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 <string.h>
22 #include <vector>
23 #include <osl/mutex.hxx>
24 #include <osl/diagnose.h>
25 #include <xmloff/xmltoken.hxx>
26 #include <comphelper/servicehelper.hxx>
28 #include <xmloff/attrlist.hxx>
31 using namespace ::osl;
32 using namespace ::com::sun::star;
33 using namespace ::xmloff::token;
35 struct SvXMLTagAttribute_Impl
37 SvXMLTagAttribute_Impl( const OUString &rName,
38 const OUString &rValue )
39 : sName(rName),
40 sValue(rValue)
44 SvXMLTagAttribute_Impl( const SvXMLTagAttribute_Impl& r ) :
45 sName(r.sName),
46 sValue(r.sValue)
50 OUString sName;
51 OUString sValue;
54 struct SvXMLAttributeList_Impl
56 SvXMLAttributeList_Impl()
58 // performance improvement during adding
59 vecAttribute.reserve(20);
62 SvXMLAttributeList_Impl( const SvXMLAttributeList_Impl& r ) :
63 vecAttribute( r.vecAttribute )
67 ::std::vector<struct SvXMLTagAttribute_Impl> vecAttribute;
68 typedef ::std::vector<struct SvXMLTagAttribute_Impl>::size_type size_type;
72 sal_Int16 SAL_CALL SvXMLAttributeList::getLength() throw( css::uno::RuntimeException, std::exception )
74 return sal::static_int_cast< sal_Int16 >(m_pImpl->vecAttribute.size());
78 SvXMLAttributeList::SvXMLAttributeList( const SvXMLAttributeList &r ) :
79 cppu::WeakImplHelper3<css::xml::sax::XAttributeList, css::util::XCloneable, css::lang::XUnoTunnel>(r),
80 m_pImpl( new SvXMLAttributeList_Impl( *r.m_pImpl ) )
84 SvXMLAttributeList::SvXMLAttributeList( const uno::Reference<
85 xml::sax::XAttributeList> & rAttrList )
86 : m_pImpl( new SvXMLAttributeList_Impl),
87 sType( GetXMLToken(XML_CDATA) )
90 SvXMLAttributeList* pImpl =
91 SvXMLAttributeList::getImplementation( rAttrList );
93 if( pImpl )
94 *m_pImpl = *(pImpl->m_pImpl);
95 else
96 AppendAttributeList( rAttrList );
99 OUString SAL_CALL SvXMLAttributeList::getNameByIndex(sal_Int16 i) throw( css::uno::RuntimeException, std::exception )
101 return ( static_cast< SvXMLAttributeList_Impl::size_type >( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sName : OUString();
105 OUString SAL_CALL SvXMLAttributeList::getTypeByIndex(sal_Int16) throw( css::uno::RuntimeException, std::exception )
107 return sType;
110 OUString SAL_CALL SvXMLAttributeList::getValueByIndex(sal_Int16 i) throw( css::uno::RuntimeException, std::exception )
112 return ( static_cast< SvXMLAttributeList_Impl::size_type >( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sValue : OUString();
115 OUString SAL_CALL SvXMLAttributeList::getTypeByName( const OUString& ) throw( css::uno::RuntimeException, std::exception )
117 return sType;
120 OUString SAL_CALL SvXMLAttributeList::getValueByName(const OUString& sName) throw( css::uno::RuntimeException, std::exception )
122 ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
124 for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
125 if( (*ii).sName == sName ) {
126 return (*ii).sValue;
129 return OUString();
133 uno::Reference< css::util::XCloneable > SvXMLAttributeList::createClone() throw( css::uno::RuntimeException, std::exception )
135 uno::Reference< css::util::XCloneable > r = new SvXMLAttributeList( *this );
136 return r;
140 SvXMLAttributeList::SvXMLAttributeList()
141 : m_pImpl( new SvXMLAttributeList_Impl ),
142 sType( GetXMLToken(XML_CDATA) )
147 SvXMLAttributeList::~SvXMLAttributeList()
152 void SvXMLAttributeList::AddAttribute( const OUString &sName ,
153 const OUString &sValue )
155 m_pImpl->vecAttribute.push_back( SvXMLTagAttribute_Impl( sName , sValue ) );
158 void SvXMLAttributeList::Clear()
160 m_pImpl->vecAttribute.clear();
162 OSL_ASSERT( ! getLength() );
165 void SvXMLAttributeList::RemoveAttribute( const OUString& sName )
167 ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
169 for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
170 if( (*ii).sName == sName ) {
171 m_pImpl->vecAttribute.erase( ii );
172 break;
177 void SvXMLAttributeList::AppendAttributeList( const uno::Reference< css::xml::sax::XAttributeList > &r )
179 OSL_ASSERT( r.is() );
181 sal_Int16 nMax = r->getLength();
182 SvXMLAttributeList_Impl::size_type nTotalSize =
183 m_pImpl->vecAttribute.size() + nMax;
184 m_pImpl->vecAttribute.reserve( nTotalSize );
186 for( sal_Int16 i = 0 ; i < nMax ; ++i ) {
187 m_pImpl->vecAttribute.push_back( SvXMLTagAttribute_Impl(
188 r->getNameByIndex( i ) ,
189 r->getValueByIndex( i )));
192 OSL_ASSERT( nTotalSize == (SvXMLAttributeList_Impl::size_type)getLength());
195 void SvXMLAttributeList::SetValueByIndex( sal_Int16 i,
196 const OUString& rValue )
198 if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
199 < m_pImpl->vecAttribute.size() )
201 m_pImpl->vecAttribute[i].sValue = rValue;
205 void SvXMLAttributeList::RemoveAttributeByIndex( sal_Int16 i )
207 if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
208 < m_pImpl->vecAttribute.size() )
209 m_pImpl->vecAttribute.erase( m_pImpl->vecAttribute.begin() + i );
212 void SvXMLAttributeList::RenameAttributeByIndex( sal_Int16 i,
213 const OUString& rNewName )
215 if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
216 < m_pImpl->vecAttribute.size() )
218 m_pImpl->vecAttribute[i].sName = rNewName;
222 sal_Int16 SvXMLAttributeList::GetIndexByName( const OUString& rName ) const
224 ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii =
225 m_pImpl->vecAttribute.begin();
227 for( sal_Int16 nIndex=0; ii!=m_pImpl->vecAttribute.end(); ++ii, ++nIndex )
229 if( (*ii).sName == rName )
231 return nIndex;
234 return -1;
237 namespace
239 class theSvXMLAttributeListUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSvXMLAttributeListUnoTunnelId> {};
242 // XUnoTunnel & co
243 const uno::Sequence< sal_Int8 > & SvXMLAttributeList::getUnoTunnelId() throw()
245 return theSvXMLAttributeListUnoTunnelId::get().getSeq();
248 SvXMLAttributeList* SvXMLAttributeList::getImplementation( const uno::Reference< uno::XInterface >& xInt ) throw()
250 uno::Reference< lang::XUnoTunnel > xUT( xInt, uno::UNO_QUERY );
251 if( xUT.is() )
253 return
254 reinterpret_cast<SvXMLAttributeList*>(
255 sal::static_int_cast<sal_IntPtr>(
256 xUT->getSomething( SvXMLAttributeList::getUnoTunnelId())));
258 else
259 return nullptr;
262 // XUnoTunnel
263 sal_Int64 SAL_CALL SvXMLAttributeList::getSomething( const uno::Sequence< sal_Int8 >& rId )
264 throw( uno::RuntimeException, std::exception )
266 if( rId.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(),
267 rId.getConstArray(), 16 ) )
269 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
271 return 0;
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */