| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.linked; |
| 9 |
|
| 10 |
import net.sf.saxon.event.Receiver; |
| 11 |
import net.sf.saxon.event.ReceiverOptions; |
| 12 |
import net.sf.saxon.expr.parser.Location; |
| 13 |
import net.sf.saxon.om.*; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.tree.util.AttributeCollectionImpl; |
| 16 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 17 |
import net.sf.saxon.type.*; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class AttributeImpl extends NodeImpl { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public AttributeImpl(ElementImpl element, int index) { |
| 42 |
setRawParent(element); |
| 43 |
setSiblingPosition(index); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
@Override |
| 52 |
public NodeName getNodeName() { |
| 53 |
if (getRawParent() == null || getSiblingPosition() == -1) { |
| 54 |
|
| 55 |
return null; |
| 56 |
} |
| 57 |
return ((ElementImpl) getRawParent()).getAttributeList().getNodeName(getSiblingPosition()); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@Override |
| 65 |
public int getFingerprint() { |
| 66 |
if (getRawParent() == null || getSiblingPosition() == -1) { |
| 67 |
|
| 68 |
return -1; |
| 69 |
} |
| 70 |
return ((ElementImpl) getRawParent()).getAttributeList().getFingerprint(getSiblingPosition()); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public SchemaType getSchemaType() { |
| 80 |
return ((ElementImpl) getRawParent()).getAttributeList().getTypeAnnotation(getSiblingPosition()); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public boolean isId() { |
| 91 |
try { |
| 92 |
return getFingerprint() == StandardNames.XML_ID || getSchemaType().isIdType(); |
| 93 |
} catch (MissingComponentException e) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public boolean isIdref() { |
| 105 |
AttributeCollection alist = ((ElementImpl) getRawParent()).getAttributeList(); |
| 106 |
if ((alist.getProperties(getSiblingPosition()) & ReceiverOptions.IS_IDREF) != 0) { |
| 107 |
return true; |
| 108 |
} |
| 109 |
return ElementImpl.isIdRefNode(this); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public boolean equals(Object other) { |
| 120 |
if (!(other instanceof AttributeImpl)) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
if (this == other) { |
| 124 |
return true; |
| 125 |
} |
| 126 |
AttributeImpl otherAtt = (AttributeImpl) other; |
| 127 |
return getRawParent().equals(otherAtt.getRawParent()) && getSiblingPosition() == otherAtt.getSiblingPosition(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public int hashCode() { |
| 136 |
return getRawParent().hashCode() ^ (getSiblingPosition() << 16); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected long getSequenceNumber() { |
| 147 |
long parseq = getRawParent().getSequenceNumber(); |
| 148 |
return (parseq == -1L ? parseq : parseq + 0x8000 + getSiblingPosition()); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public final int getNodeKind() { |
| 159 |
return Type.ATTRIBUTE; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public String getStringValue() { |
| 169 |
return ((ElementImpl) getRawParent()).getAttributeList().getValue(getSiblingPosition()); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public NodeImpl getNextSibling() { |
| 178 |
return null; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public NodeImpl getPreviousSibling() { |
| 187 |
return null; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public NodeImpl getPreviousInDocument() { |
| 196 |
return (NodeImpl) getParent(); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public NodeImpl getNextInDocument(NodeImpl anchor) { |
| 205 |
if (anchor == this) return null; |
| 206 |
return ((NodeImpl) getParent()).getNextInDocument(anchor); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public void generateId( FastStringBuffer buffer) { |
| 216 |
getParent().generateId(buffer); |
| 217 |
buffer.append('a'); |
| 218 |
buffer.append(Integer.toString(getSiblingPosition())); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public void copy( Receiver out, int copyOptions, Location locationId) throws XPathException { |
| 226 |
SimpleType typeCode = (CopyOptions.includes(copyOptions, CopyOptions.TYPE_ANNOTATIONS) ? |
| 227 |
((SimpleType) getSchemaType()) : BuiltInAtomicType.UNTYPED_ATOMIC); |
| 228 |
out.attribute(NameOfNode.makeName(this), typeCode, getStringValue(), locationId, 0); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public void delete() { |
| 236 |
if (getRawParent() != null) { |
| 237 |
getRawParent().removeAttribute(this); |
| 238 |
} |
| 239 |
setRawParent(null); |
| 240 |
setSiblingPosition(-1); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public boolean isDeleted() { |
| 252 |
|
| 253 |
return getSiblingPosition() == -1 || getFingerprint() == -1 || (getRawParent() != null && getRawParent().isDeleted()); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public void replace( NodeInfo[] replacement, boolean inherit) { |
| 272 |
if (isDeleted()) { |
| 273 |
throw new IllegalStateException("Cannot replace a deleted node"); |
| 274 |
} |
| 275 |
if (getParent() == null) { |
| 276 |
throw new IllegalStateException("Cannot replace a parentless node"); |
| 277 |
} |
| 278 |
ParentNodeImpl element = getRawParent(); |
| 279 |
delete(); |
| 280 |
for (NodeInfo n : replacement) { |
| 281 |
if (n.getNodeKind() != Type.ATTRIBUTE) { |
| 282 |
throw new IllegalArgumentException("Replacement nodes must be attributes"); |
| 283 |
} |
| 284 |
element.addAttribute(NameOfNode.makeName(n), BuiltInAtomicType.UNTYPED_ATOMIC, n.getStringValue(), 0); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public void rename(NodeName newNameCode) { |
| 295 |
|
| 296 |
if (getRawParent() != null) { |
| 297 |
((AttributeCollectionImpl) ((ElementImpl) getRawParent()).getAttributeList()).renameAttribute(getSiblingPosition(), newNameCode); |
| 298 |
String newURI = newNameCode.getURI(); |
| 299 |
if (!newURI.isEmpty()) { |
| 300 |
|
| 301 |
String newPrefix = newNameCode.getPrefix(); |
| 302 |
NamespaceBinding newBinding = new NamespaceBinding(newPrefix, newURI); |
| 303 |
String oldURI = ((ElementImpl) getRawParent()).getURIForPrefix(newPrefix, false); |
| 304 |
if (oldURI == null) { |
| 305 |
getRawParent().addNamespace(newBinding, false); |
| 306 |
} else if (!oldURI.equals(newURI)) { |
| 307 |
throw new IllegalArgumentException( |
| 308 |
"Namespace binding of new name conflicts with existing namespace binding"); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
public void replaceStringValue(CharSequence stringValue) { |
| 315 |
if (getRawParent() != null) { |
| 316 |
AttributeCollectionImpl atts = (AttributeCollectionImpl) ((ElementImpl) getRawParent()).getAttributeList(); |
| 317 |
atts.replaceAttribute(getSiblingPosition(), stringValue); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public void removeTypeAnnotation() { |
| 328 |
if (getRawParent() != null) { |
| 329 |
AttributeCollectionImpl atts = (AttributeCollectionImpl) ((ElementImpl) getRawParent()).getAttributeList(); |
| 330 |
atts.setTypeAnnotation(getSiblingPosition(), BuiltInAtomicType.UNTYPED_ATOMIC); |
| 331 |
getRawParent().removeTypeAnnotation(); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public void setTypeAnnotation(SchemaType type) { |
| 346 |
if (getRawParent() != null) { |
| 347 |
AttributeCollectionImpl atts = (AttributeCollectionImpl) ((ElementImpl) getRawParent()).getAttributeList(); |
| 348 |
atts.setTypeAnnotation(getSiblingPosition(), (SimpleType)type); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|