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>
26 #include <cppuhelper/implbase.hxx>
27 
28 #include <xmloff/attrlist.hxx>
29 
30 
31 using namespace ::com::sun::star;
32 using namespace ::xmloff::token;
33 
34 namespace {
35 
36 struct SvXMLTagAttribute_Impl
37 {
38  SvXMLTagAttribute_Impl( const OUString &rName,
39  const OUString &rValue )
40  : sName(rName),
41  sValue(rValue)
42  {
43  }
44 
45  OUString sName;
46  OUString sValue;
47 };
48 
49 }
50 
52 {
54  {
55  // performance improvement during adding
56  vecAttribute.reserve(20);
57  }
58 
59  ::std::vector<struct SvXMLTagAttribute_Impl> vecAttribute;
60  typedef ::std::vector<struct SvXMLTagAttribute_Impl>::size_type size_type;
61 };
62 
63 
64 sal_Int16 SAL_CALL SvXMLAttributeList::getLength()
65 {
66  return sal::static_int_cast< sal_Int16 >(m_pImpl->vecAttribute.size());
67 }
68 
69 
71  cppu::WeakImplHelper<css::xml::sax::XAttributeList, css::util::XCloneable, css::lang::XUnoTunnel>(r),
73 {
74 }
75 
76 SvXMLAttributeList::SvXMLAttributeList( const uno::Reference<
77  xml::sax::XAttributeList> & rAttrList )
80 {
81 
82  SvXMLAttributeList* pImpl =
83  comphelper::getUnoTunnelImplementation<SvXMLAttributeList>( rAttrList );
84 
85  if( pImpl )
86  *m_pImpl = *(pImpl->m_pImpl);
87  else
88  AppendAttributeList( rAttrList );
89 }
90 
91 OUString SAL_CALL SvXMLAttributeList::getNameByIndex(sal_Int16 i)
92 {
93  return ( o3tl::make_unsigned( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sName : OUString();
94 }
95 
96 
97 OUString SAL_CALL SvXMLAttributeList::getTypeByIndex(sal_Int16)
98 {
99  return sType;
100 }
101 
102 OUString SAL_CALL SvXMLAttributeList::getValueByIndex(sal_Int16 i)
103 {
104  return ( o3tl::make_unsigned( i ) < m_pImpl->vecAttribute.size() ) ? m_pImpl->vecAttribute[i].sValue : OUString();
105 }
106 
107 OUString SAL_CALL SvXMLAttributeList::getTypeByName( const OUString& )
108 {
109  return sType;
110 }
111 
112 OUString SAL_CALL SvXMLAttributeList::getValueByName(const OUString& sName)
113 {
114  auto ii = std::find_if(m_pImpl->vecAttribute.begin(), m_pImpl->vecAttribute.end(),
115  [&sName](struct SvXMLTagAttribute_Impl& rAttr) { return rAttr.sName == sName; });
116 
117  if (ii != m_pImpl->vecAttribute.end())
118  return (*ii).sValue;
119 
120  return OUString();
121 }
122 
123 
124 uno::Reference< css::util::XCloneable > SvXMLAttributeList::createClone()
125 {
126  uno::Reference< css::util::XCloneable > r = new SvXMLAttributeList( *this );
127  return r;
128 }
129 
130 
134 {
135 }
136 
137 
139 {
140 }
141 
142 
143 void SvXMLAttributeList::AddAttribute( const OUString &sName ,
144  const OUString &sValue )
145 {
146  m_pImpl->vecAttribute.emplace_back( sName , sValue );
147 }
148 
150 {
151  m_pImpl->vecAttribute.clear();
152 
153  OSL_ASSERT( ! getLength() );
154 }
155 
156 void SvXMLAttributeList::RemoveAttribute( const OUString& sName )
157 {
158  auto ii = std::find_if(m_pImpl->vecAttribute.begin(), m_pImpl->vecAttribute.end(),
159  [&sName](struct SvXMLTagAttribute_Impl& rAttr) { return rAttr.sName == sName; });
160 
161  if (ii != m_pImpl->vecAttribute.end())
162  m_pImpl->vecAttribute.erase( ii );
163 }
164 
165 void SvXMLAttributeList::AppendAttributeList( const uno::Reference< css::xml::sax::XAttributeList > &r )
166 {
167  OSL_ASSERT( r.is() );
168 
169  sal_Int16 nMax = r->getLength();
171  m_pImpl->vecAttribute.size() + nMax;
172  m_pImpl->vecAttribute.reserve( nTotalSize );
173 
174  for( sal_Int16 i = 0 ; i < nMax ; ++i ) {
175  m_pImpl->vecAttribute.emplace_back(
176  r->getNameByIndex( i ) ,
177  r->getValueByIndex( i ));
178  }
179 
180  OSL_ASSERT( nTotalSize == static_cast<SvXMLAttributeList_Impl::size_type>(getLength()));
181 }
182 
184  const OUString& rValue )
185 {
186  if( o3tl::make_unsigned( i )
187  < m_pImpl->vecAttribute.size() )
188  {
189  m_pImpl->vecAttribute[i].sValue = rValue;
190  }
191 }
192 
194 {
195  if( o3tl::make_unsigned( i )
196  < m_pImpl->vecAttribute.size() )
197  m_pImpl->vecAttribute.erase( m_pImpl->vecAttribute.begin() + i );
198 }
199 
201  const OUString& rNewName )
202 {
203  if( o3tl::make_unsigned( i )
204  < m_pImpl->vecAttribute.size() )
205  {
206  m_pImpl->vecAttribute[i].sName = rNewName;
207  }
208 }
209 
210 sal_Int16 SvXMLAttributeList::GetIndexByName( const OUString& rName ) const
211 {
212  auto ii = std::find_if(m_pImpl->vecAttribute.begin(), m_pImpl->vecAttribute.end(),
213  [&rName](struct SvXMLTagAttribute_Impl& rAttr) { return rAttr.sName == rName; });
214 
215  if (ii != m_pImpl->vecAttribute.end())
216  return static_cast<sal_Int16>(std::distance(m_pImpl->vecAttribute.begin(), ii));
217 
218  return -1;
219 }
220 
221 // XUnoTunnel & co
223 
224 
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void RenameAttributeByIndex(sal_Int16 i, const OUString &rNewName)
Definition: attrlist.cxx:200
virtual OUString SAL_CALL getValueByName(const OUString &aName) override
Definition: attrlist.cxx:112
virtual OUString SAL_CALL getTypeByName(const OUString &aName) override
Definition: attrlist.cxx:107
const OUString sType
Definition: attrlist.hxx:74
::std::vector< struct SvXMLTagAttribute_Impl > vecAttribute
Definition: attrlist.cxx:59
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
std::unique_ptr< SvXMLAttributeList_Impl > m_pImpl
Definition: attrlist.hxx:40
sal_Int16 GetIndexByName(const OUString &rName) const
Definition: attrlist.cxx:210
const char * sName
UNO3_GETIMPLEMENTATION_IMPL(SvXMLExport)
void RemoveAttribute(const OUString &sName)
Definition: attrlist.cxx:156
void AppendAttributeList(const css::uno::Reference< css::xml::sax::XAttributeList > &)
Definition: attrlist.cxx:165
OptionalString sType
int i
virtual ~SvXMLAttributeList() override
Definition: attrlist.cxx:138
virtual sal_Int16 SAL_CALL getLength() override
Definition: attrlist.cxx:64
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone() override
Definition: attrlist.cxx:124
::std::vector< struct SvXMLTagAttribute_Impl >::size_type size_type
Definition: attrlist.cxx:60
void RemoveAttributeByIndex(sal_Int16 i)
Definition: attrlist.cxx:193
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3366
Handling of tokens in XML:
void SetValueByIndex(sal_Int16 i, const OUString &rValue)
Definition: attrlist.cxx:183
void AddAttribute(const OUString &sName, const OUString &sValue)
Definition: attrlist.cxx:143
virtual OUString SAL_CALL getNameByIndex(sal_Int16 i) override
Definition: attrlist.cxx:91
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
virtual OUString SAL_CALL getTypeByIndex(sal_Int16 i) override
Definition: attrlist.cxx:97
virtual OUString SAL_CALL getValueByIndex(sal_Int16 i) override
Definition: attrlist.cxx:102