/src/libreoffice/comphelper/source/xml/attributelist.cxx
Line | Count | Source (jump to first uncovered line) |
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 | 791k | { |
32 | 791k | for (auto const& attribute : mAttributes) |
33 | 1.38M | { |
34 | 1.38M | if( attribute.sName == sName ) { |
35 | 726k | return attribute.sValue; |
36 | 726k | } |
37 | 1.38M | } |
38 | 64.9k | return OUString(); |
39 | 791k | } |
40 | | |
41 | | AttributeList::AttributeList() |
42 | 206k | { |
43 | | // performance improvement during adding |
44 | 206k | mAttributes.reserve(20); |
45 | 206k | } |
46 | | |
47 | | AttributeList::AttributeList(const uno::Reference< xml::sax::XAttributeList>& rAttrList) |
48 | 0 | { |
49 | 0 | if (AttributeList* pImpl = dynamic_cast<AttributeList*>(rAttrList.get())) |
50 | 0 | mAttributes = pImpl->mAttributes; |
51 | 0 | else |
52 | 0 | AppendAttributeList(rAttrList); |
53 | 0 | } |
54 | | |
55 | | AttributeList::~AttributeList() |
56 | 206k | { |
57 | 206k | } |
58 | | |
59 | | css::uno::Reference< css::util::XCloneable > AttributeList::createClone() |
60 | 0 | { |
61 | 0 | return new AttributeList( *this ); |
62 | 0 | } |
63 | | |
64 | | void AttributeList::AddAttribute(const OUString& sName, const OUString& sValue) |
65 | 6.28M | { |
66 | 6.28M | 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 | 6.28M | assert((std::count(sName.getStr(), sName.getStr() + sName.getLength(), u':') <= 1 |
70 | 6.28M | || std::count(sName.getStr(), sName.getStr() + sName.getLength(), u'^') == 1) |
71 | 6.28M | && "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 | 6.28M | mAttributes.push_back({ sName, sValue }); |
76 | 6.28M | } |
77 | | |
78 | | void AttributeList::RemoveAttribute(const OUString& sName) |
79 | 0 | { |
80 | 0 | auto ii = std::find_if(mAttributes.begin(), mAttributes.end(), |
81 | 0 | [&sName](const TagAttribute& rAttr) { return rAttr.sName == sName; }); |
82 | |
|
83 | 0 | if (ii != mAttributes.end()) |
84 | 0 | mAttributes.erase(ii); |
85 | 0 | } |
86 | | |
87 | | void AttributeList::AppendAttributeList(const uno::Reference<css::xml::sax::XAttributeList>& r) |
88 | 0 | { |
89 | 0 | assert(r.is()); |
90 | |
|
91 | 0 | sal_Int16 nMax = r->getLength(); |
92 | 0 | sal_Int16 nTotalSize = mAttributes.size() + nMax; |
93 | 0 | mAttributes.reserve(nTotalSize); |
94 | |
|
95 | 0 | for (sal_Int16 i = 0; i < nMax; ++i) |
96 | 0 | AddAttribute(r->getNameByIndex(i), r->getValueByIndex(i)); |
97 | |
|
98 | 0 | assert(nTotalSize == getLength()); |
99 | 0 | } |
100 | | |
101 | | void AttributeList::SetValueByIndex(sal_Int16 i, const OUString& rValue) |
102 | 0 | { |
103 | 0 | mAttributes[i].sValue = rValue; |
104 | 0 | } |
105 | | |
106 | | void AttributeList::RemoveAttributeByIndex(sal_Int16 i) |
107 | 0 | { |
108 | 0 | mAttributes.erase(mAttributes.begin() + i); |
109 | 0 | } |
110 | | |
111 | | void AttributeList::RenameAttributeByIndex(sal_Int16 i, const OUString& rNewName) |
112 | 0 | { |
113 | 0 | mAttributes[i].sName = rNewName; |
114 | 0 | } |
115 | | |
116 | | sal_Int16 AttributeList::GetIndexByName(const OUString& rName) const |
117 | 0 | { |
118 | 0 | auto ii = std::find_if(mAttributes.begin(), mAttributes.end(), |
119 | 0 | [&rName](const TagAttribute& rAttr) { return rAttr.sName == rName; }); |
120 | |
|
121 | 0 | if (ii != mAttributes.end()) |
122 | 0 | return static_cast<sal_Int16>(std::distance(mAttributes.begin(), ii)); |
123 | | |
124 | 0 | return -1; |
125 | 0 | } |
126 | | |
127 | | } // namespace comphelper |
128 | | |
129 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |