1 package com.icl.saxon.expr; 2 import com.icl.saxon.Context; 3 import com.icl.saxon.Controller; 4 import com.icl.saxon.om.NodeInfo; 5 import com.icl.saxon.om.NodeEnumeration; 6 7 12 13 public class NodeSetIntent extends NodeSetValue { 14 private NodeSetExpression expression; 15 private NodeSetExtent extent = null; 16 private Controller controller; 17 private boolean sorted = false; 18 private int useCount = 0; 19 20 23 24 public NodeSetIntent(NodeSetExpression exp, Controller controller) throws XPathException { 25 if (exp.getDependencies()!=0) { 26 exp.display(10); 27 throw new UnsupportedOperationException ("Cannot create intensional node-set with context dependencies: " + exp.getClass() + ":" + exp.getDependencies()); 28 } 29 expression = exp; 30 this.controller = controller; 31 } 32 33 36 37 private Context makeContext() { 38 Context c = new Context(controller); 39 c.setStaticContext(expression.getStaticContext()); 40 return c; 41 } 42 43 46 47 public NodeSetExpression getNodeSetExpression() { 48 return expression; 49 } 50 51 57 58 public void setSorted(boolean isSorted) { 59 sorted = isSorted; 60 } 61 62 67 68 public boolean isSorted() throws XPathException { 69 return (sorted || expression.enumerate(makeContext(), false).isSorted()); 70 } 71 72 77 78 public boolean isContextDocumentNodeSet() { 79 return expression.isContextDocumentNodeSet(); 80 } 81 82 87 88 public String asString() throws XPathException { 89 NodeInfo first = getFirst(); 90 return (first==null ? "" : first.getStringValue()); 91 } 92 93 97 98 public boolean asBoolean() throws XPathException { 99 return enumerate().hasMoreElements(); 100 } 101 102 106 107 public int getCount() throws XPathException { 108 if (extent == null) { 109 NodeEnumeration enumeration = expression.enumerate(makeContext(), false); 110 if (enumeration instanceof LastPositionFinder && enumeration.isSorted()) { 111 return ((LastPositionFinder)enumeration).getLastPosition(); 112 } 113 extent = new NodeSetExtent(enumeration, controller); 114 } 115 return extent.getCount(); 116 } 117 118 private void fix() throws XPathException { 119 if (extent == null) { 120 NodeEnumeration enumeration = expression.enumerate(makeContext(), false); 121 extent = new NodeSetExtent(enumeration, controller); 122 } 123 } 124 125 131 132 public NodeSetValue sort() throws XPathException { 133 if (sorted) return this; 134 fix(); 135 return extent.sort(); 136 } 137 138 142 143 public NodeInfo getFirst() throws XPathException { 144 if (extent!=null) return extent.getFirst(); 145 146 NodeEnumeration enumeration = expression.enumerate(makeContext(), false); 147 if (sorted || enumeration.isSorted()) { 148 sorted = true; 149 if (enumeration.hasMoreElements()) { 150 return enumeration.nextElement(); 151 } else { 152 return null; 153 } 154 } else { 155 NodeInfo first = null; 156 while (enumeration.hasMoreElements()) { 157 NodeInfo node = enumeration.nextElement(); 158 if (first==null || controller.compare(node, first) < 0) { 159 first = node; 160 } 161 } 162 return first; 163 } 164 } 165 166 172 173 public NodeInfo selectFirst(Context context) throws XPathException { 174 return getFirst(); 175 } 176 177 180 181 public NodeEnumeration enumerate() throws XPathException { 182 if (extent!=null) { 183 return extent.enumerate(); 184 } else { 185 useCount++; 188 if (useCount < 3) { 189 return expression.enumerate(makeContext(), false); 190 } else { 191 fix(); 192 return extent.enumerate(); 193 } 194 } 195 } 196 197 } 198 199 218 | Popular Tags |