1 package net.sf.saxon.expr; 2 3 import net.sf.saxon.om.Item; 4 import net.sf.saxon.om.Name; 5 import net.sf.saxon.om.NamePool; 6 import net.sf.saxon.om.QNameException; 7 import net.sf.saxon.style.StandardNames; 8 import net.sf.saxon.trans.StaticError; 9 import net.sf.saxon.trans.XPathException; 10 import net.sf.saxon.type.AtomicType; 11 import net.sf.saxon.type.ItemType; 12 import net.sf.saxon.type.Type; 13 import net.sf.saxon.value.*; 14 15 import java.io.PrintStream ; 16 import java.util.Iterator ; 17 18 21 22 public class CastAsQName extends ComputedExpression { 23 24 private Expression operand; 25 private AtomicType targetType; 26 27 public CastAsQName(Expression s, AtomicType target) { 28 operand = s; 29 targetType = target; 30 } 31 32 public Expression analyze(StaticContext env, ItemType contextItemType) throws XPathException { 33 if (operand instanceof StringValue) { 34 try { 35 CharSequence arg = ((StringValue)operand).getStringValueCS(); 36 String parts[] = Name.getQNameParts(arg); 37 String uri; 38 if ("".equals(parts[0])) { 39 uri = ""; 40 } else { 41 uri = env.getURIForPrefix(parts[0]); 42 if (uri==null) { 43 StaticError e = new StaticError("Prefix '" + parts[0] + "' has not been declared"); 44 throw e; 45 } 46 } 47 if (targetType.getFingerprint() == StandardNames.XS_QNAME) { 48 return new QNameValue(parts[0], uri, parts[1]); 49 } else if (Type.isSubType(targetType, Type.QNAME_TYPE)) { 50 QNameValue q = new QNameValue(parts[0], uri, parts[1]); 51 AtomicValue av = targetType.makeDerivedValue(q, arg, true); 52 if (av instanceof ValidationErrorValue) { 53 throw ((ValidationErrorValue)av).getException(); 54 } 55 return av; 56 } else { 57 NotationValue n = new NotationValue(parts[0], uri, parts[1]); 58 AtomicValue av = targetType.makeDerivedValue(n, arg, true); 59 if (av instanceof ValidationErrorValue) { 60 throw ((ValidationErrorValue)av).getException(); 61 } 62 return av; 63 } 64 } catch (QNameException err) { 65 StaticError e = new StaticError(err); 66 throw e; 67 } 68 } else { 69 StaticError err = new StaticError("The argument of a QName constructor must be a string literal"); 70 throw err; 71 } 72 } 73 74 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 75 if (operand instanceof StringValue) { 76 try { 77 CharSequence arg = ((StringValue)operand).getStringValueCS(); 78 String parts[] = Name.getQNameParts(arg); 79 String uri; 80 if ("".equals(parts[0])) { 81 uri = ""; 82 } else { 83 uri = env.getURIForPrefix(parts[0]); 84 if (uri==null) { 85 StaticError e = new StaticError("Prefix '" + parts[0] + "' has not been declared"); 86 throw e; 87 } 88 } 89 if (targetType.getFingerprint() == StandardNames.XS_QNAME) { 90 return new QNameValue(parts[0], uri, parts[1]); 91 } else if (Type.isSubType(targetType, Type.QNAME_TYPE)) { 92 QNameValue q = new QNameValue(parts[0], uri, parts[1]); 93 AtomicValue av = targetType.makeDerivedValue(q, arg, true); 94 if (av instanceof ValidationErrorValue) { 95 throw ((ValidationErrorValue)av).getException(); 96 } 97 return av; 98 } else { 99 NotationValue n = new NotationValue(parts[0], uri, parts[1]); 100 AtomicValue av = targetType.makeDerivedValue(n, arg, true); 101 if (av instanceof ValidationErrorValue) { 102 throw ((ValidationErrorValue)av).getException(); 103 } 104 return av; 105 } 106 } catch (QNameException err) { 107 StaticError e = new StaticError(err); 108 throw e; 109 } 110 } else { 111 StaticError err = new StaticError("The argument of a QName constructor must be a string literal"); 112 throw err; 113 } 114 } 115 116 133 134 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 135 operand = operand.optimize(opt, env, contextItemType); 136 if (operand instanceof Value) { 137 return Value.asValue(evaluateItem(null)); 138 } 139 return this; 140 } 141 142 public Expression promote(PromotionOffer offer) throws XPathException { 143 return super.promote(offer); 144 } 145 146 public int computeCardinality() { 147 return StaticProperty.EXACTLY_ONE; 148 } 149 150 public ItemType getItemType() { 151 return targetType; 152 } 153 154 158 159 public int computeSpecialProperties() { 160 int p = super.computeSpecialProperties(); 161 return p | StaticProperty.NON_CREATIVE; 162 } 163 164 public int getIntrinsicDependencies() { 165 return 0; 166 } 167 168 public Iterator iterateSubExpressions() { 169 return new MonoIterator(operand); 170 } 171 172 public Item evaluateItem(XPathContext context) throws XPathException { 173 throw new UnsupportedOperationException ("A QName constructor cannot be evaluated at run-time"); 174 } 175 176 public void display(int level, NamePool pool, PrintStream out) { 177 out.println(ExpressionTool.indent(level) + "cast as QName"); 178 operand.display(level+1, pool, out); 179 } 180 } 181 | Popular Tags |