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_xmloff.hxx"
26 #include <tools/debug.hxx>
27 #include <xmloff/xmlprhdl.hxx>
28 #include "xmlbahdl.hxx"
29 #include <xmloff/xmlprmap.hxx>
30 #include <xmloff/xmltypes.hxx>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/beans/XPropertyState.hpp>
33 #include <com/sun/star/uno/Any.hxx>
34 #include <xmloff/xmltoken.hxx>
35
36
37 using namespace ::std;
38 using ::rtl::OUString;
39 using ::rtl::OUStringBuffer;
40
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 using ::xmloff::token::GetXMLToken;
44
XMLPropertySetMapperEntry_Impl(const XMLPropertyMapEntry & rMapEntry,const UniReference<XMLPropertyHandlerFactory> & rFactory)45 XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
46 const XMLPropertyMapEntry& rMapEntry,
47 const UniReference< XMLPropertyHandlerFactory >& rFactory ) :
48 sXMLAttributeName( GetXMLToken(rMapEntry.meXMLName) ),
49 sAPIPropertyName( OUString(rMapEntry.msApiName, rMapEntry.nApiNameLength,
50 RTL_TEXTENCODING_ASCII_US ) ),
51 nXMLNameSpace( rMapEntry.mnNameSpace ),
52 nType( rMapEntry.mnType ),
53 nContextId( rMapEntry.mnContextId ),
54 nEarliestODFVersionForExport( rMapEntry.mnEarliestODFVersionForExport ),
55 pHdl( rFactory->GetPropertyHandler( rMapEntry.mnType & MID_FLAG_MASK ) )
56 {
57 }
58
XMLPropertySetMapperEntry_Impl(const XMLPropertySetMapperEntry_Impl & rEntry)59 XMLPropertySetMapperEntry_Impl::XMLPropertySetMapperEntry_Impl(
60 const XMLPropertySetMapperEntry_Impl& rEntry ) :
61 sXMLAttributeName( rEntry.sXMLAttributeName),
62 sAPIPropertyName( rEntry.sAPIPropertyName),
63 nXMLNameSpace( rEntry.nXMLNameSpace),
64 nType( rEntry.nType),
65 nContextId( rEntry.nContextId),
66 nEarliestODFVersionForExport( rEntry.nEarliestODFVersionForExport ),
67 pHdl( rEntry.pHdl)
68 {
69 DBG_ASSERT( pHdl, "Unknown XML property type handler!" );
70 }
71
72 ///////////////////////////////////////////////////////////////////////////
73 //
74 // Ctor
75 //
XMLPropertySetMapper(const XMLPropertyMapEntry * pEntries,const UniReference<XMLPropertyHandlerFactory> & rFactory)76 XMLPropertySetMapper::XMLPropertySetMapper(
77 const XMLPropertyMapEntry* pEntries,
78 const UniReference< XMLPropertyHandlerFactory >& rFactory )
79 {
80 aHdlFactories.push_back( rFactory );
81 if( pEntries )
82 {
83 const XMLPropertyMapEntry* pIter = pEntries;
84
85 // count entries
86 while( pIter->msApiName )
87 {
88 XMLPropertySetMapperEntry_Impl aEntry( *pIter, rFactory );
89 aMapEntries.push_back( aEntry );
90 pIter++;
91 }
92 }
93 }
94
~XMLPropertySetMapper()95 XMLPropertySetMapper::~XMLPropertySetMapper()
96 {
97 }
98
AddMapperEntry(const UniReference<XMLPropertySetMapper> & rMapper)99 void XMLPropertySetMapper::AddMapperEntry(
100 const UniReference < XMLPropertySetMapper >& rMapper )
101 {
102 for( vector < UniReference < XMLPropertyHandlerFactory > >::iterator
103 aFIter = rMapper->aHdlFactories.begin();
104 aFIter != rMapper->aHdlFactories.end();
105 aFIter++ )
106 {
107 aHdlFactories.push_back( *aFIter );
108 }
109
110 for( vector < XMLPropertySetMapperEntry_Impl >::iterator
111 aEIter = rMapper->aMapEntries.begin();
112 aEIter != rMapper->aMapEntries.end();
113 aEIter++ )
114 {
115 aMapEntries.push_back( *aEIter );
116 }
117 }
118
119 ///////////////////////////////////////////////////////////////////////////
120 //
121 // Export a Property
122 //
exportXML(OUString & rStrExpValue,const XMLPropertyState & rProperty,const SvXMLUnitConverter & rUnitConverter) const123 sal_Bool XMLPropertySetMapper::exportXML(
124 OUString& rStrExpValue,
125 const XMLPropertyState& rProperty,
126 const SvXMLUnitConverter& rUnitConverter ) const
127 {
128 sal_Bool bRet = sal_False;
129
130 const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
131
132 DBG_ASSERT( pHdl, "Unknown XML Type!" );
133 if( pHdl )
134 bRet = pHdl->exportXML( rStrExpValue, rProperty.maValue,
135 rUnitConverter );
136
137 return bRet;
138 }
139
140 ///////////////////////////////////////////////////////////////////////////
141 //
142 // Import a Property
143 //
importXML(const OUString & rStrImpValue,XMLPropertyState & rProperty,const SvXMLUnitConverter & rUnitConverter) const144 sal_Bool XMLPropertySetMapper::importXML(
145 const OUString& rStrImpValue,
146 XMLPropertyState& rProperty,
147 const SvXMLUnitConverter& rUnitConverter ) const
148 {
149 sal_Bool bRet = sal_False;
150
151 const XMLPropertyHandler* pHdl = GetPropertyHandler( rProperty.mnIndex );
152
153 if( pHdl )
154 bRet = pHdl->importXML( rStrImpValue, rProperty.maValue,
155 rUnitConverter );
156
157 return bRet;
158 }
159
160 ///////////////////////////////////////////////////////////////////////////
161 //
162 // Search for the given name and the namespace in the list and return
163 // the index of the entry
164 // If there is no matching entry the method returns -1
165 //
GetEntryIndex(sal_uInt16 nNamespace,const OUString & rStrName,sal_uInt32 nPropType,sal_Int32 nStartAt) const166 sal_Int32 XMLPropertySetMapper::GetEntryIndex(
167 sal_uInt16 nNamespace,
168 const OUString& rStrName,
169 sal_uInt32 nPropType,
170 sal_Int32 nStartAt /* = -1 */ ) const
171 {
172 sal_Int32 nEntries = GetEntryCount();
173 sal_Int32 nIndex= nStartAt == - 1? 0 : nStartAt+1;
174
175 if ( nEntries && nIndex < nEntries )
176 {
177 do
178 {
179 const XMLPropertySetMapperEntry_Impl& rEntry = aMapEntries[nIndex];
180 if( (!nPropType || nPropType == rEntry.GetPropType()) &&
181 rEntry.nXMLNameSpace == nNamespace &&
182 rStrName == rEntry.sXMLAttributeName )
183 return nIndex;
184 else
185 nIndex++;
186
187 } while( nIndex<nEntries );
188 }
189
190 return -1;
191 }
192
193
194 /** searches for an entry that matches the given api name, namespace and local name or -1 if nothing found */
FindEntryIndex(const sal_Char * sApiName,sal_uInt16 nNameSpace,const OUString & sXMLName) const195 sal_Int32 XMLPropertySetMapper::FindEntryIndex(
196 const sal_Char* sApiName,
197 sal_uInt16 nNameSpace,
198 const OUString& sXMLName ) const
199 {
200 sal_Int32 nIndex = 0;
201 sal_Int32 nEntries = GetEntryCount();
202
203 do
204 {
205 const XMLPropertySetMapperEntry_Impl& rEntry = aMapEntries[nIndex];
206 if( rEntry.nXMLNameSpace == nNameSpace &&
207 rEntry.sXMLAttributeName.equals( sXMLName ) &&
208 0 == rEntry.sAPIPropertyName.compareToAscii( sApiName ) )
209 return nIndex;
210 else
211 nIndex++;
212
213 } while( nIndex < nEntries );
214
215 return -1;
216 }
217
FindEntryIndex(const sal_Int16 nContextId) const218 sal_Int32 XMLPropertySetMapper::FindEntryIndex( const sal_Int16 nContextId ) const
219 {
220 sal_Int32 nIndex = 0;
221 const sal_Int32 nEntries = GetEntryCount();
222
223 if ( nEntries )
224 {
225 do
226 {
227 const XMLPropertySetMapperEntry_Impl& rEntry = aMapEntries[nIndex];
228 if( rEntry.nContextId == nContextId )
229 return nIndex;
230 else
231 nIndex++;
232
233 } while( nIndex < nEntries );
234 }
235
236 return -1;
237 }
238
RemoveEntry(sal_Int32 nIndex)239 void XMLPropertySetMapper::RemoveEntry( sal_Int32 nIndex )
240 {
241 const sal_Int32 nEntries = GetEntryCount();
242 if( nIndex>=nEntries || nIndex<0 )
243 return;
244 vector < XMLPropertySetMapperEntry_Impl >::iterator aEIter = aMapEntries.begin();
245 for( sal_Int32 nN=0; nN<nIndex; nN++ )
246 aEIter++;
247 aMapEntries.erase( aEIter );
248 }
249