tdf#163083: try to release lock to avoid deadlock
[LibreOffice.git] / comphelper / source / xml / attributelist.cxx
blob0b087095aa5343bd6b32d2442179f85d39546291
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 .
20 #include <comphelper/attributelist.hxx>
22 #include <algorithm>
23 #include <cassert>
25 using namespace com::sun::star;
28 namespace comphelper {
30 OUString SAL_CALL AttributeList::getValueByName(const OUString& sName)
32 for (auto const& attribute : mAttributes)
34 if( attribute.sName == sName ) {
35 return attribute.sValue;
38 return OUString();
41 AttributeList::AttributeList()
43 // performance improvement during adding
44 mAttributes.reserve(20);
47 AttributeList::AttributeList(const uno::Reference< xml::sax::XAttributeList>& rAttrList)
49 if (AttributeList* pImpl = dynamic_cast<AttributeList*>(rAttrList.get()))
50 mAttributes = pImpl->mAttributes;
51 else
52 AppendAttributeList(rAttrList);
55 AttributeList::~AttributeList()
59 css::uno::Reference< css::util::XCloneable > AttributeList::createClone()
61 return new AttributeList( *this );
64 void AttributeList::AddAttribute(const OUString& sName, const OUString& sValue)
66 assert(!sName.isEmpty() && "empty attribute name is invalid");
67 // Either it's 'namespace_prefix:attribute_name',
68 // or as in XMLNamespaces::applyNSToAttributeName, it's 'namespace:full:uri^attribute_name'.
69 assert((std::count(sName.getStr(), sName.getStr() + sName.getLength(), u':') <= 1
70 || std::count(sName.getStr(), sName.getStr() + sName.getLength(), u'^') == 1)
71 && "too many colons");
72 // TODO: this assertion fails in tests!
73 // assert(std::none_of(mAttributes.begin(), mAttributes.end(),
74 // [&sName](const TagAttribute& a) { return a.sName == sName; }));
75 mAttributes.push_back({ sName, sValue });
78 void AttributeList::RemoveAttribute(const OUString& sName)
80 auto ii = std::find_if(mAttributes.begin(), mAttributes.end(),
81 [&sName](const TagAttribute& rAttr) { return rAttr.sName == sName; });
83 if (ii != mAttributes.end())
84 mAttributes.erase(ii);
87 void AttributeList::AppendAttributeList(const uno::Reference<css::xml::sax::XAttributeList>& r)
89 assert(r.is());
91 sal_Int16 nMax = r->getLength();
92 sal_Int16 nTotalSize = mAttributes.size() + nMax;
93 mAttributes.reserve(nTotalSize);
95 for (sal_Int16 i = 0; i < nMax; ++i)
96 AddAttribute(r->getNameByIndex(i), r->getValueByIndex(i));
98 assert(nTotalSize == getLength());
101 void AttributeList::SetValueByIndex(sal_Int16 i, const OUString& rValue)
103 mAttributes[i].sValue = rValue;
106 void AttributeList::RemoveAttributeByIndex(sal_Int16 i)
108 mAttributes.erase(mAttributes.begin() + i);
111 void AttributeList::RenameAttributeByIndex(sal_Int16 i, const OUString& rNewName)
113 mAttributes[i].sName = rNewName;
116 sal_Int16 AttributeList::GetIndexByName(const OUString& rName) const
118 auto ii = std::find_if(mAttributes.begin(), mAttributes.end(),
119 [&rName](const TagAttribute& rAttr) { return rAttr.sName == rName; });
121 if (ii != mAttributes.end())
122 return static_cast<sal_Int16>(std::distance(mAttributes.begin(), ii));
124 return -1;
127 } // namespace comphelper
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */