| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.dom; |
| 9 | |
| 10 | import net.sf.saxon.om.AxisInfo; |
| 11 | import net.sf.saxon.om.NamePool; |
| 12 | import net.sf.saxon.om.NodeInfo; |
| 13 | import net.sf.saxon.pattern.NameTest; |
| 14 | import net.sf.saxon.tree.iter.AxisIterator; |
| 15 | import net.sf.saxon.type.BuiltInAtomicType; |
| 16 | import net.sf.saxon.type.SchemaType; |
| 17 | import net.sf.saxon.type.Type; |
| 18 | import net.sf.saxon.type.Untyped; |
| 19 | import org.w3c.dom.*; |
| 20 | |
| 21 | /** |
| 22 | * This class is an implementation of the DOM Element class that wraps a Saxon NodeInfo |
| 23 | * representation of an element node. |
| 24 | * <p>The class provides read-only access to the tree; methods that request updates all fail |
| 25 | * with an UnsupportedOperationException.</p> |
| 26 | * <p>Note that contrary to the DOM specification, this implementation does not expose namespace |
| 27 | * declarations as attributes.</p> |
| 28 | */ |
| 29 | |
| 30 | public class ElementOverNodeInfo extends NodeOverNodeInfo implements Element { |
| 31 | |
| 32 | /** |
| 33 | * The name of the element (DOM interface). |
| 34 | */ |
| 35 | |
| 36 | public String getTagName() { |
| 37 | return node.getDisplayName(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns a <code>NodeList</code> of all descendant <code>Elements</code> |
| 42 | * with a given tag name, in document order. |
| 43 | * |
| 44 | * @param name The name of the tag to match on. The special value "*" |
| 45 | * matches all tags. |
| 46 | * @return A list of matching <code>Element</code> nodes. |
| 47 | */ |
| 48 | public NodeList getElementsByTagName(String name) { |
| 49 | return DocumentOverNodeInfo.getElementsByTagName(node, name); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns a <code>NodeList</code> of all the descendant |
| 54 | * <code>Elements</code> with a given local name and namespace URI in |
| 55 | * document order. |
| 56 | * |
| 57 | * @param namespaceURI The namespace URI of the elements to match on. The |
| 58 | * special value "*" matches all namespaces. |
| 59 | * @param localName The local name of the elements to match on. The |
| 60 | * special value "*" matches all local names. |
| 61 | * @return A new <code>NodeList</code> object containing all the matched |
| 62 | * <code>Elements</code>. |
| 63 | * @throws org.w3c.dom.DOMException NOT_SUPPORTED_ERR: May be raised if the implementation does not |
| 64 | * support the feature <code>"XML"</code> and the language exposed |
| 65 | * through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]). |
| 66 | * @since DOM Level 2 |
| 67 | */ |
| 68 | public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException { |
| 69 | return DocumentOverNodeInfo.getElementsByTagNameNS(node, namespaceURI, localName); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Retrieves an attribute value by name. |
| 74 | * This implementation does not expose namespace nodes as attributes. |
| 75 | * |
| 76 | * @param name The QName of the attribute to retrieve. |
| 77 | * @return The <code>Attr</code> value as a string, or the empty string if |
| 78 | * that attribute does not have a specified or default value. |
| 79 | */ |
| 80 | |
| 81 | public String getAttribute(String name) { |
| 82 | AxisIterator atts = node.iterateAxis(AxisInfo.ATTRIBUTE); |
| 83 | while (true) { |
| 84 | NodeInfo att = atts.next(); |
| 85 | if (att == null) { |
| 86 | return ""; |
| 87 | } |
| 88 | if (att.getDisplayName().equals(name)) { |
| 89 | String val = att.getStringValue(); |
| 90 | if (val == null) { |
| 91 | return ""; |
| 92 | } |
| 93 | return val; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Retrieves an attribute node by name. |
| 100 | * This implementation does not expose namespace nodes as attributes. |
| 101 | * <br> To retrieve an attribute node by qualified name and namespace URI, |
| 102 | * use the <code>getAttributeNodeNS</code> method. |
| 103 | * |
| 104 | * @param name The name (<code>nodeName</code> ) of the attribute to |
| 105 | * retrieve. |
| 106 | * @return The <code>Attr</code> node with the specified name ( |
| 107 | * <code>nodeName</code> ) or <code>null</code> if there is no such |
| 108 | * attribute. |
| 109 | */ |
| 110 | |
| 111 | public Attr getAttributeNode(String name) { |
| 112 | AxisIterator atts = node.iterateAxis(AxisInfo.ATTRIBUTE); |
| 113 | while (true) { |
| 114 | NodeInfo att = atts.next(); |
| 115 | if (att == null) { |
| 116 | return null; |
| 117 | } |
| 118 | if (att.getDisplayName().equals(name)) { |
| 119 | return (AttrOverNodeInfo) wrap(att); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Adds a new attribute node. Always fails |
| 126 | * |
| 127 | * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
| 128 | */ |
| 129 | |
| 130 | public Attr setAttributeNode(Attr newAttr) throws DOMException { |
| 131 | disallowUpdate(); |
| 132 | return null; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Removes the specified attribute. Always fails |
| 137 | * |
| 138 | * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
| 139 | */ |
| 140 | |
| 141 | public void removeAttribute(String oldAttr) throws DOMException { |
| 142 | disallowUpdate(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Removes the specified attribute node. Always fails |
| 147 | * |
| 148 | * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
| 149 | */ |
| 150 | |
| 151 | public Attr removeAttributeNode(Attr oldAttr) throws DOMException { |
| 152 | disallowUpdate(); |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Retrieves an attribute value by local name and namespace URI. |
| 159 | * This implementation does not expose namespace nodes as attributes. |
| 160 | * |
| 161 | * @param namespaceURI The namespace URI of the attribute to retrieve. |
| 162 | * @param localName The local name of the attribute to retrieve. |
| 163 | * @return The <code>Attr</code> value as a string, or the empty string if |
| 164 | * that attribute does not have a specified or default value. |
| 165 | * @since DOM Level 2 |
| 166 | */ |
| 167 | |
| 168 | public String getAttributeNS(String namespaceURI, String localName) { |
| 169 | String uri = namespaceURI == null ? "" : namespaceURI; |
| 170 | String val = node.getAttributeValue(uri, localName); |
| 171 | if (val == null) { |
| 172 | return ""; |
| 173 | } |
| 174 | return val; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Adds a new attribute. Always fails |
| 179 | * |
| 180 | * @param name The name of the attribute to create or alter. |
| 181 | * @param value Value to set in string form. |
| 182 | * @throws org.w3c.dom.DOMException INVALID_CHARACTER_ERR: Raised if the specified name is not an XML |
| 183 | * name according to the XML version in use specified in the |
| 184 | * <code>Document.xmlVersion</code> attribute. |
| 185 | * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
| 186 | */ |
| 187 | public void setAttribute(String name, String value) throws DOMException { |
| 188 | disallowUpdate(); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Adds a new attribute. Always fails. |
| 193 | * |
| 194 | * @param namespaceURI The namespace URI of the attribute to create or |
| 195 | * alter. |
| 196 | * @param qualifiedName The qualified name of the attribute to create or |
| 197 | * alter. |
| 198 | * @param value The value to set in string form. |
| 199 | * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
| 200 | */ |
| 201 | |
| 202 | public void setAttributeNS(String namespaceURI, |
| 203 | String qualifiedName, |
| 204 | String value) |
| 205 | throws DOMException { |
| 206 | disallowUpdate(); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Removes an attribute by local name and namespace URI. Always fails |
| 211 | * |
| 212 | * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
| 213 | * @since DOM Level 2 |
| 214 | */ |
| 215 | |
| 216 | public void removeAttributeNS(String namespaceURI, |
| 217 | String localName) |
| 218 | throws DOMException { |
| 219 | disallowUpdate(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Retrieves an <code>Attr</code> node by local name and namespace URI. |
| 224 | * This implementation does not expose namespace nodes as attributes. |
| 225 | * |
| 226 | * @param namespaceURI The namespace URI of the attribute to retrieve. |
| 227 | * @param localName The local name of the attribute to retrieve. |
| 228 | * @return The <code>Attr</code> node with the specified attribute local |
| 229 | * name and namespace URI or <code>null</code> if there is no such |
| 230 | * attribute. |
| 231 | * @since DOM Level 2 |
| 232 | */ |
| 233 | |
| 234 | public Attr getAttributeNodeNS(String namespaceURI, String localName) { |
| 235 | NamePool pool = node.getConfiguration().getNamePool(); |
| 236 | NameTest test = new NameTest(Type.ATTRIBUTE, namespaceURI, localName, pool); |
| 237 | AxisIterator atts = node.iterateAxis(AxisInfo.ATTRIBUTE, test); |
| 238 | return (Attr) wrap(atts.next()); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Add a new attribute. Always fails. |
| 243 | * |
| 244 | * @param newAttr The <code>Attr</code> node to add to the attribute list. |
| 245 | * @return If the <code>newAttr</code> attribute replaces an existing |
| 246 | * attribute with the same local name and namespace URI , the |
| 247 | * replaced <code>Attr</code> node is returned, otherwise |
| 248 | * <code>null</code> is returned. |
| 249 | * @throws org.w3c.dom.DOMException <br> NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
| 250 | * @since DOM Level 2 |
| 251 | */ |
| 252 | |
| 253 | public Attr setAttributeNodeNS(Attr newAttr) |
| 254 | throws DOMException { |
| 255 | disallowUpdate(); |
| 256 | return null; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Returns <code>true</code> when an attribute with a given name is |
| 261 | * specified on this element or has a default value, <code>false</code> |
| 262 | * otherwise. |
| 263 | * This implementation does not expose namespace nodes as attributes. |
| 264 | * |
| 265 | * @param name The name of the attribute to look for. |
| 266 | * @return <code>true</code> if an attribute with the given name is |
| 267 | * specified on this element or has a default value, <code>false</code> |
| 268 | * otherwise. |
| 269 | * @since DOM Level 2 |
| 270 | */ |
| 271 | |
| 272 | public boolean hasAttribute(String name) { |
| 273 | AxisIterator atts = node.iterateAxis(AxisInfo.ATTRIBUTE); |
| 274 | while (true) { |
| 275 | NodeInfo att = atts.next(); |
| 276 | if (att == null) { |
| 277 | return false; |
| 278 | } |
| 279 | if (att.getDisplayName().equals(name)) { |
| 280 | return true; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Returns <code>true</code> when an attribute with a given local name |
| 287 | * and namespace URI is specified on this element or has a default value, |
| 288 | * <code>false</code> otherwise. |
| 289 | * This implementation does not expose namespace nodes as attributes. |
| 290 | * |
| 291 | * @param namespaceURI The namespace URI of the attribute to look for. |
| 292 | * @param localName The local name of the attribute to look for. |
| 293 | * @return <code>true</code> if an attribute with the given local name and |
| 294 | * namespace URI is specified or has a default value on this element, |
| 295 | * <code>false</code> otherwise. |
| 296 | * @since DOM Level 2 |
| 297 | */ |
| 298 | |
| 299 | public boolean hasAttributeNS(String namespaceURI, String localName) { |
| 300 | String uri = namespaceURI == null ? "" : namespaceURI; |
| 301 | return node.getAttributeValue(uri, localName) != null; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Mark an attribute as an ID. Always fails. |
| 306 | * |
| 307 | * @throws UnsupportedOperationException always |
| 308 | */ |
| 309 | |
| 310 | public void setIdAttribute(String name, boolean isId) throws UnsupportedOperationException { |
| 311 | disallowUpdate(); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Mark an attribute as an ID. Always fails. |
| 316 | * |
| 317 | * @throws UnsupportedOperationException always |
| 318 | */ |
| 319 | |
| 320 | public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) |
| 321 | throws UnsupportedOperationException { |
| 322 | disallowUpdate(); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Mark an attribute as an ID. Always fails. |
| 327 | * @throws UnsupportedOperationException always |
| 328 | */ |
| 329 | |
| 330 | public void setIdAttributeNode(Attr idAttr, boolean isId) throws UnsupportedOperationException { |
| 331 | disallowUpdate(); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /** |
| 336 | * Get the schema type information for this node. |
| 337 | * |
| 338 | * @return the type information. Returns null for an untyped node. |
| 339 | */ |
| 340 | |
| 341 | /*@Nullable*/ |
| 342 | public TypeInfo getSchemaTypeInfo() { |
| 343 | SchemaType type = node.getSchemaType(); |
| 344 | if (type == null || Untyped.getInstance().equals(type) || BuiltInAtomicType.UNTYPED_ATOMIC.equals(type)) { |
| 345 | return null; |
| 346 | } |
| 347 | return new TypeInfoImpl(node.getConfiguration(), type); |
| 348 | } |
| 349 | |
| 350 | } |
| 351 |