Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <string.h>
21 : #include <com/sun/star/xml/AttributeData.hpp>
22 : #include <rtl/ustrbuf.hxx>
23 : #include <comphelper/servicehelper.hxx>
24 : #include <limits.h>
25 :
26 : #include <xmloff/xmlcnimp.hxx>
27 :
28 : #include "xmloff/unoatrcn.hxx"
29 :
30 :
31 : using namespace ::com::sun::star;
32 :
33 : // --------------------------------------------------------------------
34 : // Interface implementation
35 : // --------------------------------------------------------------------
36 :
37 0 : uno::Reference< uno::XInterface > SvUnoAttributeContainer_CreateInstance()
38 : {
39 0 : return *(new SvUnoAttributeContainer);
40 : }
41 :
42 282 : SvUnoAttributeContainer::SvUnoAttributeContainer( SvXMLAttrContainerData* pContainer)
43 282 : : mpContainer( pContainer )
44 : {
45 282 : if( mpContainer == NULL )
46 0 : mpContainer = new SvXMLAttrContainerData;
47 282 : }
48 :
49 846 : SvUnoAttributeContainer::~SvUnoAttributeContainer()
50 : {
51 282 : delete mpContainer;
52 564 : }
53 :
54 : // container::XElementAccess
55 0 : uno::Type SAL_CALL SvUnoAttributeContainer::getElementType(void)
56 : throw( uno::RuntimeException )
57 : {
58 0 : return ::getCppuType((const xml::AttributeData*)0);
59 : }
60 :
61 0 : sal_Bool SAL_CALL SvUnoAttributeContainer::hasElements(void)
62 : throw( uno::RuntimeException )
63 : {
64 0 : return mpContainer->GetAttrCount() != 0;
65 : }
66 :
67 67 : sal_uInt16 SvUnoAttributeContainer::getIndexByName(const OUString& aName ) const
68 : {
69 67 : const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
70 :
71 67 : sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
72 67 : if( nPos == -1L )
73 : {
74 59 : for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
75 : {
76 168 : if( mpContainer->GetAttrLName(nAttr) == aName &&
77 152 : mpContainer->GetAttrPrefix(nAttr).isEmpty() )
78 36 : return nAttr;
79 : }
80 : }
81 : else
82 : {
83 16 : const OUString aPrefix( aName.copy( 0L, nPos ) );
84 24 : const OUString aLName( aName.copy( nPos+1L ) );
85 :
86 16 : for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
87 : {
88 32 : if( mpContainer->GetAttrLName(nAttr) == aLName &&
89 32 : mpContainer->GetAttrPrefix(nAttr) == aPrefix )
90 8 : return nAttr;
91 8 : }
92 : }
93 :
94 23 : return USHRT_MAX;
95 : }
96 :
97 : namespace
98 : {
99 : class theSvUnoAttributeContainerUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSvUnoAttributeContainerUnoTunnelId> {};
100 : }
101 :
102 30 : const ::com::sun::star::uno::Sequence< sal_Int8 > & SvUnoAttributeContainer::getUnoTunnelId() throw()
103 : {
104 30 : return theSvUnoAttributeContainerUnoTunnelId::get().getSeq();
105 : }
106 :
107 15 : sal_Int64 SAL_CALL SvUnoAttributeContainer::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rId ) throw(::com::sun::star::uno::RuntimeException)
108 : {
109 45 : if( rId.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(),
110 30 : rId.getConstArray(), 16 ) )
111 : {
112 15 : return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
113 : }
114 0 : return 0;
115 : }
116 :
117 : // container::XNameAccess
118 22 : uno::Any SAL_CALL SvUnoAttributeContainer::getByName(const OUString& aName)
119 : throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
120 : {
121 22 : sal_uInt16 nAttr = getIndexByName(aName );
122 :
123 22 : if( nAttr == USHRT_MAX )
124 0 : throw container::NoSuchElementException();
125 :
126 22 : xml::AttributeData aData;
127 22 : aData.Namespace = mpContainer->GetAttrNamespace(nAttr);
128 22 : aData.Type = OUString("CDATA");
129 22 : aData.Value = mpContainer->GetAttrValue(nAttr);
130 :
131 22 : uno::Any aAny;
132 22 : aAny <<= aData;
133 22 : return aAny;
134 : }
135 :
136 40 : uno::Sequence< OUString > SAL_CALL SvUnoAttributeContainer::getElementNames(void) throw( uno::RuntimeException )
137 : {
138 40 : const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
139 :
140 40 : uno::Sequence< OUString > aElementNames( (sal_Int32)nAttrCount );
141 40 : OUString *pNames = aElementNames.getArray();
142 :
143 56 : for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
144 : {
145 16 : OUStringBuffer sBuffer( mpContainer->GetAttrPrefix(nAttr) );
146 16 : if( !sBuffer.isEmpty() )
147 16 : sBuffer.append( (sal_Unicode)':' );
148 16 : sBuffer.append( mpContainer->GetAttrLName(nAttr) );
149 16 : *pNames++ = sBuffer.makeStringAndClear();
150 16 : }
151 :
152 40 : return aElementNames;
153 : }
154 :
155 28 : sal_Bool SAL_CALL SvUnoAttributeContainer::hasByName(const OUString& aName) throw( uno::RuntimeException )
156 : {
157 28 : return getIndexByName(aName ) != USHRT_MAX;
158 : }
159 :
160 : // container::XNameReplace
161 0 : void SAL_CALL SvUnoAttributeContainer::replaceByName(const OUString& aName, const uno::Any& aElement)
162 : throw( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
163 : {
164 0 : if( aElement.hasValue() && aElement.getValueType() == ::getCppuType((const xml::AttributeData*)0) )
165 : {
166 0 : sal_uInt16 nAttr = getIndexByName(aName );
167 0 : if( nAttr == USHRT_MAX )
168 0 : throw container::NoSuchElementException();
169 :
170 0 : xml::AttributeData* pData = (xml::AttributeData*)aElement.getValue();
171 :
172 0 : sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
173 0 : if( nPos != -1L )
174 : {
175 0 : const OUString aPrefix( aName.copy( 0L, nPos ));
176 0 : const OUString aLName( aName.copy( nPos+1L ));
177 :
178 0 : if( pData->Namespace.isEmpty() )
179 : {
180 0 : if( mpContainer->SetAt( nAttr, aPrefix, aLName, pData->Value ) )
181 0 : return;
182 : }
183 : else
184 : {
185 0 : if( mpContainer->SetAt( nAttr, aPrefix, pData->Namespace, aLName, pData->Value ) )
186 0 : return;
187 0 : }
188 : }
189 : else
190 : {
191 0 : if( pData->Namespace.isEmpty() )
192 : {
193 0 : if( mpContainer->SetAt( nAttr, aName, pData->Value ) )
194 0 : return;
195 : }
196 : }
197 : }
198 :
199 0 : throw lang::IllegalArgumentException();
200 : }
201 :
202 : // container::XNameContainer
203 13 : void SAL_CALL SvUnoAttributeContainer::insertByName(const OUString& aName, const uno::Any& aElement)
204 : throw( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException )
205 : {
206 13 : if( !aElement.hasValue() || aElement.getValueType() != ::getCppuType((const xml::AttributeData*)0) )
207 0 : throw lang::IllegalArgumentException();
208 :
209 13 : sal_uInt16 nAttr = getIndexByName(aName );
210 13 : if( nAttr != USHRT_MAX )
211 0 : throw container::ElementExistException();
212 :
213 13 : xml::AttributeData* pData = (xml::AttributeData*)aElement.getValue();
214 :
215 13 : sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
216 13 : if( nPos != -1L )
217 : {
218 8 : const OUString aPrefix( aName.copy( 0L, nPos ));
219 8 : const OUString aLName( aName.copy( nPos+1L ));
220 :
221 8 : if( pData->Namespace.isEmpty() )
222 : {
223 0 : if( mpContainer->AddAttr( aPrefix, aLName, pData->Value ) )
224 0 : return;
225 : }
226 : else
227 : {
228 8 : if( mpContainer->AddAttr( aPrefix, pData->Namespace, aLName, pData->Value ) )
229 8 : return;
230 0 : }
231 : }
232 : else
233 : {
234 5 : if( pData->Namespace.isEmpty() )
235 : {
236 5 : if( mpContainer->AddAttr( aName, pData->Value ) )
237 5 : return;
238 : }
239 : }
240 : }
241 :
242 4 : void SAL_CALL SvUnoAttributeContainer::removeByName(const OUString& Name)
243 : throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
244 : {
245 4 : sal_uInt16 nAttr = getIndexByName(Name);
246 4 : if( nAttr == USHRT_MAX )
247 0 : throw container::NoSuchElementException();
248 :
249 4 : mpContainer->Remove( nAttr );
250 4 : }
251 :
252 : //XServiceInfo
253 0 : OUString SAL_CALL SvUnoAttributeContainer::getImplementationName(void) throw( uno::RuntimeException )
254 : {
255 0 : return OUString( "SvUnoAttributeContainer" );
256 : }
257 :
258 0 : uno::Sequence< OUString > SvUnoAttributeContainer::getSupportedServiceNames(void)
259 : throw( uno::RuntimeException )
260 : {
261 0 : OUString aSN( OUString( "com.sun.star.xml.AttributeContainer" ) );
262 0 : uno::Sequence< OUString > aNS( &aSN, 1L );
263 0 : return aNS;
264 : }
265 :
266 0 : sal_Bool SvUnoAttributeContainer::supportsService(const OUString& ServiceName)
267 : throw( uno::RuntimeException )
268 : {
269 0 : const uno::Sequence < OUString > aServiceNames( getSupportedServiceNames() );
270 0 : const OUString* pNames = aServiceNames.getConstArray();
271 0 : sal_Int32 nCount = aServiceNames.getLength();
272 0 : while( nCount-- )
273 : {
274 0 : if( *pNames++ == ServiceName )
275 0 : return sal_True;
276 : }
277 :
278 0 : return sal_False;
279 : }
280 :
281 :
282 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|