1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Jonas Sicking.
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Jonas Sicking <jonas@sicking.cc>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "txNamespaceMap.h"
40 : #include "nsGkAtoms.h"
41 : #include "txXPathNode.h"
42 :
43 0 : txNamespaceMap::txNamespaceMap()
44 : {
45 0 : }
46 :
47 0 : txNamespaceMap::txNamespaceMap(const txNamespaceMap& aOther)
48 0 : : mPrefixes(aOther.mPrefixes)
49 : {
50 0 : mNamespaces = aOther.mNamespaces; //bah! I want a copy-constructor!
51 0 : }
52 :
53 : nsresult
54 0 : txNamespaceMap::mapNamespace(nsIAtom* aPrefix, const nsAString& aNamespaceURI)
55 : {
56 0 : nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? nsnull : aPrefix;
57 :
58 : PRInt32 nsId;
59 0 : if (prefix && aNamespaceURI.IsEmpty()) {
60 : // Remove the mapping
61 0 : PRInt32 index = mPrefixes.IndexOf(prefix);
62 0 : if (index >= 0) {
63 0 : mPrefixes.RemoveObjectAt(index);
64 0 : mNamespaces.RemoveElementAt(index);
65 : }
66 :
67 0 : return NS_OK;
68 : }
69 :
70 0 : if (aNamespaceURI.IsEmpty()) {
71 : // Set default to empty namespace
72 0 : nsId = kNameSpaceID_None;
73 : }
74 : else {
75 0 : nsId = txNamespaceManager::getNamespaceID(aNamespaceURI);
76 0 : NS_ENSURE_FALSE(nsId == kNameSpaceID_Unknown, NS_ERROR_FAILURE);
77 : }
78 :
79 : // Check if the mapping already exists
80 0 : PRInt32 index = mPrefixes.IndexOf(prefix);
81 0 : if (index >= 0) {
82 0 : mNamespaces.ElementAt(index) = nsId;
83 :
84 0 : return NS_OK;
85 : }
86 :
87 : // New mapping
88 0 : if (!mPrefixes.AppendObject(prefix)) {
89 0 : return NS_ERROR_OUT_OF_MEMORY;
90 : }
91 :
92 0 : if (mNamespaces.AppendElement(nsId) == nsnull) {
93 0 : mPrefixes.RemoveObjectAt(mPrefixes.Count() - 1);
94 :
95 0 : return NS_ERROR_OUT_OF_MEMORY;
96 : }
97 :
98 0 : return NS_OK;
99 : }
100 :
101 : PRInt32
102 0 : txNamespaceMap::lookupNamespace(nsIAtom* aPrefix)
103 : {
104 0 : if (aPrefix == nsGkAtoms::xml) {
105 0 : return kNameSpaceID_XML;
106 : }
107 :
108 0 : nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? 0 : aPrefix;
109 :
110 0 : PRInt32 index = mPrefixes.IndexOf(prefix);
111 0 : if (index >= 0) {
112 0 : return mNamespaces.SafeElementAt(index, kNameSpaceID_Unknown);
113 : }
114 :
115 0 : if (!prefix) {
116 0 : return kNameSpaceID_None;
117 : }
118 :
119 0 : return kNameSpaceID_Unknown;
120 : }
121 :
122 : PRInt32
123 0 : txNamespaceMap::lookupNamespaceWithDefault(const nsAString& aPrefix)
124 : {
125 0 : nsCOMPtr<nsIAtom> prefix = do_GetAtom(aPrefix);
126 0 : if (prefix != nsGkAtoms::_poundDefault) {
127 0 : return lookupNamespace(prefix);
128 : }
129 :
130 0 : return lookupNamespace(nsnull);
131 : }
|