xref: /AOO41X/main/editeng/source/items/xmlcnitm.cxx (revision 707fc0d4d52eb4f69d89a98ffec6918ca5de6326)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_editeng.hxx"
26 #include <com/sun/star/xml/AttributeData.hpp>
27 #include <com/sun/star/lang/XUnoTunnel.hpp>
28 #include <xmloff/xmlcnimp.hxx>
29 #include <xmloff/unoatrcn.hxx>
30 #include <editeng/xmlcnitm.hxx>
31 
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::container;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::xml;
36 
37 // ------------------------------------------------------------------------
38 
39 TYPEINIT1(SvXMLAttrContainerItem, SfxPoolItem);
40 
41 SvXMLAttrContainerItem::SvXMLAttrContainerItem( sal_uInt16 _nWhich ) :
42     SfxPoolItem( _nWhich )
43 {
44     pImpl = new SvXMLAttrContainerData;
45 }
46 
47 SvXMLAttrContainerItem::SvXMLAttrContainerItem(
48                                         const SvXMLAttrContainerItem& rItem ) :
49     SfxPoolItem( rItem )
50 {
51     pImpl = new SvXMLAttrContainerData( *rItem.pImpl );
52 }
53 
54 SvXMLAttrContainerItem::~SvXMLAttrContainerItem()
55 {
56     delete pImpl;
57 }
58 
59 int SvXMLAttrContainerItem::operator==( const SfxPoolItem& rItem ) const
60 {
61     DBG_ASSERT( rItem.ISA(SvXMLAttrContainerItem),
62                "SvXMLAttrContainerItem::operator ==(): Bad type");
63     return *pImpl == *((const SvXMLAttrContainerItem&)rItem).pImpl;
64 }
65 
66 int SvXMLAttrContainerItem::Compare( const SfxPoolItem &/*rWith*/ ) const
67 {
68     DBG_ASSERT( !this, "not yet implemented" );
69 
70     return 0;
71 }
72 
73 SfxItemPresentation SvXMLAttrContainerItem::GetPresentation(
74                     SfxItemPresentation /*ePresentation*/,
75                     SfxMapUnit /*eCoreMetric*/,
76                     SfxMapUnit /*ePresentationMetric*/,
77                     XubString &/*rText*/,
78                     const IntlWrapper * /*pIntlWrapper*/ ) const
79 {
80     return SFX_ITEM_PRESENTATION_NONE;
81 }
82 
83 sal_uInt16 SvXMLAttrContainerItem::GetVersion( sal_uInt16 /*nFileFormatVersion*/ ) const
84 {
85     // This item should never be stored
86     return USHRT_MAX;
87 }
88 
89 sal_Bool  SvXMLAttrContainerItem::QueryValue( com::sun::star::uno::Any& rVal, sal_uInt8 /*nMemberId*/ ) const
90 {
91     Reference<XNameContainer> xContainer =
92         new SvUnoAttributeContainer( new SvXMLAttrContainerData( *pImpl ) );
93 
94     rVal.setValue( &xContainer, ::getCppuType((Reference<XNameContainer>*)0) );
95     return sal_True;
96 }
97 sal_Bool SvXMLAttrContainerItem::PutValue( const com::sun::star::uno::Any& rVal, sal_uInt8 /*nMemberId*/ )
98 {
99     Reference<XInterface> xRef;
100     SvUnoAttributeContainer* pContainer = NULL;
101 
102     if( rVal.getValue() != NULL && rVal.getValueType().getTypeClass() == TypeClass_INTERFACE )
103     {
104         xRef = *(Reference<XInterface>*)rVal.getValue();
105         Reference<XUnoTunnel> xTunnel(xRef, UNO_QUERY);
106         if( xTunnel.is() )
107             pContainer = (SvUnoAttributeContainer*)(sal_uLong)xTunnel->getSomething(SvUnoAttributeContainer::getUnoTunnelId());
108     }
109 
110     if( pContainer )
111     {
112         delete pImpl;
113         pImpl = new SvXMLAttrContainerData( * pContainer->GetContainerImpl() );
114     }
115     else
116     {
117         SvXMLAttrContainerData* pNewImpl = new SvXMLAttrContainerData;
118 
119         try
120         {
121             Reference<XNameContainer> xContainer( xRef, UNO_QUERY );
122             if( !xContainer.is() )
123                 return sal_False;
124 
125             const Sequence< ::rtl::OUString > aNameSequence( xContainer->getElementNames() );
126             const ::rtl::OUString* pNames = aNameSequence.getConstArray();
127             const sal_Int32 nCount = aNameSequence.getLength();
128             Any aAny;
129             AttributeData* pData;
130             sal_Int32 nAttr;
131 
132             for( nAttr = 0; nAttr < nCount; nAttr++ )
133             {
134                 const ::rtl::OUString aName( *pNames++ );
135 
136                 aAny = xContainer->getByName( aName );
137                 if( aAny.getValue() == NULL || aAny.getValueType() != ::getCppuType((AttributeData*)0) )
138                     return sal_False;
139 
140                 pData = (AttributeData*)aAny.getValue();
141                 sal_Int32 pos = aName.indexOf( sal_Unicode(':') );
142                 if( pos != -1 )
143                 {
144                     const ::rtl::OUString aPrefix( aName.copy( 0, pos ));
145                     const ::rtl::OUString aLName( aName.copy( pos+1 ));
146 
147                     if( pData->Namespace.getLength() == 0 )
148                     {
149                         if( !pNewImpl->AddAttr( aPrefix, aLName, pData->Value ) )
150                             break;
151                     }
152                     else
153                     {
154                         if( !pNewImpl->AddAttr( aPrefix, pData->Namespace, aLName, pData->Value ) )
155                             break;
156                     }
157                 }
158                 else
159                 {
160                     if( !pNewImpl->AddAttr( aName, pData->Value ) )
161                         break;
162                 }
163             }
164 
165             if( nAttr == nCount )
166             {
167                 delete pImpl;
168                 pImpl = pNewImpl;
169             }
170             else
171             {
172                 delete pNewImpl;
173                 return sal_False;
174             }
175         }
176         catch(...)
177         {
178             delete pNewImpl;
179             return sal_False;
180         }
181     }
182     return sal_True;
183 }
184 
185 
186 sal_Bool SvXMLAttrContainerItem::AddAttr( const ::rtl::OUString& rLName,
187                                       const ::rtl::OUString& rValue )
188 {
189     return pImpl->AddAttr( rLName, rValue );
190 }
191 
192 sal_Bool SvXMLAttrContainerItem::AddAttr( const ::rtl::OUString& rPrefix,
193           const ::rtl::OUString& rNamespace, const ::rtl::OUString& rLName,
194           const ::rtl::OUString& rValue )
195 {
196     return pImpl->AddAttr( rPrefix, rNamespace, rLName, rValue );
197 }
198 
199 sal_uInt16 SvXMLAttrContainerItem::GetAttrCount() const
200 {
201     return (sal_uInt16)pImpl->GetAttrCount();
202 }
203 
204 ::rtl::OUString SvXMLAttrContainerItem::GetAttrNamespace( sal_uInt16 i ) const
205 {
206     return pImpl->GetAttrNamespace( i );
207 }
208 
209 ::rtl::OUString SvXMLAttrContainerItem::GetAttrPrefix( sal_uInt16 i ) const
210 {
211     return pImpl->GetAttrPrefix( i );
212 }
213 
214 const ::rtl::OUString& SvXMLAttrContainerItem::GetAttrLName( sal_uInt16 i ) const
215 {
216     return pImpl->GetAttrLName( i );
217 }
218 
219 const ::rtl::OUString& SvXMLAttrContainerItem::GetAttrValue( sal_uInt16 i ) const
220 {
221     return pImpl->GetAttrValue( i );
222 }
223 
224 
225 sal_uInt16 SvXMLAttrContainerItem::GetFirstNamespaceIndex() const
226 {
227     return pImpl->GetFirstNamespaceIndex();
228 }
229 
230 sal_uInt16 SvXMLAttrContainerItem::GetNextNamespaceIndex( sal_uInt16 nIdx ) const
231 {
232     return pImpl->GetNextNamespaceIndex( nIdx );
233 }
234 
235 const ::rtl::OUString& SvXMLAttrContainerItem::GetNamespace( sal_uInt16 i ) const
236 {
237     return pImpl->GetNamespace( i );
238 }
239 
240 const ::rtl::OUString& SvXMLAttrContainerItem::GetPrefix( sal_uInt16 i ) const
241 {
242     return pImpl->GetPrefix( i );
243 }
244 
245