xref: /trunk/main/xmloff/source/core/unoatrcn.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63bba73cSAndrew Rist  * distributed with this work for additional information
6*63bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*63bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist  * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63bba73cSAndrew Rist  * specific language governing permissions and limitations
18*63bba73cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*63bba73cSAndrew Rist  *************************************************************/
21*63bba73cSAndrew Rist 
22*63bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include <tools/debug.hxx>
27cdf0e10cSrcweir #include <com/sun/star/xml/AttributeData.hpp>
28cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
29cdf0e10cSrcweir #include <rtl/uuid.h>
30cdf0e10cSrcweir #include <rtl/memory.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <xmloff/xmlcnimp.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include "xmloff/unoatrcn.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using ::rtl::OUString;
37cdf0e10cSrcweir using ::rtl::OUStringBuffer;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace ::com::sun::star;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // --------------------------------------------------------------------
42cdf0e10cSrcweir // Interface implementation
43cdf0e10cSrcweir // --------------------------------------------------------------------
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #define IMPL    ((AttrContainerImpl*)mpData)
46cdf0e10cSrcweir 
SvUnoAttributeContainer_CreateInstance()47cdf0e10cSrcweir uno::Reference< uno::XInterface >  SvUnoAttributeContainer_CreateInstance()
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     return *(new SvUnoAttributeContainer);
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
SvUnoAttributeContainer(SvXMLAttrContainerData * pContainer)52cdf0e10cSrcweir SvUnoAttributeContainer::SvUnoAttributeContainer( SvXMLAttrContainerData* pContainer)
53cdf0e10cSrcweir : mpContainer( pContainer )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     if( mpContainer == NULL )
56cdf0e10cSrcweir         mpContainer = new SvXMLAttrContainerData;
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
~SvUnoAttributeContainer()59cdf0e10cSrcweir SvUnoAttributeContainer::~SvUnoAttributeContainer()
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     delete mpContainer;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // container::XElementAccess
getElementType(void)65cdf0e10cSrcweir uno::Type SAL_CALL SvUnoAttributeContainer::getElementType(void)
66cdf0e10cSrcweir     throw( uno::RuntimeException )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     return ::getCppuType((const xml::AttributeData*)0);
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
hasElements(void)71cdf0e10cSrcweir sal_Bool SAL_CALL SvUnoAttributeContainer::hasElements(void)
72cdf0e10cSrcweir     throw( uno::RuntimeException )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     return mpContainer->GetAttrCount() != 0;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
getIndexByName(const OUString & aName) const77cdf0e10cSrcweir sal_uInt16 SvUnoAttributeContainer::getIndexByName(const OUString& aName ) const
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
82cdf0e10cSrcweir     if( nPos == -1L )
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir         for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             if( mpContainer->GetAttrLName(nAttr) == aName &&
87cdf0e10cSrcweir                 mpContainer->GetAttrPrefix(nAttr).getLength() == 0L )
88cdf0e10cSrcweir                 return nAttr;
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir     else
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         const OUString aPrefix( aName.copy( 0L, nPos ) );
94cdf0e10cSrcweir         const OUString aLName( aName.copy( nPos+1L ) );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             if( mpContainer->GetAttrLName(nAttr) == aLName &&
99cdf0e10cSrcweir                 mpContainer->GetAttrPrefix(nAttr) == aPrefix )
100cdf0e10cSrcweir                 return nAttr;
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     return USHRT_MAX;
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
getUnoTunnelId()107cdf0e10cSrcweir const ::com::sun::star::uno::Sequence< sal_Int8 > & SvUnoAttributeContainer::getUnoTunnelId() throw()
108cdf0e10cSrcweir {
109cdf0e10cSrcweir     static ::com::sun::star::uno::Sequence< sal_Int8 > * pSeq = 0;
110cdf0e10cSrcweir     if( !pSeq )
111cdf0e10cSrcweir     {
112cdf0e10cSrcweir         ::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() );
113cdf0e10cSrcweir         if( !pSeq )
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             static ::com::sun::star::uno::Sequence< sal_Int8 > aSeq( 16 );
116cdf0e10cSrcweir             rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True );
117cdf0e10cSrcweir             pSeq = &aSeq;
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir     return *pSeq;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
getImplementation(uno::Reference<uno::XInterface> xInt)123cdf0e10cSrcweir SvUnoAttributeContainer* SvUnoAttributeContainer::getImplementation( uno::Reference< uno::XInterface > xInt ) throw()
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xUT( xInt, ::com::sun::star::uno::UNO_QUERY );
126cdf0e10cSrcweir     if( xUT.is() )
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         return
129cdf0e10cSrcweir             reinterpret_cast<SvUnoAttributeContainer*>(
130cdf0e10cSrcweir                 sal::static_int_cast<sal_IntPtr>(
131cdf0e10cSrcweir                     xUT->getSomething( SvUnoAttributeContainer::getUnoTunnelId())));
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir     else
134cdf0e10cSrcweir         return NULL;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
getSomething(const::com::sun::star::uno::Sequence<sal_Int8> & rId)137cdf0e10cSrcweir sal_Int64 SAL_CALL SvUnoAttributeContainer::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rId ) throw(::com::sun::star::uno::RuntimeException)
138cdf0e10cSrcweir {
139cdf0e10cSrcweir     if( rId.getLength() == 16 && 0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
140cdf0e10cSrcweir                                                          rId.getConstArray(), 16 ) )
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(this));
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir     return 0;
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir // container::XNameAccess
getByName(const OUString & aName)148cdf0e10cSrcweir uno::Any SAL_CALL SvUnoAttributeContainer::getByName(const OUString& aName)
149cdf0e10cSrcweir     throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     sal_uInt16 nAttr = getIndexByName(aName );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     if( nAttr == USHRT_MAX )
154cdf0e10cSrcweir         throw container::NoSuchElementException();
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     xml::AttributeData aData;
157cdf0e10cSrcweir     aData.Namespace = mpContainer->GetAttrNamespace(nAttr);
158cdf0e10cSrcweir     aData.Type = OUString::createFromAscii("CDATA");
159cdf0e10cSrcweir     aData.Value = mpContainer->GetAttrValue(nAttr);
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     uno::Any aAny;
162cdf0e10cSrcweir     aAny <<= aData;
163cdf0e10cSrcweir     return aAny;
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
getElementNames(void)166cdf0e10cSrcweir uno::Sequence< OUString > SAL_CALL SvUnoAttributeContainer::getElementNames(void) throw( uno::RuntimeException )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir     const sal_uInt16 nAttrCount = mpContainer->GetAttrCount();
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     uno::Sequence< OUString > aElementNames( (sal_Int32)nAttrCount );
171cdf0e10cSrcweir     OUString *pNames = aElementNames.getArray();
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     for( sal_uInt16 nAttr = 0; nAttr < nAttrCount; nAttr++ )
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir         OUStringBuffer sBuffer( mpContainer->GetAttrPrefix(nAttr) );
176cdf0e10cSrcweir         if( sBuffer.getLength() != 0L )
177cdf0e10cSrcweir             sBuffer.append( (sal_Unicode)':' );
178cdf0e10cSrcweir         sBuffer.append( mpContainer->GetAttrLName(nAttr) );
179cdf0e10cSrcweir         *pNames++ = sBuffer.makeStringAndClear();
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     return aElementNames;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
hasByName(const OUString & aName)185cdf0e10cSrcweir sal_Bool SAL_CALL SvUnoAttributeContainer::hasByName(const OUString& aName) throw( uno::RuntimeException )
186cdf0e10cSrcweir {
187cdf0e10cSrcweir     return getIndexByName(aName ) != USHRT_MAX;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir // container::XNameReplace
replaceByName(const OUString & aName,const uno::Any & aElement)191cdf0e10cSrcweir void SAL_CALL SvUnoAttributeContainer::replaceByName(const OUString& aName, const uno::Any& aElement)
192cdf0e10cSrcweir     throw( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
193cdf0e10cSrcweir {
194cdf0e10cSrcweir     if( aElement.hasValue() && aElement.getValueType() == ::getCppuType((const xml::AttributeData*)0) )
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir         sal_uInt16 nAttr = getIndexByName(aName );
197cdf0e10cSrcweir         if( nAttr == USHRT_MAX )
198cdf0e10cSrcweir             throw container::NoSuchElementException();
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         xml::AttributeData* pData = (xml::AttributeData*)aElement.getValue();
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
203cdf0e10cSrcweir         if( nPos != -1L )
204cdf0e10cSrcweir         {
205cdf0e10cSrcweir             const OUString aPrefix( aName.copy( 0L, nPos ));
206cdf0e10cSrcweir             const OUString aLName( aName.copy( nPos+1L ));
207cdf0e10cSrcweir 
208cdf0e10cSrcweir             if( pData->Namespace.getLength() == 0L )
209cdf0e10cSrcweir             {
210cdf0e10cSrcweir                 if( mpContainer->SetAt( nAttr, aPrefix, aLName, pData->Value ) )
211cdf0e10cSrcweir                     return;
212cdf0e10cSrcweir             }
213cdf0e10cSrcweir             else
214cdf0e10cSrcweir             {
215cdf0e10cSrcweir                 if( mpContainer->SetAt( nAttr, aPrefix, pData->Namespace, aLName, pData->Value ) )
216cdf0e10cSrcweir                     return;
217cdf0e10cSrcweir             }
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir         else
220cdf0e10cSrcweir         {
221cdf0e10cSrcweir             if( pData->Namespace.getLength() == 0L )
222cdf0e10cSrcweir             {
223cdf0e10cSrcweir                 if( mpContainer->SetAt( nAttr, aName, pData->Value ) )
224cdf0e10cSrcweir                     return;
225cdf0e10cSrcweir             }
226cdf0e10cSrcweir         }
227cdf0e10cSrcweir     }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     throw lang::IllegalArgumentException();
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir // container::XNameContainer
insertByName(const OUString & aName,const uno::Any & aElement)233cdf0e10cSrcweir void SAL_CALL SvUnoAttributeContainer::insertByName(const OUString& aName, const uno::Any& aElement)
234cdf0e10cSrcweir throw( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException )
235cdf0e10cSrcweir {
236cdf0e10cSrcweir     if( !aElement.hasValue() || aElement.getValueType() != ::getCppuType((const xml::AttributeData*)0) )
237cdf0e10cSrcweir         throw lang::IllegalArgumentException();
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     sal_uInt16 nAttr = getIndexByName(aName );
240cdf0e10cSrcweir     if( nAttr != USHRT_MAX )
241cdf0e10cSrcweir         throw container::ElementExistException();
242cdf0e10cSrcweir 
243cdf0e10cSrcweir     xml::AttributeData* pData = (xml::AttributeData*)aElement.getValue();
244cdf0e10cSrcweir 
245cdf0e10cSrcweir     sal_Int32 nPos = aName.indexOf( sal_Unicode(':') );
246cdf0e10cSrcweir     if( nPos != -1L )
247cdf0e10cSrcweir     {
248cdf0e10cSrcweir         const OUString aPrefix( aName.copy( 0L, nPos ));
249cdf0e10cSrcweir         const OUString aLName( aName.copy( nPos+1L ));
250cdf0e10cSrcweir 
251cdf0e10cSrcweir         if( pData->Namespace.getLength() == 0L )
252cdf0e10cSrcweir         {
253cdf0e10cSrcweir             if( mpContainer->AddAttr( aPrefix, aLName, pData->Value ) )
254cdf0e10cSrcweir                 return;
255cdf0e10cSrcweir         }
256cdf0e10cSrcweir         else
257cdf0e10cSrcweir         {
258cdf0e10cSrcweir             if( mpContainer->AddAttr( aPrefix, pData->Namespace, aLName, pData->Value ) )
259cdf0e10cSrcweir                 return;
260cdf0e10cSrcweir         }
261cdf0e10cSrcweir     }
262cdf0e10cSrcweir     else
263cdf0e10cSrcweir     {
264cdf0e10cSrcweir         if( pData->Namespace.getLength() == 0L )
265cdf0e10cSrcweir         {
266cdf0e10cSrcweir             if( mpContainer->AddAttr( aName, pData->Value ) )
267cdf0e10cSrcweir                 return;
268cdf0e10cSrcweir         }
269cdf0e10cSrcweir     }
270cdf0e10cSrcweir }
271cdf0e10cSrcweir 
removeByName(const OUString & Name)272cdf0e10cSrcweir void SAL_CALL SvUnoAttributeContainer::removeByName(const OUString& Name)
273cdf0e10cSrcweir     throw( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
274cdf0e10cSrcweir {
275cdf0e10cSrcweir     sal_uInt16 nAttr = getIndexByName(Name);
276cdf0e10cSrcweir     if( nAttr == USHRT_MAX )
277cdf0e10cSrcweir         throw container::NoSuchElementException();
278cdf0e10cSrcweir 
279cdf0e10cSrcweir     mpContainer->Remove( nAttr );
280cdf0e10cSrcweir }
281cdf0e10cSrcweir 
282cdf0e10cSrcweir //XServiceInfo
getImplementationName(void)283cdf0e10cSrcweir OUString SAL_CALL SvUnoAttributeContainer::getImplementationName(void) throw( uno::RuntimeException )
284cdf0e10cSrcweir {
285cdf0e10cSrcweir     return OUString::createFromAscii( "SvUnoAttributeContainer" );
286cdf0e10cSrcweir }
287cdf0e10cSrcweir 
getSupportedServiceNames(void)288cdf0e10cSrcweir uno::Sequence< OUString > SvUnoAttributeContainer::getSupportedServiceNames(void)
289cdf0e10cSrcweir     throw( uno::RuntimeException )
290cdf0e10cSrcweir {
291cdf0e10cSrcweir     OUString aSN( OUString::createFromAscii( "com.sun.star.xml.AttributeContainer" ) );
292cdf0e10cSrcweir     uno::Sequence< OUString > aNS( &aSN, 1L );
293cdf0e10cSrcweir     return aNS;
294cdf0e10cSrcweir }
295cdf0e10cSrcweir 
supportsService(const OUString & ServiceName)296cdf0e10cSrcweir sal_Bool SvUnoAttributeContainer::supportsService(const OUString& ServiceName)
297cdf0e10cSrcweir     throw( uno::RuntimeException )
298cdf0e10cSrcweir {
299cdf0e10cSrcweir     const uno::Sequence < OUString > aServiceNames( getSupportedServiceNames() );
300cdf0e10cSrcweir     const OUString* pNames = aServiceNames.getConstArray();
301cdf0e10cSrcweir     sal_Int32 nCount = aServiceNames.getLength();
302cdf0e10cSrcweir     while( nCount-- )
303cdf0e10cSrcweir     {
304cdf0e10cSrcweir         if( *pNames++ == ServiceName )
305cdf0e10cSrcweir             return sal_True;
306cdf0e10cSrcweir     }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir     return sal_False;
309cdf0e10cSrcweir }
310