| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.parser.*; |
| 12 |
import net.sf.saxon.lib.ConversionRules; |
| 13 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.type.*; |
| 16 |
import net.sf.saxon.value.AtomicValue; |
| 17 |
import net.sf.saxon.value.UntypedAtomicValue; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public final class UntypedSequenceConverter extends AtomicSequenceConverter { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public UntypedSequenceConverter(Expression sequence, PlainType requiredItemType) { |
| 36 |
super(sequence, requiredItemType); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static UntypedSequenceConverter makeUntypedSequenceConverter(Configuration config, Expression operand, PlainType requiredItemType) |
| 54 |
throws XPathException { |
| 55 |
TypeHierarchy th = config.getTypeHierarchy(); |
| 56 |
UntypedSequenceConverter atomicSeqConverter = |
| 57 |
new UntypedSequenceConverter(operand, requiredItemType); |
| 58 |
final ConversionRules rules = config.getConversionRules(); |
| 59 |
final Converter untypedConverter; |
| 60 |
if (((SimpleType) requiredItemType).isNamespaceSensitive()) { |
| 61 |
throw new XPathException("Cannot convert untyped atomic values to a namespace-sensitive type", "XPTY0117"); |
| 62 |
} |
| 63 |
if (requiredItemType.isAtomicType()) { |
| 64 |
untypedConverter = rules.getConverter(BuiltInAtomicType.UNTYPED_ATOMIC, (AtomicType) requiredItemType); |
| 65 |
} else { |
| 66 |
untypedConverter = new StringConverter.StringToUnionConverter(requiredItemType, rules); |
| 67 |
} |
| 68 |
|
| 69 |
Converter converter = new UntypedConverter(rules, untypedConverter); |
| 70 |
atomicSeqConverter.setConverter(converter); |
| 71 |
return atomicSeqConverter; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public static class UntypedConverter extends Converter { |
| 80 |
Converter untypedConverter = null; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public UntypedConverter(ConversionRules rules, Converter converter) { |
| 91 |
super(rules); |
| 92 |
untypedConverter = converter; |
| 93 |
|
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
@Override |
| 99 |
public ConversionResult convert( AtomicValue input) { |
| 100 |
if (input instanceof UntypedAtomicValue) { |
| 101 |
return untypedConverter.convert(input); |
| 102 |
} else { |
| 103 |
return input; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public static UntypedSequenceConverter makeUntypedSequenceRejector(Configuration config, final Expression operand, final PlainType requiredItemType) { |
| 109 |
UntypedSequenceConverter atomicSeqConverter = new UntypedSequenceConverter(operand, requiredItemType); |
| 110 |
final ConversionRules rules = config.getConversionRules(); |
| 111 |
final Converter untypedConverter = new Converter() { |
| 112 |
|
| 113 |
public ConversionResult convert( AtomicValue input) { |
| 114 |
ValidationFailure vf = new ValidationFailure( |
| 115 |
"Implicit conversion of untypedAtomic value to " + requiredItemType.toString() + " is not allowed"); |
| 116 |
vf.setErrorCode("XPTY0117"); |
| 117 |
vf.setLocator(operand.getLocation()); |
| 118 |
return vf; |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
Converter converter = new UntypedConverter(rules, untypedConverter); |
| 124 |
atomicSeqConverter.setConverter(converter); |
| 125 |
return atomicSeqConverter; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 130 |
Expression e2 = super.typeCheck(visitor, contextInfo); |
| 131 |
if (e2 != this) { |
| 132 |
return e2; |
| 133 |
} |
| 134 |
TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 135 |
Expression base = getBaseExpression(); |
| 136 |
if (th.relationship(base.getItemType(), BuiltInAtomicType.UNTYPED_ATOMIC) == TypeHierarchy.DISJOINT || |
| 137 |
base.hasSpecialProperty(StaticProperty.NOT_UNTYPED_ATOMIC)) { |
| 138 |
|
| 139 |
return getBaseExpression(); |
| 140 |
} |
| 141 |
return this; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public int computeSpecialProperties() { |
| 151 |
int p = super.computeSpecialProperties(); |
| 152 |
return p | StaticProperty.NO_NODES_NEWLY_CREATED | StaticProperty.NOT_UNTYPED_ATOMIC; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public Expression copy(RebindingMap rebindings) { |
| 164 |
UntypedSequenceConverter atomicConverter = new UntypedSequenceConverter(getBaseExpression().copy(rebindings), getRequiredItemType()); |
| 165 |
ExpressionTool.copyLocationInfo(this, atomicConverter); |
| 166 |
atomicConverter.setConverter(converter); |
| 167 |
atomicConverter.setRoleDiagnostic(getRoleDiagnostic()); |
| 168 |
return atomicConverter; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public ItemType getItemType() { |
| 181 |
if (getBaseExpression().getItemType() == BuiltInAtomicType.UNTYPED_ATOMIC) { |
| 182 |
return getRequiredItemType(); |
| 183 |
} else { |
| 184 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 185 |
return Type.getCommonSuperType(getRequiredItemType(), getBaseExpression().getItemType(), th); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public int computeCardinality() { |
| 194 |
return getBaseExpression().getCardinality(); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public boolean equals(Object other) { |
| 202 |
return other instanceof UntypedSequenceConverter && |
| 203 |
getBaseExpression().isEqual(((UntypedSequenceConverter)other).getBaseExpression()); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
@Override |
| 211 |
public int computeHashCode() { |
| 212 |
return super.computeHashCode(); |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
protected String displayOperator(Configuration config) { |
| 217 |
return "convertUntyped"; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
@Override |
| 228 |
public String getExpressionName() { |
| 229 |
return "convertUntyped"; |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public String toShortString() { |
| 234 |
return getBaseExpression().toShortString(); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public void export(ExpressionPresenter destination) throws XPathException { |
| 243 |
destination.startElement("cvUntyped", this); |
| 244 |
destination.emitAttribute("to", getRequiredItemType().toExportString()); |
| 245 |
if (getRoleDiagnostic() != null) { |
| 246 |
destination.emitAttribute("diag", getRoleDiagnostic().save()); |
| 247 |
} |
| 248 |
getBaseExpression().export(destination); |
| 249 |
destination.endElement(); |
| 250 |
} |
| 251 |
|
| 252 |
} |
| 253 |
|