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 .
20 #include <comphelper/attributelist.hxx>
26 using namespace com::sun::star
;
29 namespace comphelper
{
31 OUString SAL_CALL
AttributeList::getValueByName(const OUString
& sName
)
33 for (auto const& attribute
: mAttributes
)
35 if( attribute
.sName
== sName
) {
36 return attribute
.sValue
;
42 AttributeList::AttributeList()
44 // performance improvement during adding
45 mAttributes
.reserve(20);
48 AttributeList::AttributeList(const uno::Reference
< xml::sax::XAttributeList
>& rAttrList
)
50 if (AttributeList
* pImpl
= dynamic_cast<AttributeList
*>(rAttrList
.get()))
51 mAttributes
= pImpl
->mAttributes
;
53 AppendAttributeList(rAttrList
);
56 AttributeList::~AttributeList()
60 css::uno::Reference
< css::util::XCloneable
> AttributeList::createClone()
62 return new AttributeList( *this );
65 void AttributeList::AddAttribute(const OUString
& sName
, const OUString
& sValue
)
67 assert(!sName
.isEmpty() && "empty attribute name is invalid");
68 // Either it's 'namespace_prefix:attribute_name',
69 // or as in XMLNamespaces::applyNSToAttributeName, it's 'namespace:full:uri^attribute_name'.
70 assert((std::count(sName
.getStr(), sName
.getStr() + sName
.getLength(), u
':') <= 1
71 || std::count(sName
.getStr(), sName
.getStr() + sName
.getLength(), u
'^') == 1)
72 && "too many colons");
73 // TODO: this assertion fails in tests!
74 // assert(std::none_of(mAttributes.begin(), mAttributes.end(),
75 // [&sName](const TagAttribute& a) { return a.sName == sName; }));
76 mAttributes
.push_back({ sName
, sValue
});
79 void AttributeList::RemoveAttribute(const OUString
& sName
)
81 auto ii
= std::find_if(mAttributes
.begin(), mAttributes
.end(),
82 [&sName
](const TagAttribute
& rAttr
) { return rAttr
.sName
== sName
; });
84 if (ii
!= mAttributes
.end())
85 mAttributes
.erase(ii
);
88 void AttributeList::AppendAttributeList(const uno::Reference
<css::xml::sax::XAttributeList
>& r
)
92 sal_Int16 nMax
= r
->getLength();
93 sal_Int16 nTotalSize
= mAttributes
.size() + nMax
;
94 mAttributes
.reserve(nTotalSize
);
96 for (sal_Int16 i
= 0; i
< nMax
; ++i
)
97 AddAttribute(r
->getNameByIndex(i
), r
->getValueByIndex(i
));
99 assert(nTotalSize
== getLength());
102 void AttributeList::SetValueByIndex(sal_Int16 i
, const OUString
& rValue
)
104 mAttributes
[i
].sValue
= rValue
;
107 void AttributeList::RemoveAttributeByIndex(sal_Int16 i
)
109 mAttributes
.erase(mAttributes
.begin() + i
);
112 void AttributeList::RenameAttributeByIndex(sal_Int16 i
, const OUString
& rNewName
)
114 mAttributes
[i
].sName
= rNewName
;
117 sal_Int16
AttributeList::GetIndexByName(const OUString
& rName
) const
119 auto ii
= std::find_if(mAttributes
.begin(), mAttributes
.end(),
120 [&rName
](const TagAttribute
& rAttr
) { return rAttr
.sName
== rName
; });
122 if (ii
!= mAttributes
.end())
123 return static_cast<sal_Int16
>(std::distance(mAttributes
.begin(), ii
));
128 } // namespace comphelper
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */