xref: /AOO41X/main/xmloff/inc/xmloff/nmspmap.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_NMSPMAP_HXX
25 #define _XMLOFF_NMSPMAP_HXX
26 
27 #include "sal/config.h"
28 #include "xmloff/dllapi.h"
29 #include "sal/types.h"
30 #include <rtl/ustring.hxx>
31 #include <hash_map>
32 #ifndef __SGI_STL_MAP
33 #include <map>
34 #endif
35 #include <vos/ref.hxx>
36 #include <cppuhelper/weak.hxx>
37 
38 #include <limits.h>
39 
40 const sal_uInt16 XML_NAMESPACE_XMLNS  = (USHRT_MAX-2);
41 const sal_uInt16 XML_NAMESPACE_NONE  = (USHRT_MAX-1);
42 const sal_uInt16 XML_NAMESPACE_UNKNOWN  = (USHRT_MAX);
43 const sal_uInt16 XML_NAMESPACE_UNKNOWN_FLAG  = 0x8000;
44 
45 class NameSpaceEntry : public cppu::OWeakObject
46 {
47 public:
48     // sName refers to the full namespace name
49     ::rtl::OUString     sName;
50     // sPrefix is the prefix used to declare a given item to be from a given namespace
51     ::rtl::OUString     sPrefix;
52     // nKey is the unique identifier of a namespace
53     sal_uInt16          nKey;
54 };
55 
56 struct OUStringEqFunc
57 {
operator ()OUStringEqFunc58     sal_Bool operator()( const rtl::OUString &r1,
59                          const rtl::OUString &r2) const
60     {
61         return r1 == r2;
62     }
63 };
64 
65 struct uInt32lt
66 {
operator ()uInt32lt67     sal_Bool operator()( const sal_uInt32 &r1,
68                          const sal_uInt32 &r2) const
69     {
70         return r1 < r2;
71     }
72 };
73 typedef ::std::pair < sal_uInt16, const ::rtl::OUString* > QNamePair;
74 
75 struct QNamePairHash
76 {
operator ()QNamePairHash77     size_t operator()( const QNamePair &r1 ) const
78     {
79         return (size_t) r1.second->hashCode() + r1.first;
80     }
81 };
82 struct QNamePairEq
83 {
operator ()QNamePairEq84     bool operator()( const QNamePair &r1,
85                      const QNamePair &r2 ) const
86     {
87         return r1.first == r2.first && *(r1.second) == *(r2.second);
88     }
89 };
90 
91 typedef ::std::hash_map < QNamePair, ::rtl::OUString, QNamePairHash, QNamePairEq > QNameCache;
92 typedef ::std::hash_map < ::rtl::OUString, ::vos::ORef <NameSpaceEntry >, rtl::OUStringHash, OUStringEqFunc > NameSpaceHash;
93 typedef ::std::map < sal_uInt16, ::vos::ORef < NameSpaceEntry >, uInt32lt > NameSpaceMap;
94 
95 class XMLOFF_DLLPUBLIC SvXMLNamespaceMap
96 {
97     const ::rtl::OUString       sXMLNS;
98     const ::rtl::OUString       sEmpty;
99 
100     NameSpaceHash               aNameHash, aNameCache;
101     NameSpaceMap                aNameMap;
102     QNameCache                  aQNameCache;
103     SAL_DLLPRIVATE sal_uInt16 _Add( const rtl::OUString& rPrefix, const rtl::OUString &rName, sal_uInt16 nKey );
104 
105 public:
106 
107     SvXMLNamespaceMap();
108     ~SvXMLNamespaceMap();
109 
110     SvXMLNamespaceMap( const SvXMLNamespaceMap& );
111 
112     void operator =( const SvXMLNamespaceMap& rCmp );
113     int operator ==( const SvXMLNamespaceMap& rCmp ) const;
114 
115     sal_uInt16 Add( const ::rtl::OUString& rPrefix,
116                 const ::rtl::OUString& rName,
117                 sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN );
118     sal_uInt16 AddIfKnown( const ::rtl::OUString& rPrefix,
119                 const ::rtl::OUString& rName );
120 
121     sal_uInt16 GetKeyByName( const ::rtl::OUString& rName ) const;
122     const ::rtl::OUString& GetNameByKey( sal_uInt16 nKey ) const;
123 
124     sal_uInt16 GetKeyByPrefix( const ::rtl::OUString& rPrefix ) const;
125     const ::rtl::OUString& GetPrefixByKey( sal_uInt16 nKey ) const;
126 
127     ::rtl::OUString GetQNameByKey( sal_uInt16 nKey,
128                            const ::rtl::OUString& rLocalName,
129                            sal_Bool bCache = sal_True) const;
130 
131     ::rtl::OUString GetAttrNameByKey( sal_uInt16 nKey ) const;
132 
133     /* This will replace the version with the unused 5th default parameter */
134     sal_uInt16 _GetKeyByAttrName( const ::rtl::OUString& rAttrName,
135                              ::rtl::OUString *pPrefix,
136                              ::rtl::OUString *pLocalName,
137                              ::rtl::OUString *pNamespace = 0,
138                              sal_Bool bCache = sal_True) const;
139 
140     /* This will replace the version with the unused 3rd default parameter */
141     sal_uInt16 _GetKeyByAttrName( const ::rtl::OUString& rAttrName,
142                              ::rtl::OUString *pLocalName = 0,
143                              sal_Bool bCache = sal_True) const;
144 
145     sal_uInt16 GetFirstKey() const;
146     sal_uInt16 GetNextKey( sal_uInt16 nOldKey ) const;
147 
148     /* Give access to all namespace definitions, including multiple entries
149        for the same key (needed for saving sheets separately in Calc).
150        This might be replaced by a better interface later. */
GetAllEntries() const151     const NameSpaceHash& GetAllEntries() const { return aNameHash; }
152 
153     static sal_Bool NormalizeOasisURN( ::rtl::OUString& rName );
154     static sal_Bool NormalizeW3URI( ::rtl::OUString& rName );
155     static sal_Bool NormalizeURI( ::rtl::OUString& rName );
156 
157 /* deprecated */ sal_Bool AddAtIndex( sal_uInt16 nIdx, const ::rtl::OUString& rPrefix,
158                      const ::rtl::OUString& rName, sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN );
159 /* deprecated */ sal_Bool AddAtIndex( sal_uInt16 nIdx, const sal_Char *pPrefix,
160                      const sal_Char *pName, sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN );
161 /* deprecated */ sal_uInt16 GetIndexByKey( sal_uInt16 nKey ) const;
162 /* deprecated */ sal_uInt16 GetKeyByIndex( sal_uInt16 nIdx ) const;
163 /* deprecated */ sal_uInt16 GetIndexByPrefix( const ::rtl::OUString& rPrefix ) const;
164 /* deprecated */ sal_uInt16 GetFirstIndex() const;
165 /* deprecated */ sal_uInt16 GetNextIndex( sal_uInt16 nOldIdx ) const;
166 /* deprecated */ const ::rtl::OUString& GetPrefixByIndex( sal_uInt16 nIdx ) const;
167 /* deprecated */ const ::rtl::OUString& GetNameByIndex( sal_uInt16 nIdx ) const;
168 /* deprecated */ ::rtl::OUString GetAttrNameByIndex( sal_uInt16 nIdx ) const;
169 /* deprecated */ ::rtl::OUString GetQNameByIndex( sal_uInt16 nIdx,
170                             const ::rtl::OUString& rLocalName ) const;
171 /* deprecated */ sal_uInt16 GetKeyByAttrName( const ::rtl::OUString& rAttrName,
172                              ::rtl::OUString *pPrefix,
173                              ::rtl::OUString *pLocalName,
174                              ::rtl::OUString *pNamespace=0,
175                              sal_uInt16 nIdxGuess = USHRT_MAX ) const;
176 /* deprecated */ sal_uInt16 GetKeyByAttrName( const ::rtl::OUString& rAttrName,
177                              ::rtl::OUString *pLocalName = 0,
178                              sal_uInt16 nIdxGuess = USHRT_MAX ) const;
179 };
180 
181 #endif  //  _XMLOFF_NMSPMAP_HXX
182