KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > pattern > NamespaceNode


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xpath.pattern;
30
31 import com.caucho.vfs.WriteStream;
32 import com.caucho.xml.CauchoElement;
33 import com.caucho.xml.CauchoNode;
34 import com.caucho.xml.QAbstractNode;
35 import com.caucho.xml.QAttr;
36
37 import org.w3c.dom.DOMException JavaDoc;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.NamedNodeMap JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42
43 import java.io.IOException JavaDoc;
44 import java.util.HashMap JavaDoc;
45
46 /**
47  * A pseudo-node for handling the namespace:: axis.
48  */

49 public class NamespaceNode extends QAbstractNode implements CauchoNode {
50   Node _parent;
51
52   NamespaceNode _next;
53   NamespaceNode _prev;
54
55   String JavaDoc _local;
56   String JavaDoc _name;
57   String JavaDoc _url;
58
59   /**
60    * Creates a new namespace node.
61    */

62   public NamespaceNode(Node parent, NamespaceNode next,
63                        String JavaDoc prefix, String JavaDoc url)
64   {
65     _parent = parent;
66     _next = next;
67     if (next != null)
68       next._prev = this;
69     _local = prefix;
70     if (prefix == null || prefix.equals(""))
71       _name = "xmlns";
72     else
73       _name = ("xmlns:" + prefix).intern();
74     _url = url;
75   }
76
77   /**
78    * Creates a list of namespace nodes based on the current element.
79    * The list is the list of namespaces active for that element.
80    */

81   static NamespaceNode create(Node node)
82   {
83     Node top = node;
84     NamespaceNode nodes = null;
85     HashMap JavaDoc<String JavaDoc,String JavaDoc> map = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
86
87     for (; node instanceof CauchoElement; node = node.getParentNode()) {
88       CauchoElement elt = (CauchoElement) node;
89
90       String JavaDoc prefix = elt.getPrefix();
91       String JavaDoc url = elt.getNamespaceURI();
92       if (url == null)
93     url = "";
94       
95       if (map.get(prefix) == null) {
96     map.put(prefix, url);
97     if (! url.equals(""))
98       nodes = new NamespaceNode(top, nodes, prefix, url);
99       }
100
101       QAttr attr = (QAttr) elt.getFirstAttribute();
102       for (; attr != null; attr = (QAttr) attr.getNextSibling()) {
103     String JavaDoc name = attr.getNodeName();
104     prefix = null;
105     url = "";
106
107     if (name.startsWith("xmlns:")) {
108       prefix = name.substring(6);
109       url = attr.getNodeValue();
110     }
111     else if (name.equals("xmlns")) {
112       prefix = "";
113       url = attr.getNodeValue();
114     }
115     else {
116       prefix = attr.getPrefix();
117       url = attr.getNamespaceURI();
118     }
119     
120     if (url == null)
121       url = "";
122       
123     if (map.get(prefix) == null) {
124       map.put(prefix, url);
125       if (! url.equals(""))
126         nodes = new NamespaceNode(top, nodes, prefix, url);
127     }
128       }
129     }
130
131     return nodes;
132   }
133
134   public short getNodeType()
135   {
136     return ATTRIBUTE_NODE;
137   }
138
139   public String JavaDoc getNodeName()
140   {
141     return _name;
142   }
143
144   public String JavaDoc getPrefix()
145   {
146     return "xmlns";
147   }
148
149   public void setPrefix(String JavaDoc prefix)
150   {
151   }
152
153   public boolean supports(String JavaDoc feature, String JavaDoc version)
154   {
155     return false;
156   }
157   
158   public String JavaDoc getCanonicalName()
159   {
160     return "";
161   }
162
163   public String JavaDoc getLocalName()
164   {
165     return _local;
166   }
167
168   public String JavaDoc getNamespaceURI()
169   {
170     return null;
171   }
172
173   public String JavaDoc getNodeValue()
174   {
175     return _url;
176   }
177
178   public Node getParentNode()
179   {
180     return _parent;
181   }
182
183   public Node getPreviousSibling()
184   {
185     return _prev;
186   }
187
188   public Node getNextSibling()
189   {
190     return _next;
191   }
192
193   // The following are just stubs to conform to the api
194

195   public void setLocation(String JavaDoc filename, int line, int column)
196   {
197   }
198
199   public String JavaDoc getFilename()
200   {
201     return null;
202   }
203
204   public int getLine()
205   {
206     return 0;
207   }
208
209   public int getColumn() { return 0; }
210
211   public Document JavaDoc getOwnerDocument() { return null; }
212
213   public void setNodeValue(String JavaDoc value) {}
214
215   public NodeList JavaDoc getChildNodes() { return null; }
216
217   public Node getFirstChild() { return null; }
218
219   public Node getLastChild() { return null; }
220
221   public NamedNodeMap JavaDoc getAttributes() { return null; }
222
223   public Node insertBefore(Node newChild, Node refChild)
224   {
225     return null;
226   }
227
228   public Node replaceChild(Node newChild, Node refChild)
229   {
230     return null;
231   }
232
233   public Node removeChild(Node oldChild) throws DOMException JavaDoc
234   {
235     return null;
236   }
237
238   public Node appendChild(Node newNode) throws DOMException JavaDoc
239   {
240     return null;
241   }
242
243   public boolean hasChildNodes() { return false; }
244
245   public boolean equals(Node arg, boolean deep)
246   {
247     return this == arg;
248   }
249
250   public Node cloneNode(boolean deep)
251   {
252     return null;
253   }
254
255   public void normalize()
256   {
257   }
258
259   public String JavaDoc getTextValue() { return getNodeValue(); }
260   public boolean checkValid() { return false; }
261
262   public void print(WriteStream out) throws IOException JavaDoc
263   {
264   }
265
266   public void printPretty(WriteStream out) throws IOException JavaDoc
267   {
268   }
269
270   public void printHtml(WriteStream out) throws IOException JavaDoc
271   {
272   }
273
274   public boolean isSupported(String JavaDoc feature, String JavaDoc version)
275   {
276     return false;
277   }
278
279   public boolean hasAttributes()
280   {
281     return false;
282   }
283
284   public String JavaDoc toString()
285   {
286     return "NamespaceNode[" + _name + " " + _url + "]";
287   }
288 }
289
Popular Tags