nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / core / unoatrcn.cxx
blob81ec1e93ef00bcc10424e13d3098a53c4e8ddf75
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <memory>
21 #include <com/sun/star/xml/AttributeData.hpp>
22 #include <o3tl/any.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include <comphelper/servicehelper.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <limits.h>
28 #include <xmloff/xmlcnimp.hxx>
30 #include <xmloff/unoatrcn.hxx>
32 using namespace ::com::sun::star;
34 // Interface implementation
36 uno::Reference< uno::XInterface > SvUnoAttributeContainer_CreateInstance()
38 return *(new SvUnoAttributeContainer);
41 SvUnoAttributeContainer::SvUnoAttributeContainer( std::unique_ptr<SvXMLAttrContainerData> pContainer)
42 : mpContainer( std::move( pContainer ) )
44 if( !mpContainer )
45 mpContainer = std::make_unique<SvXMLAttrContainerData>();
48 // container::XElementAccess
49 uno::Type SAL_CALL SvUnoAttributeContainer::getElementType()
51 return cppu::UnoType<xml::AttributeData>::get();
54 sal_Bool SAL_CALL SvUnoAttributeContainer::hasElements()
56 return mpContainer->GetAttrCount() != 0;
59 sal_uInt16 SvUnoAttributeContainer::getIndexByName(const OUString& aName ) const
61 const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
63 sal_Int32 nPos = aName.indexOf( ':' );
64 if( nPos == -1 )
66 for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
68 if( mpContainer->GetAttrLName(nAttr) == aName &&
69 mpContainer->GetAttrPrefix(nAttr).isEmpty() )
70 return nAttr;
73 else
75 const OUString aPrefix( aName.copy( 0L, nPos ) );
76 const OUString aLName( aName.copy( nPos+1 ) );
78 for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
80 if( mpContainer->GetAttrLName(nAttr) == aLName &&
81 mpContainer->GetAttrPrefix(nAttr) == aPrefix )
82 return nAttr;
86 return USHRT_MAX;
89 namespace
91 class theSvUnoAttributeContainerUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSvUnoAttributeContainerUnoTunnelId> {};
94 const css::uno::Sequence< sal_Int8 > & SvUnoAttributeContainer::getUnoTunnelId() throw()
96 return theSvUnoAttributeContainerUnoTunnelId::get().getSeq();
99 sal_Int64 SAL_CALL SvUnoAttributeContainer::getSomething( const css::uno::Sequence< sal_Int8 >& rId )
101 if( isUnoTunnelId<SvUnoAttributeContainer>(rId) )
103 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
105 return 0;
108 // container::XNameAccess
109 uno::Any SAL_CALL SvUnoAttributeContainer::getByName(const OUString& aName)
111 sal_uInt16 nAttr = getIndexByName(aName );
113 if( nAttr == USHRT_MAX )
114 throw container::NoSuchElementException();
116 xml::AttributeData aData;
117 aData.Namespace = mpContainer->GetAttrNamespace(nAttr);
118 aData.Type = "CDATA";
119 aData.Value = mpContainer->GetAttrValue(nAttr);
121 return uno::Any(aData);
124 uno::Sequence< OUString > SAL_CALL SvUnoAttributeContainer::getElementNames()
126 const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
128 uno::Sequence< OUString > aElementNames( static_cast<sal_Int32>(nAttrCount) );
129 OUString *pNames = aElementNames.getArray();
131 for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
133 OUStringBuffer sBuffer( mpContainer->GetAttrPrefix(nAttr) );
134 if( !sBuffer.isEmpty() )
135 sBuffer.append( ':' );
136 sBuffer.append( mpContainer->GetAttrLName(nAttr) );
137 *pNames++ = sBuffer.makeStringAndClear();
140 return aElementNames;
143 sal_Bool SAL_CALL SvUnoAttributeContainer::hasByName(const OUString& aName)
145 return getIndexByName(aName ) != USHRT_MAX;
148 // container::XNameReplace
149 void SAL_CALL SvUnoAttributeContainer::replaceByName(const OUString& aName, const uno::Any& aElement)
151 if( auto pData = o3tl::tryAccess<xml::AttributeData>(aElement) )
153 sal_uInt16 nAttr = getIndexByName(aName );
154 if( nAttr == USHRT_MAX )
155 throw container::NoSuchElementException();
157 sal_Int32 nPos = aName.indexOf( ':' );
158 if( nPos != -1 )
160 const OUString aPrefix( aName.copy( 0L, nPos ));
161 const OUString aLName( aName.copy( nPos+1 ));
163 if( pData->Namespace.isEmpty() )
165 if( mpContainer->SetAt( nAttr, aPrefix, aLName, pData->Value ) )
166 return;
168 else
170 if( mpContainer->SetAt( nAttr, aPrefix, pData->Namespace, aLName, pData->Value ) )
171 return;
174 else
176 if( pData->Namespace.isEmpty() )
178 if( mpContainer->SetAt( nAttr, aName, pData->Value ) )
179 return;
184 throw lang::IllegalArgumentException();
187 // container::XNameContainer
188 void SAL_CALL SvUnoAttributeContainer::insertByName(const OUString& aName, const uno::Any& aElement)
190 auto pData = o3tl::tryAccess<xml::AttributeData>(aElement);
191 if( !pData )
192 throw lang::IllegalArgumentException();
194 sal_uInt16 nAttr = getIndexByName(aName );
195 if( nAttr != USHRT_MAX )
196 throw container::ElementExistException();
198 sal_Int32 nPos = aName.indexOf( ':' );
199 if( nPos != -1 )
201 const OUString aPrefix( aName.copy( 0L, nPos ));
202 const OUString aLName( aName.copy( nPos+1 ));
204 if( pData->Namespace.isEmpty() )
206 if( mpContainer->AddAttr( aPrefix, aLName, pData->Value ) )
207 return;
209 else
211 if( mpContainer->AddAttr( aPrefix, pData->Namespace, aLName, pData->Value ) )
212 return;
215 else
217 if( pData->Namespace.isEmpty() )
219 if( mpContainer->AddAttr( aName, pData->Value ) )
220 return;
225 void SAL_CALL SvUnoAttributeContainer::removeByName(const OUString& Name)
227 sal_uInt16 nAttr = getIndexByName(Name);
228 if( nAttr == USHRT_MAX )
229 throw container::NoSuchElementException();
231 mpContainer->Remove( nAttr );
234 //XServiceInfo
235 OUString SAL_CALL SvUnoAttributeContainer::getImplementationName()
237 return "SvUnoAttributeContainer";
240 uno::Sequence< OUString > SvUnoAttributeContainer::getSupportedServiceNames()
242 return { "com.sun.star.xml.AttributeContainer" };
245 sal_Bool SvUnoAttributeContainer::supportsService(const OUString& ServiceName)
247 return cppu::supportsService(this, ServiceName);
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */