1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <com/sun/star/xml/AttributeData.hpp>
22 #include <rtl/ustrbuf.hxx>
23 #include <comphelper/servicehelper.hxx>
24 #include <cppuhelper/supportsservice.hxx>
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()
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( ':' );
72 for( sal_uInt16 nAttr
= 0; nAttr
< nAttrCount
; nAttr
++ )
74 if( mpContainer
->GetAttrLName(nAttr
) == aName
&&
75 mpContainer
->GetAttrPrefix(nAttr
).isEmpty() )
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
)
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));
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( ':' );
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
) )
181 if( mpContainer
->SetAt( nAttr
, aPrefix
, pData
->Namespace
, aLName
, pData
->Value
) )
187 if( pData
->Namespace
.isEmpty() )
189 if( mpContainer
->SetAt( nAttr
, aName
, pData
->Value
) )
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( ':' );
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
) )
224 if( mpContainer
->AddAttr( aPrefix
, pData
->Namespace
, aLName
, pData
->Value
) )
230 if( pData
->Namespace
.isEmpty() )
232 if( mpContainer
->AddAttr( aName
, pData
->Value
) )
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
);
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 );
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: */