1 package net.sf.saxon.value; 2 import net.sf.saxon.expr.*; 3 import net.sf.saxon.om.*; 4 import net.sf.saxon.pattern.NodeKindTest; 5 import net.sf.saxon.trans.XPathException; 6 import net.sf.saxon.type.AnyItemType; 7 import net.sf.saxon.type.ItemType; 8 import net.sf.saxon.type.Type; 9 10 import java.util.List ; 11 12 13 17 18 public final class SequenceExtent extends Value { 19 private Item[] value; 20 private int start = 0; private int end; private ItemType itemType = null; 24 26 32 33 public SequenceExtent(Item[] items) { 34 this.value = items; 35 end = items.length; 36 } 37 38 45 46 public SequenceExtent(Item[] value, int start, int length) { 47 this.value = value; 48 this.start = start; 49 this.end = this.start + length; 50 } 51 52 53 60 61 public SequenceExtent(SequenceExtent ext, int start, int length) { 62 this.value = ext.value; 63 this.start = ext.start + start; 64 this.end = this.start + length; 65 } 66 67 73 74 public SequenceExtent(List list) { 75 value = new Item[list.size()]; 76 for (int i=0; i<list.size(); i++) { 77 value[i] = (Item)list.get(i); 78 } 79 end = list.size(); 80 } 81 82 91 92 public SequenceExtent(SequenceIterator iter) throws XPathException { 93 int size = 20; 94 value = new Item[size]; 98 int i = 0; 99 while (true) { 100 Item it = iter.next(); 101 if (it == null) { 102 break; 103 } 104 if (i>=size) { 105 size *= 2; 106 Item newarray[] = new Item[size]; 107 System.arraycopy(value, 0, newarray, 0, i); 108 value = newarray; 109 } 110 value[i++] = it; 111 } 112 end = i; 113 } 114 115 118 119 public static Value makeSequenceExtent(SequenceIterator iter) throws XPathException { 120 if ((iter.getProperties() & SequenceIterator.GROUNDED) != 0) { 121 return ((GroundedIterator)iter).materialize(); 122 } 123 return new SequenceExtent(iter); 125 } 126 127 131 132 public int getImplementationMethod() { 133 return ITERATE_METHOD; 134 } 135 136 139 140 public Value simplify() { 141 int n = getLength(); 142 if (n == 0) { 143 return EmptySequence.getInstance(); 144 } else if (n == 1) { 145 return Value.asValue(itemAt(0)); 146 } else { 147 return this; 148 } 149 } 150 151 157 158 public Value reduce() { 159 return simplify(); 160 } 161 162 167 168 public int getLength() { 169 return end - start; 170 } 171 172 179 180 public int getCardinality() { 181 switch (end - start) { 182 case 0: 183 return StaticProperty.EMPTY; 184 case 1: 185 return StaticProperty.EXACTLY_ONE; 186 default: 187 return StaticProperty.ALLOWS_ONE_OR_MORE; 188 } 189 } 190 191 197 198 public ItemType getItemType() { 199 if (itemType != null) { 200 return itemType; 202 } 203 if (end==start) { 204 itemType = AnyItemType.getInstance(); 205 } else { 206 itemType = computeItemType(value[start]); 207 for (int i=start+1; i<end; i++) { 208 if (itemType == AnyItemType.getInstance()) { 209 return itemType; 211 } 212 itemType = Type.getCommonSuperType(itemType, computeItemType(value[i])); 213 } 214 } 215 return itemType; 216 } 217 218 private static ItemType computeItemType(Item item) { 219 if (item instanceof AtomicValue) { 220 return ((AtomicValue)item).getItemType(); 221 } else { 222 return NodeKindTest.makeNodeKindTest(((NodeInfo)item).getNodeKind()); 223 } 225 } 226 227 228 234 235 public Item itemAt(int n) { 236 if (n<0 || n>=getLength()) { 237 return null; 238 } else { 239 return value[start+n]; 240 } 241 } 242 243 249 250 public void swap(int a, int b) { 251 Item temp = value[start+a]; 252 value[start+a] = value[start+b]; 253 value[start+b] = temp; 254 } 255 256 264 265 public SequenceIterator iterate(XPathContext context) { 266 return new ArrayIterator(value, start, end); 267 } 268 269 274 275 public AxisIterator reverseIterate() { 276 return new ReverseArrayIterator(value, start, end); 277 } 278 279 282 283 public boolean effectiveBooleanValue(XPathContext context) throws XPathException { 284 int len = getLength(); 285 if (len == 0) { 286 return false; 287 } else if (value[0] instanceof NodeInfo) { 288 return true; 289 } else if (len > 1) { 290 return ExpressionTool.effectiveBooleanValue(iterate(context)); 292 } else { 293 return ((AtomicValue)value[0]).effectiveBooleanValue(context); 294 } 295 } 296 297 298 } 299 300 318 | Popular Tags |