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
== NULL
)
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 ::com::sun::star::uno::Sequence
< sal_Int8
> & SvUnoAttributeContainer::getUnoTunnelId() throw()
102 return theSvUnoAttributeContainerUnoTunnelId::get().getSeq();
105 sal_Int64 SAL_CALL
SvUnoAttributeContainer::getSomething( const ::com::sun::star::uno::Sequence
< sal_Int8
>& rId
) throw(::com::sun::star::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
);
134 uno::Sequence
< OUString
> SAL_CALL
SvUnoAttributeContainer::getElementNames() throw( uno::RuntimeException
, std::exception
)
136 const sal_uInt16 nAttrCount
= mpContainer
->GetAttrCount();
138 uno::Sequence
< OUString
> aElementNames( (sal_Int32
)nAttrCount
);
139 OUString
*pNames
= aElementNames
.getArray();
141 for( sal_uInt16 nAttr
= 0; nAttr
< nAttrCount
; nAttr
++ )
143 OUStringBuffer
sBuffer( mpContainer
->GetAttrPrefix(nAttr
) );
144 if( !sBuffer
.isEmpty() )
145 sBuffer
.append( ':' );
146 sBuffer
.append( mpContainer
->GetAttrLName(nAttr
) );
147 *pNames
++ = sBuffer
.makeStringAndClear();
150 return aElementNames
;
153 sal_Bool SAL_CALL
SvUnoAttributeContainer::hasByName(const OUString
& aName
) throw( uno::RuntimeException
, std::exception
)
155 return getIndexByName(aName
) != USHRT_MAX
;
158 // container::XNameReplace
159 void SAL_CALL
SvUnoAttributeContainer::replaceByName(const OUString
& aName
, const uno::Any
& aElement
)
160 throw( lang::IllegalArgumentException
, container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
162 if( aElement
.hasValue() && aElement
.getValueType() == cppu::UnoType
<xml::AttributeData
>::get())
164 sal_uInt16 nAttr
= getIndexByName(aName
);
165 if( nAttr
== USHRT_MAX
)
166 throw container::NoSuchElementException();
168 xml::AttributeData
const * pData
= static_cast<xml::AttributeData
const *>(aElement
.getValue());
170 sal_Int32 nPos
= aName
.indexOf( ':' );
173 const OUString
aPrefix( aName
.copy( 0L, nPos
));
174 const OUString
aLName( aName
.copy( nPos
+1L ));
176 if( pData
->Namespace
.isEmpty() )
178 if( mpContainer
->SetAt( nAttr
, aPrefix
, aLName
, pData
->Value
) )
183 if( mpContainer
->SetAt( nAttr
, aPrefix
, pData
->Namespace
, aLName
, pData
->Value
) )
189 if( pData
->Namespace
.isEmpty() )
191 if( mpContainer
->SetAt( nAttr
, aName
, pData
->Value
) )
197 throw lang::IllegalArgumentException();
200 // container::XNameContainer
201 void SAL_CALL
SvUnoAttributeContainer::insertByName(const OUString
& aName
, const uno::Any
& aElement
)
202 throw( lang::IllegalArgumentException
, container::ElementExistException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
204 if( !aElement
.hasValue() || aElement
.getValueType() != cppu::UnoType
<xml::AttributeData
>::get())
205 throw lang::IllegalArgumentException();
207 sal_uInt16 nAttr
= getIndexByName(aName
);
208 if( nAttr
!= USHRT_MAX
)
209 throw container::ElementExistException();
211 xml::AttributeData
const * pData
= static_cast<xml::AttributeData
const *>(aElement
.getValue());
213 sal_Int32 nPos
= aName
.indexOf( ':' );
216 const OUString
aPrefix( aName
.copy( 0L, nPos
));
217 const OUString
aLName( aName
.copy( nPos
+1L ));
219 if( pData
->Namespace
.isEmpty() )
221 if( mpContainer
->AddAttr( aPrefix
, aLName
, pData
->Value
) )
226 if( mpContainer
->AddAttr( aPrefix
, pData
->Namespace
, aLName
, pData
->Value
) )
232 if( pData
->Namespace
.isEmpty() )
234 if( mpContainer
->AddAttr( aName
, pData
->Value
) )
240 void SAL_CALL
SvUnoAttributeContainer::removeByName(const OUString
& Name
)
241 throw( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
243 sal_uInt16 nAttr
= getIndexByName(Name
);
244 if( nAttr
== USHRT_MAX
)
245 throw container::NoSuchElementException();
247 mpContainer
->Remove( nAttr
);
251 OUString SAL_CALL
SvUnoAttributeContainer::getImplementationName() throw( uno::RuntimeException
, std::exception
)
253 return OUString( "SvUnoAttributeContainer" );
256 uno::Sequence
< OUString
> SvUnoAttributeContainer::getSupportedServiceNames()
257 throw( uno::RuntimeException
, std::exception
)
259 OUString
aSN( "com.sun.star.xml.AttributeContainer" );
260 uno::Sequence
< OUString
> aNS( &aSN
, 1L );
264 sal_Bool
SvUnoAttributeContainer::supportsService(const OUString
& ServiceName
)
265 throw( uno::RuntimeException
, std::exception
)
267 return cppu::supportsService(this, ServiceName
);
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */