LibreOffice Module xmloff (master) 1
attrlist.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
20
21#include <vector>
22
23#include <o3tl/safeint.hxx>
24#include <osl/diagnose.h>
25#include <xmloff/xmltoken.hxx>
27
28#include <xmloff/attrlist.hxx>
29
30
31using namespace ::com::sun::star;
32using namespace ::xmloff::token;
33
35{
36 return sal::static_int_cast< sal_Int16 >(vecAttribute.size());
37}
38
39
41 cppu::WeakImplHelper<css::xml::sax::XAttributeList, css::util::XCloneable, css::lang::XUnoTunnel>(r),
42 vecAttribute( r.vecAttribute )
43{
44}
45
46SvXMLAttributeList::SvXMLAttributeList( const uno::Reference< xml::sax::XAttributeList> & rAttrList )
47{
48 SvXMLAttributeList* pImpl =
49 comphelper::getFromUnoTunnel<SvXMLAttributeList>( rAttrList );
50
51 if( pImpl )
53 else
54 AppendAttributeList( rAttrList );
55}
56
57OUString SAL_CALL SvXMLAttributeList::getNameByIndex(sal_Int16 i)
58{
59 assert( o3tl::make_unsigned(i) < vecAttribute.size() );
60 return ( o3tl::make_unsigned( i ) < vecAttribute.size() ) ? vecAttribute[i].sName : OUString();
61}
62
63
64OUString SAL_CALL SvXMLAttributeList::getTypeByIndex(sal_Int16)
65{
66 return "CDATA";
67}
68
69OUString SAL_CALL SvXMLAttributeList::getValueByIndex(sal_Int16 i)
70{
71 assert( o3tl::make_unsigned(i) < vecAttribute.size() );
72 return ( o3tl::make_unsigned( i ) < vecAttribute.size() ) ? vecAttribute[i].sValue : OUString();
73}
74
75OUString SAL_CALL SvXMLAttributeList::getTypeByName( const OUString& )
76{
77 return "CDATA";
78}
79
80OUString SAL_CALL SvXMLAttributeList::getValueByName(const OUString& sName)
81{
82 auto ii = std::find_if(vecAttribute.begin(), vecAttribute.end(),
83 [&sName](SvXMLTagAttribute_Impl& rAttr) { return rAttr.sName == sName; });
84
85 if (ii != vecAttribute.end())
86 return (*ii).sValue;
87
88 return OUString();
89}
90
91
92uno::Reference< css::util::XCloneable > SvXMLAttributeList::createClone()
93{
94 uno::Reference< css::util::XCloneable > r = new SvXMLAttributeList( *this );
95 return r;
96}
97
98
100{
101 vecAttribute.reserve(20); // performance improvement during adding
102}
103
104
106{
107}
108
109
110void SvXMLAttributeList::AddAttribute( const OUString &sName ,
111 const OUString &sValue )
112{
113 assert( !sName.isEmpty() && "empty attribute name is invalid");
114 assert( std::count(sName.getStr(), sName.getStr() + sName.getLength(), u':') <= 1 && "too many colons");
115 vecAttribute.emplace_back( SvXMLTagAttribute_Impl { sName , sValue } );
116}
117
119{
120 vecAttribute.clear();
121}
122
123void SvXMLAttributeList::RemoveAttribute( const OUString& sName )
124{
125 auto ii = std::find_if(vecAttribute.begin(), vecAttribute.end(),
126 [&sName](SvXMLTagAttribute_Impl& rAttr) { return rAttr.sName == sName; });
127
128 if (ii != vecAttribute.end())
129 vecAttribute.erase( ii );
130}
131
132void SvXMLAttributeList::AppendAttributeList( const uno::Reference< css::xml::sax::XAttributeList > &r )
133{
134 OSL_ASSERT( r.is() );
135
136 sal_Int16 nMax = r->getLength();
137 sal_Int16 nTotalSize = vecAttribute.size() + nMax;
138 vecAttribute.reserve( nTotalSize );
139
140 for( sal_Int16 i = 0 ; i < nMax ; ++i ) {
141 OUString sName = r->getNameByIndex( i );
142 assert( !sName.isEmpty() && "empty attribute name is invalid");
143 assert( std::count(sName.getStr(), sName.getStr() + sName.getLength(), u':') <= 1 && "too many colons");
144 vecAttribute.emplace_back(SvXMLTagAttribute_Impl { sName, r->getValueByIndex( i ) });
145 }
146
147 OSL_ASSERT( nTotalSize == getLength() );
148}
149
151 const OUString& rValue )
152{
153 assert( o3tl::make_unsigned(i) < vecAttribute.size() );
154 if( o3tl::make_unsigned( i ) < vecAttribute.size() )
155 {
156 vecAttribute[i].sValue = rValue;
157 }
158}
159
161{
162 assert( o3tl::make_unsigned(i) < vecAttribute.size() );
163 if( o3tl::make_unsigned( i ) < vecAttribute.size() )
164 vecAttribute.erase( vecAttribute.begin() + i );
165}
166
168 const OUString& rNewName )
169{
170 assert( o3tl::make_unsigned(i) < vecAttribute.size() );
171 if( o3tl::make_unsigned( i ) < vecAttribute.size() )
172 {
173 vecAttribute[i].sName = rNewName;
174 }
175}
176
177sal_Int16 SvXMLAttributeList::GetIndexByName( const OUString& rName ) const
178{
179 auto ii = std::find_if(vecAttribute.begin(), vecAttribute.end(),
180 [&rName](const SvXMLTagAttribute_Impl& rAttr) { return rAttr.sName == rName; });
181
182 if (ii != vecAttribute.end())
183 return static_cast<sal_Int16>(std::distance(vecAttribute.begin(), ii));
184
185 return -1;
186}
187
188// XUnoTunnel & co
190
191
192/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int16 GetIndexByName(const OUString &rName) const
Definition: attrlist.cxx:177
void AddAttribute(const OUString &sName, const OUString &sValue)
Definition: attrlist.cxx:110
void RenameAttributeByIndex(sal_Int16 i, const OUString &rNewName)
Definition: attrlist.cxx:167
virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone() override
Definition: attrlist.cxx:92
virtual OUString SAL_CALL getTypeByName(const OUString &aName) override
Definition: attrlist.cxx:75
void SetValueByIndex(sal_Int16 i, const OUString &rValue)
Definition: attrlist.cxx:150
void RemoveAttribute(const OUString &sName)
Definition: attrlist.cxx:123
std::vector< SvXMLTagAttribute_Impl > vecAttribute
Definition: attrlist.hxx:43
virtual sal_Int16 SAL_CALL getLength() override
Definition: attrlist.cxx:34
void RemoveAttributeByIndex(sal_Int16 i)
Definition: attrlist.cxx:160
virtual OUString SAL_CALL getTypeByIndex(sal_Int16 i) override
Definition: attrlist.cxx:64
virtual OUString SAL_CALL getNameByIndex(sal_Int16 i) override
Definition: attrlist.cxx:57
virtual OUString SAL_CALL getValueByName(const OUString &aName) override
Definition: attrlist.cxx:80
virtual ~SvXMLAttributeList() override
Definition: attrlist.cxx:105
void AppendAttributeList(const css::uno::Reference< css::xml::sax::XAttributeList > &)
Definition: attrlist.cxx:132
virtual OUString SAL_CALL getValueByIndex(sal_Int16 i) override
Definition: attrlist.cxx:69
float u
const char * sName
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
Handling of tokens in XML:
UNO3_GETIMPLEMENTATION_IMPL(SvXMLExport)