1 package com.icl.saxon.functions; 2 import com.icl.saxon.*; 3 import com.icl.saxon.om.*; 4 import com.icl.saxon.expr.*; 5 import com.icl.saxon.sort.LocalOrderComparer; 6 7 import java.util.*; 8 import java.lang.Math; 9 import java.text.*; 10 11 12 13 public class Id extends Function { 14 15 private DocumentInfo boundDocument = null; 16 17 public String getName() { 18 return "id"; 19 }; 20 21 25 26 public int getDataType() { 27 return Value.NODESET; 28 } 29 30 35 36 public boolean isContextDocumentNodeSet() { 37 return true; 38 } 39 40 43 44 public Expression simplify() throws XPathException { 45 checkArgumentCount(1, 1); 46 argument[0] = argument[0].simplify(); 47 return this; 48 } 49 50 53 54 public NodeSetValue evaluateAsNodeSet(Context context) throws XPathException { 55 return findId(argument[0].evaluate(context), context); 56 } 57 58 61 62 public Value evaluate(Context context) throws XPathException { 63 return evaluateAsNodeSet(context); 64 } 65 66 71 72 public int getDependencies() { 73 int dep = argument[0].getDependencies(); 74 if (boundDocument != null) { 75 return dep; 76 } else { 77 return dep | Context.CONTEXT_NODE | Context.CONTEXT_DOCUMENT; 78 } 79 } 80 81 84 85 public Expression reduce(int dependencies, Context context) 86 throws XPathException { 87 88 Id id = new Id(); 89 id.addArgument(argument[0].reduce(dependencies, context)); 90 id.setStaticContext(getStaticContext()); 91 id.boundDocument = boundDocument; 92 93 if (boundDocument==null && 94 ((dependencies & (Context.CONTEXT_NODE | Context.CONTEXT_DOCUMENT)) != 0)) { 95 id.boundDocument = (context.getContextNodeInfo()).getDocumentRoot(); 96 } 97 return id; 98 } 99 100 103 104 private NodeSetValue findId(Value arg0, Context context) throws XPathException { 105 Vector idrefresult = null; 106 DocumentInfo doc; 107 if (boundDocument==null) { 108 doc = (context.getContextNodeInfo()).getDocumentRoot(); 109 } else { 110 doc = boundDocument; 111 } 112 113 if ((arg0 instanceof NodeSetValue) && 114 !(arg0 instanceof FragmentValue || arg0 instanceof FragmentValue)) { 115 116 NodeEnumeration enum = ((NodeSetValue)arg0).enumerate(); 117 while (enum.hasMoreElements()) { 118 NodeInfo node = enum.nextElement(); 119 String s = node.getStringValue(); 120 StringTokenizer st = new StringTokenizer(s); 121 while (st.hasMoreTokens()) { 122 NodeInfo el = doc.selectID(st.nextToken()); 123 if (el!=null) { 124 if (idrefresult==null) { 125 idrefresult = new Vector(2); 126 } 127 idrefresult.addElement(el); 128 } 129 } 130 } 131 132 } else { 133 134 String s = arg0.asString(); 135 StringTokenizer st = new StringTokenizer(s); 136 while (st.hasMoreTokens()) { 137 NodeInfo el = doc.selectID(st.nextToken()); 138 if (el!=null) { 139 if (idrefresult==null) { 140 idrefresult = new Vector(2); 141 } 142 idrefresult.addElement(el); 143 } 144 } 145 } 146 147 if (idrefresult==null) { 148 return new EmptyNodeSet(); 149 } 150 if (idrefresult.size() == 1) { 151 return new SingletonNodeSet((NodeInfo)idrefresult.elementAt(0)); 152 } 153 return new NodeSetExtent(idrefresult, LocalOrderComparer.getInstance()); 154 155 } 156 157 } 158 159 160 161 162 | Popular Tags |