| 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.type.BuiltInAtomicType; |
| 11 | import net.sf.saxon.type.SchemaType; |
| 12 | import net.sf.saxon.type.Type; |
| 13 | import org.w3c.dom.*; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
| 17 | |
| 18 | /** |
| 19 | * This class is an implementation of the DOM Attr class that wraps a Saxon NodeInfo |
| 20 | * representation of an attribute or namespace node. |
| 21 | */ |
| 22 | |
| 23 | public class AttrOverNodeInfo extends NodeOverNodeInfo implements Attr { |
| 24 | |
| 25 | /** |
| 26 | * Get the name of an attribute node (the lexical QName) (DOM method) |
| 27 | */ |
| 28 | |
| 29 | public String getName() { |
| 30 | if (node.getNodeKind() == Type.NAMESPACE) { |
| 31 | String local = node.getLocalPart(); |
| 32 | if (local.equals("")) { |
| 33 | return "xmlns"; |
| 34 | } else { |
| 35 | return "xmlns:" + local; |
| 36 | } |
| 37 | } |
| 38 | return node.getDisplayName(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Return the character value of an attribute node (DOM method) |
| 43 | * |
| 44 | * @return the attribute value |
| 45 | */ |
| 46 | |
| 47 | public String getValue() { |
| 48 | return node.getStringValue(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Determine whether the node has any children. |
| 53 | * |
| 54 | * @return <code>true</code>: a DOM Attribute has a text node as a child. |
| 55 | */ |
| 56 | |
| 57 | public boolean hasChildNodes() { |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get first child |
| 63 | * |
| 64 | * @return the first child node of this node. In this model an attribute node always has a single text |
| 65 | * node as its child. |
| 66 | */ |
| 67 | |
| 68 | public Node getFirstChild() { |
| 69 | return new TextOverAttrInfo(this); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get last child |
| 74 | * |
| 75 | * @return last child of this node, or null if it has no children |
| 76 | */ |
| 77 | |
| 78 | public Node getLastChild() { |
| 79 | return getFirstChild(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Return a <code>NodeList</code> that contains all children of this node. If |
| 84 | * there are no children, this is a <code>NodeList</code> containing no |
| 85 | * nodes. |
| 86 | */ |
| 87 | |
| 88 | public NodeList getChildNodes() { |
| 89 | List<Node> list = new ArrayList<>(1); |
| 90 | list.add(getFirstChild()); |
| 91 | return new DOMNodeList(list); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * If this attribute was explicitly given a value in the original |
| 96 | * document, this is <code>true</code> ; otherwise, it is |
| 97 | * <code>false</code>. (DOM method) |
| 98 | * |
| 99 | * @return Always true in this implementation. |
| 100 | */ |
| 101 | |
| 102 | public boolean getSpecified() { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Set the value of an attribute node. (DOM method). |
| 108 | * Always fails (because tree is readonly) |
| 109 | */ |
| 110 | |
| 111 | public void setValue(String value) throws DOMException { |
| 112 | disallowUpdate(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Determine whether this (attribute) node is an ID. This method is introduced |
| 117 | * in DOM Level 3. |
| 118 | */ |
| 119 | |
| 120 | public boolean isId() { |
| 121 | return node.isId(); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * The <code>Element</code> node this attribute is attached to or |
| 127 | * <code>null</code> if this attribute is not in use. |
| 128 | * |
| 129 | * @since DOM Level 2 |
| 130 | */ |
| 131 | |
| 132 | public Element getOwnerElement() { |
| 133 | if (node.getNodeKind() == Type.ATTRIBUTE || node.getNodeKind() == Type.NAMESPACE) { |
| 134 | return (Element) wrap(node.getParent()); |
| 135 | } else { |
| 136 | throw new UnsupportedOperationException( |
| 137 | "This method is defined only on attribute and namespace nodes"); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get the schema type information for this node. Returns null for an untyped node. |
| 143 | */ |
| 144 | |
| 145 | /*@Nullable*/ |
| 146 | public TypeInfo getSchemaTypeInfo() { |
| 147 | SchemaType type = node.getSchemaType(); |
| 148 | if (type == null || BuiltInAtomicType.UNTYPED_ATOMIC.equals(type)) { |
| 149 | return null; |
| 150 | } |
| 151 | return new TypeInfoImpl(node.getConfiguration(), type); |
| 152 | } |
| 153 | |
| 154 | } |
| 155 |