xref: /AOO41X/main/xmloff/inc/xmloff/xmlprmap.hxx (revision ecfe53c5d1886e1e0d215b0d140d05282ab1c477)
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 
GetPropTypeXMLPropertySetMapperEntry_Impl77     sal_uInt32 GetPropType() const { return nType & XML_TYPE_PROP_MASK; }
78 };
79 
80 class XMLOFF_DLLPUBLIC XMLPropertySetMapper : public UniRefBase
81 {
82     ::std::vector< XMLPropertySetMapperEntry_Impl > aMapEntries;
83     ::std::vector< UniReference < XMLPropertyHandlerFactory > > aHdlFactories;
84 
85 public:
86     /** The last element of the XMLPropertyMapEntry-array must contain NULL-values */
87     XMLPropertySetMapper(
88             const XMLPropertyMapEntry* pEntries,
89             const UniReference< XMLPropertyHandlerFactory >& rFactory );
90     virtual ~XMLPropertySetMapper();
91 
92     void AddMapperEntry( const UniReference < XMLPropertySetMapper >& rMapper );
93 
94     /** Return number of entries in input-array */
GetEntryCount() const95     sal_Int32   GetEntryCount() const { return aMapEntries.size(); }
96 
97     /** Returns the flags of an entry */
GetEntryFlags(sal_Int32 nIndex) const98     sal_uInt32 GetEntryFlags( sal_Int32 nIndex ) const
99     {
100         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
101         return aMapEntries[nIndex].nType & ~MID_FLAG_MASK;
102     }
103 
104     /** Returns the type of an entry */
GetEntryType(sal_Int32 nIndex,sal_Bool bWithFlags=sal_True) const105     sal_uInt32 GetEntryType( sal_Int32 nIndex,
106                                    sal_Bool bWithFlags = sal_True ) const
107     {
108         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
109         sal_uInt32 nType = aMapEntries[nIndex].nType;
110         if( !bWithFlags )
111             nType = nType & MID_FLAG_MASK;
112         return nType;
113     }
114 
115     /** Returns the namespace-key of an entry */
GetEntryNameSpace(sal_Int32 nIndex) const116     sal_uInt16 GetEntryNameSpace( sal_Int32 nIndex ) const
117     {
118         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
119         return aMapEntries[nIndex].nXMLNameSpace;
120     }
121 
122     /** Returns the 'local' XML-name of the entry */
GetEntryXMLName(sal_Int32 nIndex) const123     const ::rtl::OUString& GetEntryXMLName( sal_Int32 nIndex ) const
124     {
125         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
126         return aMapEntries[nIndex].sXMLAttributeName;
127     }
128 
129     /** Returns the entry API name */
GetEntryAPIName(sal_Int32 nIndex) const130     const ::rtl::OUString& GetEntryAPIName( sal_Int32 nIndex ) const
131     {
132         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
133         return aMapEntries[nIndex].sAPIPropertyName;
134     }
135 
136     /** returns the entry context id. -1 is a valid index here. */
GetEntryContextId(sal_Int32 nIndex) const137     sal_Int16 GetEntryContextId( sal_Int32 nIndex ) const
138     {
139         DBG_ASSERT( (nIndex >= -1) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
140         return nIndex == -1 ? 0 : aMapEntries[nIndex].nContextId;
141     }
142 
143     /** returns the earliest odf version for which this property should be exported. */
GetEarliestODFVersionForExport(sal_Int32 nIndex) const144     SvtSaveOptions::ODFDefaultVersion GetEarliestODFVersionForExport( sal_Int32 nIndex ) const
145     {
146         DBG_ASSERT( (nIndex >= -1) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
147         return nIndex == -1 ? SvtSaveOptions::ODFVER_UNKNOWN : aMapEntries[nIndex].nEarliestODFVersionForExport;
148     }
149 
150     /** Returns the index of an entry with the given XML-name and namespace
151         If there is no matching entry the method returns -1 */
152     sal_Int32 GetEntryIndex( sal_uInt16 nNamespace,
153                                    const ::rtl::OUString& rStrName,
154                                    sal_uInt32 nPropType,
155                                    sal_Int32 nStartAt = -1 ) const;
156 
157     /** Retrieves a PropertyHandler for that property wich placed at nIndex in the XMLPropertyMapEntry-array */
GetPropertyHandler(sal_Int32 nIndex) const158     const XMLPropertyHandler* GetPropertyHandler( sal_Int32 nIndex ) const
159     {
160         DBG_ASSERT( (nIndex >= 0) && (nIndex < (sal_Int32)aMapEntries.size() ), "illegal access to invalid entry!" );
161         return aMapEntries[nIndex].pHdl;
162     }
163 
164     /** import/export
165         This methods calls the respective im/export-method of the respective PropertyHandler. */
166     virtual sal_Bool exportXML( ::rtl::OUString& rStrExpValue,
167                                 const XMLPropertyState& rProperty,
168                                 const SvXMLUnitConverter& rUnitConverter ) const;
169     virtual sal_Bool importXML( const ::rtl::OUString& rStrImpValue,
170                                 XMLPropertyState& rProperty,
171                                 const SvXMLUnitConverter& rUnitConverter ) const;
172 
173     /** searches for an entry that matches the given api name, namespace and local name or -1 if nothing found */
174     sal_Int32 FindEntryIndex( const sal_Char* sApiName,
175                               sal_uInt16 nNameSpace,
176                               const ::rtl::OUString& sXMLName ) const;
177 
178     /** searches for an entry that matches the given ContextId or gives -1 if nothing found */
179     sal_Int32 FindEntryIndex( const sal_Int16 nContextId ) const;
180 
181     /** Remove an entry */
182     void RemoveEntry( sal_Int32 nIndex );
183 };
184 
185 #endif // _XMLOFF_PROPERTYSETMAPPER_HXX
186