1 package com.icl.saxon.expr; 2 import com.icl.saxon.*; 3 import com.icl.saxon.om.*; 4 import com.icl.saxon.pattern.NameTest; 5 import com.icl.saxon.om.Axis; 6 import java.util.*; 7 8 11 12 class AttributeReference extends SingletonExpression { 13 14 private int fingerprint; 15 private NodeInfo boundParentNode = null; 17 19 private AttributeReference() {} 20 21 24 25 public AttributeReference(int fingerprint) { 26 this.fingerprint = fingerprint; 27 } 28 29 32 33 public void bindParentNode(NodeInfo node) { 34 boundParentNode = node; 35 } 36 37 40 41 private NodeInfo getParentNode(Context context) { 42 if (boundParentNode == null) { 43 return context.getContextNodeInfo(); 44 } else { 45 return boundParentNode; 46 } 47 } 48 49 53 54 public NodeInfo getNode(Context context) throws XPathException { 55 NodeInfo node = getParentNode(context); 56 if (node.getNodeType()==NodeInfo.ELEMENT) { 57 NameTest test = new NameTest(NodeInfo.ATTRIBUTE, fingerprint); 58 NodeEnumeration enum = node.getEnumeration(Axis.ATTRIBUTE, test); 59 if (enum.hasMoreElements()) { 60 return enum.nextElement(); 61 } 62 return null; 63 } 64 return null; 65 } 66 67 73 74 public boolean evaluateAsBoolean(Context context) throws XPathException { 75 NodeInfo node = getParentNode(context); 76 if (node.getNodeType()==NodeInfo.ELEMENT) { 77 return node.getAttributeValue(fingerprint)!=null; 78 } 79 return false; 80 } 81 82 87 88 public String evaluateAsString(Context context) throws XPathException { 89 NodeInfo node = getParentNode(context); 90 if (node.getNodeType()==NodeInfo.ELEMENT) { 91 String s = node.getAttributeValue(fingerprint); 92 if (s==null) return ""; 93 return s; 94 } 95 return ""; 96 } 97 98 99 104 105 public int getDependencies() { 106 if (boundParentNode==null) { 107 return Context.CONTEXT_NODE; 108 } else { 109 return 0; 110 } 111 } 112 113 121 122 public Expression reduce(int dependencies, Context context) throws XPathException { 123 if (boundParentNode==null && ((dependencies & Context.CONTEXT_NODE) != 0)) { 124 AttributeReference a = new AttributeReference(); 125 a.fingerprint = fingerprint; 126 a.bindParentNode(context.getContextNodeInfo()); 127 a.setStaticContext(getStaticContext()); 128 return a; 129 } else { 130 return this; 131 } 132 } 133 134 137 138 public void display(int level) { 139 System.err.println(indent(level) + "@" + fingerprint); 140 } 141 142 } 143 144 145 146 | Popular Tags |