Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / xmloff / source / core / unoatrcn.cxx
blobed811df6615740f99f799153ed8ad10ab8675630
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 <string.h>
21 #include <com/sun/star/xml/AttributeData.hpp>
22 #include <rtl/ustrbuf.hxx>
23 #include <comphelper/servicehelper.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <limits.h>
27 #include <xmloff/xmlcnimp.hxx>
29 #include <xmloff/unoatrcn.hxx>
31 using namespace ::com::sun::star;
33 // Interface implementation
35 uno::Reference< uno::XInterface > SvUnoAttributeContainer_CreateInstance()
37 return *(new SvUnoAttributeContainer);
40 SvUnoAttributeContainer::SvUnoAttributeContainer( SvXMLAttrContainerData* pContainer)
41 : mpContainer( pContainer )
43 if( mpContainer == nullptr )
44 mpContainer = new SvXMLAttrContainerData;
47 SvUnoAttributeContainer::~SvUnoAttributeContainer()
49 delete mpContainer;
52 // container::XElementAccess
53 uno::Type SAL_CALL SvUnoAttributeContainer::getElementType()
54 throw( uno::RuntimeException, std::exception )
56 return cppu::UnoType<xml::AttributeData>::get();
59 sal_Bool SAL_CALL SvUnoAttributeContainer::hasElements()
60 throw( uno::RuntimeException, std::exception )
62 return mpContainer->GetAttrCount() != 0;
65 sal_uInt16 SvUnoAttributeContainer::getIndexByName(const OUString& aName ) const
67 const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
69 sal_Int32 nPos = aName.indexOf( ':' );
70 if( nPos == -1L )
72 for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
74 if( mpContainer->GetAttrLName(nAttr) == aName &&
75 mpContainer->GetAttrPrefix(nAttr).isEmpty() )
76 return nAttr;
79 else
81 const OUString aPrefix( aName.copy( 0L, nPos ) );
82 const OUString aLName( aName.copy( nPos+1L ) );
84 for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
86 if( mpContainer->GetAttrLName(nAttr) == aLName &&
87 mpContainer->GetAttrPrefix(nAttr) == aPrefix )
88 return nAttr;
92 return USHRT_MAX;
95 namespace
97 class theSvUnoAttributeContainerUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSvUnoAttributeContainerUnoTunnelId> {};
100 const css::uno::Sequence< sal_Int8 > & SvUnoAttributeContainer::getUnoTunnelId() throw()
102 return theSvUnoAttributeContainerUnoTunnelId::get().getSeq();
105 sal_Int64 SAL_CALL SvUnoAttributeContainer::getSomething( const css::uno::Sequence< sal_Int8 >& rId ) throw(css::uno::RuntimeException, std::exception)
107 if( rId.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(),
108 rId.getConstArray(), 16 ) )
110 return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
112 return 0;
115 // container::XNameAccess
116 uno::Any SAL_CALL SvUnoAttributeContainer::getByName(const OUString& aName)
117 throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception )
119 sal_uInt16 nAttr = getIndexByName(aName );
121 if( nAttr == USHRT_MAX )
122 throw container::NoSuchElementException();
124 xml::AttributeData aData;
125 aData.Namespace = mpContainer->GetAttrNamespace(nAttr);
126 aData.Type = "CDATA";
127 aData.Value = mpContainer->GetAttrValue(nAttr);
129 return uno::Any(aData);
132 uno::Sequence< OUString > SAL_CALL SvUnoAttributeContainer::getElementNames() throw( uno::RuntimeException, std::exception )
134 const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
136 uno::Sequence< OUString > aElementNames( (sal_Int32)nAttrCount );
137 OUString *pNames = aElementNames.getArray();
139 for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
141 OUStringBuffer sBuffer( mpContainer->GetAttrPrefix(nAttr) );
142 if( !sBuffer.isEmpty() )
143 sBuffer.append( ':' );
144 sBuffer.append( mpContainer->GetAttrLName(nAttr) );
145 *pNames++ = sBuffer.makeStringAndClear();
148 return aElementNames;
151 sal_Bool SAL_CALL SvUnoAttributeContainer::hasByName(const OUString& aName) throw( uno::RuntimeException, std::exception )
153 return getIndexByName(aName ) != USHRT_MAX;
156 // container::XNameReplace
157 void SAL_CALL SvUnoAttributeContainer::replaceByName(const OUString& aName, const uno::Any& aElement)
158 throw( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception )
160 if( aElement.hasValue() && aElement.getValueType() == cppu::UnoType<xml::AttributeData>::get())
162 sal_uInt16 nAttr = getIndexByName(aName );
163 if( nAttr == USHRT_MAX )
164 throw container::NoSuchElementException();
166 xml::AttributeData const * pData = static_cast<xml::AttributeData const *>(aElement.getValue());
168 sal_Int32 nPos = aName.indexOf( ':' );
169 if( nPos != -1L )
171 const OUString aPrefix( aName.copy( 0L, nPos ));
172 const OUString aLName( aName.copy( nPos+1L ));
174 if( pData->Namespace.isEmpty() )
176 if( mpContainer->SetAt( nAttr, aPrefix, aLName, pData->Value ) )
177 return;
179 else
181 if( mpContainer->SetAt( nAttr, aPrefix, pData->Namespace, aLName, pData->Value ) )
182 return;
185 else
187 if( pData->Namespace.isEmpty() )
189 if( mpContainer->SetAt( nAttr, aName, pData->Value ) )
190 return;
195 throw lang::IllegalArgumentException();
198 // container::XNameContainer
199 void SAL_CALL SvUnoAttributeContainer::insertByName(const OUString& aName, const uno::Any& aElement)
200 throw( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException, std::exception )
202 if( !aElement.hasValue() || aElement.getValueType() != cppu::UnoType<xml::AttributeData>::get())
203 throw lang::IllegalArgumentException();
205 sal_uInt16 nAttr = getIndexByName(aName );
206 if( nAttr != USHRT_MAX )
207 throw container::ElementExistException();
209 xml::AttributeData const * pData = static_cast<xml::AttributeData const *>(aElement.getValue());
211 sal_Int32 nPos = aName.indexOf( ':' );
212 if( nPos != -1L )
214 const OUString aPrefix( aName.copy( 0L, nPos ));
215 const OUString aLName( aName.copy( nPos+1L ));
217 if( pData->Namespace.isEmpty() )
219 if( mpContainer->AddAttr( aPrefix, aLName, pData->Value ) )
220 return;
222 else
224 if( mpContainer->AddAttr( aPrefix, pData->Namespace, aLName, pData->Value ) )
225 return;
228 else
230 if( pData->Namespace.isEmpty() )
232 if( mpContainer->AddAttr( aName, pData->Value ) )
233 return;
238 void SAL_CALL SvUnoAttributeContainer::removeByName(const OUString& Name)
239 throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception )
241 sal_uInt16 nAttr = getIndexByName(Name);
242 if( nAttr == USHRT_MAX )
243 throw container::NoSuchElementException();
245 mpContainer->Remove( nAttr );
248 //XServiceInfo
249 OUString SAL_CALL SvUnoAttributeContainer::getImplementationName() throw( uno::RuntimeException, std::exception )
251 return OUString( "SvUnoAttributeContainer" );
254 uno::Sequence< OUString > SvUnoAttributeContainer::getSupportedServiceNames()
255 throw( uno::RuntimeException, std::exception )
257 OUString aSN( "com.sun.star.xml.AttributeContainer" );
258 uno::Sequence< OUString > aNS( &aSN, 1L );
259 return aNS;
262 sal_Bool SvUnoAttributeContainer::supportsService(const OUString& ServiceName)
263 throw( uno::RuntimeException, std::exception )
265 return cppu::supportsService(this, ServiceName);
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */