001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xpath.pattern;
030:
031: import com.caucho.vfs.WriteStream;
032: import com.caucho.xml.CauchoElement;
033: import com.caucho.xml.CauchoNode;
034: import com.caucho.xml.QAbstractNode;
035: import com.caucho.xml.QAttr;
036:
037: import org.w3c.dom.DOMException;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.NamedNodeMap;
040: import org.w3c.dom.Node;
041: import org.w3c.dom.NodeList;
042:
043: import java.io.IOException;
044: import java.util.HashMap;
045:
046: /**
047: * A pseudo-node for handling the namespace:: axis.
048: */
049: public class NamespaceNode extends QAbstractNode implements CauchoNode {
050: Node _parent;
051:
052: NamespaceNode _next;
053: NamespaceNode _prev;
054:
055: String _local;
056: String _name;
057: String _url;
058:
059: /**
060: * Creates a new namespace node.
061: */
062: public NamespaceNode(Node parent, NamespaceNode next,
063: String prefix, String url) {
064: _parent = parent;
065: _next = next;
066: if (next != null)
067: next._prev = this ;
068: _local = prefix;
069: if (prefix == null || prefix.equals(""))
070: _name = "xmlns";
071: else
072: _name = ("xmlns:" + prefix).intern();
073: _url = url;
074: }
075:
076: /**
077: * Creates a list of namespace nodes based on the current element.
078: * The list is the list of namespaces active for that element.
079: */
080: static NamespaceNode create(Node node) {
081: Node top = node;
082: NamespaceNode nodes = null;
083: HashMap<String, String> map = new HashMap<String, String>();
084:
085: for (; node instanceof CauchoElement; node = node
086: .getParentNode()) {
087: CauchoElement elt = (CauchoElement) node;
088:
089: String prefix = elt.getPrefix();
090: String url = elt.getNamespaceURI();
091: if (url == null)
092: url = "";
093:
094: if (map.get(prefix) == null) {
095: map.put(prefix, url);
096: if (!url.equals(""))
097: nodes = new NamespaceNode(top, nodes, prefix, url);
098: }
099:
100: QAttr attr = (QAttr) elt.getFirstAttribute();
101: for (; attr != null; attr = (QAttr) attr.getNextSibling()) {
102: String name = attr.getNodeName();
103: prefix = null;
104: url = "";
105:
106: if (name.startsWith("xmlns:")) {
107: prefix = name.substring(6);
108: url = attr.getNodeValue();
109: } else if (name.equals("xmlns")) {
110: prefix = "";
111: url = attr.getNodeValue();
112: } else {
113: prefix = attr.getPrefix();
114: url = attr.getNamespaceURI();
115: }
116:
117: if (url == null)
118: url = "";
119:
120: if (map.get(prefix) == null) {
121: map.put(prefix, url);
122: if (!url.equals(""))
123: nodes = new NamespaceNode(top, nodes, prefix,
124: url);
125: }
126: }
127: }
128:
129: return nodes;
130: }
131:
132: public short getNodeType() {
133: return ATTRIBUTE_NODE;
134: }
135:
136: public String getNodeName() {
137: return _name;
138: }
139:
140: public String getPrefix() {
141: return "xmlns";
142: }
143:
144: public void setPrefix(String prefix) {
145: }
146:
147: public boolean supports(String feature, String version) {
148: return false;
149: }
150:
151: public String getCanonicalName() {
152: return "";
153: }
154:
155: public String getLocalName() {
156: return _local;
157: }
158:
159: public String getNamespaceURI() {
160: return null;
161: }
162:
163: public String getNodeValue() {
164: return _url;
165: }
166:
167: public Node getParentNode() {
168: return _parent;
169: }
170:
171: public Node getPreviousSibling() {
172: return _prev;
173: }
174:
175: public Node getNextSibling() {
176: return _next;
177: }
178:
179: // The following are just stubs to conform to the api
180:
181: public void setLocation(String filename, int line, int column) {
182: }
183:
184: public String getFilename() {
185: return null;
186: }
187:
188: public int getLine() {
189: return 0;
190: }
191:
192: public int getColumn() {
193: return 0;
194: }
195:
196: public Document getOwnerDocument() {
197: return null;
198: }
199:
200: public void setNodeValue(String value) {
201: }
202:
203: public NodeList getChildNodes() {
204: return null;
205: }
206:
207: public Node getFirstChild() {
208: return null;
209: }
210:
211: public Node getLastChild() {
212: return null;
213: }
214:
215: public NamedNodeMap getAttributes() {
216: return null;
217: }
218:
219: public Node insertBefore(Node newChild, Node refChild) {
220: return null;
221: }
222:
223: public Node replaceChild(Node newChild, Node refChild) {
224: return null;
225: }
226:
227: public Node removeChild(Node oldChild) throws DOMException {
228: return null;
229: }
230:
231: public Node appendChild(Node newNode) throws DOMException {
232: return null;
233: }
234:
235: public boolean hasChildNodes() {
236: return false;
237: }
238:
239: public boolean equals(Node arg, boolean deep) {
240: return this == arg;
241: }
242:
243: public Node cloneNode(boolean deep) {
244: return null;
245: }
246:
247: public void normalize() {
248: }
249:
250: public String getTextValue() {
251: return getNodeValue();
252: }
253:
254: public boolean checkValid() {
255: return false;
256: }
257:
258: public void print(WriteStream out) throws IOException {
259: }
260:
261: public void printPretty(WriteStream out) throws IOException {
262: }
263:
264: public void printHtml(WriteStream out) throws IOException {
265: }
266:
267: public boolean isSupported(String feature, String version) {
268: return false;
269: }
270:
271: public boolean hasAttributes() {
272: return false;
273: }
274:
275: public String toString() {
276: return "NamespaceNode[" + _name + " " + _url + "]";
277: }
278: }
|