| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.sort.DocumentOrderIterator; |
| 12 |
import net.sf.saxon.expr.sort.LocalOrderComparer; |
| 13 |
import net.sf.saxon.om.*; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.tree.iter.SingletonIterator; |
| 16 |
import net.sf.saxon.type.Type; |
| 17 |
import net.sf.saxon.value.AtomicValue; |
| 18 |
import net.sf.saxon.value.Whitespace; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public abstract class SuperId extends SystemFunction { |
| 31 |
|
| 32 |
public static final int ID = 0; |
| 33 |
public static final int ELEMENT_WITH_ID = 1; |
| 34 |
|
| 35 |
public abstract int getOp(); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public int getSpecialProperties(Expression[] arguments) { |
| 45 |
int prop = StaticProperty.ORDERED_NODESET | |
| 46 |
StaticProperty.SINGLE_DOCUMENT_NODESET | |
| 47 |
StaticProperty.NO_NODES_NEWLY_CREATED; |
| 48 |
if ((getArity() == 1) || |
| 49 |
(arguments[1].getSpecialProperties() & StaticProperty.CONTEXT_DOCUMENT_NODESET) != 0) { |
| 50 |
prop |= StaticProperty.CONTEXT_DOCUMENT_NODESET; |
| 51 |
} |
| 52 |
return prop; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public static SequenceIterator<?> getIdSingle(TreeInfo doc, String idrefs, int operation) throws XPathException { |
| 67 |
if (Whitespace.containsWhitespace(idrefs)) { |
| 68 |
Whitespace.Tokenizer tokens = new Whitespace.Tokenizer(idrefs); |
| 69 |
IdMappingFunction map = new IdMappingFunction(); |
| 70 |
map.document = doc; |
| 71 |
map.operation = operation; |
| 72 |
SequenceIterator result = new MappingIterator<>(tokens, map); |
| 73 |
return new DocumentOrderIterator(result, LocalOrderComparer.getInstance()); |
| 74 |
} else { |
| 75 |
return SingletonIterator.makeIterator(doc.selectID(idrefs, operation == ELEMENT_WITH_ID)); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public static SequenceIterator getIdMultiple( |
| 92 |
TreeInfo doc, SequenceIterator<?> idrefs, int operation) throws XPathException { |
| 93 |
IdMappingFunction map = new IdMappingFunction(); |
| 94 |
map.document = doc; |
| 95 |
map.operation = operation; |
| 96 |
SequenceIterator<?> result = new MappingIterator<>(idrefs, map); |
| 97 |
return new DocumentOrderIterator(result, LocalOrderComparer.getInstance()); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 110 |
NodeInfo start = arguments.length == 1 ? getContextNode(context) : (NodeInfo) arguments[1].head(); |
| 111 |
NodeInfo arg1 = start.getRoot(); |
| 112 |
if (arg1.getNodeKind() != Type.DOCUMENT) { |
| 113 |
throw new XPathException("In the " + getFunctionName().getLocalPart() + "() function," + |
| 114 |
" the tree being searched must be one whose root is a document node", "FODC0001", context); |
| 115 |
} |
| 116 |
TreeInfo doc = arg1.getTreeInfo(); |
| 117 |
SequenceIterator<?> result; |
| 118 |
if (arguments[0] instanceof AtomicValue) { |
| 119 |
result = getIdSingle(doc, ((AtomicValue)arguments[0]).getStringValue(), getOp()); |
| 120 |
} else { |
| 121 |
SequenceIterator<?> idrefs = arguments[0].iterate(); |
| 122 |
result = getIdMultiple(doc, idrefs, getOp()); |
| 123 |
} |
| 124 |
return SequenceTool.toLazySequence(result); |
| 125 |
} |
| 126 |
|
| 127 |
private static class IdMappingFunction implements MappingFunction<Item<?>, Item<?>> { |
| 128 |
|
| 129 |
public TreeInfo document; |
| 130 |
private int operation; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public SequenceIterator<?> map(Item item) { |
| 138 |
|
| 139 |
String idrefs = Whitespace.trim(item.getStringValueCS()); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if (Whitespace.containsWhitespace(idrefs)) { |
| 145 |
Whitespace.Tokenizer tokens = new Whitespace.Tokenizer(idrefs); |
| 146 |
IdMappingFunction submap = new IdMappingFunction(); |
| 147 |
submap.document = document; |
| 148 |
submap.operation = operation; |
| 149 |
return new MappingIterator<>(tokens, submap); |
| 150 |
|
| 151 |
} else { |
| 152 |
return SingletonIterator.makeIterator(document.selectID(idrefs, operation == ELEMENT_WITH_ID)); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
public static class Id extends SuperId { |
| 158 |
@Override |
| 159 |
public int getOp() { |
| 160 |
return ID; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public static class ElementWithId extends SuperId { |
| 165 |
@Override |
| 166 |
public int getOp() { |
| 167 |
return ELEMENT_WITH_ID; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
} |
| 174 |
|