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>
26 #include <xmloff/xmlcnimp.hxx>
28 #include "xmloff/unoatrcn.hxx"
31 using namespace ::com::sun::star
;
33 // --------------------------------------------------------------------
34 // Interface implementation
35 // --------------------------------------------------------------------
37 uno::Reference
< uno::XInterface
> SvUnoAttributeContainer_CreateInstance()
39 return *(new SvUnoAttributeContainer
);
42 SvUnoAttributeContainer::SvUnoAttributeContainer( SvXMLAttrContainerData
* pContainer
)
43 : mpContainer( pContainer
)
45 if( mpContainer
== NULL
)
46 mpContainer
= new SvXMLAttrContainerData
;
49 SvUnoAttributeContainer::~SvUnoAttributeContainer()
54 // container::XElementAccess
55 uno::Type SAL_CALL
SvUnoAttributeContainer::getElementType(void)
56 throw( uno::RuntimeException
)
58 return ::getCppuType((const xml::AttributeData
*)0);
61 sal_Bool SAL_CALL
SvUnoAttributeContainer::hasElements(void)
62 throw( uno::RuntimeException
)
64 return mpContainer
->GetAttrCount() != 0;
67 sal_uInt16
SvUnoAttributeContainer::getIndexByName(const OUString
& aName
) const
69 const sal_uInt16 nAttrCount
= mpContainer
->GetAttrCount();
71 sal_Int32 nPos
= aName
.indexOf( sal_Unicode(':') );
74 for( sal_uInt16 nAttr
= 0; nAttr
< nAttrCount
; nAttr
++ )
76 if( mpContainer
->GetAttrLName(nAttr
) == aName
&&
77 mpContainer
->GetAttrPrefix(nAttr
).isEmpty() )
83 const OUString
aPrefix( aName
.copy( 0L, nPos
) );
84 const OUString
aLName( aName
.copy( nPos
+1L ) );
86 for( sal_uInt16 nAttr
= 0; nAttr
< nAttrCount
; nAttr
++ )
88 if( mpContainer
->GetAttrLName(nAttr
) == aLName
&&
89 mpContainer
->GetAttrPrefix(nAttr
) == aPrefix
)
99 class theSvUnoAttributeContainerUnoTunnelId
: public rtl::Static
< UnoTunnelIdInit
, theSvUnoAttributeContainerUnoTunnelId
> {};
102 const ::com::sun::star::uno::Sequence
< sal_Int8
> & SvUnoAttributeContainer::getUnoTunnelId() throw()
104 return theSvUnoAttributeContainerUnoTunnelId::get().getSeq();
107 sal_Int64 SAL_CALL
SvUnoAttributeContainer::getSomething( const ::com::sun::star::uno::Sequence
< sal_Int8
>& rId
) throw(::com::sun::star::uno::RuntimeException
)
109 if( rId
.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(),
110 rId
.getConstArray(), 16 ) )
112 return sal::static_int_cast
<sal_Int64
>(reinterpret_cast<sal_uIntPtr
>(this));
117 // container::XNameAccess
118 uno::Any SAL_CALL
SvUnoAttributeContainer::getByName(const OUString
& aName
)
119 throw( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
121 sal_uInt16 nAttr
= getIndexByName(aName
);
123 if( nAttr
== USHRT_MAX
)
124 throw container::NoSuchElementException();
126 xml::AttributeData aData
;
127 aData
.Namespace
= mpContainer
->GetAttrNamespace(nAttr
);
128 aData
.Type
= OUString("CDATA");
129 aData
.Value
= mpContainer
->GetAttrValue(nAttr
);
136 uno::Sequence
< OUString
> SAL_CALL
SvUnoAttributeContainer::getElementNames(void) throw( uno::RuntimeException
)
138 const sal_uInt16 nAttrCount
= mpContainer
->GetAttrCount();
140 uno::Sequence
< OUString
> aElementNames( (sal_Int32
)nAttrCount
);
141 OUString
*pNames
= aElementNames
.getArray();
143 for( sal_uInt16 nAttr
= 0; nAttr
< nAttrCount
; nAttr
++ )
145 OUStringBuffer
sBuffer( mpContainer
->GetAttrPrefix(nAttr
) );
146 if( sBuffer
.getLength() != 0L )
147 sBuffer
.append( (sal_Unicode
)':' );
148 sBuffer
.append( mpContainer
->GetAttrLName(nAttr
) );
149 *pNames
++ = sBuffer
.makeStringAndClear();
152 return aElementNames
;
155 sal_Bool SAL_CALL
SvUnoAttributeContainer::hasByName(const OUString
& aName
) throw( uno::RuntimeException
)
157 return getIndexByName(aName
) != USHRT_MAX
;
160 // container::XNameReplace
161 void SAL_CALL
SvUnoAttributeContainer::replaceByName(const OUString
& aName
, const uno::Any
& aElement
)
162 throw( lang::IllegalArgumentException
, container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
164 if( aElement
.hasValue() && aElement
.getValueType() == ::getCppuType((const xml::AttributeData
*)0) )
166 sal_uInt16 nAttr
= getIndexByName(aName
);
167 if( nAttr
== USHRT_MAX
)
168 throw container::NoSuchElementException();
170 xml::AttributeData
* pData
= (xml::AttributeData
*)aElement
.getValue();
172 sal_Int32 nPos
= aName
.indexOf( sal_Unicode(':') );
175 const OUString
aPrefix( aName
.copy( 0L, nPos
));
176 const OUString
aLName( aName
.copy( nPos
+1L ));
178 if( pData
->Namespace
.isEmpty() )
180 if( mpContainer
->SetAt( nAttr
, aPrefix
, aLName
, pData
->Value
) )
185 if( mpContainer
->SetAt( nAttr
, aPrefix
, pData
->Namespace
, aLName
, pData
->Value
) )
191 if( pData
->Namespace
.isEmpty() )
193 if( mpContainer
->SetAt( nAttr
, aName
, pData
->Value
) )
199 throw lang::IllegalArgumentException();
202 // container::XNameContainer
203 void SAL_CALL
SvUnoAttributeContainer::insertByName(const OUString
& aName
, const uno::Any
& aElement
)
204 throw( lang::IllegalArgumentException
, container::ElementExistException
, lang::WrappedTargetException
, uno::RuntimeException
)
206 if( !aElement
.hasValue() || aElement
.getValueType() != ::getCppuType((const xml::AttributeData
*)0) )
207 throw lang::IllegalArgumentException();
209 sal_uInt16 nAttr
= getIndexByName(aName
);
210 if( nAttr
!= USHRT_MAX
)
211 throw container::ElementExistException();
213 xml::AttributeData
* pData
= (xml::AttributeData
*)aElement
.getValue();
215 sal_Int32 nPos
= aName
.indexOf( sal_Unicode(':') );
218 const OUString
aPrefix( aName
.copy( 0L, nPos
));
219 const OUString
aLName( aName
.copy( nPos
+1L ));
221 if( pData
->Namespace
.isEmpty() )
223 if( mpContainer
->AddAttr( aPrefix
, aLName
, pData
->Value
) )
228 if( mpContainer
->AddAttr( aPrefix
, pData
->Namespace
, aLName
, pData
->Value
) )
234 if( pData
->Namespace
.isEmpty() )
236 if( mpContainer
->AddAttr( aName
, pData
->Value
) )
242 void SAL_CALL
SvUnoAttributeContainer::removeByName(const OUString
& Name
)
243 throw( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
245 sal_uInt16 nAttr
= getIndexByName(Name
);
246 if( nAttr
== USHRT_MAX
)
247 throw container::NoSuchElementException();
249 mpContainer
->Remove( nAttr
);
253 OUString SAL_CALL
SvUnoAttributeContainer::getImplementationName(void) throw( uno::RuntimeException
)
255 return OUString( "SvUnoAttributeContainer" );
258 uno::Sequence
< OUString
> SvUnoAttributeContainer::getSupportedServiceNames(void)
259 throw( uno::RuntimeException
)
261 OUString
aSN( OUString( "com.sun.star.xml.AttributeContainer" ) );
262 uno::Sequence
< OUString
> aNS( &aSN
, 1L );
266 sal_Bool
SvUnoAttributeContainer::supportsService(const OUString
& ServiceName
)
267 throw( uno::RuntimeException
)
269 const uno::Sequence
< OUString
> aServiceNames( getSupportedServiceNames() );
270 const OUString
* pNames
= aServiceNames
.getConstArray();
271 sal_Int32 nCount
= aServiceNames
.getLength();
274 if( *pNames
++ == ServiceName
)
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */