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