| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.PathMap; |
| 11 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 12 |
import net.sf.saxon.om.AxisInfo; |
| 13 |
import net.sf.saxon.om.FingerprintedQName; |
| 14 |
import net.sf.saxon.om.Item; |
| 15 |
import net.sf.saxon.om.NodeInfo; |
| 16 |
import net.sf.saxon.pattern.NameTest; |
| 17 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.tree.tiny.TinyElementImpl; |
| 20 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 21 |
import net.sf.saxon.type.ItemType; |
| 22 |
import net.sf.saxon.type.Type; |
| 23 |
import net.sf.saxon.value.UntypedAtomicValue; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public final class AttributeGetter extends Expression { |
| 33 |
|
| 34 |
|
| 35 |
public static final int CHECK_CONTEXT_ITEM_IS_NODE = 2; |
| 36 |
|
| 37 |
private FingerprintedQName attributeName; |
| 38 |
private int requiredChecks = CHECK_CONTEXT_ITEM_IS_NODE; |
| 39 |
|
| 40 |
public AttributeGetter(FingerprintedQName attributeName) { |
| 41 |
this.attributeName = attributeName; |
| 42 |
} |
| 43 |
|
| 44 |
public FingerprintedQName getAttributeName() { |
| 45 |
return attributeName; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public void setRequiredChecks(int checks) { |
| 54 |
requiredChecks = checks; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public int getRequiredChecks() { |
| 63 |
return requiredChecks; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public ItemType getItemType() { |
| 68 |
return BuiltInAtomicType.UNTYPED_ATOMIC; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public int computeCardinality() { |
| 73 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public int getIntrinsicDependencies() { |
| 78 |
return StaticProperty.DEPENDS_ON_CONTEXT_ITEM; |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public AttributeGetter copy(RebindingMap rebindings) { |
| 83 |
AttributeGetter ag2 = new AttributeGetter(attributeName); |
| 84 |
ag2.setRequiredChecks(requiredChecks); |
| 85 |
return ag2; |
| 86 |
} |
| 87 |
|
| 88 |
public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 89 |
if (pathMapNodeSet == null) { |
| 90 |
ContextItemExpression cie = new ContextItemExpression(); |
| 91 |
pathMapNodeSet = new PathMap.PathMapNodeSet(pathMap.makeNewRoot(cie)); |
| 92 |
} |
| 93 |
return pathMapNodeSet.createArc(AxisInfo.ATTRIBUTE, new NameTest(Type.ATTRIBUTE, attributeName, getConfiguration().getNamePool())); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public int getImplementationMethod() { |
| 98 |
return EVALUATE_METHOD; |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public Item evaluateItem(XPathContext context) throws XPathException { |
| 103 |
Item item = context.getContextItem(); |
| 104 |
if (item instanceof TinyElementImpl) { |
| 105 |
|
| 106 |
String val = ((TinyElementImpl) item).getAttributeValue(attributeName.getFingerprint()); |
| 107 |
return val == null ? null : new UntypedAtomicValue(val); |
| 108 |
} |
| 109 |
if (item == null) { |
| 110 |
|
| 111 |
dynamicError("The context item for @" + attributeName.getDisplayName() + |
| 112 |
" is absent", "XPDY0002", context); |
| 113 |
} |
| 114 |
if (!(item instanceof NodeInfo)) { |
| 115 |
typeError("The context item for @" + attributeName.getDisplayName() + |
| 116 |
" is not a node", "XPDY0002", context); |
| 117 |
} |
| 118 |
assert item instanceof NodeInfo; |
| 119 |
NodeInfo node = (NodeInfo) item; |
| 120 |
if (node.getNodeKind() == Type.ELEMENT) { |
| 121 |
String val = node.getAttributeValue(attributeName.getURI(), attributeName.getLocalPart()); |
| 122 |
return val == null ? null : new UntypedAtomicValue(val); |
| 123 |
} else { |
| 124 |
return null; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public String getExpressionName() { |
| 130 |
return "attGetter"; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public String toShortString() { |
| 135 |
return "@" + attributeName.getDisplayName(); |
| 136 |
} |
| 137 |
|
| 138 |
public String toString() { |
| 139 |
return "data(@" + attributeName.getDisplayName() + ")"; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public boolean equals(Object obj) { |
| 144 |
return obj instanceof AttributeGetter && ((AttributeGetter)obj).attributeName.equals(attributeName); |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public int computeHashCode() { |
| 149 |
return 83571 ^ attributeName.hashCode(); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void export(ExpressionPresenter out) { |
| 154 |
out.startElement("attVal", this); |
| 155 |
out.emitAttribute("name", attributeName.getStructuredQName()); |
| 156 |
out.emitAttribute("chk", "" + requiredChecks); |
| 157 |
out.endElement(); |
| 158 |
} |
| 159 |
} |
| 160 |
|