xref: /trunk/main/xmloff/inc/xmloff/xmlprmap.hxx (revision cea84f56765a2ac06f0f5a2718f45f6430edc781)
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 #ifndef _XMLOFF_PROPERTYSETMAPPER_HXX
25 #define _XMLOFF_PROPERTYSETMAPPER_HXX
26 
27 #include "sal/config.h"
28 #include "xmloff/dllapi.h"
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <com/sun/star/uno/Any.hxx>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 
33 #ifndef __SGI_STL_VECTOR
34 #include <vector>
35 #endif
36 #include <xmloff/uniref.hxx>
37 #include <xmloff/maptype.hxx>
38 #include <xmloff/xmltypes.hxx>
39 #include <xmloff/prhdlfac.hxx>
40 
41 #include <tools/debug.hxx>
42 
43 class SvXMLUnitConverter;
44 class XMLPropertyHandler;
45 
46 ///////////////////////////////////////////////////////////////////////////
47 //
48 /** Helper-class for XML-im/export:
49     - Holds a pointer to a given array of XMLPropertyMapEntry
50     - Provides several methods to access data from this array
51     - Holds a Sequence of XML-names (for properties)
52     - The filter takes all properties of the XPropertySet which are also
53       in the XMLPropertyMapEntry and which are have not a default value
54       and put them into a vector of XMLPropertyStae
55     - this class knows how to compare, im/export properties
56 
57     Attention: At all methods, which get an index as parameter, there is no
58                range validation to save runtime !!
59 */
60 struct XMLPropertySetMapperEntry_Impl
61 {
62     ::rtl::OUString sXMLAttributeName;
63     ::rtl::OUString sAPIPropertyName;
64     sal_uInt16 nXMLNameSpace;
65     sal_Int32  nType;
66     sal_Int16  nContextId;
67     SvtSaveOptions::ODFDefaultVersion   nEarliestODFVersionForExport;
68     const XMLPropertyHandler *pHdl;
69 
70     XMLPropertySetMapperEntry_Impl(
71         const XMLPropertyMapEntry& rMapEntry,
72         const UniReference< XMLPropertyHandlerFactory >& rFactory );
73 
74     XMLPropertySetMapperEntry_Impl(
75         const XMLPropertySetMapperEntry_Impl& rEntry );
76     XMLPropertySetMapperEntry_Impl& operator=(const XMLPropertySetMapperEntry_Impl&) = default;
77 
GetPropTypeXMLPropertySetMapperEntry_Impl78     sal_uInt32 GetPropType() const { return nType & XML_TYPE_PROP_MASK; }
79 };
80 
81 class XMLOFF_DLLPUBLIC XMLPropertySetMapper : public UniRefBase
82 {
83     ::std::vector< XMLPropertySetMapperEntry_Impl > aMapEntries;
84     ::std::vector< UniReference < XMLPropertyHandlerFactory > > aHdlFactories;
85 
86 public:
87     /** The last element of the XMLPropertyMapEntry-array must contain NULL-values */
88     XMLPropertySetMapper(
89             const XMLPropertyMapEntry* pEntries,
90             const UniReference< XMLPropertyHandlerFactory >& rFactory );
91     virtual ~XMLPropertySetMapper();
92 
93     void AddMapperEntry( const UniReference < XMLPropertySetMapper >& rMapper );
94 
95     /** Return number of entries in input-array */
GetEntryCount() const96     sal_Int32   GetEntryCount() const { return aMapEntries.size(); }
97 
98     /** Returns the flags of an entry */
GetEntryFlags(sal_Int32 nIndex) const99     sal_uInt32 GetEntryFlags( sal_Int32 nIndex ) const
100     {
101         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
102         return aMapEntries[nIndex].nType & ~MID_FLAG_MASK;
103     }
104 
105     /** Returns the type of an entry */
GetEntryType(sal_Int32 nIndex,sal_Bool bWithFlags=sal_True) const106     sal_uInt32 GetEntryType( sal_Int32 nIndex,
107                                    sal_Bool bWithFlags = sal_True ) const
108     {
109         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
110         sal_uInt32 nType = aMapEntries[nIndex].nType;
111         if( !bWithFlags )
112             nType = nType & MID_FLAG_MASK;
113         return nType;
114     }
115 
116     /** Returns the namespace-key of an entry */
GetEntryNameSpace(sal_Int32 nIndex) const117     sal_uInt16 GetEntryNameSpace( sal_Int32 nIndex ) const
118     {
119         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
120         return aMapEntries[nIndex].nXMLNameSpace;
121     }
122 
123     /** Returns the 'local' XML-name of the entry */
GetEntryXMLName(sal_Int32 nIndex) const124     const ::rtl::OUString& GetEntryXMLName( sal_Int32 nIndex ) const
125     {
126         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
127         return aMapEntries[nIndex].sXMLAttributeName;
128     }
129 
130     /** Returns the entry API name */
GetEntryAPIName(sal_Int32 nIndex) const131     const ::rtl::OUString& GetEntryAPIName( sal_Int32 nIndex ) const
132     {
133         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
134         return aMapEntries[nIndex].sAPIPropertyName;
135     }
136 
137     /** returns the entry context id. -1 is a valid index here. */
GetEntryContextId(sal_Int32 nIndex) const138     sal_Int16 GetEntryContextId( sal_Int32 nIndex ) const
139     {
140         DBG_ASSERT( (nIndex >= -1) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
141         return nIndex == -1 ? 0 : aMapEntries[nIndex].nContextId;
142     }
143 
144     /** returns the earliest odf version for which this property should be exported. */
GetEarliestODFVersionForExport(sal_Int32 nIndex) const145     SvtSaveOptions::ODFDefaultVersion GetEarliestODFVersionForExport( sal_Int32 nIndex ) const
146     {
147         DBG_ASSERT( (nIndex >= -1) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
148         return nIndex == -1 ? SvtSaveOptions::ODFVER_UNKNOWN : aMapEntries[nIndex].nEarliestODFVersionForExport;
149     }
150 
151     /** Returns the index of an entry with the given XML-name and namespace
152         If there is no matching entry the method returns -1 */
153     sal_Int32 GetEntryIndex( sal_uInt16 nNamespace,
154                                    const ::rtl::OUString& rStrName,
155                                    sal_uInt32 nPropType,
156                                    sal_Int32 nStartAt = -1 ) const;
157 
158     /** Retrieves a PropertyHandler for that property which placed at nIndex in the XMLPropertyMapEntry-array */
GetPropertyHandler(sal_Int32 nIndex) const159     const XMLPropertyHandler* GetPropertyHandler( sal_Int32 nIndex ) const
160     {
161         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
162         return aMapEntries[nIndex].pHdl;
163     }
164 
165     /** import/export
166         This methods calls the respective im/export-method of the respective PropertyHandler. */
167     virtual sal_Bool exportXML( ::rtl::OUString& rStrExpValue,
168                                 const XMLPropertyState& rProperty,
169                                 const SvXMLUnitConverter& rUnitConverter ) const;
170     virtual sal_Bool importXML( const ::rtl::OUString& rStrImpValue,
171                                 XMLPropertyState& rProperty,
172                                 const SvXMLUnitConverter& rUnitConverter ) const;
173 
174     /** searches for an entry that matches the given api name, namespace and local name or -1 if nothing found */
175     sal_Int32 FindEntryIndex( const sal_Char* sApiName,
176                               sal_uInt16 nNameSpace,
177                               const ::rtl::OUString& sXMLName ) const;
178 
179     /** searches for an entry that matches the given ContextId or gives -1 if nothing found */
180     sal_Int32 FindEntryIndex( const sal_Int16 nContextId ) const;
181 
182     /** Remove an entry */
183     void RemoveEntry( sal_Int32 nIndex );
184 };
185 
186 #endif // _XMLOFF_PROPERTYSETMAPPER_HXX
187