1 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 ; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.NamedNodeMap ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.NodeList ; 42 43 import java.io.IOException ; 44 import java.util.HashMap ; 45 46 49 public class NamespaceNode extends QAbstractNode implements CauchoNode { 50 Node _parent; 51 52 NamespaceNode _next; 53 NamespaceNode _prev; 54 55 String _local; 56 String _name; 57 String _url; 58 59 62 public NamespaceNode(Node parent, NamespaceNode next, 63 String prefix, String 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 81 static NamespaceNode create(Node node) 82 { 83 Node top = node; 84 NamespaceNode nodes = null; 85 HashMap <String ,String > map = new HashMap <String ,String >(); 86 87 for (; node instanceof CauchoElement; node = node.getParentNode()) { 88 CauchoElement elt = (CauchoElement) node; 89 90 String prefix = elt.getPrefix(); 91 String 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 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 getNodeName() 140 { 141 return _name; 142 } 143 144 public String getPrefix() 145 { 146 return "xmlns"; 147 } 148 149 public void setPrefix(String prefix) 150 { 151 } 152 153 public boolean supports(String feature, String version) 154 { 155 return false; 156 } 157 158 public String getCanonicalName() 159 { 160 return ""; 161 } 162 163 public String getLocalName() 164 { 165 return _local; 166 } 167 168 public String getNamespaceURI() 169 { 170 return null; 171 } 172 173 public String 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 195 public void setLocation(String filename, int line, int column) 196 { 197 } 198 199 public String 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 getOwnerDocument() { return null; } 212 213 public void setNodeValue(String value) {} 214 215 public NodeList getChildNodes() { return null; } 216 217 public Node getFirstChild() { return null; } 218 219 public Node getLastChild() { return null; } 220 221 public NamedNodeMap 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 234 { 235 return null; 236 } 237 238 public Node appendChild(Node newNode) throws DOMException 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 getTextValue() { return getNodeValue(); } 260 public boolean checkValid() { return false; } 261 262 public void print(WriteStream out) throws IOException 263 { 264 } 265 266 public void printPretty(WriteStream out) throws IOException 267 { 268 } 269 270 public void printHtml(WriteStream out) throws IOException 271 { 272 } 273 274 public boolean isSupported(String feature, String version) 275 { 276 return false; 277 } 278 279 public boolean hasAttributes() 280 { 281 return false; 282 } 283 284 public String toString() 285 { 286 return "NamespaceNode[" + _name + " " + _url + "]"; 287 } 288 } 289 | Popular Tags |