1*ecfe53c5SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ecfe53c5SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ecfe53c5SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ecfe53c5SAndrew Rist * distributed with this work for additional information 6*ecfe53c5SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ecfe53c5SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ecfe53c5SAndrew Rist * "License"); you may not use this file except in compliance 9*ecfe53c5SAndrew Rist * with the License. You may obtain a copy of the License at 10*ecfe53c5SAndrew Rist * 11*ecfe53c5SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ecfe53c5SAndrew Rist * 13*ecfe53c5SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ecfe53c5SAndrew Rist * software distributed under the License is distributed on an 15*ecfe53c5SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ecfe53c5SAndrew Rist * KIND, either express or implied. See the License for the 17*ecfe53c5SAndrew Rist * specific language governing permissions and limitations 18*ecfe53c5SAndrew Rist * under the License. 19*ecfe53c5SAndrew Rist * 20*ecfe53c5SAndrew Rist *************************************************************/ 21*ecfe53c5SAndrew Rist 22*ecfe53c5SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef _XMLOFF_NMSPMAP_HXX 25cdf0e10cSrcweir #define _XMLOFF_NMSPMAP_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "sal/config.h" 28cdf0e10cSrcweir #include "xmloff/dllapi.h" 29cdf0e10cSrcweir #include "sal/types.h" 30cdf0e10cSrcweir #include <rtl/ustring.hxx> 31cdf0e10cSrcweir #include <hash_map> 32cdf0e10cSrcweir #ifndef __SGI_STL_MAP 33cdf0e10cSrcweir #include <map> 34cdf0e10cSrcweir #endif 35cdf0e10cSrcweir #include <vos/ref.hxx> 36cdf0e10cSrcweir #include <cppuhelper/weak.hxx> 37cdf0e10cSrcweir 38cdf0e10cSrcweir #include <limits.h> 39cdf0e10cSrcweir 40cdf0e10cSrcweir const sal_uInt16 XML_NAMESPACE_XMLNS = (USHRT_MAX-2); 41cdf0e10cSrcweir const sal_uInt16 XML_NAMESPACE_NONE = (USHRT_MAX-1); 42cdf0e10cSrcweir const sal_uInt16 XML_NAMESPACE_UNKNOWN = (USHRT_MAX); 43cdf0e10cSrcweir const sal_uInt16 XML_NAMESPACE_UNKNOWN_FLAG = 0x8000; 44cdf0e10cSrcweir 45cdf0e10cSrcweir class NameSpaceEntry : public cppu::OWeakObject 46cdf0e10cSrcweir { 47cdf0e10cSrcweir public: 48cdf0e10cSrcweir // sName refers to the full namespace name 49cdf0e10cSrcweir ::rtl::OUString sName; 50cdf0e10cSrcweir // sPrefix is the prefix used to declare a given item to be from a given namespace 51cdf0e10cSrcweir ::rtl::OUString sPrefix; 52cdf0e10cSrcweir // nKey is the unique identifier of a namespace 53cdf0e10cSrcweir sal_uInt16 nKey; 54cdf0e10cSrcweir }; 55cdf0e10cSrcweir 56cdf0e10cSrcweir struct OUStringEqFunc 57cdf0e10cSrcweir { operator ()OUStringEqFunc58cdf0e10cSrcweir sal_Bool operator()( const rtl::OUString &r1, 59cdf0e10cSrcweir const rtl::OUString &r2) const 60cdf0e10cSrcweir { 61cdf0e10cSrcweir return r1 == r2; 62cdf0e10cSrcweir } 63cdf0e10cSrcweir }; 64cdf0e10cSrcweir 65cdf0e10cSrcweir struct uInt32lt 66cdf0e10cSrcweir { operator ()uInt32lt67cdf0e10cSrcweir sal_Bool operator()( const sal_uInt32 &r1, 68cdf0e10cSrcweir const sal_uInt32 &r2) const 69cdf0e10cSrcweir { 70cdf0e10cSrcweir return r1 < r2; 71cdf0e10cSrcweir } 72cdf0e10cSrcweir }; 73cdf0e10cSrcweir typedef ::std::pair < sal_uInt16, const ::rtl::OUString* > QNamePair; 74cdf0e10cSrcweir 75cdf0e10cSrcweir struct QNamePairHash 76cdf0e10cSrcweir { operator ()QNamePairHash77cdf0e10cSrcweir size_t operator()( const QNamePair &r1 ) const 78cdf0e10cSrcweir { 79cdf0e10cSrcweir return (size_t) r1.second->hashCode() + r1.first; 80cdf0e10cSrcweir } 81cdf0e10cSrcweir }; 82cdf0e10cSrcweir struct QNamePairEq 83cdf0e10cSrcweir { operator ()QNamePairEq84cdf0e10cSrcweir bool operator()( const QNamePair &r1, 85cdf0e10cSrcweir const QNamePair &r2 ) const 86cdf0e10cSrcweir { 87cdf0e10cSrcweir return r1.first == r2.first && *(r1.second) == *(r2.second); 88cdf0e10cSrcweir } 89cdf0e10cSrcweir }; 90cdf0e10cSrcweir 91cdf0e10cSrcweir typedef ::std::hash_map < QNamePair, ::rtl::OUString, QNamePairHash, QNamePairEq > QNameCache; 92cdf0e10cSrcweir typedef ::std::hash_map < ::rtl::OUString, ::vos::ORef <NameSpaceEntry >, rtl::OUStringHash, OUStringEqFunc > NameSpaceHash; 93cdf0e10cSrcweir typedef ::std::map < sal_uInt16, ::vos::ORef < NameSpaceEntry >, uInt32lt > NameSpaceMap; 94cdf0e10cSrcweir 95cdf0e10cSrcweir class XMLOFF_DLLPUBLIC SvXMLNamespaceMap 96cdf0e10cSrcweir { 97cdf0e10cSrcweir const ::rtl::OUString sXMLNS; 98cdf0e10cSrcweir const ::rtl::OUString sEmpty; 99cdf0e10cSrcweir 100cdf0e10cSrcweir NameSpaceHash aNameHash, aNameCache; 101cdf0e10cSrcweir NameSpaceMap aNameMap; 102cdf0e10cSrcweir QNameCache aQNameCache; 103cdf0e10cSrcweir SAL_DLLPRIVATE sal_uInt16 _Add( const rtl::OUString& rPrefix, const rtl::OUString &rName, sal_uInt16 nKey ); 104cdf0e10cSrcweir 105cdf0e10cSrcweir public: 106cdf0e10cSrcweir 107cdf0e10cSrcweir SvXMLNamespaceMap(); 108cdf0e10cSrcweir ~SvXMLNamespaceMap(); 109cdf0e10cSrcweir 110cdf0e10cSrcweir SvXMLNamespaceMap( const SvXMLNamespaceMap& ); 111cdf0e10cSrcweir 112cdf0e10cSrcweir void operator =( const SvXMLNamespaceMap& rCmp ); 113cdf0e10cSrcweir int operator ==( const SvXMLNamespaceMap& rCmp ) const; 114cdf0e10cSrcweir 115cdf0e10cSrcweir sal_uInt16 Add( const ::rtl::OUString& rPrefix, 116cdf0e10cSrcweir const ::rtl::OUString& rName, 117cdf0e10cSrcweir sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN ); 118cdf0e10cSrcweir sal_uInt16 AddIfKnown( const ::rtl::OUString& rPrefix, 119cdf0e10cSrcweir const ::rtl::OUString& rName ); 120cdf0e10cSrcweir 121cdf0e10cSrcweir sal_uInt16 GetKeyByName( const ::rtl::OUString& rName ) const; 122cdf0e10cSrcweir const ::rtl::OUString& GetNameByKey( sal_uInt16 nKey ) const; 123cdf0e10cSrcweir 124cdf0e10cSrcweir sal_uInt16 GetKeyByPrefix( const ::rtl::OUString& rPrefix ) const; 125cdf0e10cSrcweir const ::rtl::OUString& GetPrefixByKey( sal_uInt16 nKey ) const; 126cdf0e10cSrcweir 127cdf0e10cSrcweir ::rtl::OUString GetQNameByKey( sal_uInt16 nKey, 128cdf0e10cSrcweir const ::rtl::OUString& rLocalName, 129cdf0e10cSrcweir sal_Bool bCache = sal_True) const; 130cdf0e10cSrcweir 131cdf0e10cSrcweir ::rtl::OUString GetAttrNameByKey( sal_uInt16 nKey ) const; 132cdf0e10cSrcweir 133cdf0e10cSrcweir /* This will replace the version with the unused 5th default parameter */ 134cdf0e10cSrcweir sal_uInt16 _GetKeyByAttrName( const ::rtl::OUString& rAttrName, 135cdf0e10cSrcweir ::rtl::OUString *pPrefix, 136cdf0e10cSrcweir ::rtl::OUString *pLocalName, 137cdf0e10cSrcweir ::rtl::OUString *pNamespace = 0, 138cdf0e10cSrcweir sal_Bool bCache = sal_True) const; 139cdf0e10cSrcweir 140cdf0e10cSrcweir /* This will replace the version with the unused 3rd default parameter */ 141cdf0e10cSrcweir sal_uInt16 _GetKeyByAttrName( const ::rtl::OUString& rAttrName, 142cdf0e10cSrcweir ::rtl::OUString *pLocalName = 0, 143cdf0e10cSrcweir sal_Bool bCache = sal_True) const; 144cdf0e10cSrcweir 145cdf0e10cSrcweir sal_uInt16 GetFirstKey() const; 146cdf0e10cSrcweir sal_uInt16 GetNextKey( sal_uInt16 nOldKey ) const; 147cdf0e10cSrcweir 148cdf0e10cSrcweir /* Give access to all namespace definitions, including multiple entries 149cdf0e10cSrcweir for the same key (needed for saving sheets separately in Calc). 150cdf0e10cSrcweir This might be replaced by a better interface later. */ GetAllEntries() const151cdf0e10cSrcweir const NameSpaceHash& GetAllEntries() const { return aNameHash; } 152cdf0e10cSrcweir 153cdf0e10cSrcweir static sal_Bool NormalizeOasisURN( ::rtl::OUString& rName ); 154cdf0e10cSrcweir static sal_Bool NormalizeW3URI( ::rtl::OUString& rName ); 155cdf0e10cSrcweir static sal_Bool NormalizeURI( ::rtl::OUString& rName ); 156cdf0e10cSrcweir 157cdf0e10cSrcweir /* deprecated */ sal_Bool AddAtIndex( sal_uInt16 nIdx, const ::rtl::OUString& rPrefix, 158cdf0e10cSrcweir const ::rtl::OUString& rName, sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN ); 159cdf0e10cSrcweir /* deprecated */ sal_Bool AddAtIndex( sal_uInt16 nIdx, const sal_Char *pPrefix, 160cdf0e10cSrcweir const sal_Char *pName, sal_uInt16 nKey = XML_NAMESPACE_UNKNOWN ); 161cdf0e10cSrcweir /* deprecated */ sal_uInt16 GetIndexByKey( sal_uInt16 nKey ) const; 162cdf0e10cSrcweir /* deprecated */ sal_uInt16 GetKeyByIndex( sal_uInt16 nIdx ) const; 163cdf0e10cSrcweir /* deprecated */ sal_uInt16 GetIndexByPrefix( const ::rtl::OUString& rPrefix ) const; 164cdf0e10cSrcweir /* deprecated */ sal_uInt16 GetFirstIndex() const; 165cdf0e10cSrcweir /* deprecated */ sal_uInt16 GetNextIndex( sal_uInt16 nOldIdx ) const; 166cdf0e10cSrcweir /* deprecated */ const ::rtl::OUString& GetPrefixByIndex( sal_uInt16 nIdx ) const; 167cdf0e10cSrcweir /* deprecated */ const ::rtl::OUString& GetNameByIndex( sal_uInt16 nIdx ) const; 168cdf0e10cSrcweir /* deprecated */ ::rtl::OUString GetAttrNameByIndex( sal_uInt16 nIdx ) const; 169cdf0e10cSrcweir /* deprecated */ ::rtl::OUString GetQNameByIndex( sal_uInt16 nIdx, 170cdf0e10cSrcweir const ::rtl::OUString& rLocalName ) const; 171cdf0e10cSrcweir /* deprecated */ sal_uInt16 GetKeyByAttrName( const ::rtl::OUString& rAttrName, 172cdf0e10cSrcweir ::rtl::OUString *pPrefix, 173cdf0e10cSrcweir ::rtl::OUString *pLocalName, 174cdf0e10cSrcweir ::rtl::OUString *pNamespace=0, 175cdf0e10cSrcweir sal_uInt16 nIdxGuess = USHRT_MAX ) const; 176cdf0e10cSrcweir /* deprecated */ sal_uInt16 GetKeyByAttrName( const ::rtl::OUString& rAttrName, 177cdf0e10cSrcweir ::rtl::OUString *pLocalName = 0, 178cdf0e10cSrcweir sal_uInt16 nIdxGuess = USHRT_MAX ) const; 179cdf0e10cSrcweir }; 180cdf0e10cSrcweir 181cdf0e10cSrcweir #endif // _XMLOFF_NMSPMAP_HXX 182