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 sal_Int16 SAL_CALL SvXMLAttributeList::getLength()
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 
46 SvXMLAttributeList::SvXMLAttributeList( const uno::Reference< xml::sax::XAttributeList> & rAttrList )
47 {
48  SvXMLAttributeList* pImpl =
49  comphelper::getUnoTunnelImplementation<SvXMLAttributeList>( rAttrList );
50 
51  if( pImpl )
52  vecAttribute = pImpl->vecAttribute;
53  else
54  AppendAttributeList( rAttrList );
55 }
56 
57 OUString SAL_CALL SvXMLAttributeList::getNameByIndex(sal_Int16 i)
58 {
60  return ( o3tl::make_unsigned( i ) < vecAttribute.size() ) ? vecAttribute[i].sName : OUString();
61 }
62 
63 
64 OUString SAL_CALL SvXMLAttributeList::getTypeByIndex(sal_Int16)
65 {
66  return "CDATA";
67 }
68 
69 OUString SAL_CALL SvXMLAttributeList::getValueByIndex(sal_Int16 i)
70 {
72  return ( o3tl::make_unsigned( i ) < vecAttribute.size() ) ? vecAttribute[i].sValue : OUString();
73 }
74 
75 OUString SAL_CALL SvXMLAttributeList::getTypeByName( const OUString& )
76 {
77  return "CDATA";
78 }
79 
80 OUString 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 
92 uno::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 
110 void 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 
123 void 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 
132 void 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 
177 sal_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: */
exports com.sun.star.lib. util
void RenameAttributeByIndex(sal_Int16 i, const OUString &rNewName)
Definition: attrlist.cxx:167
exports com.sun.star. xml
virtual OUString SAL_CALL getValueByName(const OUString &aName) override
Definition: attrlist.cxx:80
virtual OUString SAL_CALL getTypeByName(const OUString &aName) override
Definition: attrlist.cxx:75
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
sal_Int16 GetIndexByName(const OUString &rName) const
Definition: attrlist.cxx:177
const char * sName
UNO3_GETIMPLEMENTATION_IMPL(SvXMLExport)
void RemoveAttribute(const OUString &sName)
Definition: attrlist.cxx:123
void AppendAttributeList(const css::uno::Reference< css::xml::sax::XAttributeList > &)
Definition: attrlist.cxx:132
int i
virtual ~SvXMLAttributeList() override
Definition: attrlist.cxx:105
virtual sal_Int16 SAL_CALL getLength() override
Definition: attrlist.cxx:34
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
float u
virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone() override
Definition: attrlist.cxx:92
void RemoveAttributeByIndex(sal_Int16 i)
Definition: attrlist.cxx:160
std::vector< SvXMLTagAttribute_Impl > vecAttribute
Definition: attrlist.hxx:43
exports com.sun.star. lang
Handling of tokens in XML:
void SetValueByIndex(sal_Int16 i, const OUString &rValue)
Definition: attrlist.cxx:150
void AddAttribute(const OUString &sName, const OUString &sValue)
Definition: attrlist.cxx:110
virtual OUString SAL_CALL getNameByIndex(sal_Int16 i) override
Definition: attrlist.cxx:57
virtual OUString SAL_CALL getTypeByIndex(sal_Int16 i) override
Definition: attrlist.cxx:64
virtual OUString SAL_CALL getValueByIndex(sal_Int16 i) override
Definition: attrlist.cxx:69