1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/public/nsNameSpaceManager.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,111 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsNameSpaceManager_h___ 1.10 +#define nsNameSpaceManager_h___ 1.11 + 1.12 +#include "nsDataHashtable.h" 1.13 +#include "nsTArray.h" 1.14 + 1.15 +#include "mozilla/StaticPtr.h" 1.16 + 1.17 +class nsIAtom; 1.18 +class nsAString; 1.19 + 1.20 +#define kNameSpaceID_Unknown -1 1.21 +// 0 is special at C++, so use a static const int32_t for 1.22 +// kNameSpaceID_None to keep if from being cast to pointers 1.23 +// Note that the XBL cache assumes (and asserts) that it can treat a 1.24 +// single-byte value higher than kNameSpaceID_LastBuiltin specially. 1.25 +static const int32_t kNameSpaceID_None = 0; 1.26 +#define kNameSpaceID_XMLNS 1 // not really a namespace, but it needs to play the game 1.27 +#define kNameSpaceID_XML 2 1.28 +#define kNameSpaceID_XHTML 3 1.29 +#define kNameSpaceID_XLink 4 1.30 +#define kNameSpaceID_XSLT 5 1.31 +#define kNameSpaceID_XBL 6 1.32 +#define kNameSpaceID_MathML 7 1.33 +#define kNameSpaceID_RDF 8 1.34 +#define kNameSpaceID_XUL 9 1.35 +#define kNameSpaceID_SVG 10 1.36 +#define kNameSpaceID_LastBuiltin 10 // last 'built-in' namespace 1.37 + 1.38 +class nsNameSpaceKey : public PLDHashEntryHdr 1.39 +{ 1.40 +public: 1.41 + typedef const nsAString* KeyType; 1.42 + typedef const nsAString* KeyTypePointer; 1.43 + 1.44 + nsNameSpaceKey(KeyTypePointer aKey) : mKey(aKey) 1.45 + { 1.46 + } 1.47 + nsNameSpaceKey(const nsNameSpaceKey& toCopy) : mKey(toCopy.mKey) 1.48 + { 1.49 + } 1.50 + 1.51 + KeyType GetKey() const 1.52 + { 1.53 + return mKey; 1.54 + } 1.55 + bool KeyEquals(KeyType aKey) const 1.56 + { 1.57 + return mKey->Equals(*aKey); 1.58 + } 1.59 + 1.60 + static KeyTypePointer KeyToPointer(KeyType aKey) 1.61 + { 1.62 + return aKey; 1.63 + } 1.64 + static PLDHashNumber HashKey(KeyTypePointer aKey) { 1.65 + return mozilla::HashString(*aKey); 1.66 + } 1.67 + 1.68 + enum { 1.69 + ALLOW_MEMMOVE = true 1.70 + }; 1.71 + 1.72 +private: 1.73 + const nsAString* mKey; 1.74 +}; 1.75 + 1.76 +/** 1.77 + * The Name Space Manager tracks the association between a NameSpace 1.78 + * URI and the int32_t runtime id. Mappings between NameSpaces and 1.79 + * NameSpace prefixes are managed by nsINameSpaces. 1.80 + * 1.81 + * All NameSpace URIs are stored in a global table so that IDs are 1.82 + * consistent accross the app. NameSpace IDs are only consistent at runtime 1.83 + * ie: they are not guaranteed to be consistent accross app sessions. 1.84 + * 1.85 + * The nsNameSpaceManager needs to have a live reference for as long as 1.86 + * the NameSpace IDs are needed. 1.87 + * 1.88 + */ 1.89 + 1.90 +class nsNameSpaceManager 1.91 +{ 1.92 +public: 1.93 + virtual ~nsNameSpaceManager() {} 1.94 + 1.95 + virtual nsresult RegisterNameSpace(const nsAString& aURI, 1.96 + int32_t& aNameSpaceID); 1.97 + 1.98 + virtual nsresult GetNameSpaceURI(int32_t aNameSpaceID, nsAString& aURI); 1.99 + virtual int32_t GetNameSpaceID(const nsAString& aURI); 1.100 + 1.101 + virtual bool HasElementCreator(int32_t aNameSpaceID); 1.102 + 1.103 + static nsNameSpaceManager* GetInstance(); 1.104 +private: 1.105 + bool Init(); 1.106 + nsresult AddNameSpace(const nsAString& aURI, const int32_t aNameSpaceID); 1.107 + 1.108 + nsDataHashtable<nsNameSpaceKey,int32_t> mURIToIDTable; 1.109 + nsTArray< nsAutoPtr<nsString> > mURIArray; 1.110 + 1.111 + static mozilla::StaticAutoPtr<nsNameSpaceManager> sInstance; 1.112 +}; 1.113 + 1.114 +#endif // nsNameSpaceManager_h___