Debian Codegraph @

net/sf/saxon/dom/DOMAttributeMap.java

raw symbols SCIP
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.Configuration;
11 import net.sf.saxon.lib.NamespaceConstant;
12 import net.sf.saxon.om.AxisInfo;
13 import net.sf.saxon.om.NamespaceBinding;
14 import net.sf.saxon.om.NodeInfo;
15 import net.sf.saxon.tree.NamespaceNode;
16 import net.sf.saxon.tree.iter.AxisIterator;
17 import org.w3c.dom.DOMException;
18 import org.w3c.dom.NamedNodeMap;
19 import org.w3c.dom.Node;
20
21 /**
22 * Implementation of DOM NamedNodeMap used to represent the attributes of an element, for use when
23 * Saxon element and attribute nodes are accessed using the DOM API.
24 * <p>Note that namespaces are treated as attributes.</p>
25 */
26
27 class DOMAttributeMap implements NamedNodeMap {
28
29 private NodeInfo parent;
30 private int numberOfNamespaces = -1;
31 private boolean excludeNamespaceUndeclarations;
32
33 /**
34 * Construct an AttributeMap for a given element node
35 *
36 * @param parent the element node owning the attributes
37 */
38
39 public DOMAttributeMap(NodeInfo parent) {
40 this.parent = parent;
41 if (parent.getConfiguration().getXMLVersion() == Configuration.XML10) {
42 excludeNamespaceUndeclarations = true;
43 }
44 }
45
46 /**
47 * Filter out namespace undeclarations (other than the undeclaration of the default namespace)
48 */
49
50 private NamespaceBinding[] removeUndeclarations(NamespaceBinding[] bindings) {
51 if (excludeNamespaceUndeclarations) {
52 int keep = 0;
53 for (NamespaceBinding b : bindings) {
54 if (b != null && (b.getPrefix().isEmpty() || !b.getURI().isEmpty())) {
55 keep++;
56 }
57 }
58 NamespaceBinding[] b2 = new NamespaceBinding[keep];
59 keep = 0;
60 for (NamespaceBinding b : bindings) {
61 if (b != null && (b.getPrefix().isEmpty() || !b.getURI().isEmpty())) {
62 b2[keep++] = b;
63 }
64 }
65 return b2;
66 } else {
67 return bindings;
68 }
69 }
70
71 /**
72 * Get named attribute (DOM NamedNodeMap method)
73 */
74
75 public Node getNamedItem(String name) {
76 if (name.equals("xmlns")) {
77 NamespaceBinding[] nsarray = removeUndeclarations(parent.getDeclaredNamespaces(null));
78 for (int i = 0; i < nsarray.length; i++) {
79 if (nsarray[i] == null) {
80 return null;
81 } else if (nsarray[i].getPrefix().isEmpty()) {
82 NamespaceNode nn =
83 new NamespaceNode(parent, nsarray[i], i + 1);
84 return NodeOverNodeInfo.wrap(nn);
85 }
86 }
87 return null;
88 } else if (name.startsWith("xmlns:")) {
89 String prefix = name.substring(6);
90 if (prefix.equals("xml")) {
91 NamespaceNode nn =
92 new NamespaceNode(parent, NamespaceBinding.XML, 0);
93 return NodeOverNodeInfo.wrap(nn);
94 }
95 NamespaceBinding[] nsarray = removeUndeclarations(parent.getDeclaredNamespaces(null));
96 for (int i = 0; i < nsarray.length; i++) {
97 if (nsarray[i] == null) {
98 return null;
99 } else if (prefix.equals(nsarray[i].getPrefix())) {
100 NamespaceNode nn =
101 new NamespaceNode(parent, nsarray[i], i + 1);
102 return NodeOverNodeInfo.wrap(nn);
103 }
104 }
105 return null;
106 } else {
107 AxisIterator atts = parent.iterateAxis(AxisInfo.ATTRIBUTE);
108 while (true) {
109 NodeInfo att = atts.next();
110 if (att == null) {
111 return null;
112 }
113 if (name.equals(att.getDisplayName())) {
114 return NodeOverNodeInfo.wrap(att);
115 }
116 }
117 }
118 }
119
120 /**
121 * Get n'th attribute (DOM NamedNodeMap method).
122 * In this implementation we number the attributes as follows:
123 * 0 - the xmlns:xml namespace declaration
124 * 1-n further namespace declarations
125 * n+1... "real" attribute declarations
126 */
127
128 public Node item(int index) {
129 if (index < 0) {
130 return null;
131 }
132 if (index == 0) {
133 NamespaceNode nn =
134 new NamespaceNode(parent, NamespaceBinding.XML, 0);
135 return NodeOverNodeInfo.wrap(nn);
136 }
137 int nscount = getNumberOfNamespaces();
138 if (index < nscount) {
139 NamespaceBinding[] nsList = removeUndeclarations(parent.getDeclaredNamespaces(null));
140 NamespaceBinding nscode = nsList[index - 1];
141 NamespaceNode nn =
142 new NamespaceNode(parent, nscode, index);
143 return NodeOverNodeInfo.wrap(nn);
144 }
145 int pos = 0;
146 int attNr = index - nscount;
147 AxisIterator atts = parent.iterateAxis(AxisInfo.ATTRIBUTE);
148 while (true) {
149 NodeInfo att = atts.next();
150 if (att == null) {
151 return null;
152 }
153 if (pos == attNr) {
154 return NodeOverNodeInfo.wrap(att);
155 }
156 pos++;
157 }
158 }
159
160 /**
161 * Get the number of declared namespaces
162 *
163 * @return the number of namespaces declared on the parent element
164 */
165
166 private int getNumberOfNamespaces() {
167 if (numberOfNamespaces == -1) {
168 NamespaceBinding[] nsList = removeUndeclarations(parent.getDeclaredNamespaces(null));
169 int count = nsList.length;
170 for (int i = 0; i < count; i++) {
171 if (nsList[i] == null) {
172 count = i;
173 break;
174 }
175 }
176 numberOfNamespaces = count + 1; //+1 for the XML namespace
177 }
178 return numberOfNamespaces;
179 }
180
181 /**
182 * Get number of attributes and namespaces (DOM NamedNodeMap method).
183 */
184
185 public int getLength() {
186 int length = 0;
187 AxisIterator atts = parent.iterateAxis(AxisInfo.ATTRIBUTE);
188 while (atts.next() != null) {
189 length++;
190 }
191 return getNumberOfNamespaces() + length;
192 }
193
194 /**
195 * Get named attribute (DOM NamedNodeMap method)
196 */
197
198 public Node getNamedItemNS(String uri, String localName) {
199 if (uri == null) {
200 uri = "";
201 }
202 if (NamespaceConstant.XMLNS.equals(uri)) {
203 return getNamedItem("xmlns:" + localName);
204 }
205 if (uri.equals("") && localName.equals("xmlns")) {
206 return getNamedItem("xmlns");
207 }
208 AxisIterator atts = parent.iterateAxis(AxisInfo.ATTRIBUTE);
209 while (true) {
210 NodeInfo att = atts.next();
211 if (att == null) {
212 return null;
213 }
214 if (uri.equals(att.getURI()) && localName.equals(att.getLocalPart())) {
215 return NodeOverNodeInfo.wrap(att);
216 }
217 }
218 }
219
220 /**
221 * Set named attribute (DOM NamedNodeMap method: always fails)
222 */
223
224 public Node setNamedItem(Node arg) throws DOMException {
225 NodeOverNodeInfo.disallowUpdate();
226 return null;
227 }
228
229 /**
230 * Remove named attribute (DOM NamedNodeMap method: always fails)
231 */
232
233 public Node removeNamedItem(String name) throws DOMException {
234 NodeOverNodeInfo.disallowUpdate();
235 return null;
236 }
237
238 /**
239 * Set named attribute (DOM NamedNodeMap method: always fails)
240 */
241
242 public Node setNamedItemNS(Node arg) throws DOMException {
243 NodeOverNodeInfo.disallowUpdate();
244 return null;
245 }
246
247 /**
248 * Remove named attribute (DOM NamedNodeMap method: always fails)
249 */
250
251 /*@Nullable*/
252 public Node removeNamedItemNS(String uri, String localName) throws DOMException {
253 NodeOverNodeInfo.disallowUpdate();
254 return null;
255 }
256
257 }
258