Debian Codegraph @

comphelper/source/xml/attributelist.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <comphelper/attributelist.hxx>
21
22 #include <algorithm>
23 #include <cassert>
24
25 using namespace com::sun::star;
26
27
28 namespace comphelper {
29
30 OUString SAL_CALL AttributeList::getValueByName(const OUString& sName)
31 {
32 for (auto const& attribute : mAttributes)
33 {
34 if( attribute.sName == sName ) {
35 return attribute.sValue;
36 }
37 }
38 return OUString();
39 }
40
41 AttributeList::AttributeList()
42 {
43 // performance improvement during adding
44 mAttributes.reserve(20);
45 }
46
47 AttributeList::AttributeList(const uno::Reference< xml::sax::XAttributeList>& rAttrList)
48 {
49 if (AttributeList* pImpl = dynamic_cast<AttributeList*>(rAttrList.get()))
50 mAttributes = pImpl->mAttributes;
51 else
52 AppendAttributeList(rAttrList);
53 }
54
55 AttributeList::~AttributeList()
56 {
57 }
58
59 css::uno::Reference< css::util::XCloneable > AttributeList::createClone()
60 {
61 return new AttributeList( *this );
62 }
63
64 void AttributeList::AddAttribute(const OUString& sName, const OUString& sValue)
65 {
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 });
76 }
77
78 void AttributeList::RemoveAttribute(const OUString& sName)
79 {
80 auto ii = std::find_if(mAttributes.begin(), mAttributes.end(),
81 [&sName](const TagAttribute& rAttr) { return rAttr.sName == sName; });
82
83 if (ii != mAttributes.end())
84 mAttributes.erase(ii);
85 }
86
87 void AttributeList::AppendAttributeList(const uno::Reference<css::xml::sax::XAttributeList>& r)
88 {
89 assert(r.is());
90
91 sal_Int16 nMax = r->getLength();
92 sal_Int16 nTotalSize = mAttributes.size() + nMax;
93 mAttributes.reserve(nTotalSize);
94
95 for (sal_Int16 i = 0; i < nMax; ++i)
96 AddAttribute(r->getNameByIndex(i), r->getValueByIndex(i));
97
98 assert(nTotalSize == getLength());
99 }
100
101 void AttributeList::SetValueByIndex(sal_Int16 i, const OUString& rValue)
102 {
103 mAttributes[i].sValue = rValue;
104 }
105
106 void AttributeList::RemoveAttributeByIndex(sal_Int16 i)
107 {
108 mAttributes.erase(mAttributes.begin() + i);
109 }
110
111 void AttributeList::RenameAttributeByIndex(sal_Int16 i, const OUString& rNewName)
112 {
113 mAttributes[i].sName = rNewName;
114 }
115
116 sal_Int16 AttributeList::GetIndexByName(const OUString& rName) const
117 {
118 auto ii = std::find_if(mAttributes.begin(), mAttributes.end(),
119 [&rName](const TagAttribute& rAttr) { return rAttr.sName == rName; });
120
121 if (ii != mAttributes.end())
122 return static_cast<sal_Int16>(std::distance(mAttributes.begin(), ii));
123
124 return -1;
125 }
126
127 } // namespace comphelper
128
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */