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 <o3tl/any.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include <comphelper/servicehelper.hxx>
25 #include <cppuhelper/supportsservice.hxx>
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
) )
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( ':' );
66 for( sal_uInt16 nAttr
= 0; nAttr
< nAttrCount
; nAttr
++ )
68 if( mpContainer
->GetAttrLName(nAttr
) == aName
&&
69 mpContainer
->GetAttrPrefix(nAttr
).isEmpty() )
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
)
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));
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( ':' );
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
) )
170 if( mpContainer
->SetAt( nAttr
, aPrefix
, pData
->Namespace
, aLName
, pData
->Value
) )
176 if( pData
->Namespace
.isEmpty() )
178 if( mpContainer
->SetAt( nAttr
, aName
, pData
->Value
) )
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
);
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( ':' );
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
) )
211 if( mpContainer
->AddAttr( aPrefix
, pData
->Namespace
, aLName
, pData
->Value
) )
217 if( pData
->Namespace
.isEmpty() )
219 if( mpContainer
->AddAttr( aName
, pData
->Value
) )
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
);
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: */