LibreOffice Module xmloff (master)  1
unoatrcn.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 #include <memory>
21 #include <com/sun/star/xml/AttributeData.hpp>
22 #include <o3tl/any.hxx>
23 #include <rtl/ustrbuf.hxx>
26 #include <limits.h>
27 
28 #include <xmloff/xmlcnimp.hxx>
29 
30 #include <xmloff/unoatrcn.hxx>
31 
32 using namespace ::com::sun::star;
33 
34 // Interface implementation
35 
36 uno::Reference< uno::XInterface > SvUnoAttributeContainer_CreateInstance()
37 {
38  return *(new SvUnoAttributeContainer);
39 }
40 
41 SvUnoAttributeContainer::SvUnoAttributeContainer( std::unique_ptr<SvXMLAttrContainerData> pContainer)
42 : mpContainer( std::move( pContainer ) )
43 {
44  if( !mpContainer )
45  mpContainer = std::make_unique<SvXMLAttrContainerData>();
46 }
47 
48 // container::XElementAccess
50 {
52 }
53 
55 {
56  return mpContainer->GetAttrCount() != 0;
57 }
58 
59 sal_uInt16 SvUnoAttributeContainer::getIndexByName(const OUString& aName ) const
60 {
61  const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
62 
63  sal_Int32 nPos = aName.indexOf( ':' );
64  if( nPos == -1 )
65  {
66  for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
67  {
68  if( mpContainer->GetAttrLName(nAttr) == aName &&
69  mpContainer->GetAttrPrefix(nAttr).isEmpty() )
70  return nAttr;
71  }
72  }
73  else
74  {
75  const OUString aPrefix( aName.copy( 0L, nPos ) );
76  const OUString aLName( aName.copy( nPos+1 ) );
77 
78  for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
79  {
80  if( mpContainer->GetAttrLName(nAttr) == aLName &&
81  mpContainer->GetAttrPrefix(nAttr) == aPrefix )
82  return nAttr;
83  }
84  }
85 
86  return USHRT_MAX;
87 }
88 
89 const css::uno::Sequence< sal_Int8 > & SvUnoAttributeContainer::getUnoTunnelId() noexcept
90 {
91  static const UnoTunnelIdInit theSvUnoAttributeContainerUnoTunnelId;
92  return theSvUnoAttributeContainerUnoTunnelId.getSeq();
93 }
94 
95 sal_Int64 SAL_CALL SvUnoAttributeContainer::getSomething( const css::uno::Sequence< sal_Int8 >& rId )
96 {
97  if( isUnoTunnelId<SvUnoAttributeContainer>(rId) )
98  {
99  return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
100  }
101  return 0;
102 }
103 
104 // container::XNameAccess
105 uno::Any SAL_CALL SvUnoAttributeContainer::getByName(const OUString& aName)
106 {
107  sal_uInt16 nAttr = getIndexByName(aName );
108 
109  if( nAttr == USHRT_MAX )
110  throw container::NoSuchElementException();
111 
112  xml::AttributeData aData;
113  aData.Namespace = mpContainer->GetAttrNamespace(nAttr);
114  aData.Type = "CDATA";
115  aData.Value = mpContainer->GetAttrValue(nAttr);
116 
117  return uno::Any(aData);
118 }
119 
120 uno::Sequence< OUString > SAL_CALL SvUnoAttributeContainer::getElementNames()
121 {
122  const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
123 
124  uno::Sequence< OUString > aElementNames( static_cast<sal_Int32>(nAttrCount) );
125  OUString *pNames = aElementNames.getArray();
126 
127  for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
128  {
129  OUStringBuffer sBuffer( mpContainer->GetAttrPrefix(nAttr) );
130  if( !sBuffer.isEmpty() )
131  sBuffer.append( ':' );
132  sBuffer.append( mpContainer->GetAttrLName(nAttr) );
133  *pNames++ = sBuffer.makeStringAndClear();
134  }
135 
136  return aElementNames;
137 }
138 
139 sal_Bool SAL_CALL SvUnoAttributeContainer::hasByName(const OUString& aName)
140 {
141  return getIndexByName(aName ) != USHRT_MAX;
142 }
143 
144 // container::XNameReplace
145 void SAL_CALL SvUnoAttributeContainer::replaceByName(const OUString& aName, const uno::Any& aElement)
146 {
147  if( auto pData = o3tl::tryAccess<xml::AttributeData>(aElement) )
148  {
149  sal_uInt16 nAttr = getIndexByName(aName );
150  if( nAttr == USHRT_MAX )
151  throw container::NoSuchElementException();
152 
153  sal_Int32 nPos = aName.indexOf( ':' );
154  if( nPos != -1 )
155  {
156  const OUString aPrefix( aName.copy( 0L, nPos ));
157  const OUString aLName( aName.copy( nPos+1 ));
158 
159  if( pData->Namespace.isEmpty() )
160  {
161  if( mpContainer->SetAt( nAttr, aPrefix, aLName, pData->Value ) )
162  return;
163  }
164  else
165  {
166  if( mpContainer->SetAt( nAttr, aPrefix, pData->Namespace, aLName, pData->Value ) )
167  return;
168  }
169  }
170  else
171  {
172  if( pData->Namespace.isEmpty() )
173  {
174  if( mpContainer->SetAt( nAttr, aName, pData->Value ) )
175  return;
176  }
177  }
178  }
179 
180  throw lang::IllegalArgumentException();
181 }
182 
183 // container::XNameContainer
184 void SAL_CALL SvUnoAttributeContainer::insertByName(const OUString& aName, const uno::Any& aElement)
185 {
186  auto pData = o3tl::tryAccess<xml::AttributeData>(aElement);
187  if( !pData )
188  throw lang::IllegalArgumentException();
189 
190  sal_uInt16 nAttr = getIndexByName(aName );
191  if( nAttr != USHRT_MAX )
192  throw container::ElementExistException();
193 
194  sal_Int32 nPos = aName.indexOf( ':' );
195  if( nPos != -1 )
196  {
197  const OUString aPrefix( aName.copy( 0L, nPos ));
198  const OUString aLName( aName.copy( nPos+1 ));
199 
200  if( pData->Namespace.isEmpty() )
201  {
202  if( mpContainer->AddAttr( aPrefix, aLName, pData->Value ) )
203  return;
204  }
205  else
206  {
207  if( mpContainer->AddAttr( aPrefix, pData->Namespace, aLName, pData->Value ) )
208  return;
209  }
210  }
211  else
212  {
213  if( pData->Namespace.isEmpty() )
214  {
215  if( mpContainer->AddAttr( aName, pData->Value ) )
216  return;
217  }
218  }
219 }
220 
221 void SAL_CALL SvUnoAttributeContainer::removeByName(const OUString& Name)
222 {
223  sal_uInt16 nAttr = getIndexByName(Name);
224  if( nAttr == USHRT_MAX )
225  throw container::NoSuchElementException();
226 
227  mpContainer->Remove( nAttr );
228 }
229 
230 //XServiceInfo
232 {
233  return "SvUnoAttributeContainer";
234 }
235 
237 {
238  return { "com.sun.star.xml.AttributeContainer" };
239 }
240 
242 {
243  return cppu::supportsService(this, ServiceName);
244 }
245 
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: unoatrcn.cxx:120
std::unique_ptr< ContentProperties > pData
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: unoatrcn.cxx:139
virtual sal_Int64 SAL_CALL getSomething(const css::uno::Sequence< sal_Int8 > &aIdentifier) override
Definition: unoatrcn.cxx:95
std::unique_ptr< SvXMLAttrContainerData > mpContainer
Definition: unoatrcn.hxx:46
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId() noexcept
Definition: unoatrcn.cxx:89
virtual void SAL_CALL removeByName(const OUString &Name) override
Definition: unoatrcn.cxx:221
constexpr OUStringLiteral aData
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: unoatrcn.cxx:105
uno::Reference< uno::XInterface > SvUnoAttributeContainer_CreateInstance()
Definition: unoatrcn.cxx:36
virtual void SAL_CALL insertByName(const OUString &aName, const css::uno::Any &aElement) override
Definition: unoatrcn.cxx:184
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: unoatrcn.cxx:241
SAL_DLLPRIVATE sal_uInt16 getIndexByName(const OUString &aName) const
Definition: unoatrcn.cxx:59
unsigned char sal_Bool
css::uno::Type const & get()
virtual OUString SAL_CALL getImplementationName() override
Definition: unoatrcn.cxx:231
virtual void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
Definition: unoatrcn.cxx:145
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
SvUnoAttributeContainer(std::unique_ptr< SvXMLAttrContainerData > pContainer=nullptr)
Definition: unoatrcn.cxx:41
virtual css::uno::Type SAL_CALL getElementType() override
Definition: unoatrcn.cxx:49
virtual sal_Bool SAL_CALL hasElements() override
Definition: unoatrcn.cxx:54
const css::uno::Sequence< sal_Int8 > & getSeq() const
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: unoatrcn.cxx:236
sal_uInt16 nPos