bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / core / attrlist.cxx
blob148b23e99cb4d2a72c19c583c64cda86eae67bc1
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;
73 sal_Int16 SAL_CALL SvXMLAttributeList::getLength() throw( ::com::sun::star::uno::RuntimeException, std::exception )
75 return sal::static_int_cast< sal_Int16 >(m_pImpl->vecAttribute.size());
79 SvXMLAttributeList::SvXMLAttributeList( const SvXMLAttributeList &r ) :
80 cppu::WeakImplHelper3<com::sun::star::xml::sax::XAttributeList, com::sun::star::util::XCloneable, com::sun::star::lang::XUnoTunnel>(r),
81 m_pImpl( new SvXMLAttributeList_Impl( *r.m_pImpl ) )
85 SvXMLAttributeList::SvXMLAttributeList( const uno::Reference<
86 xml::sax::XAttributeList> & rAttrList )
87 : sType( GetXMLToken(XML_CDATA) )
89 m_pImpl = new SvXMLAttributeList_Impl;
91 SvXMLAttributeList* pImpl =
92 SvXMLAttributeList::getImplementation( rAttrList );
94 if( pImpl )
95 *m_pImpl = *(pImpl->m_pImpl);
96 else
97 AppendAttributeList( rAttrList );
100 OUString SAL_CALL SvXMLAttributeList::getNameByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException, std::exception )
102 return ( static_cast< SvXMLAttributeList_Impl::size_type >( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sName : OUString();
106 OUString SAL_CALL SvXMLAttributeList::getTypeByIndex(sal_Int16) throw( ::com::sun::star::uno::RuntimeException, std::exception )
108 return sType;
111 OUString SAL_CALL SvXMLAttributeList::getValueByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException, std::exception )
113 return ( static_cast< SvXMLAttributeList_Impl::size_type >( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sValue : OUString();
116 OUString SAL_CALL SvXMLAttributeList::getTypeByName( const OUString& ) throw( ::com::sun::star::uno::RuntimeException, std::exception )
118 return sType;
121 OUString SAL_CALL SvXMLAttributeList::getValueByName(const OUString& sName) throw( ::com::sun::star::uno::RuntimeException, std::exception )
123 ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
125 for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
126 if( (*ii).sName == sName ) {
127 return (*ii).sValue;
130 return OUString();
134 uno::Reference< ::com::sun::star::util::XCloneable > SvXMLAttributeList::createClone() throw( ::com::sun::star::uno::RuntimeException, std::exception )
136 uno::Reference< ::com::sun::star::util::XCloneable > r = new SvXMLAttributeList( *this );
137 return r;
141 SvXMLAttributeList::SvXMLAttributeList()
142 : sType( GetXMLToken(XML_CDATA) )
144 m_pImpl = new SvXMLAttributeList_Impl;
149 SvXMLAttributeList::~SvXMLAttributeList()
151 delete m_pImpl;
155 void SvXMLAttributeList::AddAttribute( const OUString &sName ,
156 const OUString &sValue )
158 m_pImpl->vecAttribute.push_back( SvXMLTagAttribute_Impl( sName , sValue ) );
161 void SvXMLAttributeList::Clear()
163 m_pImpl->vecAttribute.clear();
165 OSL_ASSERT( ! getLength() );
168 void SvXMLAttributeList::RemoveAttribute( const OUString& sName )
170 ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
172 for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
173 if( (*ii).sName == sName ) {
174 m_pImpl->vecAttribute.erase( ii );
175 break;
180 void SvXMLAttributeList::AppendAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList > &r )
182 OSL_ASSERT( r.is() );
184 sal_Int16 nMax = r->getLength();
185 SvXMLAttributeList_Impl::size_type nTotalSize =
186 m_pImpl->vecAttribute.size() + nMax;
187 m_pImpl->vecAttribute.reserve( nTotalSize );
189 for( sal_Int16 i = 0 ; i < nMax ; ++i ) {
190 m_pImpl->vecAttribute.push_back( SvXMLTagAttribute_Impl(
191 r->getNameByIndex( i ) ,
192 r->getValueByIndex( i )));
195 OSL_ASSERT( nTotalSize == (SvXMLAttributeList_Impl::size_type)getLength());
198 void SvXMLAttributeList::SetValueByIndex( sal_Int16 i,
199 const OUString& rValue )
201 if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
202 < m_pImpl->vecAttribute.size() )
204 m_pImpl->vecAttribute[i].sValue = rValue;
208 void SvXMLAttributeList::RemoveAttributeByIndex( sal_Int16 i )
210 if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
211 < m_pImpl->vecAttribute.size() )
212 m_pImpl->vecAttribute.erase( m_pImpl->vecAttribute.begin() + i );
215 void SvXMLAttributeList::RenameAttributeByIndex( sal_Int16 i,
216 const OUString& rNewName )
218 if( static_cast< SvXMLAttributeList_Impl::size_type >( i )
219 < m_pImpl->vecAttribute.size() )
221 m_pImpl->vecAttribute[i].sName = rNewName;
225 sal_Int16 SvXMLAttributeList::GetIndexByName( const OUString& rName ) const
227 ::std::vector<struct SvXMLTagAttribute_Impl>::iterator ii =
228 m_pImpl->vecAttribute.begin();
230 for( sal_Int16 nIndex=0; ii!=m_pImpl->vecAttribute.end(); ++ii, ++nIndex )
232 if( (*ii).sName == rName )
234 return nIndex;
237 return -1;
240 namespace
242 class theSvXMLAttributeListUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSvXMLAttributeListUnoTunnelId> {};
245 // XUnoTunnel & co
246 const uno::Sequence< sal_Int8 > & SvXMLAttributeList::getUnoTunnelId() throw()
248 return theSvXMLAttributeListUnoTunnelId::get().getSeq();
251 SvXMLAttributeList* SvXMLAttributeList::getImplementation( uno::Reference< uno::XInterface > xInt ) throw()
253 uno::Reference< lang::XUnoTunnel > xUT( xInt, uno::UNO_QUERY );
254 if( xUT.is() )
256 return
257 reinterpret_cast<SvXMLAttributeList*>(
258 sal::static_int_cast<sal_IntPtr>(
259 xUT->getSomething( SvXMLAttributeList::getUnoTunnelId())));
261 else
262 return NULL;
265 // XUnoTunnel
266 sal_Int64 SAL_CALL SvXMLAttributeList::getSomething( const uno::Sequence< sal_Int8 >& rId )
267 throw( uno::RuntimeException, std::exception )
269 if( rId.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(),
270 rId.getConstArray(), 16 ) )
272 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
274 return 0;
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */