1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #include <o3tl/safeint.hxx>
24 #include <osl/diagnose.h>
25 #include <xmloff/xmltoken.hxx>
26 #include <cppuhelper/implbase.hxx>
28 #include <xmloff/attrlist.hxx>
31 using namespace ::com::sun::star
;
32 using namespace ::xmloff::token
;
34 sal_Int16 SAL_CALL
SvXMLAttributeList::getLength()
36 return sal::static_int_cast
< sal_Int16
>(vecAttribute
.size());
40 SvXMLAttributeList::SvXMLAttributeList( const SvXMLAttributeList
&r
) :
41 cppu::WeakImplHelper
<css::xml::sax::XAttributeList
, css::util::XCloneable
, css::lang::XUnoTunnel
>(r
),
42 vecAttribute( r
.vecAttribute
)
46 SvXMLAttributeList::SvXMLAttributeList( const uno::Reference
< xml::sax::XAttributeList
> & rAttrList
)
48 SvXMLAttributeList
* pImpl
=
49 comphelper::getUnoTunnelImplementation
<SvXMLAttributeList
>( rAttrList
);
52 vecAttribute
= pImpl
->vecAttribute
;
54 AppendAttributeList( rAttrList
);
57 OUString SAL_CALL
SvXMLAttributeList::getNameByIndex(sal_Int16 i
)
59 assert( o3tl::make_unsigned(i
) < vecAttribute
.size() );
60 return ( o3tl::make_unsigned( i
) < vecAttribute
.size() ) ? vecAttribute
[i
].sName
: OUString();
64 OUString SAL_CALL
SvXMLAttributeList::getTypeByIndex(sal_Int16
)
69 OUString SAL_CALL
SvXMLAttributeList::getValueByIndex(sal_Int16 i
)
71 assert( o3tl::make_unsigned(i
) < vecAttribute
.size() );
72 return ( o3tl::make_unsigned( i
) < vecAttribute
.size() ) ? vecAttribute
[i
].sValue
: OUString();
75 OUString SAL_CALL
SvXMLAttributeList::getTypeByName( const OUString
& )
80 OUString SAL_CALL
SvXMLAttributeList::getValueByName(const OUString
& sName
)
82 auto ii
= std::find_if(vecAttribute
.begin(), vecAttribute
.end(),
83 [&sName
](SvXMLTagAttribute_Impl
& rAttr
) { return rAttr
.sName
== sName
; });
85 if (ii
!= vecAttribute
.end())
92 uno::Reference
< css::util::XCloneable
> SvXMLAttributeList::createClone()
94 uno::Reference
< css::util::XCloneable
> r
= new SvXMLAttributeList( *this );
99 SvXMLAttributeList::SvXMLAttributeList()
101 vecAttribute
.reserve(20); // performance improvement during adding
105 SvXMLAttributeList::~SvXMLAttributeList()
110 void SvXMLAttributeList::AddAttribute( const OUString
&sName
,
111 const OUString
&sValue
)
113 assert( !sName
.isEmpty() && "empty attribute name is invalid");
114 assert( std::count(sName
.getStr(), sName
.getStr() + sName
.getLength(), u
':') <= 1 && "too many colons");
115 vecAttribute
.emplace_back( SvXMLTagAttribute_Impl
{ sName
, sValue
} );
118 void SvXMLAttributeList::Clear()
120 vecAttribute
.clear();
123 void SvXMLAttributeList::RemoveAttribute( const OUString
& sName
)
125 auto ii
= std::find_if(vecAttribute
.begin(), vecAttribute
.end(),
126 [&sName
](SvXMLTagAttribute_Impl
& rAttr
) { return rAttr
.sName
== sName
; });
128 if (ii
!= vecAttribute
.end())
129 vecAttribute
.erase( ii
);
132 void SvXMLAttributeList::AppendAttributeList( const uno::Reference
< css::xml::sax::XAttributeList
> &r
)
134 OSL_ASSERT( r
.is() );
136 sal_Int16 nMax
= r
->getLength();
137 sal_Int16 nTotalSize
= vecAttribute
.size() + nMax
;
138 vecAttribute
.reserve( nTotalSize
);
140 for( sal_Int16 i
= 0 ; i
< nMax
; ++i
) {
141 OUString sName
= r
->getNameByIndex( i
);
142 assert( !sName
.isEmpty() && "empty attribute name is invalid");
143 assert( std::count(sName
.getStr(), sName
.getStr() + sName
.getLength(), u
':') <= 1 && "too many colons");
144 vecAttribute
.emplace_back(SvXMLTagAttribute_Impl
{ sName
, r
->getValueByIndex( i
) });
147 OSL_ASSERT( nTotalSize
== getLength() );
150 void SvXMLAttributeList::SetValueByIndex( sal_Int16 i
,
151 const OUString
& rValue
)
153 assert( o3tl::make_unsigned(i
) < vecAttribute
.size() );
154 if( o3tl::make_unsigned( i
) < vecAttribute
.size() )
156 vecAttribute
[i
].sValue
= rValue
;
160 void SvXMLAttributeList::RemoveAttributeByIndex( sal_Int16 i
)
162 assert( o3tl::make_unsigned(i
) < vecAttribute
.size() );
163 if( o3tl::make_unsigned( i
) < vecAttribute
.size() )
164 vecAttribute
.erase( vecAttribute
.begin() + i
);
167 void SvXMLAttributeList::RenameAttributeByIndex( sal_Int16 i
,
168 const OUString
& rNewName
)
170 assert( o3tl::make_unsigned(i
) < vecAttribute
.size() );
171 if( o3tl::make_unsigned( i
) < vecAttribute
.size() )
173 vecAttribute
[i
].sName
= rNewName
;
177 sal_Int16
SvXMLAttributeList::GetIndexByName( const OUString
& rName
) const
179 auto ii
= std::find_if(vecAttribute
.begin(), vecAttribute
.end(),
180 [&rName
](const SvXMLTagAttribute_Impl
& rAttr
) { return rAttr
.sName
== rName
; });
182 if (ii
!= vecAttribute
.end())
183 return static_cast<sal_Int16
>(std::distance(vecAttribute
.begin(), ii
));
189 UNO3_GETIMPLEMENTATION_IMPL(SvXMLAttributeList
)
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */