LibreOffice Module xmloff (master)  1
SvXMLAttrCollection.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 
10 #include "SvXMLAttrCollection.hxx"
11 #include <limits.h>
12 #include <osl/diagnose.h>
13 
15 {
16  return (rCmp.aNamespaceMap == aNamespaceMap) &&
17  (rCmp.aAttrs == aAttrs);
18 }
19 
20 bool SvXMLAttrCollection::AddAttr( const OUString& rLName,
21  const OUString& rValue )
22 {
23  assert(!rLName.isEmpty());
24  aAttrs.emplace_back(rLName, rValue );
25  return true;
26 }
27 
28 bool SvXMLAttrCollection::AddAttr( const OUString& rPrefix,
29  const OUString& rNamespace,
30  const OUString& rLName,
31  const OUString& rValue )
32 {
33  assert(!rPrefix.isEmpty());
34  assert(!rNamespace.isEmpty());
35  assert(!rLName.isEmpty());
36  sal_uInt16 nPos = aNamespaceMap.Add( rPrefix, rNamespace );
37  aAttrs.emplace_back(nPos, rLName, rValue );
38  return true;
39 }
40 
41 bool SvXMLAttrCollection::AddAttr( const OUString& rPrefix,
42  const OUString& rLName,
43  const OUString& rValue )
44 {
45  assert(!rPrefix.isEmpty());
46  assert(!rLName.isEmpty());
47  sal_uInt16 nPos = aNamespaceMap.GetIndexByPrefix( rPrefix );
48  if( USHRT_MAX == nPos )
49  return false;
50  aAttrs.emplace_back(nPos, rLName, rValue );
51  return true;
52 }
53 
55  const OUString& rLName,
56  const OUString& rValue )
57 {
58  assert(!rLName.isEmpty());
59  if( i >= GetAttrCount() )
60  return false;
61  aAttrs[i] = SvXMLAttr(rLName, rValue);
62  return true;
63 }
64 
66  const OUString& rPrefix,
67  const OUString& rNamespace,
68  const OUString& rLName,
69  const OUString& rValue )
70 {
71  assert(!rPrefix.isEmpty());
72  assert(!rNamespace.isEmpty());
73  assert(!rLName.isEmpty());
74  if( i >= GetAttrCount() )
75  return false;
76 
77  sal_uInt16 nPos = aNamespaceMap.Add( rPrefix, rNamespace );
78  if( USHRT_MAX == nPos )
79  return false;
80 
81  aAttrs[i] = SvXMLAttr(nPos, rLName, rValue);
82  return true;
83 }
84 
86  const OUString& rPrefix,
87  const OUString& rLName,
88  const OUString& rValue )
89 {
90  assert(!rPrefix.isEmpty());
91  assert(!rLName.isEmpty());
92  if( i >= GetAttrCount() )
93  return false;
94 
95  sal_uInt16 nPos = aNamespaceMap.GetIndexByPrefix( rPrefix );
96  if( USHRT_MAX == nPos )
97  return false;
98 
99  aAttrs[i] = SvXMLAttr(nPos, rLName, rValue);
100  return true;
101 }
102 
104 {
105  if( i < GetAttrCount() )
106  {
107  aAttrs.erase( aAttrs.begin() + i );
108  }
109  else
110  {
111  OSL_FAIL( "illegal index" );
112  }
113 }
114 
116 {
117  return aAttrs.size();
118 }
119 
120 const OUString& SvXMLAttrCollection::GetAttrLName(size_t i) const
121 {
122  OSL_ENSURE( i < aAttrs.size(), "SvXMLAttrContainerData::GetLName: illegal index" );
123  return aAttrs[i].getLName();
124 }
125 
126 const OUString& SvXMLAttrCollection::GetAttrValue(size_t i) const
127 {
128  OSL_ENSURE( i < aAttrs.size(), "SvXMLAttrContainerData::GetValue: illegal index" );
129  return aAttrs[i].getValue();
130 }
131 
132 OUString SvXMLAttrCollection::GetAttrNamespace( size_t i ) const
133 {
134  OUString sRet;
135  sal_uInt16 nPos = GetPrefixPos( i );
136  //Does this point to a valid namespace entry?
137  if( USHRT_MAX != nPos )
138  sRet = aNamespaceMap.GetNameByIndex( nPos );
139  return sRet;
140 }
141 
142 OUString SvXMLAttrCollection::GetAttrPrefix( size_t i ) const
143 {
144  OUString sRet;
145  sal_uInt16 nPos = GetPrefixPos( i );
146  //Does this point to a valid namespace entry?
147  if( USHRT_MAX != nPos )
148  sRet = aNamespaceMap.GetPrefixByIndex( nPos );
149  return sRet;
150 }
151 
152 const OUString& SvXMLAttrCollection::GetNamespace( sal_uInt16 i ) const
153 {
154  return aNamespaceMap.GetNameByIndex( i );
155 }
156 
157 const OUString& SvXMLAttrCollection::GetPrefix( sal_uInt16 i ) const
158 {
159  return aNamespaceMap.GetPrefixByIndex( i );
160 }
161 
163 {
164  return aNamespaceMap.GetFirstIndex();
165 }
166 
167 sal_uInt16 SvXMLAttrCollection::GetNextNamespaceIndex( sal_uInt16 nIdx ) const
168 {
169  return aNamespaceMap.GetNextIndex( nIdx );
170 }
171 
172 sal_uInt16 SvXMLAttrCollection::GetPrefixPos( size_t i ) const
173 {
174 // SAL_WARN_IF( i < 0 || i >= aAttrs.size()), "xmloff",
175 // "SvXMLAttrCollection::GetPrefixPos: illegal index" );
176  return aAttrs[i].getPrefixPos();
177 }
178 
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
180 
std::vector< SvXMLAttr > aAttrs
size_t GetAttrCount() const
sal_uInt16 GetPrefixPos(size_t i) const
bool SetAt(size_t i, const OUString &rLName, const OUString &rValue)
sal_uInt16 GetNextNamespaceIndex(sal_uInt16 nIdx) const
sal_uInt16 GetIndexByPrefix(const OUString &rPrefix) const
SvXMLNamespaceMap aNamespaceMap
sal_uInt16 GetFirstNamespaceIndex() const
OUString GetAttrNamespace(size_t i) const
OUString GetAttrPrefix(size_t i) const
const OUString & GetPrefix(sal_uInt16 i) const
sal_uInt16 Add(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey=XML_NAMESPACE_UNKNOWN)
const OUString & GetAttrValue(size_t i) const
bool operator==(const SvXMLAttrCollection &rCmp) const
const OUString & GetPrefixByIndex(sal_uInt16 nIdx) const
const OUString & GetNameByIndex(sal_uInt16 nIdx) const
const OUString & GetAttrLName(size_t i) const
const OUString & GetNamespace(sal_uInt16 i) const
sal_uInt16 GetFirstIndex() const
sal_uInt16 nPos
bool AddAttr(const OUString &rLName, const OUString &rValue)
sal_uInt16 GetNextIndex(sal_uInt16 nOldIdx) const