| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.ma.arrays; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.sort.AtomicComparer; |
| 12 |
import net.sf.saxon.functions.DeepEqual; |
| 13 |
import net.sf.saxon.om.*; |
| 14 |
import net.sf.saxon.query.AnnotationList; |
| 15 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 18 |
import net.sf.saxon.type.*; |
| 19 |
import net.sf.saxon.value.AtomicValue; |
| 20 |
import net.sf.saxon.value.Cardinality; |
| 21 |
import net.sf.saxon.value.IntegerValue; |
| 22 |
import net.sf.saxon.value.SequenceType; |
| 23 |
|
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public abstract class AbstractArrayItem implements ArrayItem { |
| 31 |
|
| 32 |
private SequenceType memberType = null; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public OperandRole[] getOperandRoles() { |
| 40 |
return new OperandRole[]{OperandRole.SINGLE_ATOMIC}; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public boolean isArray() { |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public boolean isMap() { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public AtomicSequence atomize() throws XPathException { |
| 68 |
List<AtomicValue> list = new ArrayList<>(arrayLength()); |
| 69 |
for (GroundedValue<?> seq : members()) { |
| 70 |
seq.iterate().forEachOrFail(item -> { |
| 71 |
AtomicSequence atoms = item.atomize(); |
| 72 |
for (AtomicValue atom : atoms) { |
| 73 |
list.add(atom); |
| 74 |
} |
| 75 |
}); |
| 76 |
} |
| 77 |
return new AtomicArray(list); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@Override |
| 89 |
public AnnotationList getAnnotations() { |
| 90 |
return AnnotationList.EMPTY; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public FunctionItemType getFunctionItemType() { |
| 100 |
return ArrayItemType.ANY_ARRAY_TYPE; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public StructuredQName getFunctionName() { |
| 111 |
return null; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public String getDescription() { |
| 122 |
return "array"; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public int getArity() { |
| 132 |
return 1; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public XPathContext makeNewContext(XPathContext callingContext, ContextOriginator originator) { |
| 144 |
return callingContext; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public GroundedValue<?> call(XPathContext context, Sequence[] args) throws XPathException { |
| 157 |
IntegerValue subscript = (IntegerValue) args[0].head(); |
| 158 |
return get(ArrayFunctionSet.checkSubscript(subscript) - 1); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public boolean deepEquals(Function other, XPathContext context, AtomicComparer comparer, int flags) throws XPathException { |
| 176 |
if (other instanceof ArrayItem) { |
| 177 |
ArrayItem that = (ArrayItem) other; |
| 178 |
if (this.arrayLength() != that.arrayLength()) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
for (int i = 0; i < this.arrayLength(); i++) { |
| 182 |
if (!DeepEqual.deepEqual(this.get(i).iterate(), that.get(i).iterate(), comparer, context, flags)) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
} |
| 186 |
return true; |
| 187 |
} else { |
| 188 |
return false; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public boolean effectiveBooleanValue() throws XPathException { |
| 201 |
throw new XPathException("Effective boolean value is not defined for arrays", "FORG0006"); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public String getStringValue() { |
| 217 |
throw new UnsupportedOperationException("An array does not have a string value"); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public CharSequence getStringValueCS() { |
| 233 |
throw new UnsupportedOperationException("An array does not have a string value"); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public void export(ExpressionPresenter out) throws XPathException { |
| 240 |
out.startElement("array"); |
| 241 |
out.emitAttribute("size", arrayLength() + ""); |
| 242 |
for (GroundedValue mem : members()) { |
| 243 |
Literal.exportValue(mem, out); |
| 244 |
} |
| 245 |
out.endElement(); |
| 246 |
} |
| 247 |
|
| 248 |
public boolean isTrustedResultType() { |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public String toString() { |
| 258 |
FastStringBuffer buffer = new FastStringBuffer(256); |
| 259 |
buffer.append("["); |
| 260 |
for (GroundedValue seq : members()) { |
| 261 |
if (buffer.length() > 1) { |
| 262 |
buffer.append(", "); |
| 263 |
} |
| 264 |
buffer.append(seq.toString()); |
| 265 |
} |
| 266 |
buffer.append("]"); |
| 267 |
return buffer.toString(); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public SequenceType getMemberType(TypeHierarchy th) { |
| 277 |
|
| 278 |
if (memberType == null) { |
| 279 |
if (isEmpty()) { |
| 280 |
memberType = SequenceType.makeSequenceType(ErrorType.getInstance(), StaticProperty.EXACTLY_ONE); |
| 281 |
} else { |
| 282 |
ItemType contentType = null; |
| 283 |
int contentCard = StaticProperty.EXACTLY_ONE; |
| 284 |
for (GroundedValue<?> s : members()) { |
| 285 |
if (contentType == null) { |
| 286 |
contentType = SequenceTool.getItemType(s, th); |
| 287 |
contentCard = SequenceTool.getCardinality(s); |
| 288 |
} else { |
| 289 |
contentType = Type.getCommonSuperType(contentType, SequenceTool.getItemType(s, th)); |
| 290 |
contentCard = Cardinality.union(contentCard, SequenceTool.getCardinality(s)); |
| 291 |
} |
| 292 |
} |
| 293 |
memberType = SequenceType.makeSequenceType(contentType, contentCard); |
| 294 |
} |
| 295 |
} |
| 296 |
return memberType; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
} |
| 303 |
|