| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.s9api; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.event.SequenceCopier; |
| 13 |
import net.sf.saxon.expr.sort.DocumentOrderIterator; |
| 14 |
import net.sf.saxon.expr.sort.GlobalOrderComparer; |
| 15 |
import net.sf.saxon.ma.arrays.ArrayItem; |
| 16 |
import net.sf.saxon.ma.map.MapItem; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.s9api.streams.Step; |
| 19 |
import net.sf.saxon.s9api.streams.XdmStream; |
| 20 |
import net.sf.saxon.serialize.SerializationProperties; |
| 21 |
import net.sf.saxon.trans.XPathException; |
| 22 |
import net.sf.saxon.value.AtomicValue; |
| 23 |
import net.sf.saxon.value.ExternalObject; |
| 24 |
import net.sf.saxon.value.SequenceExtent; |
| 25 |
|
| 26 |
import javax.xml.transform.OutputKeys; |
| 27 |
import javax.xml.transform.stream.StreamResult; |
| 28 |
import java.io.StringWriter; |
| 29 |
import java.util.ArrayList; |
| 30 |
import java.util.Iterator; |
| 31 |
import java.util.List; |
| 32 |
import java.util.Map; |
| 33 |
import java.util.stream.Stream; |
| 34 |
import java.util.stream.StreamSupport; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class XdmValue implements Iterable<XdmItem> { |
| 51 |
|
| 52 |
private GroundedValue<?> value; |
| 53 |
|
| 54 |
protected XdmValue() { |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public XdmValue(Iterable<? extends XdmItem> items) { |
| 67 |
List<Item<?>> values = new ArrayList<>(); |
| 68 |
for (XdmItem item : items) { |
| 69 |
values.add(item.getUnderlyingValue()); |
| 70 |
} |
| 71 |
value = new SequenceExtent<>(values); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public XdmValue(Iterator<? extends XdmItem> iterator) throws SaxonApiException { |
| 83 |
try { |
| 84 |
List<Item<?>> values = new ArrayList<>(); |
| 85 |
while (iterator.hasNext()) { |
| 86 |
values.add(iterator.next().getUnderlyingValue()); |
| 87 |
} |
| 88 |
value = new SequenceExtent<>(values); |
| 89 |
} catch (SaxonApiUncheckedException e) { |
| 90 |
throw new SaxonApiException(e.getCause()); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public XdmValue(Stream<? extends XdmItem> stream) throws SaxonApiException { |
| 100 |
this(stream.iterator()); |
| 101 |
} |
| 102 |
|
| 103 |
protected static XdmValue fromGroundedValue(GroundedValue<?> value) { |
| 104 |
XdmValue xv = new XdmValue(); |
| 105 |
xv.setValue(value); |
| 106 |
return xv; |
| 107 |
} |
| 108 |
|
| 109 |
protected void setValue(GroundedValue<?> value) { |
| 110 |
this.value = value; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public static XdmValue wrap(Sequence<?> value) { |
| 133 |
if (value == null) { |
| 134 |
return XdmEmptySequence.getInstance(); |
| 135 |
} |
| 136 |
GroundedValue<?> gv; |
| 137 |
try { |
| 138 |
gv = value.materialize(); |
| 139 |
} catch (XPathException e) { |
| 140 |
throw new SaxonApiUncheckedException(e); |
| 141 |
} |
| 142 |
if (gv.getLength() == 0) { |
| 143 |
return XdmEmptySequence.getInstance(); |
| 144 |
} else if (gv.getLength() == 1) { |
| 145 |
Item first = gv.head(); |
| 146 |
if (first instanceof NodeInfo) { |
| 147 |
return new XdmNode((NodeInfo) first); |
| 148 |
} else if (first instanceof AtomicValue) { |
| 149 |
return new XdmAtomicValue((AtomicValue) first, true); |
| 150 |
} else if (first instanceof MapItem) { |
| 151 |
return new XdmMap((MapItem)first); |
| 152 |
} else if (first instanceof ArrayItem) { |
| 153 |
return new XdmArray((ArrayItem)first); |
| 154 |
} else if (first instanceof Function) { |
| 155 |
return new XdmFunctionItem((Function) first); |
| 156 |
} else if (first instanceof ExternalObject) { |
| 157 |
return new XdmExternalObject(first); |
| 158 |
} else { |
| 159 |
throw new IllegalArgumentException("Unknown item type " + first.getClass()); |
| 160 |
} |
| 161 |
} else { |
| 162 |
return fromGroundedValue(gv); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
public static XdmValue wrap(AtomicSequence value) { |
| 167 |
switch (value.getLength()) { |
| 168 |
case 0: |
| 169 |
return XdmEmptySequence.getInstance(); |
| 170 |
case 1: |
| 171 |
return new XdmAtomicValue(value.head(), true); |
| 172 |
default: |
| 173 |
return fromGroundedValue(value); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public XdmValue append(XdmValue otherValue) { |
| 190 |
List<Item<?>> values = new ArrayList<>(); |
| 191 |
for (XdmItem item : this) { |
| 192 |
values.add(item.getUnderlyingValue()); |
| 193 |
} |
| 194 |
for (XdmItem item : otherValue) { |
| 195 |
values.add(item.getUnderlyingValue()); |
| 196 |
} |
| 197 |
GroundedValue<Item<?>> gv = SequenceExtent.makeSequenceExtent(values); |
| 198 |
return XdmValue.fromGroundedValue(gv); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public int size() { |
| 211 |
try { |
| 212 |
return SequenceTool.getLength(value); |
| 213 |
} catch (XPathException err) { |
| 214 |
throw new SaxonApiUncheckedException(err); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public XdmItem itemAt(int n) throws IndexOutOfBoundsException, SaxonApiUncheckedException { |
| 230 |
if (n < 0 || n >= size()) { |
| 231 |
throw new IndexOutOfBoundsException("" + n); |
| 232 |
} |
| 233 |
try { |
| 234 |
Item<?> item = SequenceTool.itemAt(value, n); |
| 235 |
return (XdmItem)XdmItem.wrap(item); |
| 236 |
} catch (XPathException e) { |
| 237 |
throw new SaxonApiUncheckedException(e); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public Iterator<XdmItem> iterator() throws SaxonApiUncheckedException { |
| 249 |
try { |
| 250 |
Sequence<?> v = getUnderlyingValue(); |
| 251 |
return new XdmSequenceIterator<>(v.iterate()); |
| 252 |
} catch (XPathException e) { |
| 253 |
throw new SaxonApiUncheckedException(e); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public GroundedValue<?> getUnderlyingValue() { |
| 268 |
return value; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public String toString() { |
| 278 |
try { |
| 279 |
Configuration config = null; |
| 280 |
SequenceIterator iter = value.iterate(); |
| 281 |
Item item; |
| 282 |
while ((item = iter.next()) != null) { |
| 283 |
if (item instanceof NodeInfo) { |
| 284 |
config = ((NodeInfo)item).getConfiguration(); |
| 285 |
break; |
| 286 |
} |
| 287 |
} |
| 288 |
if (config == null) { |
| 289 |
config = Configuration.newConfiguration(); |
| 290 |
} |
| 291 |
StringWriter writer = new StringWriter(); |
| 292 |
StreamResult result = new StreamResult(writer); |
| 293 |
SerializationProperties properties = new SerializationProperties(); |
| 294 |
properties.setProperty(OutputKeys.METHOD, "adaptive"); |
| 295 |
properties.setProperty(OutputKeys.INDENT, "true"); |
| 296 |
properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "true"); |
| 297 |
Receiver r = config.getSerializerFactory().getReceiver(result, properties); |
| 298 |
SequenceCopier.copySequence(value.iterate(), r); |
| 299 |
return writer.toString(); |
| 300 |
} catch (XPathException e) { |
| 301 |
return super.toString(); |
| 302 |
} |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public static XdmValue makeSequence(Iterable<?> list) throws IllegalArgumentException { |
| 316 |
List<Item<?>> result = new ArrayList<>(); |
| 317 |
for (Object o : list) { |
| 318 |
XdmValue v = XdmValue.makeValue(o); |
| 319 |
if (v instanceof XdmItem) { |
| 320 |
result.add((Item)v.getUnderlyingValue()); |
| 321 |
} else { |
| 322 |
result.add(new XdmArray(v).getUnderlyingValue()); |
| 323 |
} |
| 324 |
} |
| 325 |
return XdmValue.wrap(SequenceExtent.makeSequenceExtent(result)); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public static XdmValue makeValue(Object o) throws IllegalArgumentException { |
| 352 |
if (o instanceof Sequence) { |
| 353 |
return XdmValue.wrap((Sequence<?>) o); |
| 354 |
} else if (o instanceof XdmValue) { |
| 355 |
return (XdmValue) o; |
| 356 |
} else if (o instanceof Map) { |
| 357 |
return XdmMap.makeMap((Map) o); |
| 358 |
} else if (o instanceof Object[]) { |
| 359 |
return XdmArray.makeArray((Object[]) o); |
| 360 |
} else if (o instanceof Iterable) { |
| 361 |
return XdmValue.makeSequence((Iterable) o); |
| 362 |
} else { |
| 363 |
return XdmAtomicValue.makeAtomicValue(o); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public XdmValue documentOrder() throws SaxonApiException { |
| 378 |
try { |
| 379 |
SequenceIterator<?> iter = value.iterate(); |
| 380 |
SequenceIterator<NodeInfo> sorted = new DocumentOrderIterator(iter, GlobalOrderComparer.getInstance()); |
| 381 |
return XdmValue.fromGroundedValue(sorted.materialize()); |
| 382 |
} catch (XPathException e) { |
| 383 |
throw new SaxonApiException(e); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public XdmStream<? extends XdmItem> stream() { |
| 394 |
return new XdmStream<>(StreamSupport.stream(spliterator(), false)); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public <T extends XdmItem> XdmStream<T> select(Step<T> step) { |
| 409 |
return stream().flatMapToXdm(step); |
| 410 |
} |
| 411 |
|
| 412 |
} |
| 413 |
|