1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsXMLNameSpaceMap_h_
8 #define nsXMLNameSpaceMap_h_
9 
10 #include "nsString.h"
11 #include "nsTArray.h"
12 #include "nsCOMPtr.h"
13 #include "nsAtom.h"
14 
15 struct nsNameSpaceEntry {
nsNameSpaceEntrynsNameSpaceEntry16   explicit nsNameSpaceEntry(nsAtom *aPrefix) : prefix(aPrefix) {}
17 
18   RefPtr<nsAtom> prefix;
19   MOZ_INIT_OUTSIDE_CTOR int32_t nameSpaceID;
20 };
21 
22 /**
23  * nsXMLNameSpaceMap contains a set of prefixes which are mapped onto
24  * namespaces.  It allows the set to be searched by prefix or by namespace ID.
25  */
26 class nsXMLNameSpaceMap {
27  public:
28   /**
29    * Allocates a new nsXMLNameSpaceMap (with new()) and if aForXML is
30    * true initializes it with the xmlns and xml namespaces.
31    */
32   static nsXMLNameSpaceMap *Create(bool aForXML);
33 
34   /**
35    * Add a prefix and its corresponding namespace ID to the map.
36    * Passing a null |aPrefix| corresponds to the default namespace, which may
37    * be set to something other than kNameSpaceID_None.
38    */
39   nsresult AddPrefix(nsAtom *aPrefix, int32_t aNameSpaceID);
40 
41   /**
42    * Add a prefix and a namespace URI to the map.  The URI will be converted
43    * to its corresponding namespace ID.
44    */
45   nsresult AddPrefix(nsAtom *aPrefix, nsString &aURI);
46 
47   /*
48    * Returns the namespace ID for the given prefix, if it is in the map.
49    * If |aPrefix| is null and is not in the map, then a null namespace
50    * (kNameSpaceID_None) is returned.  If |aPrefix| is non-null and is not in
51    * the map, then kNameSpaceID_Unknown is returned.
52    */
53   int32_t FindNameSpaceID(nsAtom *aPrefix) const;
54 
55   /**
56    * If the given namespace ID is in the map, then the first prefix which
57    * maps to that namespace is returned.  Otherwise, null is returned.
58    */
59   nsAtom *FindPrefix(int32_t aNameSpaceID) const;
60 
61   /* Removes all prefix mappings. */
62   void Clear();
63 
~nsXMLNameSpaceMap()64   ~nsXMLNameSpaceMap() { Clear(); }
65 
66  private:
67   nsXMLNameSpaceMap();  // use Create() to create new instances
68 
69   nsTArray<nsNameSpaceEntry> mNameSpaces;
70 };
71 
72 #endif
73